Skip to content

Commit

Permalink
Specify source AND binary dirs in add_rust_* fun. Breaks API!
Browse files Browse the repository at this point in the history
Changed the `add_rust_*` functions to have SOURCE_DIRECTORY and
BINARY_DIRECTORY parameters instead of WORKING_DIRECTORY.
This allows you to build and run the Rust unit tests from the same
BINARY_DIRECTORy as you do for the Rust library build. That means it
won't recompile all of the dependencies when building the unit test
executable.

A side-effect of this change is that the CMakeLists.txt file that uses
`add_rust_library()` must be in the same directory as the Rust module.
I.e. you should co-locate the CMakeLists.txt with the Cargo.toml file.
You can no longer have a parent directory's CMakeFiles.txt add a
library for a subdirectory.
The reason is that CMake will set the INTERFACE_INCLUDE_DIRECTORIES
property for your library's CMake target use both the SOURCE_DIRECTORY
and BINARY_DIRECTORY. This ensures that generated headers will be placed
in the include path for any downstream CMake targets.
CMake requires both directories to exist. When CMake does the build, it
will create subdirectories in the build path for each CMakeLists.txt
file. Co-locating CMakeLists.txt with Cargo.toml ensures that the
BUILD_DIRECTORY is automatically created by CMake.

Important: This commit breaks the API for the FindRust.cmake module!
  • Loading branch information
val-ms committed Sep 7, 2022
1 parent b7057a3 commit 2f14936
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 62 deletions.
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Build Test](https://github.com/micahsnyder/cmake-rust-demo/workflows/Build%20Test/badge.svg)

A C CMake demo using Rust static library components.
A C CMake demo using Rust static library components and building Rust executables.
The notable feature of this project is [cmake/FindRust.cmake](cmake/FindRust.cmake)

## Usage
Expand All @@ -13,21 +13,34 @@ Add `FindRust.cmake` to your project's `cmake` directory and use the following t
find_package(Rust REQUIRED)
```

### Rust-based C-style Static Libraries

To build a rust library and link it into your app, use:

```cmake
add_rust_library(TARGET yourlib WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/yourlib")
add_rust_library(TARGET yourlib
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_executable(yourexe)
target_sources(yourexe PRIVATE yourexe.c)
target_link_libraries(yourexe yourlib)
```

### Rust Library Unit Tests

For unit test support, you can use the `add_rust_test()` function, like this:

```cmake
add_rust_library(TARGET yourlib WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/yourlib")
add_rust_test(NAME yourlib WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/yourlib")
add_rust_library(TARGET yourlib
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_rust_test(NAME yourlib
SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/path/to/yourlib"
BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/path/to/yourlib"
)
```

And don't forget to enable CTest early in your top-level `CMakeLists.txt` file:
Expand All @@ -40,6 +53,18 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
endif()
```

### Rust-based Executables

To build a rust executable use:

```cmake
add_rust_executable(TARGET yourexe
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_executable(YourProject::yourexe ALIAS yourexe)
```

## Minimum Rust version

You may set the CMake variable `RUSTC_MINIMUM_REQUIRED` to enforce a minimum Rust version, such as "1.56" for the 2021 edition support.
Expand Down Expand Up @@ -118,28 +143,28 @@ Rust binaries aren't treated quite the same by CMake as native C binaries, but y

The first thing you'll probably need if you want to use CMake to install stuff, whether or not you bundle in some Rust binaries, is to include the GNUEInstallDirs module somewhere at the top of your top-level `CMakeLists.txt`:

```c
```cmake
include(GNUInstallDirs)
```

Now with a regular C library or executable CMake target, you might configure them for installation like this:
```c
```cmake
install(TARGETS demo DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
```
or:
```c
```cmake
install(TARGETS app DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT programs)
```

Rust library CMake targets aren't normal CMake binary targets though. They're "custom" targets, which means you will instead have to use `install(FILES` instead of `install(TARGETS`, and then point CMake at the specific file you need installed instead of at a target. Our `FindRust.cmake`'s `add_rust_library()` function makes this easy. WHen you add a Rust library, it sets the target properties such that you can simply use CMake's `$<TARGET_FILE:target>` [generator expression](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html) to provide the file path.
Rust library CMake targets aren't normal CMake binary targets though. They're "custom" targets, which means you will instead have to use `install(FILES` instead of `install(TARGETS`, and then point CMake at the specific file you need installed instead of at a target. Our `FindRust.cmake`'s `add_rust_library()` function makes this easy. When you add a Rust library, it sets the target properties such that you can simply use CMake's `$<TARGET_FILE:target>` [generator expression](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html) to provide the file path.

In this demo, we configure installation for our `demorust` Rust static library like this:
```c
```cmake
install(FILES $<TARGET_FILE:demorust> DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
```

And for our `app_rust` Rust executable, we install like this:
```c
```cmake
get_target_property(app_rust_EXECUTABLE app_rust IMPORTED_LOCATION)
install(PROGRAMS ${app_rust_EXECUTABLE} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT programs)
```
Expand Down
5 changes: 4 additions & 1 deletion app_rust/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# Example app that links with our C (and Rust) libs.
#

add_rust_executable(TARGET app_rust WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.")
add_rust_executable(TARGET app_rust
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
# Linking with C libraries is done within `build.rs`
# Linking with nearby Rust crates is done within `Cargo.toml`
get_target_property(app_rust_EXECUTABLE app_rust IMPORTED_LOCATION)
Expand Down
87 changes: 50 additions & 37 deletions cmake/FindRust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
# Example `add_rust_library()` usage:
#
# ```cmake
# add_rust_library(TARGET yourlib WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
# add_rust_library(TARGET yourlib
# SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
# BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
# add_library(YourProject::yourlib ALIAS yourlib)
#
# add_executable(yourexe)
Expand Down Expand Up @@ -78,7 +80,10 @@
# Example `add_rust_test()` usage:
#
# ```cmake
# add_rust_test(NAME yourlib WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/yourlib")
# add_rust_test(NAME yourlib
# SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/path/to/yourlib"
# BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/path/to/yourlib"
# )
# set_property(TEST yourlib PROPERTY ENVIRONMENT ${ENVIRONMENT})
# ```
#
Expand All @@ -96,7 +101,9 @@
# For example:
#
# ```cmake
# add_rust_test(NAME yourlib WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/yourlib"
# add_rust_test(NAME yourlib
# SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/yourlib"
# BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/yourlib"
# PRECOMPILE_TESTS TRUE
# DEPENDS ClamAV::libclamav
# ENVIRONMENT "${ENVIRONMENT}"
Expand All @@ -112,11 +119,11 @@
# Example `add_rust_executable()` usage:
#
# ```cmake
# add_rust_executable(TARGET yourapp WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
# add_executable(YourProject::yourapp ALIAS yourapp)
#
# add_executable(yourexe)
# target_link_libraries(yourexe YourProject::yourlib)
# add_rust_executable(TARGET yourexe
# SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
# BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
# )
# add_executable(YourProject::yourexe ALIAS yourexe)
# ```

if(NOT DEFINED CARGO_HOME)
Expand Down Expand Up @@ -166,19 +173,19 @@ endfunction()

function(cargo_vendor)
set(options)
set(oneValueArgs TARGET WORKING_DIRECTORY)
set(oneValueArgs TARGET SOURCE_DIRECTORY BINARY_DIRECTORY)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT EXISTS ${ARGS_WORKING_DIRECTORY}}/.cargo/config.toml)
if(NOT EXISTS ${ARGS_SOURCE_DIRECTORY}/.cargo/config.toml)
# Vendor the dependencies and create .cargo/config.toml
# Vendored dependencies will be used during the build.
# This will allow us to package vendored dependencies in source tarballs
# for online builds when we run `cpack --config CPackSourceConfig.cmake`
message(STATUS "Running `cargo vendor` to collect dependencies for ${ARGS_TARGET}. This may take a while if the local crates.io index needs to be updated ...")
make_directory(${ARGS_WORKING_DIRECTORY}/.cargo)
make_directory(${ARGS_SOURCE_DIRECTORY}/.cargo)
execute_process(
COMMAND ${CMAKE_COMMAND} -E env "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} vendor ".cargo/vendor"
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" ${cargo_EXECUTABLE} vendor ".cargo/vendor"
WORKING_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
OUTPUT_VARIABLE CARGO_VENDOR_OUTPUT
ERROR_VARIABLE CARGO_VENDOR_ERROR
RESULT_VARIABLE CARGO_VENDOR_RESULT
Expand All @@ -190,7 +197,7 @@ function(cargo_vendor)
message("Success!")
endif()

write_file(${ARGS_WORKING_DIRECTORY}/.cargo/config.toml "
write_file(${ARGS_SOURCE_DIRECTORY}/.cargo/config.toml "
[source.crates-io]
replace-with = \"vendored-sources\"
Expand All @@ -203,7 +210,7 @@ endfunction()

function(add_rust_executable)
set(options)
set(oneValueArgs TARGET WORKING_DIRECTORY)
set(oneValueArgs TARGET SOURCE_DIRECTORY BINARY_DIRECTORY)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(WIN32)
Expand All @@ -212,7 +219,7 @@ function(add_rust_executable)
set(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${RUST_COMPILER_TARGET}/${CARGO_BUILD_TYPE}/${ARGS_TARGET}")
endif()

file(GLOB_RECURSE EXE_SOURCES "${ARGS_WORKING_DIRECTORY}/*.rs")
file(GLOB_RECURSE EXE_SOURCES "${ARGS_SOURCE_DIRECTORY}/*.rs")

set(MY_CARGO_ARGS ${CARGO_ARGS})
list(APPEND MY_CARGO_ARGS "--target-dir" ${CMAKE_CURRENT_BINARY_DIR})
Expand All @@ -221,10 +228,10 @@ function(add_rust_executable)
# Build the executable.
add_custom_command(
OUTPUT "${OUTPUT}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS}
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS}
WORKING_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
DEPENDS ${EXE_SOURCES}
COMMENT "Building ${ARGS_TARGET} in ${ARGS_WORKING_DIRECTORY} with:\n\t ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")
COMMENT "Building ${ARGS_TARGET} in ${ARGS_BINARY_DIRECTORY} with:\n\t ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")

# Create a target from the build output
add_custom_target(${ARGS_TARGET}_target
Expand All @@ -241,13 +248,16 @@ function(add_rust_executable)

# Vendor the dependencies, if desired
if(VENDOR_DEPENDENCIES)
cargo_vendor(TARGET "${ARGS_TARGET}" WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}")
cargo_vendor(TARGET "${ARGS_TARGET}"
SOURCE_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
BINARY_DIRECTORY "${ARGS_BINARY_DIRECTORY}"
)
endif()
endfunction()

function(add_rust_library)
set(options)
set(oneValueArgs TARGET WORKING_DIRECTORY PRECOMPILE_TESTS)
set(oneValueArgs TARGET SOURCE_DIRECTORY BINARY_DIRECTORY PRECOMPILE_TESTS)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(WIN32)
Expand All @@ -256,7 +266,7 @@ function(add_rust_library)
set(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${RUST_COMPILER_TARGET}/${CARGO_BUILD_TYPE}/lib${ARGS_TARGET}.a")
endif()

file(GLOB_RECURSE LIB_SOURCES "${ARGS_WORKING_DIRECTORY}/*.rs")
file(GLOB_RECURSE LIB_SOURCES "${ARGS_SOURCE_DIRECTORY}/*.rs")

set(MY_CARGO_ARGS ${CARGO_ARGS})
if(ARGS_PRECOMPILE_TESTS)
Expand All @@ -269,20 +279,20 @@ function(add_rust_library)
if("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^(arm64;x86_64|x86_64;arm64)$")
add_custom_command(
OUTPUT "${OUTPUT}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=x86_64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=aarch64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=x86_64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=aarch64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${RUST_COMPILER_TARGET}/${CARGO_BUILD_TYPE}"
COMMAND lipo ARGS -create ${CMAKE_CURRENT_BINARY_DIR}/x86_64-apple-darwin/${CARGO_BUILD_TYPE}/lib${ARGS_TARGET}.a ${CMAKE_CURRENT_BINARY_DIR}/aarch64-apple-darwin/${CARGO_BUILD_TYPE}/lib${ARGS_TARGET}.a -output "${OUTPUT}"
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
WORKING_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
DEPENDS ${LIB_SOURCES}
COMMENT "Building ${ARGS_TARGET} in ${ARGS_WORKING_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")
COMMENT "Building ${ARGS_TARGET} in ${ARGS_BINARY_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")
else()
add_custom_command(
OUTPUT "${OUTPUT}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS}
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" "MAINTAINER_MODE=${MAINTAINER_MODE}" "RUSTFLAGS=\"${RUSTFLAGS}\"" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS}
WORKING_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
DEPENDS ${LIB_SOURCES}
COMMENT "Building ${ARGS_TARGET} in ${ARGS_WORKING_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")
COMMENT "Building ${ARGS_TARGET} in ${ARGS_BINARY_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")
endif()

# Create a target from the build output
Expand All @@ -298,18 +308,20 @@ function(add_rust_library)
set_target_properties(${ARGS_TARGET}
PROPERTIES
IMPORTED_LOCATION "${OUTPUT}"
INTERFACE_INCLUDE_DIRECTORIES "${ARGS_WORKING_DIRECTORY};${CMAKE_CURRENT_BINARY_DIR}"
INTERFACE_INCLUDE_DIRECTORIES "${ARGS_SOURCE_DIRECTORY};${ARGS_BINARY_DIRECTORY}"
)

# Vendor the dependencies, if desired
if(VENDOR_DEPENDENCIES)
cargo_vendor(TARGET "${ARGS_TARGET}" WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}")
cargo_vendor(TARGET "${ARGS_TARGET}"
SOURCE_DIRECTORY "${ARGS_SOURCE_DIRECTORY}"
BINARY_DIRECTORY "${ARGS_BINARY_DIRECTORY}")
endif()
endfunction()

function(add_rust_test)
set(options)
set(oneValueArgs NAME WORKING_DIRECTORY PRECOMPILE_TESTS DEPENDS)
set(oneValueArgs NAME SOURCE_DIRECTORY BINARY_DIRECTORY PRECOMPILE_TESTS DEPENDS)
set(multiValueArgs ENVIRONMENT)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Expand All @@ -323,21 +335,22 @@ function(add_rust_test)
list(APPEND MY_CARGO_ARGS "--release")
endif()

list(APPEND MY_CARGO_ARGS "--target-dir" ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND MY_CARGO_ARGS "--target-dir" ${ARGS_BINARY_DIRECTORY})
list(JOIN MY_CARGO_ARGS " " MY_CARGO_ARGS_STRING)

if(ARGS_PRECOMPILE_TESTS)
list(APPEND ARGS_ENVIRONMENT "CARGO_CMD=test" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}")
add_custom_target(${ARGS_NAME}_precompile ALL
list(APPEND ARGS_ENVIRONMENT "CARGO_CMD=test" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}")
add_custom_target(${ARGS_NAME}_tests ALL
COMMAND ${CMAKE_COMMAND} -E env ${ARGS_ENVIRONMENT} ${cargo_EXECUTABLE} ${MY_CARGO_ARGS} --color always --no-run
DEPENDS ${ARGS_DEPENDS}
WORKING_DIRECTORY ${ARGS_SOURCE_DIRECTORY}
)
endif()

add_test(
NAME ${ARGS_NAME}
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=test" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} ${MY_CARGO_ARGS} --color always
WORKING_DIRECTORY ${ARGS_WORKING_DIRECTORY}
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=test" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}" ${cargo_EXECUTABLE} ${MY_CARGO_ARGS} --color always
WORKING_DIRECTORY ${ARGS_SOURCE_DIRECTORY}
)
endfunction()

Expand Down
13 changes: 1 addition & 12 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# Copyright (C) 2020-2022 Micah Snyder.

#
# Libraries that may be used by the applications.
#

# A library to generate UUID's
add_rust_library(TARGET gen_uuid WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/gen_uuid")

# The unit tests for this module have no dependencies on C libraries or other special
# test environment considerations, so we may as well add the test right here instead of
# adding it in the `test/CMakeLists.txt` file.
add_rust_test(NAME gen_uuid WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/gen_uuid")
add_library(demo::gen_uuid ALIAS gen_uuid)
add_subdirectory(gen_uuid)
20 changes: 20 additions & 0 deletions common/gen_uuid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2020-2022 Micah Snyder.

#
# Libraries that may be used by the applications.
#

# A library to generate UUID's
add_rust_library(TARGET gen_uuid
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)

# The unit tests for this module have no dependencies on C libraries or other special
# test environment considerations, so we may as well add the test right here instead of
# adding it in the `test/CMakeLists.txt` file.
add_rust_test(NAME gen_uuid
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
add_library(demo::gen_uuid ALIAS gen_uuid)
5 changes: 4 additions & 1 deletion lib/rust/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# Note: The rust port will be compiled to static library target and must be added as
# a dependency to both the C object targets and shared/static library targets.
#
add_rust_library(TARGET demorust WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
add_rust_library(TARGET demorust
SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
if (WIN32)
# You can add link library dependencies for the rust libarry target, if you need.
target_link_libraries(demorust PUBLIC INTERFACE Userenv)
Expand Down
5 changes: 4 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ set(ENVIRONMENT
LIBDEMO=${LIBDEMO}
)

add_rust_test(NAME demorust WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/rust")
add_rust_test(NAME demorust
SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/rust"
BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/lib/rust"
)
set_property(TEST demorust PROPERTY ENVIRONMENT ${ENVIRONMENT})

0 comments on commit 2f14936

Please sign in to comment.