Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exemple Integration of Rust Code into C++ Base #1580

Closed
wants to merge 16 commits into from
23 changes: 20 additions & 3 deletions .github/workflows/build-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Build - Ubuntu
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
types: [ opened, synchronize, reopened, ready_for_review ]
paths:
- 'src/**'
push:
Expand All @@ -26,8 +26,8 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, ubuntu-22.04]
buildtype: [linux-release, linux-debug]
os: [ ubuntu-20.04, ubuntu-22.04 ]
buildtype: [ linux-release, linux-debug ]
include:
- os: ubuntu-20.04
triplet: x64-linux
Expand All @@ -42,6 +42,23 @@ jobs:
concurrent_skipping: 'same_content'
cancel_others: true

- name: Setup Rust tools
run: |
rustup toolchain install nightly
rustup component add rustfmt --toolchain nightly
rustup component add clippy --toolchain nightly

- name: Remove existing Rust tools
run: |
rm -f ~/.cargo/bin/rust-analyzer
rm -f ~/.cargo/bin/rustfmt
rm -f ~/.cargo/bin/cargo-fmt

- name: Update Rust
run: |
rustup self update
rustup update stable

- name: Checkout repository
uses: actions/checkout@main

Expand Down
23 changes: 20 additions & 3 deletions .github/workflows/build-windows-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build - Windows - CMake
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
types: [ opened, synchronize, reopened, ready_for_review ]
paths:
- 'src/**'
push:
Expand All @@ -21,8 +21,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-2022]
buildtype: [windows-release]
os: [ windows-2022 ]
buildtype: [ windows-release ]
include:
- os: windows-2022
triplet: x64-windows-static
Expand All @@ -36,6 +36,23 @@ jobs:
concurrent_skipping: 'same_content'
cancel_others: true

- name: Setup Rust tools
run: |
rustup toolchain install nightly
rustup component add rustfmt --toolchain nightly
rustup component add clippy --toolchain nightly

- name: Remove existing Rust tools
run: |
Remove-Item -Path $env:USERPROFILE\.cargo\bin\rust-analyzer.exe -Force
Remove-Item -Path $env:USERPROFILE\.cargo\bin\rustfmt.exe -Force
Remove-Item -Path $env:USERPROFILE\.cargo\bin\cargo-fmt.exe -Force

- name: Update Rust
run: |
rustup self update
rustup update stable

- name: Checkout repository
uses: actions/checkout@main

Expand Down
58 changes: 29 additions & 29 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ if (CMAKE_COMPILER_IS_GNUCXX)
message("-- Compiler: GCC - Version: ${CMAKE_CXX_COMPILER_VERSION}")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS GNUCXX_MINIMUM_VERSION)
message(FATAL_ERROR "GCC version must be at least ${GNUCXX_MINIMUM_VERSION}!")
endif()
endif()
endif ()
endif ()

# === Minimum required version for visual studio ===
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message("-- Compiler: Visual Studio - Version: ${CMAKE_CXX_COMPILER_VERSION}")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MSVC_MINIMUM_VERSION)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS MSVC_MINIMUM_VERSION)
message(FATAL_ERROR "Visual Studio version must be at least ${MSVC_MINIMUM_VERSION}")
endif()
endif()
endif ()
endif ()

# *****************************************************************************
# Sanity Checks
Expand All @@ -69,50 +69,50 @@ option(BUILD_STATIC_LIBRARY "Build using static libraries" OFF)
option(SPEED_UP_BUILD_UNITY "Compile using build unity for speed up build" ON)

# === ASAN ===
if(ASAN_ENABLED)
if (ASAN_ENABLED)
log_option_enabled("asan")
if(MSVC)
if (MSVC)
add_compile_options(/fsanitize=address)
else()
else ()
add_compile_options(-fsanitize=address)
link_libraries(-fsanitize=address)
endif()
else()
endif ()
else ()
log_option_disabled("asan")
endif()
endif ()

# Build static libs
if(BUILD_STATIC_LIBRARY)
if (BUILD_STATIC_LIBRARY)
log_option_enabled("STATIC_LIBRARY")

if(MSVC)
if (MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
elseif(UNIX AND NOT APPLE)
elseif (UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
elseif(APPLE)
elseif (APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".dylib")
endif()
else()
endif ()
else ()
log_option_disabled("STATIC_LIBRARY")
endif()
endif ()

# === DEBUG LOG ===
# cmake -DDEBUG_LOG=ON ..
if(DEBUG_LOG)
if (DEBUG_LOG)
add_definitions(-DDEBUG_LOG=ON)
log_option_enabled("DEBUG LOG")
else()
else ()
log_option_disabled("DEBUG LOG")
endif(DEBUG_LOG)
endif (DEBUG_LOG)

# *****************************************************************************
# Compiler Options
# *****************************************************************************
if (MSVC)
foreach(type RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
foreach (type RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${type} "${CMAKE_CXX_FLAGS_${type}}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${type} "${CMAKE_C_FLAGS_${type}}")
endforeach(type)
endforeach (type)

add_compile_options(/MP /FS /Zf /EHsc)
endif (MSVC)
Expand All @@ -123,20 +123,20 @@ function(set_output_directory target_name)
set_target_properties(${target_name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
else()
)
else ()
set_target_properties(${target_name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/"
)
endif()
)
endif ()
endfunction()

## Setup shared target basic configurations
function(setup_target TARGET_NAME)
if (MSVC AND BUILD_STATIC_LIBRARY)
set_property(TARGET ${TARGET_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif ()
endfunction()

# *****************************************************************************
Expand Down
46 changes: 23 additions & 23 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ add_subdirectory(utils)

# Add more global sources - please add preferably in the sub_directory CMakeLists.
set(ProtobufFiles
protobuf/appearances.pb.cc
protobuf/kv.pb.cc
protobuf/appearances.pb.cc
protobuf/kv.pb.cc
)

# Add more global sources - please add preferably in the sub_directory CMakeLists.
target_sources(${PROJECT_NAME}_lib PRIVATE canary_server.cpp ${ProtobufFiles})

# Skip unity build inclusion for protobuf files
set_source_files_properties(
${ProtobufFiles} PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON
${ProtobufFiles} PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON
)


Expand All @@ -41,21 +41,21 @@ target_precompile_headers(${PROJECT_NAME}_lib PUBLIC pch.hpp)
# *****************************************************************************
if (CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${PROJECT_NAME}_lib PRIVATE -Wno-deprecated-declarations)
endif()
endif ()

# === IPO ===
check_ipo_supported(RESULT result OUTPUT output)
if(result)
if (result)
set_property(TARGET ${PROJECT_NAME}_lib PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
else ()
message(WARNING "IPO is not supported: ${output}")
endif()
endif ()

# === UNITY BUILD (compile time reducer) ===
if(SPEED_UP_BUILD_UNITY)
if (SPEED_UP_BUILD_UNITY)
set_target_properties(${PROJECT_NAME}_lib PROPERTIES UNITY_BUILD ON)
log_option_enabled("Build unity for speed up compilation")
endif()
endif ()

# *****************************************************************************
# Target include directories - to allow #include
Expand All @@ -67,13 +67,13 @@ target_include_directories(${PROJECT_NAME}_lib
${GMP_INCLUDE_DIRS}
${LUAJIT_INCLUDE_DIRS}
${PARALLEL_HASHMAP_INCLUDE_DIRS}
)
)

# *****************************************************************************
# Target links to external dependencies
# *****************************************************************************
target_link_libraries(${PROJECT_NAME}_lib
PUBLIC
PUBLIC
${GMP_LIBRARIES}
${LUAJIT_LIBRARIES}
CURL::libcurl
Expand All @@ -92,31 +92,31 @@ target_link_libraries(${PROJECT_NAME}_lib
unofficial::mariadbclient
)

if(CMAKE_BUILD_TYPE MATCHES Debug)
if (CMAKE_BUILD_TYPE MATCHES Debug)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${ZLIB_LIBRARY_DEBUG})
else()
else ()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${ZLIB_LIBRARY_RELEASE})
endif()
endif ()

if (MSVC)
if(BUILD_STATIC_LIBRARY)
if (BUILD_STATIC_LIBRARY)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static)
else()
else ()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_lib)
endif()
endif ()

target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${MYSQL_CLIENT_LIBS})
else()
else ()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static Threads::Threads)
endif (MSVC)

# === OpenMP ===
if(OPTIONS_ENABLE_OPENMP)
if (OPTIONS_ENABLE_OPENMP)
log_option_enabled("openmp")
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
if (OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC OpenMP::OpenMP_CXX)
endif()
else()
endif ()
else ()
log_option_disabled("openmp")
endif()
endif ()
45 changes: 45 additions & 0 deletions cmake/modules/RustLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Check if Rust's package manager, Cargo, is installed
find_program(CARGO cargo)
if (NOT CARGO)
message(FATAL_ERROR "Cargo (Rust package manager) not found. Please install Rust and Cargo to continue.")
endif ()

# Determine the correct library extension based on the operating system
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(RUST_LIB_EXT "beats.lib")
else ()
set(RUST_LIB_EXT "libbeats.a")
endif ()

# Determine the build mode (Release, Debug, or RelWithDebInfo) for Rust compilation
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(RUST_BUILD_FLAG "")
elseif (CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
set(RUST_BUILD_FLAG "--profile=RelWithDebInfo")
elseif (CMAKE_BUILD_TYPE MATCHES Release)
set(RUST_BUILD_FLAG " --release ")
endif ()

# Set base paths for the Rust target directory and full library path
set(RUST_LIB_FULL_PATH ${CMAKE_CURRENT_BINARY_DIR}/rust/${CMAKE_BUILD_TYPE}/${RUST_LIB_EXT})

# Define Rust source files that the compilation depends on
file(GLOB RUST_SOURCE_FILES "${CMAKE_SOURCE_DIR}/rust/src/*.rs")

# Always compile the Rust library when any .rs file is modified
add_custom_command(
OUTPUT ${RUST_LIB_FULL_PATH}
COMMAND cargo build ${RUST_BUILD_FLAG} --target-dir=${CMAKE_CURRENT_BINARY_DIR}/rust
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/rust
DEPENDS ${RUST_SOURCE_FILES}
COMMENT " Compiling the Rust library "
)

# Add a custom target that depends on the Rust library
add_custom_target(
RustLibTarget ALL
DEPENDS ${RUST_LIB_FULL_PATH}
)

# Make the main project depend on the custom Rust target
add_dependencies(${PROJECT_NAME} RustLibTarget)
4 changes: 4 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/Cargo.lock
*.lib
*.a
14 changes: 14 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "beats"
version = "0.1.0"
edition = "2021"

# Custom profile
[profile.RelWithDebInfo]
inherits = "release"
debug = true

build = "build.rs"

[lib]
crate-type = ["staticlib"]
Loading