|
| 1 | +from conan import ConanFile |
| 2 | +from conan.errors import ConanInvalidConfiguration |
| 3 | +from conan.tools.android import android_abi |
| 4 | +from conan.tools.build import check_min_cppstd |
| 5 | +from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout |
| 6 | +from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, rmdir, copy |
| 7 | +import os |
| 8 | + |
| 9 | +required_conan_version = ">=2.1" |
| 10 | + |
| 11 | + |
| 12 | +class SfmlConan(ConanFile): |
| 13 | + name = "sfml" |
| 14 | + description = "Simple and Fast Multimedia Library." |
| 15 | + license = "Zlib" |
| 16 | + url = "https://github.com/conan-io/conan-center-index" |
| 17 | + homepage = "https://www.sfml-dev.org" |
| 18 | + topics = ("multimedia", "games", "graphics", "audio") |
| 19 | + package_type = "library" |
| 20 | + settings = "os", "arch", "compiler", "build_type" |
| 21 | + options = { |
| 22 | + "shared": [True, False], |
| 23 | + "fPIC": [True, False], |
| 24 | + # modules |
| 25 | + "window": [True, False], |
| 26 | + "graphics": [True, False], |
| 27 | + "network": [True, False], |
| 28 | + "audio": [True, False], |
| 29 | + # window module options |
| 30 | + "opengl": ["es", "desktop"], |
| 31 | + # "use_drm": [True, False], # Linux only, no support for now, PR welcome |
| 32 | + # "use_mesa3d": [True, False], # Windows only, not available in CCI |
| 33 | + |
| 34 | + } |
| 35 | + default_options = { |
| 36 | + "shared": False, |
| 37 | + "fPIC": True, |
| 38 | + "window": True, |
| 39 | + "graphics": True, |
| 40 | + "network": True, |
| 41 | + "audio": True, |
| 42 | + "opengl": "desktop", |
| 43 | + } |
| 44 | + implements = ["auto_shared_fpic"] |
| 45 | + |
| 46 | + def export_sources(self): |
| 47 | + export_conandata_patches(self) |
| 48 | + |
| 49 | + def configure(self): |
| 50 | + if self.options.get_safe("shared"): |
| 51 | + self.options.rm_safe("fPIC") |
| 52 | + |
| 53 | + if not self.options.window: |
| 54 | + del self.options.opengl |
| 55 | + |
| 56 | + # As per CMakeLists.txt#L44, Android is always shared |
| 57 | + if self.settings.os == "Android": |
| 58 | + del self.options.shared |
| 59 | + del self.options.fPIC |
| 60 | + self.package_type = "shared-library" |
| 61 | + |
| 62 | + def layout(self): |
| 63 | + cmake_layout(self, src_folder="src") |
| 64 | + |
| 65 | + def requirements(self): |
| 66 | + if self.options.window: |
| 67 | + if self.settings.os in ["Linux", "FreeBSD"]: |
| 68 | + self.requires("xorg/system") |
| 69 | + if self.settings.os == "Linux": |
| 70 | + self.requires("libudev/system") |
| 71 | + |
| 72 | + if self.settings.os not in ("iOS", "Android"): # Handled as a framework |
| 73 | + self.requires("opengl/system") |
| 74 | + self.requires("vulkan-headers/[~1]") |
| 75 | + |
| 76 | + if self.options.graphics: |
| 77 | + if self.settings.os == "Android" or self.settings.os == "iOS": |
| 78 | + self.requires("zlib/[>=1.2.11 <2]") |
| 79 | + if self.settings.os == "iOS": |
| 80 | + self.requires("bzip2/1.0.8") |
| 81 | + self.requires("freetype/2.13.2") |
| 82 | + self.requires("stb/[>=cci.20240531]") |
| 83 | + |
| 84 | + if self.options.audio: |
| 85 | + self.requires("vorbis/1.3.7") |
| 86 | + self.requires("flac/1.4.3") |
| 87 | + self.requires("minimp3/cci.20211201") |
| 88 | + self.requires("miniaudio/0.11.22", transitive_headers=True) |
| 89 | + |
| 90 | + def build_requirements(self): |
| 91 | + self.tool_requires("cmake/[>=3.24]") |
| 92 | + |
| 93 | + def validate(self): |
| 94 | + check_min_cppstd(self, 17) |
| 95 | + |
| 96 | + if self.options.graphics and not self.options.window: |
| 97 | + raise ConanInvalidConfiguration(f"-o={self.ref}:graphics=True requires -o={self.ref}:window=True") |
| 98 | + |
| 99 | + if self.settings.os == "Windows" and self.options.get_safe("shared") and self.settings.get_safe("compiler.runtime") == "static": |
| 100 | + raise ConanInvalidConfiguration(f"{self.ref} does not support shared libraries with static runtime") |
| 101 | + |
| 102 | + def validate_build(self): |
| 103 | + if self.settings.os == "Macos" and self.settings.compiler != "apple-clang": |
| 104 | + raise ConanInvalidConfiguration(f"{self.ref} is not supported on {self.settings.os} with {self.settings.compiler}") |
| 105 | + |
| 106 | + def source(self): |
| 107 | + get(self, **self.conan_data["sources"][self.version], strip_root=True) |
| 108 | + apply_conandata_patches(self) |
| 109 | + |
| 110 | + def generate(self): |
| 111 | + tc = CMakeToolchain(self) |
| 112 | + |
| 113 | + tc.cache_variables["SFML_BUILD_WINDOW"] = self.options.window |
| 114 | + tc.cache_variables["SFML_BUILD_GRAPHICS"] = self.options.graphics |
| 115 | + tc.cache_variables["SFML_BUILD_NETWORK"] = self.options.network |
| 116 | + tc.cache_variables["SFML_BUILD_AUDIO"] = self.options.audio |
| 117 | + |
| 118 | + if self.options.window: |
| 119 | + tc.cache_variables["SFML_OPENGL_ES"] = self.options.opengl == "es" |
| 120 | + |
| 121 | + tc.cache_variables["SFML_GENERATE_PDB"] = False # PDBs not allowed in CCI |
| 122 | + |
| 123 | + if self.settings.os == "Windows": |
| 124 | + tc.cache_variables["SFML_USE_STATIC_STD_LIBS"] = self.settings.get_safe("compiler.runtime") == "static" |
| 125 | + tc.cache_variables["SFML_USE_MESA3D"] = False # self.options.use_mesa3d |
| 126 | + |
| 127 | + tc.cache_variables["SFML_USE_SYSTEM_DEPS"] = True |
| 128 | + |
| 129 | + tc.cache_variables["SFML_INSTALL_PKGCONFIG_FILES"] = False |
| 130 | + tc.cache_variables["SFML_WARNINGS_AS_ERRORS"] = False |
| 131 | + tc.cache_variables["SFML_CONFIGURE_EXTRAS"] = False |
| 132 | + |
| 133 | + # Tip: You can use this locally to test the extras when adding a new version, |
| 134 | + # uncomment both to build examples, or run them manually |
| 135 | + # tc.cache_variables["SFML_CONFIGURE_EXTRAS"] = True |
| 136 | + # tc.cache_variables["SFML_BUILD_EXAMPLES"] = True |
| 137 | + |
| 138 | + tc.generate() |
| 139 | + deps = CMakeDeps(self) |
| 140 | + if self.options.audio: |
| 141 | + deps.set_property("flac", "cmake_file_name", "FLAC") |
| 142 | + |
| 143 | + if self.options.graphics: |
| 144 | + deps.set_property("freetype", "cmake_file_name", "Freetype") |
| 145 | + deps.set_property("freetype", "cmake_target_name", "Freetype::Freetype") |
| 146 | + deps.generate() |
| 147 | + |
| 148 | + def build(self): |
| 149 | + cmake = CMake(self) |
| 150 | + cmake.configure() |
| 151 | + cmake.build() |
| 152 | + |
| 153 | + def package(self): |
| 154 | + copy(self, "license.md", self.source_folder, os.path.join(self.package_folder, "licenses")) |
| 155 | + cmake = CMake(self) |
| 156 | + cmake.install() |
| 157 | + |
| 158 | + rmdir(self, os.path.join(self.package_folder, "lib", "cmake")) |
| 159 | + rmdir(self, os.path.join(self.package_folder, "share")) |
| 160 | + |
| 161 | + def _basic_module_definition(self, name): |
| 162 | + self.cpp_info.components[name].set_property("cmake_target_name", f"SFML::{name.capitalize()}") |
| 163 | + |
| 164 | + libname = f"sfml-{name}" |
| 165 | + |
| 166 | + if self.package_type == "static-library" and name != "main": |
| 167 | + libname += "-s" |
| 168 | + self.cpp_info.components[name].defines = ["SFML_STATIC"] |
| 169 | + if self.settings.build_type == "Debug": |
| 170 | + libname += "-d" |
| 171 | + self.cpp_info.components[name].libs = [libname] |
| 172 | + |
| 173 | + # CMakeLists.txt#L87 - Android libs are in lib/<CMAKE_ANDROID_ARCH_ABI> |
| 174 | + if self.settings.os == "Android": |
| 175 | + self.cpp_info.components[name].libdirs = [os.path.join("lib", android_abi(self))] |
| 176 | + |
| 177 | + def package_info(self): |
| 178 | + self.cpp_info.set_property("cmake_file_name", "SFML") |
| 179 | + self.cpp_info.set_property("pkg_config_name", "sfml-all") |
| 180 | + |
| 181 | + modules = ["system"] |
| 182 | + if self.settings.os in ["Windows", "iOS", "Android"]: |
| 183 | + modules.append("main") |
| 184 | + |
| 185 | + modules.extend(module for module in ["window", "graphics", "network", "audio"] if self.options.get_safe(module)) |
| 186 | + |
| 187 | + for module in modules: |
| 188 | + self._basic_module_definition(module) |
| 189 | + |
| 190 | + # System module is always required |
| 191 | + if self.settings.os in ["Linux", "FreeBSD"]: |
| 192 | + self.cpp_info.components["system"].system_libs = ["pthread", "rt"] |
| 193 | + elif self.settings.os == "Windows": |
| 194 | + self.cpp_info.components["system"].system_libs = ["winmm"] |
| 195 | + elif self.settings.os == "Android": |
| 196 | + self.cpp_info.components["system"].system_libs = ["log", "android"] |
| 197 | + |
| 198 | + if self.options.window: |
| 199 | + self.cpp_info.components["window"].requires = ["system", "vulkan-headers::vulkan-headers"] |
| 200 | + if self.settings.os in ["Linux", "FreeBSD"]: |
| 201 | + self.cpp_info.components["window"].system_libs.append("dl") |
| 202 | + self.cpp_info.components["window"].requires.extend(["xorg::x11", "xorg::xrandr", "xorg::xcursor", "xorg::xi"]) |
| 203 | + |
| 204 | + if self.settings.os == "iOS": |
| 205 | + self.cpp_info.components["window"].frameworks = ["OpenGLES"] |
| 206 | + elif self.settings.os == "Android": |
| 207 | + self.cpp_info.components["window"].system_libs.extend(["egl", "GLESv2"]) |
| 208 | + else: |
| 209 | + self.cpp_info.components["window"].requires.append("opengl::opengl") |
| 210 | + |
| 211 | + if self.settings.os == "Linux": |
| 212 | + self.cpp_info.components["window"].requires.append("libudev::libudev") |
| 213 | + elif self.settings.os == "Windows": |
| 214 | + self.cpp_info.components["window"].system_libs.extend(["gdi32", "winmm"]) |
| 215 | + elif self.settings.os == "FreeBSD": |
| 216 | + self.cpp_info.components["window"].system_libs.append("usbhid") |
| 217 | + elif self.settings.os == "Macos": |
| 218 | + # CoreServices is pulled from Carbon, even if it does not show up in the upstream CMakeLists.txt |
| 219 | + self.cpp_info.components["window"].frameworks = ["Foundation", "AppKit", "IOKit", "Carbon", "CoreServices"] |
| 220 | + elif self.settings.os == "iOS": |
| 221 | + self.cpp_info.components["window"].frameworks = ["Foundation", "UIKit", "CoreGraphics", "QuartzCore", "CoreMotion"] |
| 222 | + elif self.settings.os == "Android": |
| 223 | + self.cpp_info.components["window"].system_libs = ["android"] |
| 224 | + |
| 225 | + if self.options.graphics: |
| 226 | + self.cpp_info.components["graphics"].requires = ["window", "stb::stb"] |
| 227 | + if self.settings.os == "Android" or self.settings.os == "iOS": |
| 228 | + self.cpp_info.components["graphics"].requires.append("zlib::zlib") |
| 229 | + if self.settings.os == "iOS": |
| 230 | + self.cpp_info.components["graphics"].requires.append("bzip2::bzip2") |
| 231 | + self.cpp_info.components["graphics"].requires.append("freetype::freetype") |
| 232 | + |
| 233 | + if self.options.network: |
| 234 | + self.cpp_info.components["network"].requires = ["system"] |
| 235 | + |
| 236 | + if self.settings.os == "Windows": |
| 237 | + self.cpp_info.components["network"].system_libs = ["ws2_32"] |
| 238 | + |
| 239 | + if self.options.audio: |
| 240 | + self.cpp_info.components["audio"].requires = ["vorbis::vorbis", "flac::flac", "system", "minimp3::minimp3", "miniaudio::miniaudio"] |
| 241 | + if self.settings.os == "iOS": |
| 242 | + self.cpp_info.components["audio"].frameworks = ["Foundation", "CoreFoundation", "CoreAudio", "AudioToolbox", "AVFoundation"] |
| 243 | + else: |
| 244 | + self.cpp_info.components["audio"].requires.extend(["vorbis::vorbisfile", "vorbis::vorbisenc"]) |
| 245 | + |
| 246 | + if self.settings.os == "Android": |
| 247 | + self.cpp_info.components["audio"].system_libs = ["android", "OpenSLES"] |
| 248 | + |
| 249 | + if self.settings.os == "Linux": |
| 250 | + self.cpp_info.components["audio"].system_libs = ["pthread", "dl"] |
0 commit comments