conanfile.py 16 KB

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