Skip to content

Commit 9606062

Browse files
authored
chore: Switch cloud-collector and k8s-collector to Rust mains (#429)
Generalize cmake infra for building Rust binaries. This also causes compiler include paths to shift, which produces header conflicts. Include absolute paths in generated code. (cherry picked from commit 9b22fd7)
1 parent 876ab02 commit 9606062

24 files changed

Lines changed: 393 additions & 135 deletions

File tree

Cargo.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
members = [
33
"crates/kernel-collector-sys",
44
"crates/kernel-collector-bin",
5+
"crates/cloud-collector-sys",
6+
"crates/cloud-collector-bin",
7+
"crates/k8s-relay-sys",
8+
"crates/k8s-relay-bin",
59
"crates/perfect_hash_map",
610
"crates/render_parser",
711
]
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ endif()
1212
if(NOT DEFINED RUST_BIN_TARGET_DIR)
1313
message(FATAL_ERROR "RUST_BIN_TARGET_DIR not provided to cargo_build_rust.cmake")
1414
endif()
15+
if(NOT DEFINED RUST_PACKAGE)
16+
message(FATAL_ERROR "RUST_PACKAGE not provided to cargo_build_rust.cmake")
17+
endif()
1518

16-
# Read the CMake-generated link command for the kernel-collector C++ target
19+
# Read the CMake-generated link command for the dummy/existing C++ target
1720
file(READ "${LINK_FILE}" LINK_CONTENT)
1821
get_filename_component(LINK_DIR "${LINK_FILE}" DIRECTORY)
22+
# Derive the target binary directory (two levels up from CMakeFiles/<target>.dir)
23+
get_filename_component(_cmakefiles_dir "${LINK_DIR}" DIRECTORY)
24+
get_filename_component(TARGET_BIN_DIR "${_cmakefiles_dir}" DIRECTORY)
1925

2026
# Seed library search paths with known build output dirs and system dirs
2127
set(SEARCH_DIRS
2228
"${BIN_DIR}/collector/kernel"
2329
"${BIN_DIR}/collector"
30+
"${TARGET_BIN_DIR}"
2431
"${BIN_DIR}/render"
2532
"${BIN_DIR}/channel"
2633
"${BIN_DIR}/config"
@@ -115,7 +122,7 @@ execute_process(
115122
OTN_LINK_SEARCH=${OTN_LINK_SEARCH}
116123
OTN_LINK_LIBS=${OTN_LINK_LIBS_ESC}
117124
OTN_LINK_ARGS=${OTN_LINK_ARGS_ESC}
118-
cargo build --release --package kernel-collector-bin --manifest-path ${PROJ_DIR}/Cargo.toml
125+
cargo build --release --package ${RUST_PACKAGE} --manifest-path ${PROJ_DIR}/Cargo.toml
119126
WORKING_DIRECTORY ${PROJ_DIR}
120127
RESULT_VARIABLE CARGO_RES
121128
OUTPUT_VARIABLE CARGO_OUT

cmake/golang.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ function(build_go_package NAME MODULE)
8282
get_target_property(MOD_BUILD_DIR "${MODULE}" MOD_BUILD_DIR)
8383
get_target_property(GO_MOD_FILE "${MODULE}" GO_MOD_FILE)
8484
set(BUILD_DIR "${MOD_BUILD_DIR}/${NAME}")
85-
set(OUT_BINARY "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")
85+
# Place Go binaries under a dedicated bin/ directory to avoid
86+
# colliding with CMake's phony target paths when the target name
87+
# matches the binary name (required by Ninja generator).
88+
set(OUT_BINARY "${CMAKE_CURRENT_BINARY_DIR}/bin/${NAME}")
8689

8790
set(TARGET_OPTIONS)
8891
if(ARG_ALL)
@@ -126,6 +129,9 @@ function(build_go_package NAME MODULE)
126129
TARGET
127130
"${TARGET}"
128131
POST_BUILD
132+
COMMAND
133+
${CMAKE_COMMAND} -E make_directory
134+
"${CMAKE_CURRENT_BINARY_DIR}/bin"
129135
WORKING_DIRECTORY
130136
"${BUILD_DIR}"
131137
BYPRODUCTS

cmake/rust_main.cmake

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
include_guard()
2+
3+
function(add_rust_main)
4+
cmake_parse_arguments(ARG "" "TARGET;STRIPPED_TARGET;PACKAGE;BIN_NAME;PROJ_DIR;RUST_BIN_TARGET_DIR;DUMMY_TARGET" "LINK_LIBS" ${ARGN})
5+
6+
if(NOT DEFINED ARG_TARGET)
7+
message(FATAL_ERROR "add_rust_main: TARGET is required")
8+
endif()
9+
if(NOT DEFINED ARG_STRIPPED_TARGET)
10+
message(FATAL_ERROR "add_rust_main: STRIPPED_TARGET is required")
11+
endif()
12+
if(NOT DEFINED ARG_PACKAGE)
13+
message(FATAL_ERROR "add_rust_main: PACKAGE is required (Cargo package)")
14+
endif()
15+
if(NOT DEFINED ARG_BIN_NAME)
16+
message(FATAL_ERROR "add_rust_main: BIN_NAME is required (expected executable name)")
17+
endif()
18+
if(NOT ARG_LINK_LIBS)
19+
message(FATAL_ERROR "add_rust_main: LINK_LIBS is required (C++ link libraries)")
20+
endif()
21+
22+
if(NOT DEFINED ARG_PROJ_DIR)
23+
set(ARG_PROJ_DIR "${PROJECT_SOURCE_DIR}")
24+
endif()
25+
if(NOT DEFINED ARG_RUST_BIN_TARGET_DIR)
26+
set(ARG_RUST_BIN_TARGET_DIR "${CMAKE_BINARY_DIR}/target")
27+
endif()
28+
if(NOT DEFINED ARG_DUMMY_TARGET)
29+
set(ARG_DUMMY_TARGET "${ARG_TARGET}-dummy")
30+
endif()
31+
32+
# 1) Create a tiny dummy main to force generation of a link.txt for the link line
33+
set(_dummy_main "${CMAKE_CURRENT_BINARY_DIR}/${ARG_DUMMY_TARGET}_main.cc")
34+
file(WRITE "${_dummy_main}" "int main(int, char**) { return 0; }\n")
35+
36+
add_executable(${ARG_DUMMY_TARGET} "${_dummy_main}")
37+
target_link_libraries(
38+
${ARG_DUMMY_TARGET}
39+
PUBLIC
40+
${ARG_LINK_LIBS}
41+
static-executable
42+
)
43+
44+
# Path to dummy's link.txt produced by the generator
45+
set(_link_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${ARG_DUMMY_TARGET}.dir/link.txt")
46+
47+
# 2) Build the Rust binary via cargo using link hints from the dummy
48+
set(_rust_bin_path "${ARG_RUST_BIN_TARGET_DIR}/release/${ARG_BIN_NAME}")
49+
50+
add_custom_command(
51+
OUTPUT "${_rust_bin_path}"
52+
COMMAND ${CMAKE_COMMAND}
53+
-DLINK_FILE=${_link_file}
54+
-DBIN_DIR=${CMAKE_BINARY_DIR}
55+
-DPROJ_DIR=${ARG_PROJ_DIR}
56+
-DRUST_BIN_TARGET_DIR=${ARG_RUST_BIN_TARGET_DIR}
57+
-DRUST_PACKAGE=${ARG_PACKAGE}
58+
-P ${CMAKE_SOURCE_DIR}/cmake/cargo_build_rust.cmake
59+
WORKING_DIRECTORY ${ARG_PROJ_DIR}
60+
DEPENDS ${ARG_LINK_LIBS}
61+
VERBATIM
62+
)
63+
64+
add_custom_target(
65+
${ARG_TARGET}
66+
DEPENDS "${_rust_bin_path}"
67+
)
68+
69+
# 3) Stripped variant
70+
add_custom_command(
71+
OUTPUT ${_rust_bin_path}-stripped
72+
COMMAND ${CMAKE_SOURCE_DIR}/dev/strip-symbols.sh ${_rust_bin_path}
73+
DEPENDS ${_rust_bin_path}
74+
VERBATIM
75+
)
76+
add_custom_target(
77+
${ARG_STRIPPED_TARGET}
78+
DEPENDS "${_rust_bin_path}-stripped"
79+
)
80+
set_property(
81+
TARGET ${ARG_STRIPPED_TARGET}
82+
PROPERTY
83+
"OUTPUT" "${_rust_bin_path}-stripped"
84+
)
85+
86+
endfunction()

collector/cloud/CMakeLists.txt

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
add_executable(
5-
cloud-collector
6-
main.cc
4+
add_library(
5+
cloud_agentlib
6+
STATIC
7+
entrypoint.cc
78
collector.cc
89
enumerator.cc
910
ingest_connection.cc
1011
)
11-
harden_executable(cloud-collector)
12-
13-
add_dependencies(collectors cloud-collector)
14-
15-
target_compile_options(
16-
cloud-collector
12+
target_include_directories(
13+
cloud_agentlib
1714
PRIVATE
18-
${CXX_ERROR_LIMIT_FLAG}=1
15+
${PROJECT_SOURCE_DIR}
16+
${CMAKE_CURRENT_BINARY_DIR}
1917
)
20-
2118
target_link_libraries(
22-
cloud-collector
19+
cloud_agentlib
2320
PUBLIC
21+
signal_handler
22+
)
23+
24+
set(CLOUD_COLLECTOR_LINK_LIBRARIES
25+
cloud_agentlib
2426
render_ebpf_net_cloud_collector
2527
render_ebpf_net_ingest_writer
26-
render_rust_ebpf_net
27-
signal_handler
2828
aws-sdk-cpp
2929
reconnecting_channel
3030
connection_caretaker
@@ -38,40 +38,33 @@ target_link_libraries(
3838
aws_instance_metadata
3939
spdlog
4040
static-executable
41-
"-Wl,--no-as-needed"
4241
protobuf::libprotobuf
4342
ZLIB::ZLIB
4443
versions
45-
"-Wl,--as-needed"
46-
)
47-
48-
set_target_properties(
49-
cloud-collector
50-
PROPERTIES LINK_FLAGS "-pthread"
5144
)
5245

53-
strip_binary(cloud-collector)
54-
55-
install(
56-
TARGETS
57-
cloud-collector
58-
RUNTIME
59-
DESTINATION ${CMAKE_INSTALL_BINOIR}
60-
COMPONENT cloud-collector
61-
)
6246

6347
lint_shell_script_bundle(
6448
cloud-collector-scripts
6549
SOURCES
6650
entrypoint.sh
6751
)
6852

53+
include(rust_main)
54+
add_rust_main(
55+
TARGET cloud-collector
56+
STRIPPED_TARGET cloud-collector-stripped
57+
PACKAGE cloud-collector-bin
58+
BIN_NAME cloud-collector
59+
LINK_LIBS ${CLOUD_COLLECTOR_LINK_LIBRARIES}
60+
)
61+
62+
add_dependencies(collectors cloud-collector)
63+
6964
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
7065
build_custom_docker_image(
7166
cloud-collector
7267
OUT_DIR srv
73-
ARTIFACTS_OF
74-
cloud-collector
7568
OUTPUT_OF
7669
cloud-collector-scripts
7770
cloud-collector-stripped
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
3737
*/
3838

39-
int main(int argc, char *argv[])
39+
static int cloud_collector_run(int argc, char **argv)
4040
{
4141
::uv_loop_t loop;
4242
if (auto const error = ::uv_loop_init(&loop)) {
@@ -120,3 +120,9 @@ int main(int argc, char *argv[])
120120

121121
return EXIT_SUCCESS;
122122
}
123+
124+
extern "C" int otn_cloud_collector_main(int argc, const char **argv)
125+
{
126+
return cloud_collector_run(argc, const_cast<char **>(argv));
127+
}
128+

0 commit comments

Comments
 (0)