aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/2022-05-30-cmake-cargo-example/libcalc/CMakeLists.txt
blob: 2abab174180f562603dbfcfdc03beb5514da43a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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}")