Files
scylladb/rust/CMakeLists.txt
Radosław Cybulski d589e68642 Add precompiled headers to CMakeLists.txt
Add precompiled header support to CMakeLists.txt and configure.py -
it improves compilation time by approximately 10%.

New header `stdafx.hh` is added, don't include it manually -
the compiler will include it for you. The header contains includes from
external libraries used by Scylla - seastar, standard library,
linux headers and zlib.

The feature is enabled by default, use CMake option `Scylla_USE_PRECOMPILED_HEADER`
or configure.py --disable-precompiled-header to disable.

The feature should be disabled, when trying to check headers - otherwise
you might get false negatives on missing includes from seastar / abseil and so on.

Note: following configuration needs to be added to ccache.conf:

    sloppiness = pch_defines,time_macros,include_file_mtime,include_file_ctime

Closes scylladb/scylladb#26617
2025-11-21 12:27:41 +02:00

106 lines
3.3 KiB
CMake

find_program(CARGO cargo
REQUIRED)
function(add_rust_library name)
# used for profiles defined in Cargo.toml
if(CMAKE_CONFIGURATION_TYPES)
foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(APPEND build_mode
"$<$<CONFIG:${config}>:${scylla_build_mode_${config}}>")
endforeach()
else()
set(build_mode ${scylla_build_mode_${CMAKE_BUILD_TYPE}})
endif()
set(profile "rust-${build_mode}")
set(target_dir ${CMAKE_CURRENT_BINARY_DIR})
set(library ${target_dir}/lib${name}.a)
add_custom_command(
OUTPUT ${library}
COMMAND ${CMAKE_COMMAND} -E env CARGO_BUILD_DEP_INFO_BASEDIR=. ${CARGO} build --locked --target-dir=${target_dir} --profile=${profile}
COMMAND ${CMAKE_COMMAND} -E copy ${target_dir}/${profile}/lib${name}.a ${library}
DEPENDS Cargo.lock
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Building Rust library: ${library}")
add_custom_target(rust-${name}
DEPENDS ${target_dir}/lib${name}.a)
add_library(Rust::${name} STATIC IMPORTED)
add_dependencies(Rust::${name} rust-${name})
set_target_properties(Rust::${name} PROPERTIES
IMPORTED_LOCATION "${library}")
endfunction(add_rust_library)
find_program(CXXBRIDGE cxxbridge
REQUIRED)
# Generate cxxbridge header and source
function(generate_cxxbridge name)
cmake_parse_arguments(parsed_args
""
"INPUT;OUTPUT_DIR;INCLUDE;SOURCES" "" ${ARGN})
set(input ${CMAKE_CURRENT_SOURCE_DIR}/${parsed_args_INPUT})
set(output_dir ${parsed_args_OUTPUT_DIR})
set(header ${output_dir}/${name}.hh)
set(source ${output_dir}/${name}.cc)
set(outputs ${header} ${source})
set(command ${CXXBRIDGE} ${input} --include ${parsed_args_INCLUDE})
add_custom_command(
DEPENDS ${input}
OUTPUT ${outputs}
COMMAND ${command} --header --output ${header}
COMMAND ${command} --output ${source})
set(${parsed_args_SOURCES} ${outputs} PARENT_SCOPE)
endfunction(generate_cxxbridge)
add_rust_library(rust_combined)
set(binding_gen_build_dir "${scylla_gen_build_dir}/rust")
set(cxx_header "${binding_gen_build_dir}/cxx.h")
add_custom_command(
OUTPUT ${cxx_header}
COMMAND ${CXXBRIDGE} --header --output ${cxx_header})
generate_cxxbridge(wasmtime_bindings
INPUT wasmtime_bindings/src/lib.rs
INCLUDE rust/cxx.h
OUTPUT_DIR "${binding_gen_build_dir}"
SOURCES wasmtime_bindings_sources)
set_target_properties(Rust::rust_combined PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}")
add_library(wasmtime_bindings STATIC)
target_sources(wasmtime_bindings
PRIVATE
${cxx_header}
${wasmtime_bindings_sources})
target_include_directories(wasmtime_bindings
PUBLIC
${scylla_gen_build_dir})
target_link_libraries(wasmtime_bindings
INTERFACE Rust::rust_combined)
if (Scylla_USE_PRECOMPILED_HEADER_USE)
target_precompile_headers(wasmtime_bindings REUSE_FROM scylla-precompiled-header)
endif()
# only for testing
generate_cxxbridge(inc
INPUT inc/src/lib.rs
INCLUDE rust/cxx.h
OUTPUT_DIR "${binding_gen_build_dir}"
SOURCES inc_sources)
add_library(inc STATIC)
target_sources(inc
PRIVATE
${cxx_header}
${inc_sources})
target_include_directories(inc
PUBLIC
${scylla_gen_build_dir})
target_link_libraries(inc
INTERFACE Rust::rust_combined)
if (Scylla_USE_PRECOMPILED_HEADER_USE)
target_precompile_headers(inc REUSE_FROM scylla-precompiled-header)
endif()