Skip to content

Commit 4c37203

Browse files
authored
Create CMake snippet for finding protobuf (#9646)
Redo #9633 to (1) refactor messy code to find protobuf in config or module node, (2) work with Yocto builds. This simplifies xbtracer CMakeLists.txt removing the logic behind finding protobuf. Signed-off-by: Soren Soe <2106410+stsoe@users.noreply.github.com>
1 parent 564bb81 commit 4c37203

2 files changed

Lines changed: 164 additions & 76 deletions

File tree

src/CMake/protobuf.cmake

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
3+
#
4+
# Generic Protobuf setup: find Protobuf (CONFIG or MODULE), resolve
5+
# protoc, and provide a function to generate C++ from .proto files.
6+
7+
# Set CMP0144 early for consistent ROOT variable behavior
8+
if(POLICY CMP0144)
9+
cmake_policy(SET CMP0144 NEW)
10+
endif()
11+
12+
# Now Protobuf_ROOT works reliably across CMake versions
13+
set(Protobuf_ROOT "" CACHE PATH "Path to Protobuf installation")
14+
15+
# Prefer config mode for Protobuf, fallback to module mode.
16+
# Do not use REQUIRED so callers can skip (e.g. xbtracer) when Protobuf is not
17+
# available (common on Windows if Protobuf is not installed or not in PATH).
18+
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
19+
find_package(Protobuf QUIET)
20+
unset(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
21+
22+
if(NOT Protobuf_FOUND)
23+
return()
24+
endif()
25+
26+
# Check if we're in config mode and enable module-compatible functions if needed
27+
if(TARGET protobuf::libprotobuf)
28+
set(PROTOBUF_CONFIG_MODE TRUE)
29+
set(protobuf_MODULE_COMPATIBLE TRUE)
30+
message(STATUS "Protobuf found in CONFIG mode")
31+
32+
# CRITICAL: Ensure protobuf::protoc exists for Yocto cross-compilation
33+
if(NOT TARGET protobuf::protoc)
34+
if(NOT Protobuf_PROTOC_EXECUTABLE)
35+
find_program(Protobuf_PROTOC_EXECUTABLE protoc
36+
PATHS ${CMAKE_FIND_ROOT_PATH}/usr/bin
37+
NO_DEFAULT_PATH
38+
)
39+
endif()
40+
if(Protobuf_PROTOC_EXECUTABLE)
41+
add_executable(protobuf::protoc IMPORTED)
42+
set_target_properties(protobuf::protoc PROPERTIES
43+
IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}"
44+
)
45+
message(STATUS "Created protobuf::protoc target: ${Protobuf_PROTOC_EXECUTABLE}")
46+
endif()
47+
endif()
48+
elseif(Protobuf_LIBRARIES)
49+
set(PROTOBUF_CONFIG_MODE FALSE)
50+
add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
51+
set_target_properties(protobuf::libprotobuf PROPERTIES
52+
IMPORTED_LOCATION "${Protobuf_LIBRARIES}"
53+
INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIRS}"
54+
INTERFACE_COMPILE_DEFINITIONS "${Protobuf_DEFINITIONS}"
55+
)
56+
if(Protobuf_PROTOC_EXECUTABLE)
57+
add_executable(protobuf::protoc IMPORTED)
58+
set_target_properties(protobuf::protoc PROPERTIES
59+
IMPORTED_LOCATION "${Protobuf_PROTOC_EXECUTABLE}"
60+
)
61+
endif()
62+
message(STATUS "Protobuf found in MODULE mode - created compatibility targets")
63+
else()
64+
set(Protobuf_FOUND FALSE)
65+
return()
66+
endif()
67+
68+
# Set global compatibility flag for config mode (enables legacy functions)
69+
set(protobuf_MODULE_COMPATIBLE TRUE CACHE BOOL "Enable module-compatible functions in config mode" FORCE)
70+
71+
# Function to generate C++ headers and sources from .proto files
72+
# Usage: protobuf_generate_cpp(<target> <proto_files>...)
73+
# Adds generated sources/headers to <target> and depends on them
74+
function(protobuf_generate_cpp target)
75+
if(NOT Protobuf_FOUND)
76+
message(FATAL_ERROR "Protobuf not found")
77+
endif()
78+
79+
set(proto_files ${ARGN})
80+
if(NOT proto_files)
81+
message(FATAL_ERROR "No proto files provided to protobuf_generate_cpp")
82+
endif()
83+
84+
if(PROTOBUF_CONFIG_MODE)
85+
# Use modern protobuf_generate with TARGET
86+
message("-- Use modern protobuf_generate with TARGET")
87+
protobuf_generate(
88+
TARGET ${target}
89+
PROTOS ${proto_files}
90+
LANGUAGE cpp
91+
)
92+
else()
93+
# Module mode: use legacy protobuf_generate_cpp
94+
message("-- Module mode: use legacy protobuf_generate_cpp")
95+
set(proto_sources)
96+
set(proto_headers)
97+
protobuf_generate_cpp(
98+
proto_sources
99+
proto_headers
100+
${proto_files}
101+
)
102+
target_sources(${target} PRIVATE ${proto_sources} ${proto_headers})
103+
endif()
104+
endfunction()

src/runtime_src/core/tools/xbtracer/CMakeLists.txt

Lines changed: 60 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,106 @@
22
# Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
33

44
cmake_minimum_required(VERSION 3.10...4.0)
5-
if (POLICY CMP0144)
6-
message("--using CMP0144 for Protobuf checking")
7-
cmake_policy(SET CMP0144 NEW)
8-
else (POLICY CMP0144)
9-
message("--CMP0144 is not supported, skipping xbtracer")
10-
return()
11-
endif ()
125

13-
find_package(Protobuf)
6+
include(${XRT_SOURCE_DIR}/CMake/protobuf.cmake)
147
if (NOT Protobuf_FOUND)
15-
message("Protobuf is not found, skipping xbtracer")
8+
message("Protobuf was not found, skip xbtracer.")
169
return()
17-
endif (NOT Protobuf_FOUND)
18-
19-
# Protobuf 22+ uses abseil for logging, need to link against it
20-
# Note: Protobuf versioning changed - version 22.0 may report as 4.22.0 or 6.22.0
21-
# We check for abseil linking if protobuf version >= 4.0 (corresponds to protobuf 22+)
22-
find_package(absl QUIET)
23-
if (absl_FOUND AND Protobuf_VERSION VERSION_GREATER_EQUAL 4.0)
24-
set(XBTRACER_ABSL_LIBS absl::log_internal_check_op absl::log_internal_message)
25-
message(STATUS "Found abseil, will link xbtracer against abseil logging libraries")
26-
endif()
27-
message("Protobuf version is ${Protobuf_VERSION}.")
10+
endif ()
2811
if (Protobuf_VERSION VERSION_LESS 3.0)
29-
# we use timestamp feature of protobuf
30-
message("Protobuf version ${Protobuf_VERSION} is less than 3.0, skip xbtracer.")
12+
message("Protobuf ${Protobuf_VERSION} < 3.0, skip xbtracer.")
3113
return()
32-
endif (Protobuf_VERSION VERSION_LESS 3.0)
33-
34-
# Imported targets should link the .lib (not the .dll) when compiling with MSVC
35-
set(XBTRACER_PROTOBUF_LINK "")
36-
if (TARGET protobuf::libprotobuf)
37-
set(XBTRACER_PROTOBUF_LINK protobuf::libprotobuf)
38-
elseif (TARGET Protobuf::libprotobuf)
39-
set(XBTRACER_PROTOBUF_LINK Protobuf::libprotobuf)
40-
else()
41-
set(XBTRACER_PROTOBUF_LINK ${Protobuf_LIBRARIES})
42-
endif()
43-
44-
include_directories (
45-
${Protobuf_INCLUDE_DIR}
46-
${CMAKE_CURRENT_BINARY_DIR}
47-
)
48-
# Generate Cpp files from Proto file
49-
file(GLOB PROTO_SRC_FILES
50-
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.proto"
51-
)
52-
53-
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${PROTO_SRC_FILES})
54-
55-
add_custom_target(xbtracer_generated_code DEPENDS ${ProtoSources} ${ProtoHeaders})
14+
endif ()
5615

57-
add_library(xbtracer_protobuf STATIC ${ProtoSources} ${ProtoHeaders})
58-
add_dependencies(xbtracer_protobuf xbtracer_generated_code)
16+
################################################################
17+
# Generate C++ from .proto and create xbtracer proto library
18+
################################################################
19+
set(XBTRACER_PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/func.proto)
20+
add_library(xbtracer_protobuf STATIC)
21+
protobuf_generate_cpp(xbtracer_protobuf ${XBTRACER_PROTO_FILES})
22+
target_include_directories(xbtracer_protobuf PRIVATE
23+
${Protobuf_INCLUDE_DIR}
24+
${CMAKE_CURRENT_BINARY_DIR}
25+
${CMAKE_CURRENT_BINARY_DIR}/src
26+
)
5927
if (MSVC)
60-
target_compile_options(xbtracer_protobuf PRIVATE /wd4244 /wd4267 /wd4100)
61-
endif(MSVC)
62-
# Link protobuf static library against abseil if needed
63-
if (XBTRACER_ABSL_LIBS)
64-
target_link_libraries(xbtracer_protobuf PUBLIC ${XBTRACER_ABSL_LIBS})
65-
endif()
66-
67-
include_directories(
68-
"${CMAKE_CURRENT_SOURCE_DIR}/src"
69-
${XRT_BINARY_DIR}/gen
70-
)
71-
72-
file(GLOB XBTRACER_COMMON_SRC_FILES
73-
"${CMAKE_CURRENT_SOURCE_DIR}/src/common/*.cpp"
74-
)
28+
target_compile_options(xbtracer_protobuf PRIVATE /wd4244 /wd4267 /wd4100 /wd4141 /wd4189)
29+
endif ()
30+
target_link_libraries(xbtracer_protobuf PUBLIC protobuf::libprotobuf)
7531

32+
################################################################
33+
# Create xbtracer common library
34+
################################################################
35+
file(GLOB XBTRACER_COMMON_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/common/*.cpp)
7636
add_library(xbtracer_common STATIC ${XBTRACER_COMMON_SRC_FILES})
37+
target_include_directories(xbtracer_common PRIVATE
38+
${CMAKE_CURRENT_SOURCE_DIR}/src
39+
)
7740
if (NOT WIN32)
7841
target_link_libraries(xbtracer_common PRIVATE dl)
7942
endif (NOT WIN32)
8043

81-
file(GLOB XBTRACER_WRAPPER_SRC_FILES
82-
"${CMAKE_CURRENT_SOURCE_DIR}/src/wrapper/*.cpp"
83-
)
84-
85-
add_library(xrt_trace SHARED ${XBTRACER_WRAPPER_SRC_FILES} ${ProtoHeaders})
44+
################################################################
45+
# Create xbtracer wrapper library
46+
################################################################
47+
file(GLOB XBTRACER_WRAPPER_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/wrapper/*.cpp)
48+
add_library(xrt_trace SHARED ${XBTRACER_WRAPPER_SRC_FILES})
8649
set_target_properties(xrt_trace PROPERTIES VERSION ${XRT_VERSION_STRING} SOVERSION ${XRT_SOVERSION})
8750
target_compile_definitions(xrt_trace PRIVATE XRT_ABI_VERSION=${XRT_VERSION_MAJOR})
88-
89-
target_link_libraries(xrt_trace PRIVATE xbtracer_common xbtracer_protobuf ${XBTRACER_PROTOBUF_LINK} xrt_coreutil)
51+
target_include_directories(xrt_trace PRIVATE
52+
${CMAKE_CURRENT_SOURCE_DIR}/src
53+
${CMAKE_CURRENT_BINARY_DIR}
54+
${CMAKE_CURRENT_BINARY_DIR}/src
55+
)
56+
target_link_libraries(xrt_trace PRIVATE xbtracer_common xbtracer_protobuf protobuf::libprotobuf xrt_coreutil)
9057
add_dependencies(xrt_trace xbtracer_common xbtracer_protobuf xrt_coreutil)
9158

59+
################################################################
60+
# Create xbtracer tracer executable
61+
################################################################
9262
file(GLOB XBTRACER_CAPTURE_SRC_FILES
9363
"${CMAKE_CURRENT_SOURCE_DIR}/src/capture/*.cpp"
9464
)
9565

9666
add_executable(xrt-tracer ${XBTRACER_CAPTURE_SRC_FILES})
9767
target_link_libraries(xrt-tracer PRIVATE xbtracer_common)
68+
target_include_directories(xrt-tracer PRIVATE
69+
${CMAKE_CURRENT_SOURCE_DIR}/src
70+
)
9871
add_dependencies(xrt-tracer xbtracer_common)
9972

73+
################################################################
74+
# Create xbtracer replay executable
75+
################################################################
10076
file(GLOB XBREPLAY_SRC_FILES
10177
"${CMAKE_CURRENT_SOURCE_DIR}/src/replay/*.cpp"
10278
)
10379
add_executable(xrt-replay ${XBREPLAY_SRC_FILES})
104-
target_link_libraries(xrt-replay PRIVATE xbtracer_common xbtracer_protobuf ${XBTRACER_PROTOBUF_LINK} xrt_coreutil)
80+
target_include_directories(xrt-replay PRIVATE
81+
${CMAKE_CURRENT_SOURCE_DIR}/src
82+
${CMAKE_CURRENT_BINARY_DIR}
83+
${CMAKE_CURRENT_BINARY_DIR}/src
84+
)
85+
target_link_libraries(xrt-replay PRIVATE xbtracer_common xbtracer_protobuf protobuf::libprotobuf xrt_coreutil)
10586
if (NOT WIN32)
10687
target_link_libraries(xrt-replay PRIVATE pthread)
10788
endif (NOT WIN32)
10889
add_dependencies(xrt-replay xbtracer_common xbtracer_protobuf xrt_coreutil)
109-
# TODO: when buiding with yocto for APU in CI, the status return from message to jason convertion function
110-
# provided from protobuf built from yocto doesn't match the one in the absl library, which results in
111-
# build failure. After fixing this issue in yocto APU build, we can always print message as JSON.
112-
# for now, we by default disable it, as it is not the key feature in the tracer/replay prototype, and replay
90+
91+
# TODO: when buiding with yocto for APU in CI, the status return from
92+
# message to jason convertion function provided from protobuf built
93+
# from yocto doesn't match the one in the absl library, which results
94+
# in build failure. After fixing this issue in yocto APU build, we can
95+
# always print message as JSON. for now, we by default disable it, as
96+
# it is not the key feature in the tracer/replay prototype, and replay
11397
# only tries to dump JSON message when it fails to replay a function.
11498
if (XRT_XBTRACER_ENABLE_JSON)
11599
target_compile_options(xrt-replay PRIVATE XBTRACER_PROTOBUF_HAS_JASON)
116100

117101
add_executable(xbtracer_dump
118102
src/misc/xbtracer_dump.cpp
119103
)
120-
target_link_libraries(xbtracer_dump PRIVATE xbtracer_common xbtracer_protobuf ${XBTRACER_PROTOBUF_LINK})
104+
target_link_libraries(xbtracer_dump PRIVATE xbtracer_common xbtracer_protobuf protobuf::libprotobuf)
121105
add_dependencies(xbtracer_dump xbtracer_common xbtracer_protobuf xrt_coreutil)
122106
endif (XRT_XBTRACER_ENABLE_JSON)
123107

0 commit comments

Comments
 (0)