Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add CPM and Rust support #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.38.7)
set(CPM_HASH_SUM "83e5eb71b2bbb8b1f2ad38f1950287a057624e385c238f6087f94cdfc44af9c5")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best practice for finding this hash and bumping the version, etc?...


if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})
39 changes: 39 additions & 0 deletions Rust.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Dependencies that are downloaded using the CPM package manager
include("${CMAKE_CURRENT_LIST_DIR}/CPM.cmake")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be a problem if CPM is already included? Do we need something like https://cmake.org/cmake/help/latest/command/include_guard.html ?


option(USE_RUST "Use Rust" ON)
if(USE_RUST)

# Define USING_RUST to be used in C++ code
add_definitions(-DUSING_RUST)

# Install corrosion – a Rust to C++ bridge
cpmaddpackage(NAME Corrosion GITHUB_REPOSITORY corrosion-rs/corrosion GIT_TAG
v0.4.7)

# Rust directory: rust in root dir
set(RUST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rust")

# Add a rust library After this, all you need to do is link to the rust
# library in your C++ like this: target_link_libraries(SharedCode INTERFACE
# myrustlib)
corrosion_import_crate(MANIFEST_PATH "${RUST_DIR}/{{ cookiecutter.__rust_lib_name }}/Cargo.toml")

corrosion_add_cxxbridge(
{{ cookiecutter.__rust_lib_name }}
CRATE
{{ cookiecutter.__rust_crate_name }}
MANIFEST_PATH
"${RUST_DIR}/{{ cookiecutter.__rust_lib_name }}"
# NOTE: These file paths are relative to the root of the rust crate's src
# dir eg rust/{{ cookiecutter.__rust_lib_name }}/src.
FILES
"lib.rs")

if(MSVC)
# Note: This is required because we use `cxx` which uses `cc` to compile and
# link C++ code.
corrosion_set_env_vars({{ cookiecutter.__rust_crate_name }} "CFLAGS=-MDd" "CXXFLAGS=-MDd")
endif()

endif()