Skip to content

Commit

Permalink
Build full-static LLVM tools binaries (#216)
Browse files Browse the repository at this point in the history
* 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.

* Disable gflags and unwind for glog.

* Added comments.

* Re-enable zlib.

* Updated comment.

* Make all llvm tools static executable binaries by adding "-static" to the linking command.
  • Loading branch information
shenhanc78 authored Jul 22, 2024
1 parent 4765163 commit ad21603
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -150,6 +151,12 @@ function (build_llvm)
endif()
endif()

# 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)

### LLVM Configuration
set (LLVM_INCLUDE_UTILS OFF)
set (LLVM_INCLUDE_TESTS OFF)
Expand All @@ -162,6 +169,9 @@ 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")
# terminfo is not needed by create_llvm_prof
set (LLVM_ENABLE_TERMINFO OFF CACHE BOOL "enable terminfo")
###

add_subdirectory(third_party/abseil)
Expand Down Expand Up @@ -362,6 +372,7 @@ function (build_llvm)
add_test(NAME symbol_map_test COMMAND symbol_map_test)

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)
Expand Down Expand Up @@ -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
Expand All @@ -801,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
Expand Down

0 comments on commit ad21603

Please sign in to comment.