conanfile.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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["dulcificum"].shared = True
  166. self.options["cpython"].shared = True
  167. self.options["boost"].header_only = True
  168. if self.settings.os == "Linux":
  169. self.options["curaengine_grpc_definitions"].shared = True
  170. def validate(self):
  171. version = self.conf_info.get("user.cura:version", default = self.version, check_type = str)
  172. if version and Version(version) <= Version("4"):
  173. raise ConanInvalidConfiguration("Only versions 5+ are support")
  174. def requirements(self):
  175. self.requires("boost/1.82.0")
  176. self.requires("fmt/9.0.0")
  177. self.requires("curaengine_grpc_definitions/(latest)@ultimaker/testing")
  178. self.requires("zlib/1.2.13")
  179. self.requires("pyarcus/5.3.0")
  180. self.requires("dulcificum/(latest)@ultimaker/cura_10561")
  181. self.requires("curaengine/(latest)@ultimaker/testing")
  182. self.requires("pysavitar/5.3.0")
  183. self.requires("pynest2d/5.3.0")
  184. self.requires("curaengine_plugin_gradual_flow/0.1.0")
  185. self.requires("uranium/(latest)@ultimaker/cura_10561")
  186. self.requires("cura_binary_data/(latest)@ultimaker/testing")
  187. self.requires("cpython/3.10.4")
  188. if self.options.internal:
  189. self.requires("cura_private_data/(latest)@ultimaker/testing")
  190. self.requires("fdm_materials/(latest)@internal/testing")
  191. else:
  192. self.requires("fdm_materials/(latest)@ultimaker/testing")
  193. def build_requirements(self):
  194. if self.options.devtools:
  195. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type = str):
  196. # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  197. self.tool_requires("gettext/0.21@ultimaker/testing", force_host_context = True)
  198. def layout(self):
  199. self.folders.source = "."
  200. self.folders.build = "venv"
  201. self.folders.generators = os.path.join(self.folders.build, "conan")
  202. self.cpp.package.libdirs = [os.path.join("site-packages", "cura")]
  203. self.cpp.package.bindirs = ["bin"]
  204. self.cpp.package.resdirs = ["resources", "plugins", "packaging", "pip_requirements"] # pip_requirements should be the last item in the list
  205. def generate(self):
  206. copy(self, "cura_app.py", self.source_folder, str(self._script_dir))
  207. cura_run_envvars = self._cura_run_env.vars(self, scope = "run")
  208. ext = ".ps1" if self.settings.os == "Windows" else ".sh"
  209. cura_run_envvars.save_script(os.path.join(self.folders.generators, f"cura_run_environment{ext}"))
  210. vr = VirtualRunEnv(self)
  211. vr.generate()
  212. self._generate_cura_version(os.path.join(self.source_folder, "cura"))
  213. if not self.in_local_cache:
  214. # Copy CuraEngine.exe to bindirs of Virtual Python Environment
  215. curaengine = self.dependencies["curaengine"].cpp_info
  216. copy(self, "CuraEngine.exe", curaengine.bindirs[0], self.source_folder, keep_path = False)
  217. copy(self, "CuraEngine", curaengine.bindirs[0], self.source_folder, keep_path = False)
  218. # Copy the external plugins that we want to bundle with Cura
  219. rmdir(self,str(self.source_path.joinpath("plugins", "CuraEngineGradualFlow")))
  220. curaengine_plugin_gradual_flow = self.dependencies["curaengine_plugin_gradual_flow"].cpp_info
  221. copy(self, "*", curaengine_plugin_gradual_flow.resdirs[0], str(self.source_path.joinpath("plugins", "CuraEngineGradualFlow")), keep_path = True)
  222. copy(self, "*", curaengine_plugin_gradual_flow.bindirs[0], self.source_folder, keep_path = False)
  223. copy(self, "bundled_*.json", curaengine_plugin_gradual_flow.resdirs[1], str(self.source_path.joinpath("resources", "bundled_packages")), keep_path = False)
  224. # Copy resources of cura_binary_data
  225. cura_binary_data = self.dependencies["cura_binary_data"].cpp_info
  226. copy(self, "*", cura_binary_data.resdirs[0], str(self._share_dir.joinpath("cura")), keep_path = True)
  227. copy(self, "*", cura_binary_data.resdirs[1], str(self._share_dir.joinpath("uranium")), keep_path = True)
  228. if self.settings.os == "Windows":
  229. copy(self, "*", cura_binary_data.resdirs[2], str(self._share_dir.joinpath("windows")), keep_path = True)
  230. for dependency in self.dependencies.host.values():
  231. for bindir in dependency.cpp_info.bindirs:
  232. copy(self, "*.dll", bindir, str(self._site_packages), keep_path = False)
  233. for libdir in dependency.cpp_info.libdirs:
  234. copy(self, "*.pyd", libdir, str(self._site_packages), keep_path = False)
  235. copy(self, "*.pyi", libdir, str(self._site_packages), keep_path = False)
  236. copy(self, "*.dylib", libdir, str(self._base_dir.joinpath("lib")), keep_path = False)
  237. # Copy materials (flat)
  238. rmdir(self, os.path.join(self.source_folder, "resources", "materials"))
  239. fdm_materials = self.dependencies["fdm_materials"].cpp_info
  240. copy(self, "*", fdm_materials.resdirs[0], self.source_folder)
  241. # Copy internal resources
  242. if self.options.internal:
  243. cura_private_data = self.dependencies["cura_private_data"].cpp_info
  244. copy(self, "*", cura_private_data.resdirs[0], str(self._share_dir.joinpath("cura")))
  245. if self.options.devtools:
  246. # Update the po and pot files
  247. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type=str):
  248. vb = VirtualBuildEnv(self)
  249. vb.generate()
  250. # # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  251. # cpp_info = self.dependencies["gettext"].cpp_info
  252. # pot = self.python_requires["translationextractor"].module.ExtractTranslations(self, cpp_info.bindirs[0])
  253. # pot.generate()
  254. def build(self):
  255. if self.options.devtools:
  256. if self.settings.os != "Windows" or self.conf.get("tools.microsoft.bash:path", check_type = str):
  257. # FIXME: once m4, autoconf, automake are Conan V2 ready use self.win_bash and add gettext as base tool_requirement
  258. for po_file in self.source_path.joinpath("resources", "i18n").glob("**/*.po"):
  259. mo_file = Path(self.build_folder, po_file.with_suffix('.mo').relative_to(self.source_path))
  260. mo_file = mo_file.parent.joinpath("LC_MESSAGES", mo_file.name)
  261. mkdir(self, str(unix_path(self, Path(mo_file).parent)))
  262. cpp_info = self.dependencies["gettext"].cpp_info
  263. self.run(f"{cpp_info.bindirs[0]}/msgfmt {po_file} -o {mo_file} -f", env="conanbuild", ignore_errors=True)
  264. def deploy(self):
  265. copy(self, "*", os.path.join(self.package_folder, self.cpp.package.resdirs[2]), os.path.join(self.install_folder, "packaging"), keep_path = True)
  266. # Copy resources of Cura (keep folder structure) needed by pyinstaller to determine the module structure
  267. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.bindirs[0]), str(self._base_dir), keep_path = False)
  268. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.libdirs[0]), str(self._site_packages.joinpath("cura")), keep_path = True)
  269. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.resdirs[0]), str(self._share_dir.joinpath("cura", "resources")), keep_path = True)
  270. copy(self, "*", os.path.join(self.package_folder, self.cpp_info.resdirs[1]), str(self._share_dir.joinpath("cura", "plugins")), keep_path = True)
  271. # Copy resources of Uranium (keep folder structure)
  272. uranium = self.dependencies["uranium"].cpp_info
  273. copy(self, "*", uranium.resdirs[0], str(self._share_dir.joinpath("uranium", "resources")), keep_path = True)
  274. copy(self, "*", uranium.resdirs[1], str(self._share_dir.joinpath("uranium", "plugins")), keep_path = True)
  275. copy(self, "*", uranium.libdirs[0], str(self._site_packages.joinpath("UM")), keep_path = True)
  276. # Generate the GitHub Action version info Environment
  277. version = self.conf_info.get("user.cura:version", default = self.version, check_type = str)
  278. cura_version = Version(version)
  279. env_prefix = "Env:" if self.settings.os == "Windows" else ""
  280. activate_github_actions_version_env = Template(r"""echo "CURA_VERSION_MAJOR={{ cura_version_major }}" >> ${{ env_prefix }}GITHUB_ENV
  281. echo "CURA_VERSION_MINOR={{ cura_version_minor }}" >> ${{ env_prefix }}GITHUB_ENV
  282. echo "CURA_VERSION_PATCH={{ cura_version_patch }}" >> ${{ env_prefix }}GITHUB_ENV
  283. echo "CURA_VERSION_BUILD={{ cura_version_build }}" >> ${{ env_prefix }}GITHUB_ENV
  284. echo "CURA_VERSION_FULL={{ cura_version_full }}" >> ${{ env_prefix }}GITHUB_ENV
  285. echo "CURA_APP_NAME={{ cura_app_name }}" >> ${{ env_prefix }}GITHUB_ENV
  286. """).render(cura_version_major = cura_version.major,
  287. cura_version_minor = cura_version.minor,
  288. cura_version_patch = cura_version.patch,
  289. cura_version_build = cura_version.build if cura_version.build != "" else "0",
  290. cura_version_full = self.version,
  291. cura_app_name = self._app_name,
  292. env_prefix = env_prefix)
  293. ext = ".sh" if self.settings.os != "Windows" else ".ps1"
  294. save(self, os.path.join(self._script_dir, f"activate_github_actions_version_env{ext}"), activate_github_actions_version_env)
  295. self._generate_cura_version(os.path.join(self._site_packages, "cura"))
  296. def package(self):
  297. copy(self, "cura_app.py", src = self.source_folder, dst = os.path.join(self.package_folder, self.cpp.package.bindirs[0]))
  298. copy(self, "*", src = os.path.join(self.source_folder, "cura"), dst = os.path.join(self.package_folder, self.cpp.package.libdirs[0]))
  299. copy(self, "*", src = os.path.join(self.source_folder, "resources"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[0]))
  300. copy(self, "*.mo", os.path.join(self.build_folder, "resources"), os.path.join(self.package_folder, "resources"))
  301. copy(self, "*", src = os.path.join(self.source_folder, "plugins"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[1]))
  302. copy(self, "requirement*.txt", src = self.source_folder, dst = os.path.join(self.package_folder, self.cpp.package.resdirs[-1]))
  303. copy(self, "*", src = os.path.join(self.source_folder, "packaging"), dst = os.path.join(self.package_folder, self.cpp.package.resdirs[2]))
  304. # Remove the CuraEngineGradualFlow plugin from the package
  305. rmdir(self, os.path.join(self.package_folder, self.cpp.package.resdirs[1], "CuraEngineGradualFlow"))
  306. rm(self, "bundled_*.json", os.path.join(self.package_folder, self.cpp.package.resdirs[0], "bundled_packages"), recursive = False)
  307. # Remove the fdm_materials from the package
  308. rmdir(self, os.path.join(self.package_folder, self.cpp.package.resdirs[0], "materials"))
  309. def package_info(self):
  310. self.user_info.pip_requirements = "requirements.txt"
  311. self.user_info.pip_requirements_git = "requirements-ultimaker.txt"
  312. self.user_info.pip_requirements_build = "requirements-dev.txt"
  313. if self.in_local_cache:
  314. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.package_folder, "site-packages"))
  315. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.package_folder, "plugins"))
  316. else:
  317. self.runenv_info.append_path("PYTHONPATH", self.source_folder)
  318. self.runenv_info.append_path("PYTHONPATH", os.path.join(self.source_folder, "plugins"))
  319. def package_id(self):
  320. self.info.clear()
  321. # The following options shouldn't be used to determine the hash, since these are only used to set the CuraVersion.py
  322. # which will als be generated by the deploy method during the `conan install cura/5.1.0@_/_`
  323. del self.info.options.enterprise
  324. del self.info.options.staging
  325. del self.info.options.devtools
  326. del self.info.options.cloud_api_version
  327. del self.info.options.display_name
  328. del self.info.options.cura_debug_mode
  329. # TODO: Use the hash of requirements.txt and requirements-ultimaker.txt, Because changing these will actually result in a different
  330. # Cura. This is needed because the requirements.txt aren't managed by Conan and therefor not resolved in the package_id. This isn't
  331. # ideal but an acceptable solution for now.