cmake_minimum_required(VERSION 3.21)
project(___name___ LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(DORA_ROOT_DIR "__DORA_PATH__" CACHE FILEPATH "Path to the root of dora")
set(dora_cxx_include_dir "${CMAKE_CURRENT_BINARY_DIR}/include/cxx")
set(node_bridge "${CMAKE_CURRENT_BINARY_DIR}/node_bridge.cc")
# Map CMake's CMAKE_BUILD_TYPE to Cargo's profile so a Release build
# links against target/release/ and a Debug build (or no build type
# specified) links against target/debug/. Without this the template
# would hardcode target/debug regardless of the user's build config.
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR NOT CMAKE_BUILD_TYPE)
set(dora_cargo_profile_dir "debug")
set(dora_cargo_release_arg "")
else()
set(dora_cargo_profile_dir "release")
set(dora_cargo_release_arg "--release")
endif()
if(DORA_ROOT_DIR)
include(ExternalProject)
ExternalProject_Add(Dora
SOURCE_DIR ${DORA_ROOT_DIR}
BUILD_IN_SOURCE True
CONFIGURE_COMMAND ""
BUILD_COMMAND
cargo build
${dora_cargo_release_arg}
--package dora-node-api-cxx
INSTALL_COMMAND ""
)
add_custom_command(OUTPUT ${node_bridge} ${dora_cxx_include_dir}
WORKING_DIRECTORY ${DORA_ROOT_DIR}
DEPENDS Dora
COMMAND ${CMAKE_COMMAND} -E make_directory ${dora_cxx_include_dir}
COMMAND ${CMAKE_COMMAND} -E copy target/cxxbridge/dora-node-api-cxx/src/lib.rs.cc ${node_bridge}
COMMAND ${CMAKE_COMMAND} -E copy target/cxxbridge/dora-node-api-cxx/src/lib.rs.h ${dora_cxx_include_dir}/dora-node-api.h
)
add_custom_target(Dora_cxx DEPENDS ${node_bridge} ${dora_cxx_include_dir})
set(dora_link_dirs ${DORA_ROOT_DIR}/target/${dora_cargo_profile_dir})
else()
include(ExternalProject)
ExternalProject_Add(Dora
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dora
GIT_REPOSITORY https://github.com/dora-rs/dora.git
GIT_TAG main
BUILD_IN_SOURCE True
CONFIGURE_COMMAND ""
BUILD_COMMAND
cargo build
${dora_cargo_release_arg}
--package dora-node-api-cxx
--target-dir ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target
INSTALL_COMMAND ""
)
add_custom_command(OUTPUT ${node_bridge} ${dora_cxx_include_dir}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target
DEPENDS Dora
COMMAND ${CMAKE_COMMAND} -E make_directory ${dora_cxx_include_dir}
COMMAND ${CMAKE_COMMAND} -E copy cxxbridge/dora-node-api-cxx/src/lib.rs.cc ${node_bridge}
COMMAND ${CMAKE_COMMAND} -E copy cxxbridge/dora-node-api-cxx/src/lib.rs.h ${dora_cxx_include_dir}/dora-node-api.h
)
set(dora_link_dirs ${CMAKE_CURRENT_BINARY_DIR}/dora/src/Dora/target/${dora_cargo_profile_dir})
add_custom_target(Dora_cxx DEPENDS ${node_bridge} ${dora_cxx_include_dir})
endif()
link_directories(${dora_link_dirs})
add_executable(___name___ node.cc ${node_bridge})
add_dependencies(___name___ Dora_cxx)
target_include_directories(___name___ PRIVATE ${dora_cxx_include_dir})
if(WIN32)
target_link_libraries(___name___ dora_node_api_cxx ws2_32 userenv ntdll bcrypt)
elseif(APPLE)
# CoreFoundation: chrono time-zone APIs (CFRelease, CFTimeZoneCopySystem).
# Security + SystemConfiguration: pulled by transitive Rust deps that
# touch TLS and network config. Without these the link fails on macOS
# with missing _CF* and _Sec* symbols.
target_link_libraries(___name___
dora_node_api_cxx
z
"-framework CoreFoundation"
"-framework Security"
"-framework SystemConfiguration"
)
else()
target_link_libraries(___name___ dora_node_api_cxx z)
endif()
install(TARGETS ___name___ DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/bin)