|
| 1 | +import os |
| 2 | + |
| 3 | +from conans import ConanFile, CMake, tools |
| 4 | +from conans.errors import ConanInvalidConfiguration |
| 5 | + |
| 6 | +class CspiceConan(ConanFile): |
| 7 | + name = "cspice" |
| 8 | + description = "NASA C SPICE library" |
| 9 | + license = "TSPA" |
| 10 | + topics = ("conan", "spice", "naif", "kernels", "space", "nasa", "jpl", "spacecraft", "planet", "robotics") |
| 11 | + homepage = "https://naif.jpl.nasa.gov/naif/toolkit.html" |
| 12 | + url = "https://github.com/conan-io/conan-center-index" |
| 13 | + exports_sources = ["CMakeLists.txt", "patches/**"] |
| 14 | + exports = ["TSPA.txt"] |
| 15 | + generators = "cmake" |
| 16 | + settings = "os", "arch", "compiler", "build_type" |
| 17 | + options = {"shared": [True, False], "fPIC": [True, False]} |
| 18 | + default_options = {"shared": False, "fPIC": True} |
| 19 | + |
| 20 | + _cmake = None |
| 21 | + |
| 22 | + @property |
| 23 | + def _source_subfolder(self): |
| 24 | + return "source_subfolder" |
| 25 | + |
| 26 | + @property |
| 27 | + def _build_subfolder(self): |
| 28 | + return "build_subfolder" |
| 29 | + |
| 30 | + @property |
| 31 | + def _sources_idx_per_triplet(self): |
| 32 | + # Index of source code per triplet in conadata's "sources" field. |
| 33 | + return { |
| 34 | + "Macos": { |
| 35 | + "x86": {"apple-clang": 0}, |
| 36 | + "x86_64": {"apple-clang": 1} |
| 37 | + }, |
| 38 | + "Linux": { |
| 39 | + "x86": {"gcc": 2}, |
| 40 | + "x86_64": {"gcc": 3} |
| 41 | + }, |
| 42 | + "Windows": { |
| 43 | + "x86": {"Visual Studio": 4}, |
| 44 | + "x86_64": {"Visual Studio": 5} |
| 45 | + }, |
| 46 | + "cygwin": { |
| 47 | + "x86": {"gcc": 6}, |
| 48 | + "x86_64": {"gcc": 7} |
| 49 | + }, |
| 50 | + "SunOs": { |
| 51 | + "x86": {"sun-cc": 8}, |
| 52 | + "x86_64": {"sun-cc": 9}, |
| 53 | + "sparc": {"gcc": 10, "sun-cc": 11}, |
| 54 | + "sparcv9": {"gcc": 12, "sun-cc": 13} |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + def config_options(self): |
| 59 | + if self.settings.os == "Windows": |
| 60 | + del self.options.fPIC |
| 61 | + |
| 62 | + def configure(self): |
| 63 | + del self.settings.compiler.libcxx |
| 64 | + del self.settings.compiler.cppstd |
| 65 | + |
| 66 | + self._raise_if_not_supported_triplet() |
| 67 | + |
| 68 | + def _raise_if_not_supported_triplet(self): |
| 69 | + the_os = self._get_os_or_subsystem() |
| 70 | + arch = str(self.settings.arch) |
| 71 | + compiler = str(self.settings.compiler) |
| 72 | + if the_os not in self._sources_idx_per_triplet: |
| 73 | + raise ConanInvalidConfiguration("cspice does not support {0}".format(the_os)) |
| 74 | + if arch not in self._sources_idx_per_triplet[the_os]: |
| 75 | + raise ConanInvalidConfiguration("cspice does not support {0} {1}".format(the_os, arch)) |
| 76 | + if compiler not in self._sources_idx_per_triplet[the_os][arch]: |
| 77 | + raise ConanInvalidConfiguration("cspice does not support {0} on {1} {2}".format(compiler, the_os, arch)) |
| 78 | + |
| 79 | + def _get_os_or_subsystem(self): |
| 80 | + if self.settings.os == "Windows" and self.settings.os.subsystem != "None": |
| 81 | + os_or_subsystem = str(self.settings.os.subsystem) |
| 82 | + else: |
| 83 | + os_or_subsystem = str(self.settings.os) |
| 84 | + return os_or_subsystem |
| 85 | + |
| 86 | + def source(self): |
| 87 | + pass |
| 88 | + |
| 89 | + def build(self): |
| 90 | + self._get_sources() |
| 91 | + os.rename(self.name, self._source_subfolder) |
| 92 | + for patch in self.conan_data["patches"][self.version]: |
| 93 | + tools.patch(**patch) |
| 94 | + cmake = self._configure_cmake() |
| 95 | + cmake.build() |
| 96 | + |
| 97 | + def _get_sources(self): |
| 98 | + the_os = self._get_os_or_subsystem() |
| 99 | + arch = str(self.settings.arch) |
| 100 | + compiler = str(self.settings.compiler) |
| 101 | + source_idx = self._sources_idx_per_triplet[the_os][arch][compiler] |
| 102 | + source = self.conan_data["sources"][self.version][source_idx] |
| 103 | + url = source["url"] |
| 104 | + if url.endswith(".tar.Z"): # Python doesn't have any module to uncompress .Z files |
| 105 | + filename = os.path.basename(url) |
| 106 | + tools.download(url, filename, sha256=source["sha256"]) |
| 107 | + command = "zcat {} | tar -xf -".format(filename) |
| 108 | + self.run(command=command) |
| 109 | + os.remove(filename) |
| 110 | + else: |
| 111 | + tools.get(**source) |
| 112 | + |
| 113 | + def _configure_cmake(self): |
| 114 | + if self._cmake: |
| 115 | + return self._cmake |
| 116 | + self._cmake = CMake(self) |
| 117 | + self._cmake.configure(build_folder=self._build_subfolder) |
| 118 | + return self._cmake |
| 119 | + |
| 120 | + def package(self): |
| 121 | + self.copy("TSPA.txt", dst="licenses") |
| 122 | + cmake = self._configure_cmake() |
| 123 | + cmake.install() |
| 124 | + |
| 125 | + def package_info(self): |
| 126 | + self.cpp_info.libs = tools.collect_libs(self) |
| 127 | + if self.settings.os == "Linux": |
| 128 | + self.cpp_info.system_libs.append("m") |
0 commit comments