conanfile.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import os
  2. from pathlib import Path
  3. from jinja2 import Template
  4. from conan import ConanFile
  5. from conan.tools.files import copy, rmdir, save, mkdir, rm
  6. from conan.tools.microsoft import unix_path
  7. from conan.tools.env import VirtualRunEnv, Environment, VirtualBuildEnv
  8. from conan.tools.scm import Version
  9. from conan.errors import ConanInvalidConfiguration, ConanException
  10. required_conan_version = ">=1.58.0 <2.0.0"
  11. class CuraConan(ConanFile):
  12. name = "cura"
  13. license = "LGPL-3.0"
  14. author = "UltiMaker"
  15. url = "https://github.com/Ultimaker/cura"
  16. description = "3D printer / slicing GUI built on top of the Uranium framework"
  17. topics = ("conan", "python", "pyqt6", "qt", "qml", "3d-printing", "slicer")
  18. build_policy = "missing"
  19. exports = "LICENSE*", "*.jinja"
  20. settings = "os", "compiler", "build_type", "arch"
  21. # FIXME: Remove specific branch once merged to main
  22. python_requires = "translationextractor/[>=2.1.1]@ultimaker/stable"
  23. options = {
  24. "enterprise": ["True", "False", "true", "false"], # Workaround for GH Action passing boolean as lowercase string
  25. "staging": ["True", "False", "true", "false"], # Workaround for GH Action passing boolean as lowercase string
  26. "devtools": [True, False], # FIXME: Split this up in testing and (development / build (pyinstaller) / system installer) tools
  27. "cloud_api_version": "ANY",
  28. "display_name": "ANY", # TODO: should this be an option??
  29. "cura_debug_mode": [True, False], # FIXME: Use profiles
  30. "internal": [True, False]
  31. }
  32. default_options = {
  33. "enterprise": "False",
  34. "staging": "False",
  35. "devtools": False,
  36. "cloud_api_version": "1",
  37. "display_name": "UltiMaker Cura",
  38. "cura_debug_mode": False, # Not yet implemented
  39. "internal": False,
  40. }
  41. def set_version(self):
  42. if not self.version:
  43. self.version = "5.6.0-alpha"
  44. @property
  45. def _pycharm_targets(self):
  46. return self.conan_data["pycharm_targets"]
  47. # FIXME: These env vars should be defined in the runenv.
  48. _cura_env = None
  49. @property
  50. def _cura_run_env(self):
  51. if self._cura_env:
  52. return self._cura_env
  53. self._cura_env = Environment()
  54. self._cura_env.define("QML2_IMPORT_PATH", str(self._site_packages.joinpath("PyQt6", "Qt6", "qml")))
  55. self._cura_env.define("QT_PLUGIN_PATH", str(self._site_packages.joinpath("PyQt6", "Qt6", "plugins")))
  56. if self.settings.os == "Linux":
  57. self._cura_env.define("QT_QPA_FONTDIR", "/usr/share/fonts")
  58. self._cura_env.define("QT_QPA_PLATFORMTHEME", "xdgdesktopportal")
  59. self._cura_env.define("QT_XKB_CONFIG_ROOT", "/usr/share/X11/xkb")
  60. return self._cura_env
  61. @property
  62. def _enterprise(self):
  63. return self.options.enterprise in ["True", 'true']
  64. @property
  65. def _app_name(self):
  66. if self._enterprise:
  67. return str(self.options.display_name) + " Enterprise"
  68. return str(self.options.display_name)
  69. @property
  70. def _urls(self):
  71. if self.options.staging in ["True", 'true']:
  72. return "staging"
  73. return "default"
  74. @property
  75. def requirements_txts(self):
  76. if self.options.devtools:
  77. return ["requirements.txt", "requirements-ultimaker.txt", "requirements-dev.txt"]
  78. return ["requirements.txt", "requirements-ultimaker.txt"]
  79. @property
  80. def _base_dir(self):
  81. if self.install_folder is None:
  82. if self.build_folder is not None:
  83. return Path(self.build_folder)
  84. return Path(os.getcwd(), "venv")
  85. if self.in_local_cache:
  86. return Path(self.install_folder)
  87. else:
  88. return Path(self.source_folder, "venv")
  89. @property
  90. def _share_dir(self):
  91. return self._base_dir.joinpath("share")
  92. @property
  93. def _script_dir(self):
  94. if self.settings.os == "Windows":
  95. return self._base_dir.joinpath("Scripts")
  96. return self._base_dir.joinpath("bin")
  97. @property
  98. def _site_packages(self):
  99. if self.settings.os == "Windows":
  100. return self._base_dir.joinpath("Lib", "site-packages")
  101. py_version = Version(self.deps_cpp_info["cpython"].version)
  102. return self._base_dir.joinpath("lib", f"python{py_version.major}.{py_version.minor}", "site-packages")
  103. @property
  104. def _py_interp(self):
  105. py_interp = self._script_dir.joinpath(Path(self.deps_user_info["cpython"].python).name)
  106. if self.settings.os == "Windows":
  107. py_interp = Path(*[f'"{p}"' if " " in p else p for p in py_interp.parts])
  108. return py_interp
  109. @property
  110. def _pyinstaller_spec_arch(self):
  111. if self.settings.os == "Macos":
  112. if self.settings.arch == "armv8":
  113. return "'arm64'"
  114. return "'x86_64'"
  115. return "None"
  116. def _conan_installs(self):
  117. conan_installs = {}
  118. # list of conan installs
  119. for dependency in self.dependencies.host.values():
  120. conan_installs[dependency.ref.name] = {
  121. "version": dependency.ref.version,
  122. "revision": dependency.ref.revision
  123. }
  124. return conan_installs
  125. def _generate_cura_version(self, location):
  126. with open(os.path.join(self.recipe_folder, "CuraVersion.py.jinja"), "r") as f:
  127. cura_version_py = Template(f.read())
  128. # If you want a specific Cura version to show up on the splash screen add the user configuration `user.cura:version=VERSION`
  129. # the global.conf, profile, package_info (of dependency) or via the cmd line `-c user.cura:version=VERSION`
  130. cura_version = Version(self.conf.get("user.cura:version", default = self.version, check_type = str))
  131. pre_tag = f"-{cura_version.pre}" if cura_version.pre else ""
  132. build_tag = f"+{cura_version.build}" if cura_version.build else ""
  133. internal_tag = f"+internal" if self.options.internal else ""
  134. cura_version = f"{cura_version.major}.{cura_version.minor}.{cura_version.patch}{pre_tag}{build_tag}{internal_tag}"
  135. with open(os.path.join(location, "CuraVersion.py"), "w") as f:
  136. f.write(cura_version_py.render(
  137. cura_app_name = self.name,
  138. cura_app_display_name = self._app_name,
  139. cura_version = cura_version,
  140. cura_build_type = "Enterprise" if self._enterprise else "",
  141. cura_debug_mode = self.options.cura_debug_mode,
  142. cura_cloud_api_root = self.conan_data["urls"][self._urls]["cloud_api_root"],
  143. cura_cloud_api_version = self.options.cloud_api_version,
  144. cura_cloud_account_api_root = self.conan_data["urls"][self._urls]["cloud_account_api_root"],
  145. cura_marketplace_root = self.conan_data["urls"][self._urls]["marketplace_root"],
  146. cura_digital_factory_url = self.conan_data["urls"][self._urls]["digital_factory_url"],
  147. cura_latest_url=self.conan_data["urls"][self._urls]["cura_latest_url"],
  148. conan_installs=self._conan_installs(),
  149. ))
  150. def export_sources(self):
  151. copy(self, "*", os.path.join(self.recipe_folder, "plugins"), os.path.join(self.export_sources_folder, "plugins"))
  152. copy(self, "*", os.path.join(self.recipe_folder, "resources"), os.path.join(self.export_sources_folder, "resources"), excludes = "*.mo")
  153. copy(self, "*", os.path.join(self.recipe_folder, "tests"), os.path.join(self.export_sources_folder, "tests"))
  154. copy(self, "*", os.path.join(self.recipe_folder, "cura"), os.path.join(self.export_sources_folder, "cura"), excludes="CuraVersion.py")
  155. copy(self, "*", os.path.join(self.recipe_folder, "packaging"), os.path.join(self.export_sources_folder, "packaging"))
  156. copy(self, "*", os.path.join(self.recipe_folder, ".run_templates"), os.path.join(self.export_sources_folder, ".run_templates"))
  157. copy(self, "requirements.txt", self.recipe_folder, self.export_sources_folder)
  158. copy(self, "requirements-dev.txt", self.recipe_folder, self.export_sources_folder)
  159. copy(self, "requirements-ultimaker.txt", self.recipe_folder, self.export_sources_folder)
  160. copy(self, "cura_app.py", self.recipe_folder, self.export_sources_folder)
  161. def configure(self):
  162. self.options["pyarcus"].shared = True
  163. self.options["pysavitar"].shared = True
  164. self.options["pynest2d"].shared = True
  165. self.options["cpython"].shared = True
  166. self.options["boost"].header_only = True
  167. if self.settings.os == "Linux":
  168. self.options["curaengine_grpc_definitions"].shared = True
  169. def validate(self):
  170. version = self.conf_info.get("user.cura:version", default = self.version, check_type = str)
  171. if version and Version(version) <= Version("4"):
  172. raise ConanInvalidConfiguration("Only versions 5+ are support")
  173. def requirements(self):
  174. self.requires("boost/1.82.0")
  175. self.requires("curaengine_grpc_definitions/(latest)@ultimaker/testing")
  176. self.requires("zlib/1.2.13")
  177. self.requires("pyarcus/5.3.0")
  178. self.requires("curaengine/(latest)@ultimaker/testing")
  179. self.requires("pysavitar/5.3.0")
  180. self.requires("pynest2d/5.3.0")
  181. self.requires("curaengine_plugin_gradual_flow/0.1.0")
  182. self.requires("uranium/(latest)@ultimaker/testing")
  183. self.requires("cura_binary_data/(latest)@ultimaker/testing")
  184. self.requires("cpython/3.10.4")
  185. if self.options.internal:
  186. self.requires("cura_private_data/(latest)@ultimaker/testing")
  187. self.requires("fdm_materials/(latest)@internal/testing")
  188. else:
  189. self.requires("fdm_materials/(latest)@ultimaker/testing")
  190. def build_requirements(self):
  191. if self.options.devtools:
  192. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type = str):
  193. # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  194. self.tool_requires("gettext/0.21@ultimaker/testing", force_host_context = True)
  195. def layout(self):
  196. self.folders.source = "."
  197. self.folders.build = "venv"
  198. self.folders.generators = os.path.join(self.folders.build, "conan")
  199. self.cpp.package.libdirs = [os.path.join("site-packages", "cura")]
  200. self.cpp.package.bindirs = ["bin"]
  201. self.cpp.package.resdirs = ["resources", "plugins", "packaging", "pip_requirements"] # pip_requirements should be the last item in the list
  202. def generate(self):
  203. copy(self, "cura_app.py", self.source_folder, str(self._script_dir))
  204. cura_run_envvars = self._cura_run_env.vars(self, scope = "run")
  205. ext = ".ps1" if self.settings.os == "Windows" else ".sh"
  206. cura_run_envvars.save_script(os.path.join(self.folders.generators, f"cura_run_environment{ext}"))
  207. vr = VirtualRunEnv(self)
  208. vr.generate()
  209. self._generate_cura_version(os.path.join(self.source_folder, "cura"))
  210. if not self.in_local_cache:
  211. # Copy CuraEngine.exe to bindirs of Virtual Python Environment
  212. curaengine = self.dependencies["curaengine"].cpp_info
  213. copy(self, "CuraEngine.exe", curaengine.bindirs[0], self.source_folder, keep_path = False)
  214. copy(self, "CuraEngine", curaengine.bindirs[0], self.source_folder, keep_path = False)
  215. # Copy the external plugins that we want to bundle with Cura
  216. rmdir(self,str(self.source_path.joinpath("plugins", "CuraEngineGradualFlow")))
  217. curaengine_plugin_gradual_flow = self.dependencies["curaengine_plugin_gradual_flow"].cpp_info
  218. copy(self, "*", curaengine_plugin_gradual_flow.resdirs[0], str(self.source_path.joinpath("plugins", "CuraEngineGradualFlow")), keep_path = True)
  219. copy(self, "*", curaengine_plugin_gradual_flow.bindirs[0], self.source_folder, keep_path = False)
  220. copy(self, "bundled_*.json", curaengine_plugin_gradual_flow.resdirs[1], str(self.source_path.joinpath("resources", "bundled_packages")), keep_path = False)
  221. # Copy resources of cura_binary_data
  222. cura_binary_data = self.dependencies["cura_binary_data"].cpp_info
  223. copy(self, "*", cura_binary_data.resdirs[0], str(self._share_dir.joinpath("cura")), keep_path = True)
  224. copy(self, "*", cura_binary_data.resdirs[1], str(self._share_dir.joinpath("uranium")), keep_path = True)
  225. if self.settings.os == "Windows":
  226. copy(self, "*", cura_binary_data.resdirs[2], str(self._share_dir.joinpath("windows")), keep_path = True)
  227. for dependency in self.dependencies.host.values():
  228. for bindir in dependency.cpp_info.bindirs:
  229. copy(self, "*.dll", bindir, str(self._site_packages), keep_path = False)
  230. for libdir in dependency.cpp_info.libdirs:
  231. copy(self, "*.pyd", libdir, str(self._site_packages), keep_path = False)
  232. copy(self, "*.pyi", libdir, str(self._site_packages), keep_path = False)
  233. copy(self, "*.dylib", libdir, str(self._base_dir.joinpath("lib")), keep_path = False)
  234. # Copy materials (flat)
  235. rmdir(self, os.path.join(self.source_folder, "resources", "materials"))
  236. fdm_materials = self.dependencies["fdm_materials"].cpp_info
  237. copy(self, "*", fdm_materials.resdirs[0], self.source_folder)
  238. # Copy internal resources
  239. if self.options.internal:
  240. cura_private_data = self.dependencies["cura_private_data"].cpp_info
  241. copy(self, "*", cura_private_data.resdirs[0], str(self._share_dir.joinpath("cura")))
  242. if self.options.devtools:
  243. # Update the po and pot files
  244. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type=str):
  245. vb = VirtualBuildEnv(self)
  246. vb.generate()
  247. # # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  248. # cpp_info = self.dependencies["gettext"].cpp_info
  249. # pot = self.python_requires["translationextractor"].module.ExtractTranslations(self, cpp_info.bindirs[0])
  250. # pot.generate()
  251. def build(self):
  252. if self.options.devtools:
  253. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type = str):
  254. # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  255. for po_file in self.source_path.joinpath("resources", "i18n").glob("**/*.po"):
  256. mo_file = Path(self.build_folder, po_file.with_suffix('.mo').relative_to(self.source_path))
  257. mo_file = mo_file.parent.joinpath("LC_MESSAGES", mo_file.name)
  258. mkdir(self, str(unix_path(self, Path(mo_file).parent)))
  259. cpp_info = self.dependencies["gettext"].cpp_info
  260. self.run(f"{cpp_info.bindirs[0]}/msgfmt {po_file} -o {mo_file} -f", env="conanbuild", ignore_errors=True)
  261. def deploy(self):
  262. copy(self, "*", os.path.join(self.package_folder, self.cpp.package.resdirs[2]), os.path.join(self.install_folder, "packaging"), keep_path = True)
  263. # Copy resources of Cura (keep folder structure) needed by pyinstaller to determine the module structure
  264. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.bindirs[0]), str(self._base_dir), keep_path = False)
  265. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.libdirs[0]), str(self._site_packages.joinpath("cura")), keep_path = True)
  266. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.resdirs[0]), str(self._share_dir.joinpath("cura", "resources")), keep_path = True)
  267. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.resdirs[1]), str(self._share_dir.joinpath("cura", "plugins")), keep_path = True)
  268. # Copy resources of Uranium (keep folder structure)
  269. uranium = self.dependencies["uranium"].cpp_info
  270. copy(self, "*", uranium.resdirs[0], str(self._share_dir.joinpath("uranium", "resources")), keep_path = True)
  271. copy(self, "*", uranium.resdirs[1], str(self._share_dir.joinpath("uranium", "plugins")), keep_path = True)
  272. copy(self, "*", uranium.libdirs[0], str(self._site_packages.joinpath("UM")), keep_path = True)
  273. # Generate the GitHub Action version info Environment
  274. version = self.conf_info.get("user.cura:version", default = self.version, check_type = str)
  275. cura_version = Version(version)
  276. env_prefix = "Env:" if self.settings.os == "Windows" else ""
  277. activate_github_actions_version_env = Template(r"""echo "CURA_VERSION_MAJOR={{ cura_version_major }}" >> ${{ env_prefix }}GITHUB_ENV
  278. echo "CURA_VERSION_MINOR={{ cura_version_minor }}" >> ${{ env_prefix }}GITHUB_ENV
  279. echo "CURA_VERSION_PATCH={{ cura_version_patch }}" >> ${{ env_prefix }}GITHUB_ENV
  280. echo "CURA_VERSION_BUILD={{ cura_version_build }}" >> ${{ env_prefix }}GITHUB_ENV
  281. echo "CURA_VERSION_FULL={{ cura_version_full }}" >> ${{ env_prefix }}GITHUB_ENV
  282. echo "CURA_APP_NAME={{ cura_app_name }}" >> ${{ env_prefix }}GITHUB_ENV
  283. """).render(cura_version_major = cura_version.major,
  284. cura_version_minor = cura_version.minor,
  285. cura_version_patch = cura_version.patch,
  286. cura_version_build = cura_version.build if cura_version.build != "" else "0",
  287. cura_version_full = self.version,
  288. cura_app_name = self._app_name,
  289. env_prefix = env_prefix)
  290. ext = ".sh" if self.settings.os != "Windows" else ".ps1"
  291. save(self, os.path.join(self._script_dir, f"activate_github_actions_version_env{ext}"), activate_github_actions_version_env)
  292. self._generate_cura_version(os.path.join(self._site_packages, "cura"))
  293. def package(self):
  294. copy(self, "cura_app.py", src = self.source_folder, dst = os.path.join(self.package_folder, self.cpp.package.bindirs[0]))
  295. copy(self, "*", src = os.path.join(self.source_folder, "cura"), dst = os.path.join(self.package_folder, self.cpp.package.libdirs[0]))
  296. copy(self, "*", src = os.path.join(self.source_folder, "resources"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[0]))
  297. copy(self, "*.mo", os.path.join(self.build_folder, "resources"), os.path.join(self.package_folder, "resources"))
  298. copy(self, "*", src = os.path.join(self.source_folder, "plugins"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[1]))
  299. copy(self, "requirement*.txt", src = self.source_folder, dst = os.path.join(self.package_folder, self.cpp.package.resdirs[-1]))
  300. copy(self, "*", src = os.path.join(self.source_folder, "packaging"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[2]))
  301. # Remove the CuraEngineGradualFlow plugin from the package
  302. rmdir(self, os.path.join(self.package_folder, self.cpp.package.resdirs[1], "CuraEngineGradualFlow"))
  303. rm(self, "bundled_*.json", os.path.join(self.package_folder, self.cpp.package.resdirs[0], "bundled_packages"), recursive = False)
  304. # Remove the fdm_materials from the package
  305. rmdir(self, os.path.join(self.package_folder, self.cpp.package.resdirs[0], "materials"))
  306. def package_info(self):
  307. self.user_info.pip_requirements = "requirements.txt"
  308. self.user_info.pip_requirements_git = "requirements-ultimaker.txt"
  309. self.user_info.pip_requirements_build = "requirements-dev.txt"
  310. if self.in_local_cache:
  311. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.package_folder, "site-packages"))
  312. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.package_folder, "plugins"))
  313. else:
  314. self.runenv_info.append_path("PYTHONPATH", self.source_folder)
  315. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.source_folder, "plugins"))
  316. def package_id(self):
  317. self.info.clear()
  318. # The following options shouldn't be used to determine the hash, since these are only used to set the CuraVersion.py
  319. # which will als be generated by the deploy method during the `conan install cura/5.1.0@_/_`
  320. del self.info.options.enterprise
  321. del self.info.options.staging
  322. del self.info.options.devtools
  323. del self.info.options.cloud_api_version
  324. del self.info.options.display_name
  325. del self.info.options.cura_debug_mode
  326. # TODO: Use the hash of requirements.txt and requirements-ultimaker.txt, Because changing these will actually result in a different
  327. # Cura. This is needed because the requirements.txt aren't managed by Conan and therefor not resolved in the package_id. This isn't
  328. # ideal but an acceptable solution for now.