aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt
diff options
context:
space:
mode:
Diffstat (limited to 'content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt')
-rw-r--r--content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt48
1 files changed, 48 insertions, 0 deletions
diff --git a/content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt b/content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt
new file mode 100644
index 0000000..2abab17
--- /dev/null
+++ b/content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt
@@ -0,0 +1,48 @@
+cmake_minimum_required(VERSION 3.14)
+
+set(BDIR ${CMAKE_BINARY_DIR}/libcalc)
+
+# Define external project to build rust lib with cargo.
+include(ExternalProject)
+ExternalProject_Add(
+ ext_libcalc
+ # Always trigger build, let cargo decide if we want to rebuild.
+ BUILD_ALWAYS ON
+ CONFIGURE_COMMAND ""
+ # Can not pass arguments to build.rs and was not able to set env variables
+ # for the external build command, therefore use 'env' to setup env var.
+ BUILD_COMMAND env LIBCALC_BUILD_DIR=${BDIR}
+ cargo build --target-dir ${BDIR} "$<IF:$<CONFIG:Release>,--release,>"
+ BUILD_BYPRODUCTS ${BDIR}/$<IF:$<CONFIG:Release>,release,debug>/libcalc.a
+ ${BDIR}/libcalc.h
+ INSTALL_COMMAND ""
+ # Location of sources (since we don't download).
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
+ # Build dir location used as CWD for build commands.
+ BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
+ # Root directory for external project in cmake build dir.
+ PREFIX "libcalc"
+ # Log directory (relative to PREFIX).
+ LOG_DIR "log"
+ # Log build step.
+ LOG_BUILD ON
+ # In case of error output log on terminal.
+ LOG_OUTPUT_ON_FAILURE ON
+)
+
+# Define pseudo target (import lib) for usage in cmake and let it depend on
+# the cargo build.
+add_library(libcalc STATIC IMPORTED GLOBAL)
+add_dependencies(libcalc ext_libcalc)
+
+# Configure the import locations (libs) for the import lib.
+
+set_target_properties(libcalc PROPERTIES
+ IMPORTED_CONFIGURATIONS "Debug;Release"
+ IMPORTED_LOCATION "${BDIR}/release/libcalc.a"
+ IMPORTED_LOCATION_DEBUG "${BDIR}/debug/libcalc.a"
+)
+
+# Configure the additional interface for they pseudo target.
+
+target_include_directories(libcalc INTERFACE "${BDIR}")