conanfile.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "sdl/[~2.26.1 || >=2.0.20 <=2.22.0]", # versions in between have broken sound
  16. "sdl_image/[~2.0.5]",
  17. "sdl_mixer/[~2.0.4]",
  18. "sdl_ttf/[~2.0.18]",
  19. "onetbb/[^2021.3]",
  20. ]
  21. _nullkillerAIRequires = [
  22. ]
  23. requires = _libRequires + _clientRequires + _nullkillerAIRequires
  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. "onetbb/*:shared": True,
  38. }
  39. def configure(self):
  40. self.options["ffmpeg"].shared = self.settings.os == "Android" # using shared version results in less total project size on Android
  41. self.options["freetype"].shared = self.settings.os == "Android"
  42. # SDL_image and Qt depend on it, in iOS both are static
  43. # Enable static libpng due to https://github.com/conan-io/conan-center-index/issues/15440,
  44. # which leads to VCMI crashes of MinGW
  45. self.options["libpng"].shared = not (self.settings.os == "Windows" and cross_building(self)) and self.settings.os != "iOS"
  46. # static Qt for iOS is the only viable option at the moment
  47. self.options["qt"].shared = self.settings.os != "iOS"
  48. if self.settings.os == "Android":
  49. self.options["qt"].android_sdk = tools.get_env("ANDROID_HOME", default="")
  50. # TODO: enable for all platforms
  51. if self.settings.os == "Android":
  52. self.options["bzip2"].shared = True
  53. self.options["libiconv"].shared = True
  54. self.options["zlib"].shared = True
  55. if self.options.default_options_of_requirements:
  56. return
  57. # we need only the following Boost parts:
  58. # date_time filesystem iostreams locale program_options system thread
  59. # some other parts are also enabled because they're dependents
  60. # see e.g. conan-center-index/recipes/boost/all/dependencies
  61. self.options["boost"].without_context = True
  62. self.options["boost"].without_contract = True
  63. self.options["boost"].without_coroutine = True
  64. self.options["boost"].without_fiber = True
  65. self.options["boost"].without_graph = True
  66. self.options["boost"].without_graph_parallel = True
  67. self.options["boost"].without_json = True
  68. self.options["boost"].without_log = True
  69. self.options["boost"].without_math = True
  70. self.options["boost"].without_mpi = True
  71. self.options["boost"].without_nowide = True
  72. self.options["boost"].without_python = True
  73. self.options["boost"].without_serialization = True
  74. self.options["boost"].without_stacktrace = True
  75. self.options["boost"].without_test = True
  76. self.options["boost"].without_timer = True
  77. self.options["boost"].without_type_erasure = True
  78. self.options["boost"].without_wave = True
  79. self.options["ffmpeg"].avdevice = False
  80. self.options["ffmpeg"].avfilter = False
  81. self.options["ffmpeg"].postproc = False
  82. self.options["ffmpeg"].swresample = False
  83. self.options["ffmpeg"].with_asm = self.settings.os != "Android"
  84. self.options["ffmpeg"].with_freetype = False
  85. self.options["ffmpeg"].with_libfdk_aac = False
  86. self.options["ffmpeg"].with_libmp3lame = False
  87. self.options["ffmpeg"].with_libvpx = False
  88. self.options["ffmpeg"].with_libwebp = False
  89. self.options["ffmpeg"].with_libx264 = False
  90. self.options["ffmpeg"].with_libx265 = False
  91. self.options["ffmpeg"].with_openh264 = False
  92. self.options["ffmpeg"].with_openjpeg = False
  93. self.options["ffmpeg"].with_opus = False
  94. self.options["ffmpeg"].with_programs = False
  95. self.options["ffmpeg"].with_ssl = False
  96. self.options["ffmpeg"].with_vorbis = False
  97. self.options["sdl"].sdl2main = self.settings.os != "iOS"
  98. self.options["sdl"].vulkan = False
  99. self.options["sdl_image"].lbm = False
  100. self.options["sdl_image"].pnm = False
  101. self.options["sdl_image"].svg = False
  102. self.options["sdl_image"].tga = False
  103. self.options["sdl_image"].with_libjpeg = False
  104. self.options["sdl_image"].with_libtiff = False
  105. self.options["sdl_image"].with_libwebp = False
  106. self.options["sdl_image"].xcf = False
  107. self.options["sdl_image"].xpm = False
  108. self.options["sdl_image"].xv = False
  109. if is_apple_os(self):
  110. self.options["sdl_image"].imageio = True
  111. self.options["sdl_mixer"].flac = False
  112. self.options["sdl_mixer"].mad = False
  113. self.options["sdl_mixer"].mikmod = False
  114. self.options["sdl_mixer"].modplug = False
  115. self.options["sdl_mixer"].nativemidi = False
  116. self.options["sdl_mixer"].opus = False
  117. self.options["sdl_mixer"].wav = False
  118. def _disableQtOptions(disableFlag, options):
  119. return " ".join([f"-{disableFlag}-{tool}" for tool in options])
  120. _qtOptions = [
  121. _disableQtOptions("no", [
  122. "gif",
  123. "ico",
  124. ]),
  125. _disableQtOptions("no-feature", [
  126. # xpm format is required for Drag'n'Drop support
  127. "imageformat_bmp",
  128. "imageformat_jpeg",
  129. "imageformat_ppm",
  130. "imageformat_xbm",
  131. # we need only macdeployqt
  132. # TODO: disabling these doesn't disable generation of CMake targets
  133. # TODO: in Qt 6.3 it's a part of qtbase
  134. # "assistant",
  135. # "designer",
  136. # "distancefieldgenerator",
  137. # "kmap2qmap",
  138. # "linguist",
  139. # "makeqpf",
  140. # "pixeltool",
  141. # "qdbus",
  142. # "qev",
  143. # "qtattributionsscanner",
  144. # "qtdiag",
  145. # "qtpaths",
  146. # "qtplugininfo",
  147. ]),
  148. ]
  149. self.options["qt"].config = " ".join(_qtOptions)
  150. self.options["qt"].qttools = True
  151. self.options["qt"].qtandroidextras = self.settings.os == "Android" # TODO: in Qt 6 it's part of Core
  152. self.options["qt"].with_freetype = self.settings.os == "Android"
  153. self.options["qt"].with_libjpeg = False
  154. self.options["qt"].with_md4c = False
  155. self.options["qt"].with_mysql = False
  156. self.options["qt"].with_odbc = False
  157. self.options["qt"].with_openal = False
  158. self.options["qt"].with_pq = False
  159. self.options["qt"].openssl = not is_apple_os(self)
  160. if self.settings.os == "iOS" or self.settings.os == "Android":
  161. self.options["qt"].opengl = "es2"
  162. if not is_apple_os(self) and self.settings.os != "Android" and cross_building(self):
  163. self.options["qt"].cross_compile = self.env["CONAN_CROSS_COMPILE"]
  164. # TODO: add for all platforms after updating recipe
  165. if self.settings.os == "Android":
  166. self.options["qt"].essential_modules = False
  167. # No Qt OpenGL for cross-compiling for Windows, Conan does not support it
  168. if self.settings.os == "Windows" and cross_building(self):
  169. self.options["qt"].opengl = "no"
  170. # transitive deps
  171. # doesn't link to overridden bzip2 & zlib, the tool isn't needed anyway
  172. self.options["pcre2"].build_pcre2grep = False
  173. # executable not needed
  174. if self.settings.os == "Android":
  175. self.options["sqlite3"].build_executable = False
  176. def requirements(self):
  177. self.requires("freetype/[~2.12.1]", override=True) # sdl_ttf / Qt
  178. self.requires("libpng/[~1.6.39]", override=True) # freetype / qt / sdl_image
  179. # client
  180. if self.options.with_ffmpeg:
  181. self.requires("ffmpeg/[^4.4]")
  182. # launcher
  183. if self.settings.os == "Android":
  184. self.requires("qt/[~5.15.14]")
  185. else:
  186. self.requires("qt/[~5.15.2]")
  187. # TODO: version range doesn't work in Conan v1
  188. if self.options["qt"].openssl:
  189. self.requires("openssl/1.1.1s")
  190. # use Apple system libraries instead of external ones
  191. if self.options.with_apple_system_libs and is_apple_os(self):
  192. systemLibsOverrides = [
  193. "bzip2/1.0.8",
  194. "libiconv/1.17",
  195. "sqlite3/3.39.2",
  196. "zlib/1.2.12",
  197. ]
  198. for lib in systemLibsOverrides:
  199. self.requires(f"{lib}@vcmi/apple", override=True)
  200. elif self.settings.os == "Android":
  201. self.requires("zlib/1.2.12@vcmi/android", override=True)
  202. else:
  203. self.requires("zlib/[~1.2.13]", override=True) # minizip / Qt
  204. self.requires("libiconv/[~1.17]", override=True) # ffmpeg / sdl
  205. # TODO: the latest official release of LuaJIT (which is quite old) can't be built for arm
  206. if self.options.with_luajit and not str(self.settings.arch).startswith("arm"):
  207. self.requires("luajit/[~2.0.5]")
  208. def validate(self):
  209. if self.options.with_apple_system_libs and not is_apple_os(self):
  210. raise ConanInvalidConfiguration("with_apple_system_libs is only for Apple platforms")
  211. if self.options.with_apple_system_libs and self.options.default_options_of_requirements:
  212. raise ConanInvalidConfiguration("with_apple_system_libs and default_options_of_requirements can't be True at the same time")
  213. def generate(self):
  214. tc = CMakeToolchain(self)
  215. tc.variables["USING_CONAN"] = True
  216. tc.variables["CONAN_INSTALL_FOLDER"] = self.install_folder
  217. if self.settings.os == "Android":
  218. tc.variables["CMAKE_ANDROID_API"] = str(self.settings.os.api_level)
  219. tc.variables["ANDROID_SYSROOT_LIB_SUBDIR"] = {
  220. 'armv7': 'arm-linux-androideabi',
  221. 'armv8': 'aarch64-linux-android',
  222. 'x86': 'i686-linux-android',
  223. 'x86_64': 'x86_64-linux-android',
  224. }.get(str(self.settings.arch))
  225. if cross_building(self) and self.settings.os == "Windows":
  226. tc.variables["CONAN_SYSTEM_LIBRARY_LOCATION"] = self.env["CONAN_SYSTEM_LIBRARY_LOCATION"]
  227. tc.generate()
  228. deps = CMakeDeps(self)
  229. if tools.get_env("GENERATE_ONLY_BUILT_CONFIG", default=False):
  230. deps.generate()
  231. return
  232. # allow using prebuilt deps with all configs
  233. # credits to https://github.com/conan-io/conan/issues/11607#issuecomment-1188500937 for the workaround
  234. configs = [
  235. "Debug",
  236. "MinSizeRel",
  237. "Release",
  238. "RelWithDebInfo",
  239. ]
  240. for config in configs:
  241. print(f"generating CMakeDeps for {config}")
  242. deps.configuration = config
  243. deps.generate()
  244. def imports(self):
  245. if is_apple_os(self):
  246. self.copy("*.dylib", "Frameworks", "lib")
  247. elif self.settings.os == "Windows":
  248. self.copy("*.dll", src="bin/archdatadir/plugins/platforms", dst="platforms")
  249. self.copy("*.dll", src="bin/archdatadir/plugins/styles", dst="styles")
  250. self.copy("*.dll", src="@bindirs", dst="", excludes="archdatadir/*")
  251. elif self.settings.os == "Android":
  252. self.copy("*.so", ".", "lib")