From 6ea85a1ad2a8ad4fd0b52d7e0cfc0528a579b043 Mon Sep 17 00:00:00 2001 From: Gtker Date: Fri, 2 Aug 2024 22:51:57 +0200 Subject: [PATCH 01/10] Make corrosion_install support include directories Supports directories added with `target_include_directories` like normal. There is a slight oddity. Because of how install(DIRECTORY) works, we can't put the include directory inside `${CMAKE_INSTALL_PREFIX}/include`, since that would create `${CMAKE_INSTALL_PREFIX}/include/`. Instead we copy directly to the CMAKE_INSTALL_PREFIX, and then use whatever name the include directory had (hopefully just `include`). This means that when installing the path to include files will be -- Installing: /tmp/install/./include -- Installing: /tmp/install/./include/is_odd -- Installing: /tmp/install/./include/is_odd/is_odd.h Instead of just -- Installing: /tmp/install/include -- Installing: /tmp/install/include/is_odd -- Installing: /tmp/install/include/is_odd/is_odd.h --- cmake/Corrosion.cmake | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index f5132ead..8d346f31 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1333,6 +1333,41 @@ function(corrosion_install) ) endif() endif() + + # Executables can also have export tables, so they _might_ also need header files + if (DEFINED COR_INSTALL_PUBLIC_HEADER_PERMISSIONS) + set(PERMISSIONS ${COR_INSTALL_PUBLIC_HEADER_PERMISSIONS}) + elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS) + set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS}) + else() + # Directories need OWNER_EXECUTE in order to be deletable by owner + set(PERMISSIONS ${DEFAULT_PERMISSIONS} OWNER_EXECUTE) + endif() + + if (DEFINED COR_INSTALL_PUBLIC_HEADER_CONFIGURATIONS) + set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_PUBLIC_HEADER_CONFIGURATIONS}) + elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS) + set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS}) + else() + set(CONFIGURATIONS) + endif() + + set(PUBLIC_HEADER_PROPERTIES INCLUDE_DIRECTORIES PUBLIC_INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES) + foreach(PUBLIC_HEADER_PROPERTY ${PUBLIC_HEADER_PROPERTIES}) + get_target_property(PUBLIC_HEADER ${INSTALL_TARGET} ${PUBLIC_HEADER_PROPERTY}) + + if(NOT PUBLIC_HEADER MATCHES .*-NOTFOUND) + foreach(INCLUDE_DIRECTORY ${PUBLIC_HEADER}) + install( + DIRECTORY ${INCLUDE_DIRECTORY} + DESTINATION . + FILE_PERMISSIONS ${PERMISSIONS} + DIRECTORY_PERMISSIONS ${PERMISSIONS} + ${CONFIGURATIONS} + ) + endforeach() + endif() + endforeach() endforeach() elseif(INSTALL_TYPE STREQUAL "EXPORT") From 7d36fc52828467f6a654d8cd4c4b184f22598979 Mon Sep 17 00:00:00 2001 From: Gtker Date: Sat, 3 Aug 2024 17:52:09 +0200 Subject: [PATCH 02/10] Add special case for FILE_SETS in corrosion_install This adds better `install` integration for targets with headers defined through target_sources. The PUBLIC_HEADER option can now be used to change the DIRECTORY. --- cmake/Corrosion.cmake | 65 +++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 8d346f31..d65975f9 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1099,17 +1099,21 @@ ANCHOR: corrosion-install ```cmake corrosion_install(TARGETS ... - [[ARCHIVE|LIBRARY|RUNTIME] + [[ARCHIVE|LIBRARY|RUNTIME|PUBLIC_HEADER] [DESTINATION ] [PERMISSIONS ] [CONFIGURATIONS [Debug|Release|]] ] [...]) ``` * **TARGETS**: Target or targets to install. -* **ARCHIVE**/**LIBRARY**/**RUNTIME**: Designates that the following settings only apply to that specific type of object. +* **ARCHIVE**/**LIBRARY**/**RUNTIME**/PUBLIC_HEADER: Designates that the following settings only apply to that specific type of object. * **DESTINATION**: The subdirectory within the CMAKE_INSTALL_PREFIX that a specific object should be placed. Defaults to values from GNUInstallDirs. * **PERMISSIONS**: The permissions of files copied into the install prefix. +Any `PUBLIC` or `INTERFACE` [file sets] will be installed. + +[file sets]: https://cmake.org/cmake/help/latest/command/target_sources.html#file-sets + ANCHOR_END: corrosion-install #]=======================================================================] function(corrosion_install) @@ -1335,6 +1339,14 @@ function(corrosion_install) endif() # Executables can also have export tables, so they _might_ also need header files + if (DEFINED COR_INSTALL_PUBLIC_HEADER_DESTINATION) + set(DESTINATION ${COR_INSTALL_PUBLIC_HEADER_DESTINATION}) + elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION) + set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION}) + else() + set(DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + endif() + if (DEFINED COR_INSTALL_PUBLIC_HEADER_PERMISSIONS) set(PERMISSIONS ${COR_INSTALL_PUBLIC_HEADER_PERMISSIONS}) elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS) @@ -1352,22 +1364,39 @@ function(corrosion_install) set(CONFIGURATIONS) endif() - set(PUBLIC_HEADER_PROPERTIES INCLUDE_DIRECTORIES PUBLIC_INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES) - foreach(PUBLIC_HEADER_PROPERTY ${PUBLIC_HEADER_PROPERTIES}) - get_target_property(PUBLIC_HEADER ${INSTALL_TARGET} ${PUBLIC_HEADER_PROPERTY}) - - if(NOT PUBLIC_HEADER MATCHES .*-NOTFOUND) - foreach(INCLUDE_DIRECTORY ${PUBLIC_HEADER}) - install( - DIRECTORY ${INCLUDE_DIRECTORY} - DESTINATION . - FILE_PERMISSIONS ${PERMISSIONS} - DIRECTORY_PERMISSIONS ${PERMISSIONS} - ${CONFIGURATIONS} - ) - endforeach() - endif() - endforeach() + get_target_property(HEADER_SETS ${INSTALL_TARGET} INTERFACE_HEADER_SETS) + if(NOT HEADER_SETS OR HEADER_SETS MATCHES .*-NOTFOUND) + set(TARGET_HAS_FILE_SET FALSE) + else() + set(TARGET_HAS_FILE_SET TRUE) + endif() + + if(NOT TARGET_HAS_FILE_SET) + set(PUBLIC_HEADER_PROPERTIES INCLUDE_DIRECTORIES PUBLIC_INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES) + foreach(PUBLIC_HEADER_PROPERTY ${PUBLIC_HEADER_PROPERTIES}) + get_target_property(PUBLIC_HEADER ${INSTALL_TARGET} ${PUBLIC_HEADER_PROPERTY}) + + if(NOT PUBLIC_HEADER MATCHES .*-NOTFOUND) + foreach(INCLUDE_DIRECTORY ${PUBLIC_HEADER}) + install( + DIRECTORY ${INCLUDE_DIRECTORY} + DESTINATION . + FILE_PERMISSIONS ${PERMISSIONS} + DIRECTORY_PERMISSIONS ${PERMISSIONS} + ${CONFIGURATIONS} + ) + endforeach() + endif() + endforeach() + else() + install( + TARGETS ${INSTALL_TARGET} + FILE_SET ${HEADER_SETS} + DESTINATION ${DESTINATION} + PERMISSIONS ${PERMISSIONS} + ${CONFIGURATIONS} + ) + endif() endforeach() elseif(INSTALL_TYPE STREQUAL "EXPORT") From de1acc9adb047dd4b162637e90dec564e4c8ff3a Mon Sep 17 00:00:00 2001 From: Gtker Date: Sat, 3 Aug 2024 21:21:33 +0200 Subject: [PATCH 03/10] Add EXPORT to corrosion_install This does an install(TARGETS EXPORT) and creates a file at ${CMAKE_BINARY_DIR}/corrosion/TargetsCorrosion.cmake that contains the *-static and *-shared targets used by the main target. The examples use RustLib instead of ${PROJECT_NAME} for visual clarity. --- cmake/Corrosion.cmake | 42 ++++++++++++++++++++-- doc/src/quick_start.md | 80 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 4 deletions(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index d65975f9..54e7b8c5 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -409,6 +409,7 @@ function(_corrosion_add_library_target) if(has_staticlib) add_library(${target_name}-static STATIC IMPORTED GLOBAL) add_dependencies(${target_name}-static cargo-build_${target_name}) + set_target_properties(${target_name}-static PROPERTIES COR_FILE_NAME ${static_lib_name}) _corrosion_set_imported_location("${target_name}-static" "IMPORTED_LOCATION" "ARCHIVE_OUTPUT_DIRECTORY" @@ -435,6 +436,7 @@ function(_corrosion_add_library_target) if(has_cdylib) add_library(${target_name}-shared SHARED IMPORTED GLOBAL) add_dependencies(${target_name}-shared cargo-build_${target_name}) + set_target_properties(${target_name}-shared PROPERTIES COR_FILE_NAME ${dynamic_lib_name}) # Todo: (Not new issue): What about IMPORTED_SONAME and IMPORTED_NO_SYSTEM? _corrosion_set_imported_location("${target_name}-shared" "IMPORTED_LOCATION" @@ -1098,7 +1100,7 @@ ANCHOR: corrosion-install and is not officially released yet. Feedback and Suggestions are welcome. ```cmake -corrosion_install(TARGETS ... +corrosion_install(TARGETS ... [EXPORT ] [PERMISSIONS ] @@ -1106,6 +1108,7 @@ corrosion_install(TARGETS ... ] [...]) ``` * **TARGETS**: Target or targets to install. +* **EXPORT**: Creates an export that can be installed with `install(EXPORT)`. Also creates a file at ${CMAKE_BINARY_DIR}/corrosion/TargetsCorrosion.cmake that must be included in the installed config file. * **ARCHIVE**/**LIBRARY**/**RUNTIME**/PUBLIC_HEADER: Designates that the following settings only apply to that specific type of object. * **DESTINATION**: The subdirectory within the CMAKE_INSTALL_PREFIX that a specific object should be placed. Defaults to values from GNUInstallDirs. * **PERMISSIONS**: The permissions of files copied into the install prefix. @@ -1163,7 +1166,8 @@ function(corrosion_install) list(GET ARGN 0 EXPORT_NAME) list(REMOVE_AT ARGN 0) # Pop - message(FATAL_ERROR "EXPORT keyword not yet implemented!") + set(EXTRA_TARGETS_EXPORT_NAME ${EXPORT_NAME}Corrosion.cmake) + set(EXPORT_NAME EXPORT ${EXPORT_NAME}) endif() endif() @@ -1297,6 +1301,20 @@ function(corrosion_install) PERMISSIONS ${PERMISSIONS} ${CONFIGURATIONS} ) + + if(EXPORT_NAME) + get_target_property(COR_FILE_NAME ${INSTALL_TARGET}-static COR_FILE_NAME) + file(APPEND + ${CMAKE_BINARY_DIR}/corrosion/${EXTRA_TARGETS_EXPORT_NAME} +" +add_library(${INSTALL_TARGET}-static STATIC IMPORTED) +set_target_properties(${INSTALL_TARGET}-static + PROPERTIES + IMPORTED_LOCATION \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME} +) +" + ) + endif() else() message(FATAL_ERROR "Unknown target type ${TARGET_TYPE} for install target ${INSTALL_TARGET}") endif() @@ -1335,6 +1353,20 @@ function(corrosion_install) DESTINATION ${DESTINATION} ${CONFIGURATIONS} ) + + if(EXPORT_NAME) + get_target_property(COR_FILE_NAME ${INSTALL_TARGET}-shared COR_FILE_NAME) + file(APPEND + ${CMAKE_BINARY_DIR}/corrosion/${EXTRA_TARGETS_EXPORT_NAME} + " +add_library(${INSTALL_TARGET}-shared SHARED IMPORTED) +set_target_properties(${INSTALL_TARGET}-shared + PROPERTIES + IMPORTED_LOCATION \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME} +) +" + ) + endif() endif() endif() @@ -1372,6 +1404,11 @@ function(corrosion_install) endif() if(NOT TARGET_HAS_FILE_SET) + if(EXPORT_NAME) + # We still need to generate a EXPORT but we can't do that with install(DIRECTORY) + install(TARGETS ${INSTALL_TARGET} ${EXPORT_NAME}) + endif() + set(PUBLIC_HEADER_PROPERTIES INCLUDE_DIRECTORIES PUBLIC_INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES) foreach(PUBLIC_HEADER_PROPERTY ${PUBLIC_HEADER_PROPERTIES}) get_target_property(PUBLIC_HEADER ${INSTALL_TARGET} ${PUBLIC_HEADER_PROPERTY}) @@ -1391,6 +1428,7 @@ function(corrosion_install) else() install( TARGETS ${INSTALL_TARGET} + ${EXPORT_NAME} FILE_SET ${HEADER_SETS} DESTINATION ${DESTINATION} PERMISSIONS ${PERMISSIONS} diff --git a/doc/src/quick_start.md b/doc/src/quick_start.md index ad29e57e..7a1a88d2 100644 --- a/doc/src/quick_start.md +++ b/doc/src/quick_start.md @@ -31,9 +31,85 @@ add_executable(your_cool_cpp_bin main.cpp) # A target with the same name is now available in CMake and you can use it to link the rust library into # your C/C++ CMake target(s). target_link_libraries(your_cool_cpp_bin PUBLIC rust-lib) +``` + +The example below shows how to import a rust library and make it available for install through CMake. + + +```cmake +include(FetchContent) + +FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.5 # Optionally specify a commit hash, version tag or branch here +) +# Set any global configuration variables such as `Rust_TOOLCHAIN` before this line! +FetchContent_MakeAvailable(Corrosion) + +# Import targets defined in a package or workspace manifest `Cargo.toml` file +corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml) -# Rust libraries and executables can also be installed using `corrosion_install`. -corrosion_install(TARGETS rust-lib) +# Add a manually written header file which will be exported +# Requires CMake >=3.23 +target_sources(rust-lib INTERFACE + FILE_SET HEADERS + BASE_DIRS include + FILES + include/rust-lib/rust-lib.h +) + +# OR for CMake <= 3.23 +target_include_directories(is_odd INTERFACE + $ + $ +) +target_sources(is_odd + INTERFACE + $ + $ +) + +# Rust libraries must be installed using `corrosion_install`. +corrosion_install(TARGETS rust-lib EXPORT RustLibTargets) + +# Installs the main target +install( + EXPORT RustLibTargets + NAMESPACE RustLib:: + DESTINATION lib/cmake/RustLib +) + +# Necessary for packaging helper commands +include(CMakePackageConfigHelpers) +# Create a file for checking version compatibility +# Optional +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake" + VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion +) + +# Configures the main config file that cmake loads +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake" + INSTALL_DESTINATION lib/cmake/RustLib + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) +# Config.cmake.in contains +# @PACKAGE_INIT@ +# +# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargetsCorrosion.cmake) +# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargets.cmake) + +# Install all generated files +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake + ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/corrosion/RustLibTargetsCorrosion.cmake + DESTINATION lib/cmake/RustLib +) ``` Please see the [Usage chapter](usage.md) for a complete discussion of possible configuration options. From ceeeee7be9a75650f16734f4e1d6d988e19cc209 Mon Sep 17 00:00:00 2001 From: Gtker Date: Sat, 3 Aug 2024 23:32:20 +0200 Subject: [PATCH 04/10] Add implib support to corrosion_install exports --- cmake/Corrosion.cmake | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 54e7b8c5..6376b5d8 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -452,6 +452,7 @@ function(_corrosion_add_library_target) "ARCHIVE_OUTPUT_DIRECTORY" "${implib_name}" ) + set_target_properties(${target_name}-shared PROPERTIES COR_IMPLIB_FILE_NAME ${implib_name}) endif() if(is_macos) @@ -1366,6 +1367,18 @@ set_target_properties(${INSTALL_TARGET}-shared ) " ) + + get_target_property(COR_IMPLIB_FILE_NAME ${INSTALL_TARGET}-shared COR_IMPLIB_FILE_NAME) + if (NOT COR_IMPLIB_FILE_NAME MATCHES .*-NOTFOUND) + file(APPEND + ${CMAKE_BINARY_DIR}/corrosion/${EXTRA_TARGETS_EXPORT_NAME} + " +set_target_properties(${INSTALL_TARGET}-shared + PROPERTIES + IMPORTED_IMPLIB \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_IMPLIB_FILE_NAME} +)" + ) + endif() endif() endif() endif() From 21bdfd88671962b9ee3d59bb1f4cb4af9e623fa4 Mon Sep 17 00:00:00 2001 From: gtker <75587547+gtker@users.noreply.github.com> Date: Sat, 10 Aug 2024 13:52:34 +0200 Subject: [PATCH 05/10] Update docs for corrosion_install Co-authored-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com> --- cmake/Corrosion.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 6376b5d8..de928482 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1109,7 +1109,7 @@ corrosion_install(TARGETS ... [EXPORT TargetsCorrosion.cmake that must be included in the installed config file. +* **EXPORT**: Creates an export that can be installed with `install(EXPORT)`. Also creates a file at ${CMAKE_BINARY_DIR}/corrosion/Corrosion.cmake that must be included in the installed config file. * **ARCHIVE**/**LIBRARY**/**RUNTIME**/PUBLIC_HEADER: Designates that the following settings only apply to that specific type of object. * **DESTINATION**: The subdirectory within the CMAKE_INSTALL_PREFIX that a specific object should be placed. Defaults to values from GNUInstallDirs. * **PERMISSIONS**: The permissions of files copied into the install prefix. From 907057a4fd100c160930007ebd91c8e076914164 Mon Sep 17 00:00:00 2001 From: Gtker Date: Sat, 10 Aug 2024 13:54:29 +0200 Subject: [PATCH 06/10] Update corrosion_install to deliberately unset EXPORT_NAME --- cmake/Corrosion.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index de928482..47711563 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1170,6 +1170,9 @@ function(corrosion_install) set(EXTRA_TARGETS_EXPORT_NAME ${EXPORT_NAME}Corrosion.cmake) set(EXPORT_NAME EXPORT ${EXPORT_NAME}) endif() + else() + # Prevent variable set in user code from interfering + set(EXPORT_NAME) endif() # Loop over all arguments and get options for each install target type From 684eb57b21e36f558a414c6094dd7cdc896261ab Mon Sep 17 00:00:00 2001 From: Gtker Date: Sat, 10 Aug 2024 13:56:44 +0200 Subject: [PATCH 07/10] Add strings around paths in corrosion_install export file --- cmake/Corrosion.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 47711563..74443faa 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1314,7 +1314,7 @@ function(corrosion_install) add_library(${INSTALL_TARGET}-static STATIC IMPORTED) set_target_properties(${INSTALL_TARGET}-static PROPERTIES - IMPORTED_LOCATION \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME} + IMPORTED_LOCATION \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME}\" ) " ) @@ -1366,7 +1366,7 @@ set_target_properties(${INSTALL_TARGET}-static add_library(${INSTALL_TARGET}-shared SHARED IMPORTED) set_target_properties(${INSTALL_TARGET}-shared PROPERTIES - IMPORTED_LOCATION \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME} + IMPORTED_LOCATION \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME}\" ) " ) @@ -1378,7 +1378,7 @@ set_target_properties(${INSTALL_TARGET}-shared " set_target_properties(${INSTALL_TARGET}-shared PROPERTIES - IMPORTED_IMPLIB \${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_IMPLIB_FILE_NAME} + IMPORTED_IMPLIB \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_IMPLIB_FILE_NAME}\" )" ) endif() From e30004692eb0f28461619a05ef9d5ed127435c45 Mon Sep 17 00:00:00 2001 From: Gtker Date: Mon, 12 Aug 2024 15:35:32 +0200 Subject: [PATCH 08/10] Make corrosion_install only install HEADERS file set --- cmake/Corrosion.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 74443faa..2fafda8a 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1412,8 +1412,8 @@ set_target_properties(${INSTALL_TARGET}-shared set(CONFIGURATIONS) endif() - get_target_property(HEADER_SETS ${INSTALL_TARGET} INTERFACE_HEADER_SETS) - if(NOT HEADER_SETS OR HEADER_SETS MATCHES .*-NOTFOUND) + get_target_property(FILE_SET ${INSTALL_TARGET} INTERFACE_HEADER_SETS) + if(NOT FILE_SET OR FILE_SET MATCHES .*-NOTFOUND) set(TARGET_HAS_FILE_SET FALSE) else() set(TARGET_HAS_FILE_SET TRUE) @@ -1445,7 +1445,7 @@ set_target_properties(${INSTALL_TARGET}-shared install( TARGETS ${INSTALL_TARGET} ${EXPORT_NAME} - FILE_SET ${HEADER_SETS} + FILE_SET HEADERS DESTINATION ${DESTINATION} PERMISSIONS ${PERMISSIONS} ${CONFIGURATIONS} From f1839065a6219d9590a0cae0bd5660527e0134b1 Mon Sep 17 00:00:00 2001 From: Gtker Date: Mon, 12 Aug 2024 15:39:15 +0200 Subject: [PATCH 09/10] Move corrosion_install example into usages.md --- doc/src/quick_start.md | 78 ------------------------------------------ doc/src/usage.md | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 78 deletions(-) diff --git a/doc/src/quick_start.md b/doc/src/quick_start.md index 7a1a88d2..ec1600a6 100644 --- a/doc/src/quick_start.md +++ b/doc/src/quick_start.md @@ -33,83 +33,5 @@ add_executable(your_cool_cpp_bin main.cpp) target_link_libraries(your_cool_cpp_bin PUBLIC rust-lib) ``` -The example below shows how to import a rust library and make it available for install through CMake. - - -```cmake -include(FetchContent) - -FetchContent_Declare( - Corrosion - GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git - GIT_TAG v0.5 # Optionally specify a commit hash, version tag or branch here -) -# Set any global configuration variables such as `Rust_TOOLCHAIN` before this line! -FetchContent_MakeAvailable(Corrosion) - -# Import targets defined in a package or workspace manifest `Cargo.toml` file -corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml) - -# Add a manually written header file which will be exported -# Requires CMake >=3.23 -target_sources(rust-lib INTERFACE - FILE_SET HEADERS - BASE_DIRS include - FILES - include/rust-lib/rust-lib.h -) - -# OR for CMake <= 3.23 -target_include_directories(is_odd INTERFACE - $ - $ -) -target_sources(is_odd - INTERFACE - $ - $ -) - -# Rust libraries must be installed using `corrosion_install`. -corrosion_install(TARGETS rust-lib EXPORT RustLibTargets) - -# Installs the main target -install( - EXPORT RustLibTargets - NAMESPACE RustLib:: - DESTINATION lib/cmake/RustLib -) - -# Necessary for packaging helper commands -include(CMakePackageConfigHelpers) -# Create a file for checking version compatibility -# Optional -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake" - VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" - COMPATIBILITY AnyNewerVersion -) - -# Configures the main config file that cmake loads -configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in - "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake" - INSTALL_DESTINATION lib/cmake/RustLib - NO_SET_AND_CHECK_MACRO - NO_CHECK_REQUIRED_COMPONENTS_MACRO -) -# Config.cmake.in contains -# @PACKAGE_INIT@ -# -# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargetsCorrosion.cmake) -# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargets.cmake) - -# Install all generated files -install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake - ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake - ${CMAKE_CURRENT_BINARY_DIR}/corrosion/RustLibTargetsCorrosion.cmake - DESTINATION lib/cmake/RustLib -) -``` Please see the [Usage chapter](usage.md) for a complete discussion of possible configuration options. diff --git a/doc/src/usage.md b/doc/src/usage.md index fbbf1036..50eb098c 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -39,6 +39,84 @@ Corrosion provides `corrosion_install` to automatically install relevant files: {{#include ../../cmake/Corrosion.cmake:corrosion-install}} +The example below shows how to import a rust library and make it available for install through CMake. + +```cmake +include(FetchContent) + +FetchContent_Declare( + Corrosion + GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git + GIT_TAG v0.5 # Optionally specify a commit hash, version tag or branch here +) +# Set any global configuration variables such as `Rust_TOOLCHAIN` before this line! +FetchContent_MakeAvailable(Corrosion) + +# Import targets defined in a package or workspace manifest `Cargo.toml` file +corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml) + +# Add a manually written header file which will be exported +# Requires CMake >=3.23 +target_sources(rust-lib INTERFACE + FILE_SET HEADERS + BASE_DIRS include + FILES + include/rust-lib/rust-lib.h +) + +# OR for CMake <= 3.23 +target_include_directories(is_odd INTERFACE + $ + $ +) +target_sources(is_odd + INTERFACE + $ + $ +) + +# Rust libraries must be installed using `corrosion_install`. +corrosion_install(TARGETS rust-lib EXPORT RustLibTargets) + +# Installs the main target +install( + EXPORT RustLibTargets + NAMESPACE RustLib:: + DESTINATION lib/cmake/RustLib +) + +# Necessary for packaging helper commands +include(CMakePackageConfigHelpers) +# Create a file for checking version compatibility +# Optional +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake" + VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion +) + +# Configures the main config file that cmake loads +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake" + INSTALL_DESTINATION lib/cmake/RustLib + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) +# Config.cmake.in contains +# @PACKAGE_INIT@ +# +# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargetsCorrosion.cmake) +# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargets.cmake) + +# Install all generated files +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake + ${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/corrosion/RustLibTargetsCorrosion.cmake + DESTINATION lib/cmake/RustLib +) +``` + [install commands]: https://cmake.org/cmake/help/latest/command/install.html ### Per Target options From 849474ca9c8b3961fd4da5c87a390338f40460ea Mon Sep 17 00:00:00 2001 From: Gtker Date: Tue, 13 Aug 2024 15:09:09 +0200 Subject: [PATCH 10/10] Add header file to install_lib test This is not actually part of the test. --- test/corrosion_install/install_lib/rust_lib/CMakeLists.txt | 1 + .../install_lib/rust_lib/include/rust_lib/rust_lib.hpp | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 test/corrosion_install/install_lib/rust_lib/include/rust_lib/rust_lib.hpp diff --git a/test/corrosion_install/install_lib/rust_lib/CMakeLists.txt b/test/corrosion_install/install_lib/rust_lib/CMakeLists.txt index d05838ba..3e39de83 100644 --- a/test/corrosion_install/install_lib/rust_lib/CMakeLists.txt +++ b/test/corrosion_install/install_lib/rust_lib/CMakeLists.txt @@ -13,5 +13,6 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") set_target_properties(rust_lib-shared PROPERTIES IMPORTED_SONAME librust_lib.dylib) endif() +target_sources(rust_lib INTERFACE include/rust_lib/rust_lib.hpp) corrosion_install(TARGETS rust_lib) diff --git a/test/corrosion_install/install_lib/rust_lib/include/rust_lib/rust_lib.hpp b/test/corrosion_install/install_lib/rust_lib/include/rust_lib/rust_lib.hpp new file mode 100644 index 00000000..827ae6e5 --- /dev/null +++ b/test/corrosion_install/install_lib/rust_lib/include/rust_lib/rust_lib.hpp @@ -0,0 +1,3 @@ +#include + +extern "C" uint64_t add(uint64_t left, uint64_t right);