From 4bc456d41e347f18cf969a946820e5840c58ee19 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 10 Sep 2024 09:17:20 +0200 Subject: [PATCH] fix(cmake): add zig workarounds for libelf and grpc cmake modules. Signed-off-by: Federico Di Pierro --- cmake/modules/grpc.cmake | 14 ++++++++++++++ cmake/modules/libelf.cmake | 24 ++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cmake/modules/grpc.cmake b/cmake/modules/grpc.cmake index 7eb395814b..6e7616b4ea 100644 --- a/cmake/modules/grpc.cmake +++ b/cmake/modules/grpc.cmake @@ -174,6 +174,19 @@ else() "${GRPC_SRC}/third_party/abseil-cpp/absl/random/libabsl_random_seed_gen_exception.a" ) + set(GRPC_PATCH_CMD "") + # Zig workaround: Add a PATCH_COMMAND to grpc cmake to fixup emitted -march by abseil-cpp + # cmake module, making it use a name understood by zig for arm64. See + # https://github.com/abseil/abseil-cpp/blob/master/absl/copts/GENERATED_AbseilCopts.cmake#L226. + if(CMAKE_C_COMPILER MATCHES "zig") + message(STATUS "Enabling zig workaround for abseil-cpp") + set(GRPC_PATCH_CMD + sh + -c + "sed -i 's/armv8-a/cortex_a57/g' ${GRPC_SRC}/third_party/abseil-cpp/absl/copts/GENERATED_AbseilCopts.cmake" + ) + endif() + ExternalProject_Add( grpc PREFIX "${PROJECT_BINARY_DIR}/grpc-prefix" @@ -223,6 +236,7 @@ else() # Keep installation files into the local ${GRPC_INSTALL_DIR} since here is the case when # we are embedding gRPC UPDATE_COMMAND "" + PATCH_COMMAND ${GRPC_PATCH_CMD} INSTALL_COMMAND DESTDIR= ${CMAKE_MAKE_PROGRAM} install ) install( diff --git a/cmake/modules/libelf.cmake b/cmake/modules/libelf.cmake index 809acf7e3f..c37b0c74e1 100644 --- a/cmake/modules/libelf.cmake +++ b/cmake/modules/libelf.cmake @@ -25,15 +25,31 @@ elseif(NOT USE_BUNDLED_LIBELF) else() set(LIBELF_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) endif() - find_library(LIBELF_LIB NAMES libelf${LIBELF_LIB_SUFFIX}) + # Zig workaround: since it won't look up in /usr/lib/..., add an HINT + if(CMAKE_C_COMPILER MATCHES "zig") + find_library( + LIBELF_LIB + NAMES libelf${LIBELF_LIB_SUFFIX} + HINTS /usr/lib/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/ + ) + else() + find_library(LIBELF_LIB NAMES libelf${LIBELF_LIB_SUFFIX}) + endif() if(LIBELF_LIB) + # Zig workaround: avoid include whole /usr/include because it would include also system + # glibc headers breaking the build since we are targeting the build against our boostrapped + # zig. + if(CMAKE_C_COMPILER MATCHES "zig") + message(STATUS "Enabling zig workaround for libelf") + configure_file(${LIBELF_INCLUDE}/libelf.h libelf/libelf.h COPYONLY) + configure_file(${LIBELF_INCLUDE}/elf.h libelf/elf.h COPYONLY) + configure_file(${LIBELF_INCLUDE}/gelf.h libelf/gelf.h COPYONLY) + set(LIBELF_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/libelf) + endif() message(STATUS "Found LIBELF: include: ${LIBELF_INCLUDE}, lib: ${LIBELF_LIB}") else() message(FATAL_ERROR "Couldn't find system libelf") endif() - # We add a custom target, in this way we can always depend on `libelf` without distinguishing - # between "bundled" and "not-bundled" case - add_custom_target(libelf) else() if(BUILD_SHARED_LIBS) set(LIBELF_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})