Skip to content

Instantly share code, notes, and snippets.

@GamePad64
Created December 2, 2021 09:36
Show Gist options
  • Save GamePad64/05ef352ad93adf3fcec78bdcdfc32a5e to your computer and use it in GitHub Desktop.
Save GamePad64/05ef352ad93adf3fcec78bdcdfc32a5e to your computer and use it in GitHub Desktop.
macro(add_cxxbridge CXXBRIDGE_LIBRARY_NAME CXXBRIDGES_TXT CRATE_NAME)
if (CMAKE_VS_PLATFORM_NAME)
set (build_dir "${CMAKE_VS_PLATFORM_NAME}/$<CONFIG>")
elseif(CMAKE_CONFIGURATION_TYPES)
set (build_dir "$<CONFIG>")
else()
set (build_dir .)
endif()
# cxxbridge
set(CXXBRIDGE_ROOT "${CMAKE_BINARY_DIR}/${build_dir}/cargo/build/${Rust_CARGO_HOST_TARGET}/cxxbridge")
file(STRINGS ${CXXBRIDGES_TXT} CXX_BRIDGES)
foreach(module ${CXX_BRIDGES})
list(APPEND CXXBRIDGE_SRCS "${CXXBRIDGE_ROOT}/${CRATE_NAME}/${module}.cc" "${CXXBRIDGE_ROOT}/${CRATE_NAME}/${module}.h")
endforeach()
list(APPEND CXXBRIDGE_SRCS "${CXXBRIDGE_ROOT}/rust/cxx.h")
expand_config_genex(SRCS_EXPANDED "${CXXBRIDGE_SRCS}")
message(${SRCS_EXPANDED})
set_source_files_properties(${SRCS_EXPANDED} PROPERTIES GENERATED 1)
add_library(${CXXBRIDGE_LIBRARY_NAME} STATIC ${CXXBRIDGE_SRCS})
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CXXBRIDGES_TXT})
set_target_properties(${CXXBRIDGE_LIBRARY_NAME} PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(${CXXBRIDGE_LIBRARY_NAME} INTERFACE "${CXXBRIDGE_ROOT}")
endmacro()
macro(expand_config_genex EXPANDED_LIST GENEX_LIST)
if(CMAKE_CONFIGURATION_TYPES)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
foreach(src_genex ${GENEX_LIST})
string(REPLACE "$<CONFIG>" "${config}" expanded_src "${src_genex}")
list(APPEND ${EXPANDED_LIST} "${expanded_src}")
endforeach()
endforeach()
else()
set(${EXPANDED_LIST} "${GENEX_LIST}")
endif()
endmacro()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment