conanfile.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. from conan import ConanFile
  2. from conan.errors import ConanInvalidConfiguration
  3. from conan.tools.apple import is_apple_os
  4. from conan.tools.build import cross_building
  5. from conan.tools.cmake import CMakeDeps, CMakeToolchain
  6. from conans import tools
  7. required_conan_version = ">=1.51.3"
  8. class VCMI(ConanFile):
  9. settings = "os", "compiler", "build_type", "arch"
  10. _libRequires = [
  11. "boost/[^1.69]",
  12. "minizip/[~1.2.12]",
  13. ]
  14. _clientRequires = [
  15. # Versions between 2.5-2.8 have broken loading of palette sdl images which a lot of mods use
  16. # there is workaround that require disabling cmake flag which is not available in conan recipes.
  17. # Bug is fixed in version 2.8, however it is not available in conan at the moment
  18. "sdl_image/2.0.5",
  19. "sdl_ttf/[>=2.0.18]",
  20. "onetbb/[^2021.7 <2021.10]", # 2021.10+ breaks mobile builds due to added hwloc dependency
  21. "xz_utils/[>=5.2.5]", # Required for innoextract
  22. ]
  23. requires = _libRequires + _clientRequires
  24. options = {
  25. "default_options_of_requirements": [True, False],
  26. "with_apple_system_libs": [True, False],
  27. "with_ffmpeg": [True, False],
  28. "with_luajit": [True, False],
  29. }
  30. default_options = {
  31. "default_options_of_requirements": False,
  32. "with_apple_system_libs": False,
  33. "with_ffmpeg": True,
  34. "with_luajit": False,
  35. "boost/*:shared": True,
  36. "minizip/*:shared": True,
  37. }
  38. def configure(self):
  39. self.options["ffmpeg"].shared = self.settings.os == "Android" # using shared version results in less total project size on Android
  40. self.options["freetype"].shared = self.settings.os == "Android"
  41. # SDL_image and Qt depend on it, in iOS both are static
  42. self.options["libpng"].shared = self.settings.os != "iOS"
  43. # static Qt for iOS is the only viable option at the moment
  44. self.options["qt"].shared = self.settings.os != "iOS"
  45. # TODO: enable for all platforms
  46. if self.settings.os == "Android":
  47. self.options["bzip2"].shared = True
  48. self.options["libiconv"].shared = True
  49. self.options["zlib"].shared = True
  50. # TODO: enable for all platforms?
  51. if self.settings.os == "Windows":
  52. self.options["sdl"].shared = True
  53. self.options["sdl_image"].shared = True
  54. self.options["sdl_mixer"].shared = True
  55. self.options["sdl_ttf"].shared = True
  56. if self.settings.os == "iOS":
  57. # TODO: ios - newer sdl fails to link
  58. self.requires("sdl/2.26.1")
  59. self.requires("sdl_mixer/2.0.4")
  60. elif self.settings.os == "Android":
  61. # On Android SDL version must be same as version of Java wrapper for SDL in VCMI source code
  62. # Wrapper can be found in following directory: android/vcmi-app/src/main/java/org/libsdl/app
  63. self.requires("sdl/2.26.5")
  64. self.requires("sdl_mixer/2.0.4")
  65. else:
  66. # upcoming SDL version 3.0+ is not supported at the moment due to API breakage
  67. # SDL versions between 2.22-2.26.1 have broken sound
  68. self.requires("sdl/[^2.26 || >=2.0.20 <=2.22.0]")
  69. self.requires("sdl_mixer/[>=2.0.4]")
  70. if self.settings.os == "Android":
  71. self.options["qt"].android_sdk = tools.get_env("ANDROID_HOME", default="")
  72. if self.options.default_options_of_requirements:
  73. return
  74. # we need only the following Boost parts:
  75. # date_time filesystem iostreams locale program_options system thread
  76. # some other parts are also enabled because they're dependents
  77. # see e.g. conan-center-index/recipes/boost/all/dependencies
  78. self.options["boost"].without_context = True
  79. self.options["boost"].without_contract = True
  80. self.options["boost"].without_coroutine = True
  81. self.options["boost"].without_fiber = True
  82. self.options["boost"].without_graph = True
  83. self.options["boost"].without_graph_parallel = True
  84. self.options["boost"].without_json = True
  85. self.options["boost"].without_log = True
  86. self.options["boost"].without_math = True
  87. self.options["boost"].without_mpi = True
  88. self.options["boost"].without_nowide = True
  89. self.options["boost"].without_python = True
  90. self.options["boost"].without_serialization = True
  91. self.options["boost"].without_stacktrace = True
  92. self.options["boost"].without_test = True
  93. self.options["boost"].without_timer = True
  94. self.options["boost"].without_type_erasure = True
  95. self.options["boost"].without_wave = True
  96. self.options["boost"].without_url = True
  97. self.options["ffmpeg"].disable_all_bitstream_filters = True
  98. self.options["ffmpeg"].disable_all_decoders = True
  99. self.options["ffmpeg"].disable_all_demuxers = True
  100. self.options["ffmpeg"].disable_all_encoders = True
  101. self.options["ffmpeg"].disable_all_filters = True
  102. self.options["ffmpeg"].disable_all_hardware_accelerators = True
  103. self.options["ffmpeg"].disable_all_muxers = True
  104. self.options["ffmpeg"].disable_all_parsers = True
  105. self.options["ffmpeg"].disable_all_protocols = True
  106. self.options["ffmpeg"].with_asm = False
  107. self.options["ffmpeg"].with_bzip2 = False
  108. self.options["ffmpeg"].with_freetype = False
  109. self.options["ffmpeg"].with_libaom = False
  110. self.options["ffmpeg"].with_libdav1d = False
  111. self.options["ffmpeg"].with_libiconv = False
  112. self.options["ffmpeg"].with_libmp3lame = False
  113. self.options["ffmpeg"].with_libsvtav1 = False
  114. self.options["ffmpeg"].with_libvpx = False
  115. self.options["ffmpeg"].with_libwebp = False
  116. self.options["ffmpeg"].with_libx264 = False
  117. self.options["ffmpeg"].with_libx265 = False
  118. self.options["ffmpeg"].with_lzma = True
  119. self.options["ffmpeg"].with_openh264 = False
  120. self.options["ffmpeg"].with_openjpeg = False
  121. self.options["ffmpeg"].with_opus = False
  122. self.options["ffmpeg"].with_programs = False
  123. self.options["ffmpeg"].with_sdl = False
  124. self.options["ffmpeg"].with_ssl = False
  125. self.options["ffmpeg"].with_vorbis = False
  126. self.options["ffmpeg"].with_zlib = False
  127. if self.settings.os != "Android":
  128. self.options["ffmpeg"].with_libfdk_aac = False
  129. self.options["ffmpeg"].avcodec = True
  130. self.options["ffmpeg"].avdevice = False
  131. self.options["ffmpeg"].avfilter = False
  132. self.options["ffmpeg"].avformat = True
  133. self.options["ffmpeg"].postproc = False
  134. self.options["ffmpeg"].swresample = True # For resampling of audio in 'planar' formats
  135. self.options["ffmpeg"].swscale = True # For video scaling
  136. # We want following options supported:
  137. # H3:SoD - .bik and .smk
  138. # H3:HD - ogg container / theora video / vorbis sound (not supported by vcmi at the moment, but might be supported in future)
  139. # and for mods - webm container / vp8 or vp9 video / opus sound
  140. # TODO: add av1 support for mods (requires enabling libdav1d which currently fails to build via Conan)
  141. self.options["ffmpeg"].enable_protocols = "file"
  142. self.options["ffmpeg"].enable_demuxers = "bink,binka,ogg,smacker,webm_dash_manifest"
  143. self.options["ffmpeg"].enable_parsers = "opus,vorbis,vp8,vp9,webp"
  144. self.options["ffmpeg"].enable_decoders = "bink,binkaudio_dct,binkaudio_rdft,smackaud,smacker,theora,vorbis,vp8,vp9,opus"
  145. #optionally, for testing - enable ffplay/ffprobe binaries in conan package:
  146. #if self.settings.os == "Windows":
  147. # self.options["ffmpeg"].with_programs = True
  148. # self.options["ffmpeg"].avfilter = True
  149. # self.options["ffmpeg"].with_sdl = True
  150. # self.options["ffmpeg"].enable_filters = "aresample,scale"
  151. self.options["sdl"].sdl2main = self.settings.os != "iOS"
  152. self.options["sdl"].vulkan = False
  153. # bmp, png are the only ones that needs to be supported
  154. # dds support may be useful for HD edition, but not supported by sdl_image at the moment
  155. self.options["sdl_image"].gif = False
  156. self.options["sdl_image"].lbm = False
  157. self.options["sdl_image"].pnm = False
  158. self.options["sdl_image"].pcx = False
  159. #self.options["sdl_image"].qoi = False # sdl_image >=2.6
  160. self.options["sdl_image"].svg = False
  161. self.options["sdl_image"].tga = False
  162. self.options["sdl_image"].with_libjpeg = False
  163. self.options["sdl_image"].with_libtiff = False
  164. self.options["sdl_image"].with_libwebp = False
  165. self.options["sdl_image"].xcf = False
  166. self.options["sdl_image"].xpm = False
  167. self.options["sdl_image"].xv = False
  168. if is_apple_os(self):
  169. self.options["sdl_image"].imageio = True
  170. # mp3, ogg and wav are the only ones that needs to be supported
  171. # opus is nice to have, but fails to build in CI
  172. # flac can be considered, but generally unnecessary
  173. self.options["sdl_mixer"].flac = False
  174. self.options["sdl_mixer"].modplug = False
  175. self.options["sdl_mixer"].opus = False
  176. if self.settings.os == "iOS" or self.settings.os == "Android":
  177. # only available in older sdl_mixer version, removed in newer version
  178. self.options["sdl_mixer"].mad = False
  179. self.options["sdl_mixer"].mikmod = False
  180. self.options["sdl_mixer"].nativemidi = False
  181. def _disableQtOptions(disableFlag, options):
  182. return " ".join([f"-{disableFlag}-{tool}" for tool in options])
  183. _qtOptions = [
  184. _disableQtOptions("no", [
  185. "gif",
  186. "ico",
  187. ]),
  188. _disableQtOptions("no-feature", [
  189. # xpm format is required for Drag'n'Drop support
  190. "imageformat_bmp",
  191. "imageformat_jpeg",
  192. "imageformat_ppm",
  193. "imageformat_xbm",
  194. # we need only macdeployqt
  195. # TODO: disabling these doesn't disable generation of CMake targets
  196. # TODO: in Qt 6.3 it's a part of qtbase
  197. # "assistant",
  198. # "designer",
  199. # "distancefieldgenerator",
  200. # "kmap2qmap",
  201. # "linguist",
  202. # "makeqpf",
  203. # "pixeltool",
  204. # "qdbus",
  205. # "qev",
  206. # "qtattributionsscanner",
  207. # "qtdiag",
  208. # "qtpaths",
  209. # "qtplugininfo",
  210. ]),
  211. ]
  212. self.options["qt"].config = " ".join(_qtOptions)
  213. self.options["qt"].qttools = True
  214. self.options["qt"].qtandroidextras = self.settings.os == "Android" # TODO: in Qt 6 it's part of Core
  215. self.options["qt"].with_freetype = self.settings.os == "Android"
  216. self.options["qt"].with_libjpeg = False
  217. self.options["qt"].with_md4c = False
  218. self.options["qt"].with_mysql = False
  219. self.options["qt"].with_odbc = False
  220. self.options["qt"].with_openal = False
  221. self.options["qt"].with_pq = False
  222. self.options["qt"].openssl = not is_apple_os(self)
  223. if self.settings.os == "iOS" or self.settings.os == "Android":
  224. self.options["qt"].opengl = "es2"
  225. if not is_apple_os(self) and self.settings.os != "Android" and cross_building(self):
  226. self.options["qt"].cross_compile = self.env["CONAN_CROSS_COMPILE"]
  227. # TODO: add for all platforms after updating recipe
  228. if self.settings.os == "Android":
  229. self.options["qt"].essential_modules = False
  230. # No Qt OpenGL for cross-compiling for Windows, Conan does not support it
  231. if self.settings.os == "Windows" and cross_building(self):
  232. self.options["qt"].opengl = "no"
  233. # transitive deps
  234. # doesn't link to overridden bzip2 & zlib, the tool isn't needed anyway
  235. self.options["pcre2"].build_pcre2grep = False
  236. # executable not needed
  237. if self.settings.os == "Android":
  238. self.options["sqlite3"].build_executable = False
  239. def requirements(self):
  240. self.requires("freetype/[~2.12.1]", override=True) # sdl_ttf / Qt
  241. self.requires("libpng/[~1.6.39]", override=True) # freetype / qt / sdl_image
  242. # client
  243. if self.options.with_ffmpeg:
  244. self.requires("ffmpeg/[>=4.4]")
  245. # launcher
  246. if self.settings.os == "Android":
  247. self.requires("qt/[~5.15.14]")
  248. else:
  249. self.requires("qt/[~5.15.2]")
  250. # TODO: version range doesn't work in Conan v1
  251. if self.options["qt"].openssl:
  252. self.requires("openssl/1.1.1s")
  253. # use Apple system libraries instead of external ones
  254. if self.options.with_apple_system_libs and is_apple_os(self):
  255. systemLibsOverrides = [
  256. "bzip2/1.0.8",
  257. "libiconv/1.17",
  258. "sqlite3/3.39.2",
  259. "zlib/1.2.12",
  260. ]
  261. for lib in systemLibsOverrides:
  262. self.requires(f"{lib}@vcmi/apple", override=True)
  263. elif self.settings.os == "Android":
  264. self.requires("zlib/1.2.12@vcmi/android", override=True)
  265. else:
  266. self.requires("zlib/[~1.2.13]", override=True) # minizip / Qt
  267. self.requires("libiconv/[~1.17]", override=True) # ffmpeg / sdl
  268. # TODO: the latest official release of LuaJIT (which is quite old) can't be built for arm
  269. if self.options.with_luajit and not str(self.settings.arch).startswith("arm"):
  270. self.requires("luajit/[~2.0.5]")
  271. def validate(self):
  272. if self.options.with_apple_system_libs and not is_apple_os(self):
  273. raise ConanInvalidConfiguration("with_apple_system_libs is only for Apple platforms")
  274. if self.options.with_apple_system_libs and self.options.default_options_of_requirements:
  275. raise ConanInvalidConfiguration("with_apple_system_libs and default_options_of_requirements can't be True at the same time")
  276. def generate(self):
  277. tc = CMakeToolchain(self)
  278. tc.variables["USING_CONAN"] = True
  279. tc.variables["CONAN_INSTALL_FOLDER"] = self.install_folder
  280. if self.settings.os == "Android":
  281. tc.variables["CMAKE_ANDROID_API"] = str(self.settings.os.api_level)
  282. tc.variables["ANDROID_SYSROOT_LIB_SUBDIR"] = {
  283. 'armv7': 'arm-linux-androideabi',
  284. 'armv8': 'aarch64-linux-android',
  285. 'x86': 'i686-linux-android',
  286. 'x86_64': 'x86_64-linux-android',
  287. }.get(str(self.settings.arch))
  288. if cross_building(self) and self.settings.os == "Windows":
  289. tc.variables["CONAN_SYSTEM_LIBRARY_LOCATION"] = self.env["CONAN_SYSTEM_LIBRARY_LOCATION"]
  290. tc.generate()
  291. deps = CMakeDeps(self)
  292. if tools.get_env("GENERATE_ONLY_BUILT_CONFIG", default=False):
  293. deps.generate()
  294. return
  295. # allow using prebuilt deps with all configs
  296. # credits to https://github.com/conan-io/conan/issues/11607#issuecomment-1188500937 for the workaround
  297. configs = [
  298. "Debug",
  299. "MinSizeRel",
  300. "Release",
  301. "RelWithDebInfo",
  302. ]
  303. for config in configs:
  304. print(f"generating CMakeDeps for {config}")
  305. deps.configuration = config
  306. deps.generate()
  307. def imports(self):
  308. if is_apple_os(self):
  309. self.copy("*.dylib", "Frameworks", "lib")
  310. elif self.settings.os == "Windows":
  311. self.copy("*.dll", src="bin/archdatadir/plugins/platforms", dst="platforms")
  312. self.copy("*.dll", src="bin/archdatadir/plugins/styles", dst="styles")
  313. self.copy("*.dll", src="@bindirs", dst="", excludes="archdatadir/*")
  314. elif self.settings.os == "Android":
  315. self.copy("*.so", ".", "lib")