From 3dc3db971c91ef5545961c594f17d9c9a2d0d1c3 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 12:06:57 -0700 Subject: [PATCH 1/6] Build static llvm tool binaries. Previously, the binaries built when ENABLE_TOOL=LLVM are all dynamically linked, there are many .so dependencies that prevent the tool binaries from be deployed to other systems. The change is also required when building release binaries. --- CMakeLists.txt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b3c339..ef4a291 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ set(CMAKE_CXX_STANDARD 17) set(ABSL_PROPAGATE_CXX_STD on) project(autofdo) +set (Protobuf_USE_STATIC_LIBS TRUE) function (execute_perf_protobuf) @@ -150,6 +151,11 @@ function (build_llvm) endif() endif() + # Build static binaries. + set (BUILD_SHARED_LIBS OFF) + set (WITH_GFLAGS OFF) + set (WITH_UNWIND OFF) + ### LLVM Configuration set (LLVM_INCLUDE_UTILS OFF) set (LLVM_INCLUDE_TESTS OFF) @@ -162,6 +168,10 @@ function (build_llvm) set (LLVM_TARGETS_TO_BUILD X86 AArch64 CACHE STRING "Semicolon-separated list of LLVM targets to build, or \"all\".") set (LLVM_ENABLE_ZSTD FORCE_ON) + set (LLVM_USE_STATIC_ZSTD TRUE CACHE BOOL "use static zstd" FORCE) + set (LLVM_ENABLE_ZLIB OFF CACHE BOOL "turn off zlib" FORCE) + # terminfo is not needed by create_llvm_prof + set (LLVM_ENABLE_TERMINFO OFF CACHE BOOL "disable terminfo" FORCE) ### add_subdirectory(third_party/abseil) @@ -361,8 +371,9 @@ function (build_llvm) LLVMSupport) add_test(NAME symbol_map_test COMMAND symbol_map_test) - find_library (LIBELF_LIBRARIES NAMES elf REQUIRED) - find_library (LIBCRYPTO_LIBRARIES NAMES crypto REQUIRED) + find_library (LIBELF_LIBRARIES NAMES libelf.a REQUIRED) + find_library (LIBZ_LIBRARIES NAMES libz.a REQUIRED) + find_library (LIBCRYPTO_LIBRARIES NAMES libcrypto.a REQUIRED) add_executable(llvm_profile_reader_test llvm_profile_reader_test.cc) target_link_libraries(llvm_profile_reader_test @@ -779,7 +790,7 @@ function (build_llvm) third_party/perf_data_converter/src/quipper ${PROJECT_BINARY_DIR}/third_party/perf_data_converter/src/quipper) target_link_libraries(quipper_perf - perf_proto ${LIBELF_LIBRARIES} ${LIBCRYPTO_LIBRARIES}) + perf_proto ${LIBELF_LIBRARIES} ${LIBZ_LIBRARIES} ${LIBCRYPTO_LIBRARIES}) add_custom_command(PRE_BUILD OUTPUT prepare_cmds From 162157641d359058f0bd4a48bf07e9a130365289 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 12:10:32 -0700 Subject: [PATCH 2/6] Disable gflags and unwind for glog. --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ef4a291..ea26c23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,8 @@ function (build_llvm) # Build static binaries. set (BUILD_SHARED_LIBS OFF) + + # Disable GFLAGS and UNWIND for glog. set (WITH_GFLAGS OFF) set (WITH_UNWIND OFF) From 16d7fbeb5f471f3947f37b57fc43647cd6ca23a8 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 14:01:54 -0700 Subject: [PATCH 3/6] Added comments. --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea26c23..0b5994e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,7 +154,9 @@ function (build_llvm) # Build static binaries. set (BUILD_SHARED_LIBS OFF) - # Disable GFLAGS and UNWIND for glog. + # Disable GFLAGS and UNWIND for glog, we do not control glog using + # gflags, nor does call stacks used in log information. And glog + # does not provide a way to force static link these two libraries. set (WITH_GFLAGS OFF) set (WITH_UNWIND OFF) @@ -170,10 +172,10 @@ function (build_llvm) set (LLVM_TARGETS_TO_BUILD X86 AArch64 CACHE STRING "Semicolon-separated list of LLVM targets to build, or \"all\".") set (LLVM_ENABLE_ZSTD FORCE_ON) - set (LLVM_USE_STATIC_ZSTD TRUE CACHE BOOL "use static zstd" FORCE) - set (LLVM_ENABLE_ZLIB OFF CACHE BOOL "turn off zlib" FORCE) + set (LLVM_USE_STATIC_ZSTD TRUE CACHE BOOL "use static zstd") + set (LLVM_ENABLE_ZLIB OFF CACHE BOOL "enable zlib") # terminfo is not needed by create_llvm_prof - set (LLVM_ENABLE_TERMINFO OFF CACHE BOOL "disable terminfo" FORCE) + set (LLVM_ENABLE_TERMINFO OFF CACHE BOOL "enable terminfo") ### add_subdirectory(third_party/abseil) From 95603a87b3d3f5b1f50de4b5fcf900427c69e146 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 14:16:10 -0700 Subject: [PATCH 4/6] Re-enable zlib. --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b5994e..a59c633 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,7 +173,6 @@ function (build_llvm) "Semicolon-separated list of LLVM targets to build, or \"all\".") set (LLVM_ENABLE_ZSTD FORCE_ON) set (LLVM_USE_STATIC_ZSTD TRUE CACHE BOOL "use static zstd") - set (LLVM_ENABLE_ZLIB OFF CACHE BOOL "enable zlib") # terminfo is not needed by create_llvm_prof set (LLVM_ENABLE_TERMINFO OFF CACHE BOOL "enable terminfo") ### From 120b7ccdb16b389f476dc4dc3110157f62dc9775 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 14:21:18 -0700 Subject: [PATCH 5/6] Updated comment. --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a59c633..0e3d4c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -374,6 +374,8 @@ function (build_llvm) LLVMSupport) add_test(NAME symbol_map_test COMMAND symbol_map_test) + # Using ".a" to force find static libraries. + # (https://cmake.org/cmake/help/latest/command/find_library.html) find_library (LIBELF_LIBRARIES NAMES libelf.a REQUIRED) find_library (LIBZ_LIBRARIES NAMES libz.a REQUIRED) find_library (LIBCRYPTO_LIBRARIES NAMES libcrypto.a REQUIRED) From ab72a3987b55b0ffbd4dc5581b5c82472e05fc84 Mon Sep 17 00:00:00 2001 From: Han Shen Date: Thu, 18 Jul 2024 16:25:21 -0700 Subject: [PATCH 6/6] Make all llvm tools static executable binaries by adding "-static" to the linking command. --- CMakeLists.txt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e3d4c2..dc10c28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,9 +151,6 @@ function (build_llvm) endif() endif() - # Build static binaries. - set (BUILD_SHARED_LIBS OFF) - # Disable GFLAGS and UNWIND for glog, we do not control glog using # gflags, nor does call stacks used in log information. And glog # does not provide a way to force static link these two libraries. @@ -374,11 +371,9 @@ function (build_llvm) LLVMSupport) add_test(NAME symbol_map_test COMMAND symbol_map_test) - # Using ".a" to force find static libraries. - # (https://cmake.org/cmake/help/latest/command/find_library.html) - find_library (LIBELF_LIBRARIES NAMES libelf.a REQUIRED) - find_library (LIBZ_LIBRARIES NAMES libz.a REQUIRED) - find_library (LIBCRYPTO_LIBRARIES NAMES libcrypto.a REQUIRED) + find_library (LIBELF_LIBRARIES NAMES elf REQUIRED) + find_library (LIBZ_LIBRARIES NAMES z REQUIRED) + find_library (LIBCRYPTO_LIBRARIES NAMES crypto REQUIRED) add_executable(llvm_profile_reader_test llvm_profile_reader_test.cc) target_link_libraries(llvm_profile_reader_test @@ -817,6 +812,13 @@ if (${enable_tool} STREQUAL gcov) build_gcov() elseif (${enable_tool} STREQUAL llvm) message(STATUS "Building tool \"LLVM\" ...") + + # Build static binaries. + set (BUILD_SHARED_LIBS OFF) + set (CMAKE_FIND_LIBRARY_SUFFIXES ".a") + # Link static executables. + set (CMAKE_EXE_LINKER_FLAGS "-static") + build_llvm() else () message(FATAL_ERROR