Skip to content

Commit

Permalink
feat: add rattler-build recipe (#47)
Browse files Browse the repository at this point in the history
Adds a recipe to build a conda package.
  • Loading branch information
baszalmstra authored Jun 11, 2024
1 parent 10d75e9 commit b938910
Show file tree
Hide file tree
Showing 14 changed files with 1,034 additions and 80 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/cpp-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
git diff --exit-code
test:
name: Test
strategy:
fail-fast: false
matrix:
Expand All @@ -45,3 +46,32 @@ jobs:
- name: Run the tests
run: |
pixi run test-cpp
package:
name: Create conda packages
strategy:
matrix:
include:
- os: ubuntu-latest
target-platform: linux-64
- os: ubuntu-latest
target-platform: linux-aarch64
- os: windows-latest
target-platform: win-64
- os: macos-latest
target-platform: osx-64
- os: macos-14
target-platform: osx-arm64
runs-on: ${{ matrix.os }}
needs: [ format ]
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Build conda package
uses: prefix-dev/rattler-build-action@v0.2.9
with:
recipe-path: recipe/recipe.yaml
# needs to be unique for each matrix entry
artifact-name: package-${{ matrix.target-platform }}
build-args: --target-platform ${{ matrix.target-platform }}${{ matrix.target-platform == 'linux-aarch64' && ' --no-test' || '' }}

2 changes: 1 addition & 1 deletion .github/workflows/rust-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Run clippy
run: cargo clippy

build:
test:
name: Test
runs-on: ubuntu-latest
needs: [ format_and_lint ]
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ build/
.pixi
*.egg-info

# Ignore rattler-build output directory
output/

# Any resolvo snapshots in the root
snapshot-*.json
29 changes: 15 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FeatureSummary)

option(RESOLVO_BUILD_TESTING "Build tests" OFF)
add_feature_info(RESOLVO_BUILD_TESTING RESOLVO_BUILD_TESTING "configure whether to build the test suite")
add_feature_info(RESOLVO_BUILD_TESTING RESOLVO_BUILD_TESTING
"configure whether to build the test suite")
include(CTest)

set(RESOLVO_IS_TOPLEVEL_BUILD TRUE)

# Place all compiled examples into the same bin directory
# on Windows, where we'll also put the dll
if (WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/debug)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin/release)
# Place all compiled examples into the same bin directory on Windows, where
# we'll also put the dll
if(WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/debug)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin/release)
elseif(APPLE)
# On macOS, the resolvo_cpp.dylib's install_name uses @rpath. CMake doesn't
# set BUILD_RPATH for imported targets though, so include the directory here
# by hand in the rpath used to build binaries in the build tree (such as our
# examples or tests).
set(CMAKE_BUILD_RPATH ${CMAKE_BINARY_DIR}/cpp)
# On macOS, the resolvo_cpp.dylib's install_name uses @rpath. CMake doesn't
# set BUILD_RPATH for imported targets though, so include the directory here
# by hand in the rpath used to build binaries in the build tree (such as our
# examples or tests).
set(CMAKE_BUILD_RPATH ${CMAKE_BINARY_DIR}/cpp)
endif()

add_subdirectory(cpp/)

feature_summary(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
feature_summary(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")
feature_summary(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ members = ["cpp", "tools/*"]
resolver = "2"

[workspace.package]
name = "resolvo"
version = "0.6.1"
authors = ["Adolfo Ochagavía <github@adolfo.ochagavia.nl>", "Bas Zalmstra <zalmstra.bas@gmail.com>", "Tim de Jager <tdejager89@gmail.com>"]
homepage = "https://github.com/mamba-org/resolvo"
Expand Down
174 changes: 135 additions & 39 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,86 +1,182 @@
cmake_minimum_required(VERSION 3.21)

# Select C++ and C as languages, as Corrosion needs ${CMAKE_C_COMPILER}
# for linking
project(Resolvo LANGUAGES C CXX VERSION 0.1.0)
# Select C++ and C as languages, as Corrosion needs ${CMAKE_C_COMPILER} for
# linking
project(Resolvo LANGUAGES C CXX)

# Add the Corrosion dependency (used to build Rust code)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.4.9
)
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.4.9)
FetchContent_MakeAvailable(Corrosion)

# Extract the version from the rust crate
corrosion_parse_package_version("${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml"
resolvo_cpp_version)
string(REPLACE "." ";" VERSION_LIST ${resolvo_cpp_version})
list(GET VERSION_LIST 0 resolvo_cpp_version_major)
list(GET VERSION_LIST 1 resolvo_cpp_version_minor)
list(GET VERSION_LIST 2 resolvo_cpp_version_patch)

# Add the Corrosion CMake module path to the list of paths to search for modules
list(PREPEND CMAKE_MODULE_PATH ${Corrosion_SOURCE_DIR}/cmake)
find_package(Rust 1.75 REQUIRED MODULE)

option(BUILD_SHARED_LIBS "Build Resolvo as shared library" ON)

set(RESOLVO_LIBRARY_CARGO_FLAGS "" CACHE STRING
"Flags to pass to cargo when building the Resolvo runtime library")
set(RESOLVO_LIBRARY_CARGO_FLAGS
""
CACHE STRING
"Flags to pass to cargo when building the Resolvo runtime library")

if(BUILD_SHARED_LIBS)
set(rustc_lib_type "cdylib")
set(resolvo_cpp_impl "resolvo_cpp-shared")
set(cmake_lib_type "SHARED")
set(rustc_lib_type "cdylib")
set(resolvo_cpp_impl "resolvo_cpp-shared")
set(cmake_lib_type "SHARED")
else()
set(rustc_lib_type "staticlib")
set(resolvo_cpp_impl "resolvo_cpp-static")
set(cmake_lib_type "STATIC")
set(rustc_lib_type "staticlib")
set(resolvo_cpp_impl "resolvo_cpp-static")
set(cmake_lib_type "STATIC")
endif()

corrosion_import_crate(MANIFEST_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml"
CRATES resolvo_cpp CRATE_TYPES bin ${rustc_lib_type})
corrosion_import_crate(
MANIFEST_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml"
CRATES
resolvo_cpp
CRATE_TYPES
bin
${rustc_lib_type})

# See
# https://corrosion-rs.github.io/corrosion/common_issues.html#missing-install_name-on-macos-for-ccdylibs--hardcoded-references-to-the-build-directory
if(APPLE
AND RESOLVO_IS_TOPLEVEL_BUILD
AND BUILD_SHARED_LIBS)
corrosion_add_target_local_rustflags(
resolvo_cpp
-Clink-arg=-Wl,-install_name,@rpath/libresolvo_cpp.dylib,-current_version,${resolvo_cpp_version_major}.${resolvo_cpp_version_minor}.${resolvo_cpp_version_patch},-compatibility_version,${resolvo_cpp_version_major}.${resolvo_cpp_version_minor}.0
)
set_target_properties(resolvo_cpp-shared PROPERTIES IMPORTED_NO_SONAME 0)
set_target_properties(resolvo_cpp-shared PROPERTIES IMPORTED_SONAME
libresolvo_cpp.dylib)
endif()

add_library(Resolvo INTERFACE)
add_library(Resolvo::Resolvo ALIAS Resolvo)
target_link_libraries(Resolvo INTERFACE resolvo_cpp)

set_property(
TARGET resolvo_cpp
APPEND
PROPERTY CORROSION_ENVIRONMENT_VARIABLES
TARGET resolvo_cpp
APPEND
PROPERTY
CORROSION_ENVIRONMENT_VARIABLES
"RESOLVO_GENERATED_INCLUDE_DIR=${CMAKE_CURRENT_BINARY_DIR}/generated_include/"
)

file(GLOB api_headers RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include/"
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(
GLOB api_headers
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include/"
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")

foreach(header IN LISTS api_headers)
set_property(TARGET Resolvo APPEND PROPERTY PUBLIC_HEADER include/${header})
set_property(
TARGET Resolvo
APPEND
PROPERTY PUBLIC_HEADER include/${header})
endforeach()

set(generated_headers
${CMAKE_CURRENT_BINARY_DIR}/generated_include/resolvo_vector_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/resolvo_string_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/resolvo_internal.h
)
${CMAKE_CURRENT_BINARY_DIR}/generated_include/resolvo_internal.h)

foreach(header IN LISTS generated_headers)
set_property(TARGET Resolvo APPEND PROPERTY PUBLIC_HEADER ${header})
set_property(
TARGET Resolvo
APPEND
PROPERTY PUBLIC_HEADER ${header})
endforeach()

target_include_directories(Resolvo INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated_include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/resolvo>
)

export(TARGETS Resolvo resolvo_cpp
NAMESPACE Resolvo:: FILE "${CMAKE_BINARY_DIR}/lib/cmake/Resolvo/ResolvoTargets.cmake")
install(EXPORT ResolvoTargets NAMESPACE Resolvo:: DESTINATION lib/cmake/Resolvo)
install(TARGETS Resolvo resolvo_cpp
EXPORT ResolvoTargets LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include/resolvo)
target_include_directories(
Resolvo
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated_include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/resolvo>)

export(
TARGETS Resolvo resolvo_cpp
NAMESPACE Resolvo::
FILE "${CMAKE_BINARY_DIR}/lib/cmake/Resolvo/ResolvoTargets.cmake")
install(
EXPORT ResolvoTargets
NAMESPACE Resolvo::
DESTINATION lib/cmake/Resolvo)
install(
TARGETS Resolvo resolvo_cpp
EXPORT ResolvoTargets
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include/resolvo)

install(FILES $<TARGET_FILE:${resolvo_cpp_impl}> TYPE LIB)

if(WIN32)
install(FILES $<TARGET_LINKER_FILE:${resolvo_cpp_impl}> TYPE LIB)
install(FILES $<TARGET_LINKER_FILE:${resolvo_cpp_impl}> TYPE LIB)
endif()

include(CMakePackageConfigHelpers)

function(_resolvo_write_configure_file)
foreach(
prop
IMPORTED_LOCATION
IMPORTED_LOCATION_DEBUG
IMPORTED_LOCATION_RELEASE
IMPORTED_LOCATION_RELWITHDEBINFO
IMPORTED_LOCATION_MINSIZEREL
IMPORTED_IMPLIB
IMPORTED_IMPLIB_DEBUG
IMPORTED_IMPLIB_RELEASE
IMPORTED_IMPLIB_RELWITHDEBINFO
IMPORTED_IMPLIB_MINSIZEREL)
get_target_property(value ${resolvo_cpp_impl} ${prop})

if(value)
get_filename_component(value ${value} NAME)
list(APPEND RESOLVO_LIB_PROPERTIES ${prop}
"\${_IMPORT_PREFIX}/${CMAKE_INSTALL_LIBDIR}/${value}")
endif()
endforeach()

foreach(prop IMPORTED_NO_SONAME IMPORTED_SONAME)
get_target_property(value ${resolvo_cpp_impl} ${prop})
if(value)
list(APPEND RESOLVO_LIB_PROPERTIES ${prop} ${value})
endif()
endforeach()

configure_package_config_file(
"cmake/ResolvoConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Resolvo/ResolvoConfig.cmake"
INSTALL_DESTINATION lib/cmake/resolvo)
endfunction()

cmake_language(DEFER CALL _resolvo_write_configure_file)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Resolvo/ResolvoConfigVersion.cmake
VERSION
${resolvo_cpp_version_major}.${resolvo_cpp_version_minor}.${resolvo_cpp_version_patch}
COMPATIBILITY SameMinorVersion)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Resolvo/ResolvoConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Resolvo/ResolvoConfigVersion.cmake"
DESTINATION lib/cmake/Resolvo)

if(RESOLVO_BUILD_TESTING)
add_subdirectory(tests)
add_subdirectory(tests)
endif()
2 changes: 1 addition & 1 deletion cpp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "resolvo_cpp"
description = "Resolvo C++ integration"
version.workspace = true
version = "0.1.0"
authors.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
16 changes: 16 additions & 0 deletions cpp/cmake/ResolvoConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@PACKAGE_INIT@

get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()

add_library(@resolvo_cpp_impl@ @cmake_lib_type@ IMPORTED)
set_target_properties(@resolvo_cpp_impl@ PROPERTIES @RESOLVO_LIB_PROPERTIES@)

set(_IMPORT_PREFIX)

include("${CMAKE_CURRENT_LIST_DIR}/ResolvoTargets.cmake")
Loading

0 comments on commit b938910

Please sign in to comment.