Skip to content

Commit

Permalink
Create our own shared library
Browse files Browse the repository at this point in the history
There's a pretty nasty bug where Cargo can't generate the shared library
itself, and we need to do in CMake. This isn't a huge deal, as we can
simply link to the static library created by Cargo.

This is an upstream issue where Rust marks unknown symbols as local, and
they never appear in the dynamic symbol table. There is an open issue
to allow more granular filtering
(rust-lang/rust#104130) as well as an upstream
CXX issue (dtolnay/cxx#1331)
  • Loading branch information
redstrate committed Aug 31, 2024
1 parent 4e7fb44 commit ffb85f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# 3.24 is needed for LINK_LIBRARY:WHOLE_ARCHIVE support
cmake_minimum_required(VERSION 3.24)

project(Vodozemac-cpp)
project(vodozemac-cpp)

find_package(Corrosion REQUIRED)

corrosion_import_crate(
MANIFEST_PATH Cargo.toml
)

add_custom_target(copy_header ALL DEPENDS cargo-build_vodozemac BYPRODUCTS ${CMAKE_BINARY_DIR}/vodozemac.h VERBATIM COMMAND
cp -L ${CMAKE_BINARY_DIR}/cargo/build/${Rust_CARGO_TARGET}/cxxbridge/vodozemac/src/lib.rs.h ${CMAKE_BINARY_DIR}/vodozemac.h
)

corrosion_install(TARGETS vodozemac)
install(FILES "${CMAKE_BINARY_DIR}/vodozemac.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vodozemac)
# We create our own shared library instead of having Cargo do it
# To work around https://github.com/dtolnay/cxx/issues/1331
add_library(vodozemac-cpp SHARED)
# We need a dummy file so CMake will have something to link with
target_sources(vodozemac-cpp PRIVATE src/dummy.cpp)
# We need to link to the whole archive, otherwise important symbols are thrown away
target_link_libraries(vodozemac-cpp PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,vodozemac-static>")
1 change: 1 addition & 0 deletions src/dummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file is intentionally left blank

0 comments on commit ffb85f4

Please sign in to comment.