|
| 1 | +import os |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from conan import ConanFile |
| 6 | +from conan.errors import ConanInvalidConfiguration |
| 7 | +from conan.tools.files import copy, download, get, load, rename, replace_in_file |
| 8 | + |
| 9 | +class CudaToolkitConan(ConanFile): |
| 10 | + name = "cuda-toolkit" |
| 11 | + package_type = "shared-library" |
| 12 | + version = "13.3.1" |
| 13 | + license = "CUDA Toolkit End-User License Agreement" |
| 14 | + url = "https://github.com/conan-io/cuda-conan" |
| 15 | + homepage = "https://developer.nvidia.com/cuda-toolkit" |
| 16 | + description = "A development environment for creating high performance GPU-accelerated applications" |
| 17 | + settings = "os", "arch", "compiler", "build_type" |
| 18 | + |
| 19 | + def validate(self): |
| 20 | + if self.settings.os not in ["Windows", "Linux"]: |
| 21 | + raise ConanInvalidConfiguration("Operating system not supported") |
| 22 | + |
| 23 | + # TODO: Add checks for supported compilers as per https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#id56 |
| 24 | + |
| 25 | + def build(self): |
| 26 | + base_url = "https://developer.download.nvidia.com/compute/cuda/redist" |
| 27 | + distrib = f"redistrib_{self.version}.json" |
| 28 | + json_distrib = f"{base_url}/{distrib}" |
| 29 | + download(self, url=json_distrib, filename=distrib) |
| 30 | + json_content = load(self, distrib) |
| 31 | + cuda_distrib = json.loads(json_content) |
| 32 | + |
| 33 | + if self.settings.os == "Linux" and self.settings.arch == "armv8": |
| 34 | + cuda_platform = "linux-sbsa" |
| 35 | + else: |
| 36 | + cuda_platform = f"{str(self.settings.os).lower()}-{self.settings.arch}" |
| 37 | + |
| 38 | + self.output.info(f"About to download CUDA toolkit components for {cuda_platform}") |
| 39 | + |
| 40 | + components = [ |
| 41 | + "cccl", |
| 42 | + "cuda_crt", |
| 43 | + "cuda_cudart", |
| 44 | + "cuda_cupti", |
| 45 | + "cuda_cuxxfilt", |
| 46 | + "cuda_nvcc", |
| 47 | + "cuda_nvml_dev", |
| 48 | + "cuda_nvrtc", |
| 49 | + "cuda_nvtx", |
| 50 | + "cuda_profiler_api", |
| 51 | + "cuda_sanitizer_api", |
| 52 | + "libcublas", |
| 53 | + "libcufft", |
| 54 | + "libcurand", |
| 55 | + "libcusolver", |
| 56 | + "libcusparse", |
| 57 | + "libnpp", |
| 58 | + "libnvfatbin", |
| 59 | + "libnvjitlink", |
| 60 | + "libnvjpeg", |
| 61 | + "libnvptxcompiler", |
| 62 | + "libnvvm", |
| 63 | + "cuda_tileiras", |
| 64 | + ] |
| 65 | + |
| 66 | + platform_components = { |
| 67 | + "linux-x86_64": ["libcufile", "cuda_sandbox_dev", "cuda_opencl", "cuda_culibos", "cuda_compat", "libcuobjclient"], |
| 68 | + "linux-sbsa": ["libcufile", "cuda_sandbox_dev", "cuda_culibos", "cuda_compat", "cuda_compat_orin", "libcudla", "libcuobjclient"], |
| 69 | + "windows-x86_64": ["cuda_opencl", "visual_studio_integration"], |
| 70 | + } |
| 71 | + components += platform_components.get(cuda_platform, []) |
| 72 | + |
| 73 | + for component_name, data in cuda_distrib.items(): |
| 74 | + if component_name not in components: |
| 75 | + self.output.info(f"Skipping {component_name}") |
| 76 | + continue |
| 77 | + else: |
| 78 | + self.output.info(f"About to download {component_name} for {cuda_platform}") |
| 79 | + |
| 80 | + url = f"{base_url}/{(data[cuda_platform]['cuda13.3'] if component_name == 'cuda_compat' else data[cuda_platform])['relative_path']}" |
| 81 | + checksum = (data[cuda_platform]['cuda13.3'] if component_name == 'cuda_compat' else data[cuda_platform])['sha256'] |
| 82 | + |
| 83 | + get(self, url, sha256=checksum, destination="cuda", strip_root=True, keep_permissions=False) |
| 84 | + rename(self, f"{self.build_folder}/cuda/LICENSE", f"LICENSE_{component_name}") |
| 85 | + if self.settings.os == "Linux": |
| 86 | + # When crossbuilding, point nvcc to the target directory in the "host" package (for the target architecture), |
| 87 | + # rather than have it look relative to itself (which is the current architecture of the build machine) |
| 88 | + replace_in_file(self, "cuda/bin/nvcc.profile", '$(TOP)/$(_TARGET_DIR_)/', '$(CONAN_CUDA_TARGET_DIR)/') |
| 89 | + replace_in_file(self, "cuda/bin/nvcc.profile", "lib$(_TARGET_SIZE_)", "lib" ) |
| 90 | + |
| 91 | + def package(self): |
| 92 | + copy(self, "LICENSE*", src=self.build_folder, dst=os.path.join(self.package_folder, "licenses")) |
| 93 | + |
| 94 | + for folder in ["bin", "nvml", "nvvm", "compat", "compat_orin"]: |
| 95 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", folder), dst=os.path.join(self.package_folder, folder)) |
| 96 | + |
| 97 | + if self.settings.os == "Linux": |
| 98 | + arch = "sbsa" if self.settings.arch == "armv8" else str(self.settings.arch) |
| 99 | + targets_folder = f"targets/{arch}-linux" |
| 100 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", "lib"), dst=os.path.join(self.package_folder, targets_folder, "lib")) |
| 101 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", "include"), dst=os.path.join(self.package_folder, targets_folder, "include")) |
| 102 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", "res"), dst=os.path.join(self.package_folder, targets_folder, "res")) |
| 103 | + |
| 104 | + include_symlink = Path(f"targets/{arch}-linux/include") |
| 105 | + lib64_symlink = Path(f"targets/{arch}-linux/lib") |
| 106 | + res_symlink = Path(f"targets/{arch}-linux/res") |
| 107 | + include = Path(os.path.join(self.package_folder, "include")) |
| 108 | + lib = Path(os.path.join(self.package_folder, "lib64")) |
| 109 | + res = Path(os.path.join(self.package_folder, "res")) |
| 110 | + include.symlink_to(include_symlink) |
| 111 | + lib.symlink_to(lib64_symlink) |
| 112 | + res.symlink_to(res_symlink) |
| 113 | + elif self.settings.os == "Windows": |
| 114 | + for folder in ["compute-sanitizer", "include", "lib"]: |
| 115 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", folder), dst=os.path.join(self.package_folder, folder)) |
| 116 | + |
| 117 | + copy(self, "*", src=os.path.join(self.build_folder, "cuda", "visual_studio_integration"), |
| 118 | + dst=os.path.join(self.package_folder, "extras", "visual_studio_integration") ) |
| 119 | + |
| 120 | + def package_id(self): |
| 121 | + # only os and arch matter for this package |
| 122 | + self.info.settings.rm_safe("compiler") |
| 123 | + self.info.settings.rm_safe("build_type") |
| 124 | + |
| 125 | + def package_info(self): |
| 126 | + self.cpp_info.set_property("cmake_file_name", "CUDAToolkit") # what consumers will expect |
| 127 | + self.cpp_info.set_property("cmake_find_mode", "none") # but don't generate it |
| 128 | + self.cpp_info.set_property("cmake_target_name", "CUDA::toolkit") |
| 129 | + |
| 130 | + nvcc = "nvcc.exe" if self.settings.os == "Windows" else "nvcc" |
| 131 | + self.buildenv_info.define_path("CUDACXX", os.path.join(self.package_folder, "bin", nvcc)) |
| 132 | + |
| 133 | + if not self.settings_target: |
| 134 | + arch = "sbsa" if self.settings.arch == "armv8" else str(self.settings.arch) |
| 135 | + self.buildenv_info.define_path("CUDAToolkit_ROOT", self.package_folder) |
| 136 | + self.buildenv_info.define_path("CONAN_CUDA_TARGET_DIR", os.path.join(self.package_folder, "targets", f"{arch}-linux")) |
| 137 | + |
| 138 | + if self.settings.os == "Linux": |
| 139 | + self.cpp_info.libdirs = ["lib64"] |
| 140 | + else: |
| 141 | + self.cpp_info.libdirs = ["lib", "lib/x64"] |
| 142 | + self.cpp_info.bindirs = ["bin", "bin/x64"] |
| 143 | + |
| 144 | + if self.settings_build.os == "Windows": |
| 145 | + # Only has effect when Windows and the CMake generator is Visual Studio |
| 146 | + package_folder = f"{self.package_folder}".replace('\\', '/') |
| 147 | + self.conf_info.define("tools.cmake.cmaketoolchain:toolset_cuda", package_folder) |
| 148 | + |
| 149 | + if self.settings_target and self.settings_target.get_safe("build_type"): |
| 150 | + self.conf_info.update("tools.cmake.cmaketoolchain:extra_variables", |
| 151 | + {"CMAKE_TRY_COMPILE_CONFIGURATION": str(self.settings_target.build_type)}) |
0 commit comments