Skip to content

Commit

Permalink
Paramatized rust_static_library to enable CXX support
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/folly#2092

X-link: facebook/fb303#42

X-link: facebookincubator/velox#7301

We are now using CXX (and not just bindgen/cbindgen) for building our Rust C/C++ APIS, but our OSS tooling did not ergomically support this. This change adds a single option, `USE_CXX_INCLUDE`, to the CMake function, `rust_static_library`, to enable adding the `cxxbridge` path to the include path.

Differential Revision: D50772544
  • Loading branch information
John Elliott authored and facebook-github-bot committed Oct 29, 2023
1 parent b1e4816 commit b642b70
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions build/fbcode_builder/CMake/RustStaticLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ set_property(GLOBAL APPEND PROPERTY JOB_POOLS rust_job_pool=1)
# Cargo build static library.
#
# ```cmake
# rust_static_library(<TARGET> [CRATE <CRATE_NAME>] [FEATURES <FEATURE_NAME>])
# rust_static_library(<TARGET> [CRATE <CRATE_NAME>] [FEATURES <FEATURE_NAME>] [USE_CXX_INCLUDE])
# ```
#
# Parameters:
Expand All @@ -92,6 +92,8 @@ set_property(GLOBAL APPEND PROPERTY JOB_POOLS rust_job_pool=1)
# fallback to `${TARGET}`.
# - FEATURE_NAME:
# Name of the Rust feature to enable.
# - USE_CXX_INCLUDE:
# Include cxx.rs include path in `${TARGET}` INTERFACE.
#
# This function creates two targets:
# - "${TARGET}": an interface library target contains the static library built
Expand All @@ -103,7 +105,7 @@ set_property(GLOBAL APPEND PROPERTY JOB_POOLS rust_job_pool=1)
# headers with the interface library.
#
function(rust_static_library TARGET)
fb_cmake_parse_args(ARG "" "CRATE;FEATURES" "" "${ARGN}")
fb_cmake_parse_args(ARG "USE_CXX_INCLUDE" "CRATE;FEATURES" "" "${ARGN}")

if(DEFINED ARG_CRATE)
set(crate_name "${ARG_CRATE}")
Expand Down Expand Up @@ -158,6 +160,14 @@ function(rust_static_library TARGET)
INTERFACE_INSTALL_LIBNAME
"${CMAKE_STATIC_LIBRARY_PREFIX}${crate_name}_rs${CMAKE_STATIC_LIBRARY_SUFFIX}"
)

if(DEFINED ARG_USE_CXX_INCLUDE)
target_include_directories(
${TARGET}
INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/
)
endif()

target_link_libraries(
${TARGET}
INTERFACE "$<BUILD_INTERFACE:${rust_staticlib}>"
Expand Down

0 comments on commit b642b70

Please sign in to comment.