conanfile.py 12 KB

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