Skip to content

Commit

Permalink
Fix up handling of libunwind in CMake. (netdata#19451)
Browse files Browse the repository at this point in the history
* Fix up handling of libunwind in CMake.

- Fix the questionable default handling of CMAKE_SYSTEM_PROCESSOR so
  that it reliably reflects the target architecture.
- Add a case for handling 32-bit x86 builds with libunwind.
- Tweak the match cases for the various architectures to be more
  reliable.

* Fix libbpf usage of CMAKE_SYSTEM_PROCESSOR.
  • Loading branch information
Ferroin authored Jan 22, 2025
1 parent 2133107 commit 8fca437
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ project(netdata
HOMEPAGE_URL "https://www.netdata.cloud"
LANGUAGES C CXX)
include(CMakeDependentOption)
include(NetdataUtil)
netdata_fixup_system_processor()

if(DEFINED BUILD_SHARED_LIBS)
if(NOT BUILD_SHARED_LIBS)
Expand Down Expand Up @@ -2167,11 +2169,13 @@ if(ENABLE_LIBUNWIND)

if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(amd64)")
target_link_libraries(libnetdata PUBLIC PkgConfig::LIBUNWIND -lunwind-x86_64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i?.86")
target_link_libraries(libnetdata PUBLIC PkgCOnfig::LIBUNWIND -lunwind-x86)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm64)|(aarch64)")
target_link_libraries(libnetdata PUBLIC PkgConfig::LIBUNWIND -lunwind-aarch64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
target_link_libraries(libnetdata PUBLIC PkgConfig::LIBUNWIND -lunwind-arm)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "powerpc|ppc")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "p(ower)?pc64")
target_link_libraries(libnetdata PUBLIC PkgConfig::LIBUNWIND -lunwind-ppc64)
else()
message(WARNING "Unknown architecture ${CMAKE_SYSTEM_PROCESSOR} for libunwind. Stack traces may not work.")
Expand Down
2 changes: 1 addition & 1 deletion packaging/cmake/Modules/NetdataLibBPF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function(netdata_bundle_libbpf)

set(_libbpf_lib_dir lib)

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(amd64)")
set(_libbpf_lib_dir lib64)
endif()

Expand Down
24 changes: 24 additions & 0 deletions packaging/cmake/Modules/NetdataUtil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@

include_guard()

# Fix up CMAKE_SYSTEM_PROCESSOR to actually match the build target
function(netdata_fixup_system_processor)
if(OS_WINDOWS)
return()
endif()

if(CMAKE_TOOLCHAIN_FILE)
return()
endif()

execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} -dumpmachine
COMMAND cut -f 1 -d -
RESULT_VARIABLE return_code
OUTPUT_VARIABLE output_data
)

if(return_code EQUAL 0)
set(CMAKE_SYSTEM_PROCESSOR "${output_data}" PARENT_SCOPE)
else()
message(WARNING "Failed to detect target processor architecture, using CMake default")
endif()
endfunction()

# Determine the version of the host kernel.
#
# Only works on UNIX-like systems, stores the version in the cache
Expand Down

0 comments on commit 8fca437

Please sign in to comment.