conanfile.py 16 KB

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