Skip to content

Commit

Permalink
Make corrosion_install work for library files and update docs (corros…
Browse files Browse the repository at this point in the history
…ion-rs#543)

* Make corrosion_install work for library files

Works on corrosion-rs#415.

This only installs the actual library file(s) and not extras such as
headers.

Not tested on Windows for compatibility with import library files for
shared libraries.

* Add `corrosion_install` to public docs

Works on corrosion-rs#63.

The function definition is currently prescriptive, since it describes
features that don't currently work.

EXPORT causes a FATAL_ERROR, and PRIVATE_HEADER/PUBLIC_HEADER do
nothing.

* Change corrosion_install according to PR feedback

Changes requested in corrosion-rs#543.

* Fix typo in corrosion_install docs

Co-authored-by: Tobias Fella <9750016+TobiasFella@users.noreply.github.com>

* Update corrosion_install docstring to remove TODO

---------

Co-authored-by: Gtker <git@gtker.com>
Co-authored-by: Tobias Fella <9750016+TobiasFella@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 3, 2024
1 parent b45275c commit 9f7c08e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
96 changes: 89 additions & 7 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,26 @@ function(corrosion_link_libraries target_name)
endforeach()
endfunction()

#[=======================================================================[.md:
ANCHOR: corrosion-install
** EXPERIMENTAL **: This function is currently still considered experimental
and is not officially released yet. Feedback and Suggestions are welcome.

```cmake
corrosion_install(TARGETS <target1> ... <targetN>
[[ARCHIVE|LIBRARY|RUNTIME]
[DESTINATION <dir>]
[PERMISSIONS <permissions...>]
[CONFIGURATIONS [Debug|Release|<other-configuration>]]
] [...])
```
* **TARGETS**: Target or targets to install.
* **ARCHIVE**/**LIBRARY**/**RUNTIME**: 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.

ANCHOR_END: corrosion-install
#]=======================================================================]
function(corrosion_install)
# Default install dirs
include(GNUInstallDirs)
Expand All @@ -1112,13 +1132,6 @@ function(corrosion_install)
set(TARGET_ARGS ${OPTIONS} ${ONE_VALUE_ARGS} ${MULTI_VALUE_ARGS})

if (INSTALL_TYPE STREQUAL "TARGETS")
# corrosion_install(TARGETS ... [EXPORT <export-name>]
# [[ARCHIVE|LIBRARY|RUNTIME|PRIVATE_HEADER|PUBLIC_HEADER]
# [DESTINATION <dir>]
# [PERMISSIONS permissions...]
# [CONFIGURATIONS [Debug|Release|...]]
# ] [...])

# Extract targets
set(INSTALL_TARGETS)
list(LENGTH ARGN ARGN_LENGTH)
Expand Down Expand Up @@ -1245,6 +1258,75 @@ function(corrosion_install)
PERMISSIONS ${PERMISSIONS}
${CONFIGURATIONS}
)
elseif(TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
if(TARGET ${INSTALL_TARGET}-static)
if (DEFINED COR_INSTALL_ARCHIVE_DESTINATION)
set(DESTINATION ${COR_INSTALL_ARCHIVE_DESTINATION})
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
else()
set(DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if (DEFINED COR_INSTALL_ARCHIVE_PERMISSIONS)
set(PERMISSIONS ${COR_INSTALL_ARCHIVE_PERMISSIONS})
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
else()
set(PERMISSIONS ${DEFAULT_PERMISSIONS})
endif()

if (DEFINED COR_INSTALL_ARCHIVE_CONFIGURATIONS)
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_ARCHIVE_CONFIGURATIONS})
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
else()
set(CONFIGURATIONS)
endif()

install(
FILES $<TARGET_PROPERTY:${INSTALL_TARGET}-static,IMPORTED_LOCATION>
PERMISSIONS ${PERMISSIONS}
DESTINATION ${DESTINATION}
${CONFIGURATIONS}
)
endif()

if(TARGET ${INSTALL_TARGET}-shared)
if (DEFINED COR_INSTALL_LIBRARY_DESTINATION)
set(DESTINATION ${COR_INSTALL_LIBRARY_DESTINATION})
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
else()
set(DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if (DEFINED COR_INSTALL_LIBRARY_PERMISSIONS)
set(PERMISSIONS ${COR_INSTALL_LIBRARY_PERMISSIONS})
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
else()
set(
PERMISSIONS
${DEFAULT_PERMISSIONS} OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
)
endif()

if (DEFINED COR_INSTALL_LIBRARY_CONFIGURATIONS)
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_LIBRARY_CONFIGURATIONS})
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
else()
set(CONFIGURATIONS)
endif()

install(
IMPORTED_RUNTIME_ARTIFACTS ${INSTALL_TARGET}-shared
PERMISSIONS ${PERMISSIONS}
DESTINATION ${DESTINATION}
${CONFIGURATIONS}
)
endif()
endif()
endforeach()

Expand Down
3 changes: 3 additions & 0 deletions doc/src/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ 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)
# Rust libraries and executables can also be installed using `corrosion_install`.
corrosion_install(TARGETS rust-lib)
```

Please see the [Usage chapter](usage.md) for a complete discussion of possible configuration options.
9 changes: 9 additions & 0 deletions doc/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ target, see [Per Target options](#per-target-options) for details.
[`INTERFACE`]: https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries
[target_link_libraries]: https://cmake.org/cmake/help/latest/command/target_link_libraries.html

### Experimental: Install crate and headers with `corrosion_install`

The default CMake [install commands] do not work correctly with the targets exported from `corrosion_import_crate()`.
Corrosion provides `corrosion_install` to automatically install relevant files:

{{#include ../../cmake/Corrosion.cmake:corrosion-install}}

[install commands]: https://cmake.org/cmake/help/latest/command/install.html

### Per Target options

Some configuration options can be specified individually for each target. You can set them via the
Expand Down

0 comments on commit 9f7c08e

Please sign in to comment.