Skip to content

Commit

Permalink
(#12639) cspice: conan v2 support
Browse files Browse the repository at this point in the history
* conan v2 support

* add patch_type & patch_source
  • Loading branch information
SpaceIm authored Aug 31, 2022
1 parent a391ec6 commit e907fe2
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 76 deletions.
13 changes: 5 additions & 8 deletions recipes/cspice/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.7)
project(cspice C)

include(conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
project(cspice LANGUAGES C)

include(GNUInstallDirs)

option(BUILD_UTILITIES "Build cspice utilities" ON)
option(CSPICE_BUILD_UTILITIES "Build cspice utilities" ON)

add_compile_options(
$<$<OR:$<C_COMPILER_ID:GNU>,$<C_COMPILER_ID:AppleClang>>:-Wno-implicit-int>
Expand All @@ -30,7 +27,7 @@ add_compile_options(
$<$<AND:$<C_COMPILER_ID:AppleClang>,$<VERSION_GREATER_EQUAL:${CMAKE_C_COMPILER_VERSION},12>>:-Wno-error=implicit-function-declaration>
)

set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source_subfolder/src)
set(SRC_DIR ${CSPICE_SRC_DIR}/src)

file(GLOB CSPICE_SRC_FILES ${SRC_DIR}/cspice/*.c)
add_library(cspice ${CSPICE_SRC_FILES})
Expand All @@ -54,10 +51,10 @@ install(
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

file(GLOB INCLUDE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/source_subfolder/include/*.h)
file(GLOB INCLUDE_FILES ${CSPICE_SRC_DIR}/include/*.h)
install(FILES ${INCLUDE_FILES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

if(BUILD_UTILITIES)
if(CSPICE_BUILD_UTILITIES)
# csupport is common to all utilities
file(GLOB CSUPPORT_SRC_FILES "${SRC_DIR}/csupport/*.c")
add_library(csupport STATIC ${CSUPPORT_SRC_FILES})
Expand Down
7 changes: 3 additions & 4 deletions recipes/cspice/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ sources:
patches:
"0067":
- patch_file: "patches/isatty.patch"
base_path: "source_subfolder"
"0066":
- patch_file: "patches/isatty.patch"
base_path: "source_subfolder"
- patch_file: "patches/patches20171106.patch" # from https://naif.jpl.nasa.gov/pub/naif/misc/toolkit_N0066_patches
base_path: "source_subfolder"
- patch_file: "patches/patches20171106.patch"
patch_type: "backport"
patch_source: "https://naif.jpl.nasa.gov/pub/naif/misc/toolkit_N0066_patches"
117 changes: 62 additions & 55 deletions recipes/cspice/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, chdir, copy, download, get, load, rename, rmdir, save
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.47.0"


class CspiceConan(ConanFile):
Expand All @@ -25,17 +27,10 @@ class CspiceConan(ConanFile):
"utilities": True,
}

generators = "cmake"
_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])
copy(self, "CMakeLists.txt", self.recipe_folder, self.export_sources_folder)
for p in self.conan_data.get("patches", {}).get(self.version, []):
copy(self, p["patch_file"], self.recipe_folder, self.export_sources_folder)

def config_options(self):
if self.settings.os == "Windows":
Expand All @@ -44,25 +39,31 @@ def config_options(self):
def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass

def validate(self):
sources_url_per_triplet = self.conan_data["sources"][self.version]
the_os = self._get_os_or_subsystem()
if the_os not in sources_url_per_triplet:
host_os = self._get_os_or_subsystem()
if host_os not in sources_url_per_triplet:
raise ConanInvalidConfiguration(
"cspice N{0} does not support {1}".format(self.version, the_os)
f"cspice N{self.version} does not support {host_os}",
)
compiler = str(self.settings.compiler)
if compiler not in sources_url_per_triplet[the_os]:
compiler = str(self.info.settings.compiler)
if compiler not in sources_url_per_triplet[host_os]:
raise ConanInvalidConfiguration(
"cspice N{0} does not support {1} on {2}".format(self.version, compiler, the_os)
f"cspice N{self.version} does not support {compiler} on {host_os}",
)
arch = str(self.settings.arch)
if arch not in sources_url_per_triplet[the_os][compiler]:
arch = str(self.info.settings.arch)
if arch not in sources_url_per_triplet[host_os][compiler]:
raise ConanInvalidConfiguration(
"cspice N{0} does not support {1} on {2} {3}".format(self.version, compiler, the_os, arch)
f"cspice N{self.version} does not support {compiler} on {host_os} {arch}",
)

def _get_os_or_subsystem(self):
Expand All @@ -72,47 +73,53 @@ def _get_os_or_subsystem(self):
os_or_subsystem = str(self.settings.os)
return os_or_subsystem

def layout(self):
cmake_layout(self, src_folder="src")

def source(self):
pass

def build(self):
self._get_sources()
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
cmake.build()
def generate(self):
tc = CMakeToolchain(self)
tc.variables["CSPICE_SRC_DIR"] = self.source_folder.replace("\\", "/")
tc.variables["CSPICE_BUILD_UTILITIES"] = self.options.utilities
tc.generate()

@property
def _parent_source_folder(self):
return os.path.join(self.source_folder, os.pardir)

def _get_sources(self):
the_os = self._get_os_or_subsystem()
compiler = str(self.settings.compiler)
arch = str(self.settings.arch)
data = self.conan_data["sources"][self.version][the_os][compiler][arch]
url = data["url"]
if url.endswith(".tar.Z"): # Python doesn't have any module to uncompress .Z files
filename = os.path.basename(url)
tools.download(url, filename, sha256=data["sha256"])
command = "zcat {} | tar -xf -".format(filename)
self.run(command=command)
os.remove(filename)
else:
tools.get(**data)
tools.rename(self.name, self._source_subfolder)
with chdir(self, self._parent_source_folder):
host_os = self._get_os_or_subsystem()
compiler = str(self.settings.compiler)
arch = str(self.settings.arch)
data = self.conan_data["sources"][self.version][host_os][compiler][arch]
url = data["url"]
if url.endswith(".tar.Z"): # Python doesn't have any module to uncompress .Z files
tarball = os.path.basename(url)
download(self, url, tarball, sha256=data["sha256"])
self.run(f"zcat {tarball} | tar -xf -")
os.remove(tarball)
else:
get(self, **data)
rmdir(self, self.source_folder)
rename(self, "cspice", os.path.basename(self.source_folder))

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["BUILD_UTILITIES"] = self.options.utilities
self._cmake.configure()
return self._cmake
def build(self):
self._get_sources()
apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure(build_script_folder=self._parent_source_folder)
cmake.build()

def package(self):
tools.save(os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license())
cmake = self._configure_cmake()
save(self, os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license())
cmake = CMake(self)
cmake.install()

def _extract_license(self):
spiceusr_header = tools.load(os.path.join(self._source_subfolder, "include", "SpiceUsr.h"))
spiceusr_header = load(self, os.path.join(self.source_folder, "include", "SpiceUsr.h"))
begin = spiceusr_header.find("-Disclaimer")
end = spiceusr_header.find("-Required_Reading", begin)
return spiceusr_header[begin:end]
Expand All @@ -124,5 +131,5 @@ def package_info(self):

if self.options.utilities:
bin_path = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bin_path))
self.output.info(f"Appending PATH environment variable: {bin_path}")
self.env_info.PATH.append(bin_path)
7 changes: 3 additions & 4 deletions recipes/cspice/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(cspice REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
target_link_libraries(${PROJECT_NAME} PRIVATE cspice::cspice)
18 changes: 13 additions & 5 deletions recipes/cspice/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
10 changes: 10 additions & 0 deletions recipes/cspice/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(cspice REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ../test_package/test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE cspice::cspice)
17 changes: 17 additions & 0 deletions recipes/cspice/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit e907fe2

Please sign in to comment.