Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Sep 12, 2023
1 parent cccb0d2 commit 375b716
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 49 deletions.
Binary file removed beats-rust/beats.lib
Binary file not shown.
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}/beats-rust/${CMAKE_BUILD_TYPE}/${BEATS_LIB_EXT})

# Define Rust source files that the compilation depends on
file(GLOB RUST_SOURCE_FILES "${CMAKE_SOURCE_DIR}/beats-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}/beats-rust
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/beats-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)
2 changes: 2 additions & 0 deletions beats-rust/.gitignore → rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
/Cargo.lock
*.lib
*.a
5 changes: 5 additions & 0 deletions beats-rust/Cargo.toml → rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "beats"
version = "0.1.0"
edition = "2021"

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

build = "build.rs"

[lib]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 2 additions & 49 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,7 @@ include(CanaryLib)
# Define main executable target, set it up and link to main library
add_executable(${PROJECT_NAME} main.cpp)

# Tente encontrar a biblioteca
find_library(BEATS_LIB beats
PATHS
"${CMAKE_CURRENT_BINARY_DIR}/beats-rust/target/release"
"${CMAKE_CURRENT_BINARY_DIR}/beats-rust"
NO_DEFAULT_PATH
)

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

# Full path to the library
set(BEATS_LIB_FULL_PATH ${CMAKE_CURRENT_BINARY_DIR}/beats-rust/target/release/${BEATS_LIB_EXT})

# Try to find the library
find_library(BEATS_LIB ${BEATS_LIB_EXT}
PATHS
"${CMAKE_CURRENT_BINARY_DIR}/beats-rust/target/release"
"${CMAKE_CURRENT_BINARY_DIR}/beats-rust"
NO_DEFAULT_PATH
)

# If the library is not found, set up for build
if (NOT BEATS_LIB)
message("-- Unable to find the beats library. Setting up for build.")

add_custom_command(
OUTPUT ${BEATS_LIB_FULL_PATH}
COMMAND cargo build --release --target-dir=${CMAKE_CURRENT_BINARY_DIR}/beats-rust/target
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/beats-rust
COMMENT "Compiling Rust library"
)

add_custom_target(
BeatsTarget ALL
DEPENDS ${BEATS_LIB_FULL_PATH}
)

add_dependencies(${PROJECT_NAME} BeatsTarget)

set(BEATS_LIB ${BEATS_LIB_FULL_PATH})
else ()
message("lib beats found at ${BEATS_LIB}.")
endif ()
include(RustLib)

if (MSVC)
# Add executable icon for Windows
Expand All @@ -65,4 +18,4 @@ endif ()
setup_target(${PROJECT_NAME})
set_output_directory(${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib)
target_link_libraries(${PROJECT_NAME} PRIVATE ${BEATS_LIB})
target_link_libraries(${PROJECT_NAME} PRIVATE ${RUST_LIB_FULL_PATH})

0 comments on commit 375b716

Please sign in to comment.