Skip to content

Commit

Permalink
feat(cmake): cmake install tiflash (pingcap#4092)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu authored Feb 23, 2022
1 parent a6b7fd6 commit 0d541dd
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 91 deletions.
28 changes: 21 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
project (TiFlash)
cmake_minimum_required (VERSION 2.8)

set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${TiFlash_SOURCE_DIR}/cmake/Modules/")
set(CMAKE_MACOSX_RPATH 1)

option(TIFLASH_ENABLE_RUNTIME_RPATH "Add $ORIGIN/ to tiflash installation rpath" OFF)
option(TIFLASH_ENABLE_LLVM_DEVELOPMENT "enable facilities for development with LLVM" OFF)

if(TIFLASH_ENABLE_LLVM_DEVELOPMENT)
# enable RPATHs to sysroot to make it easier for LLVM development
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(ENV{LD_LIBRARY_PATH} "${CMAKE_PREFIX_PATH}/lib:${CMAKE_PREFIX_PATH}/lib/x86_64-unknown-linux-gnu/:${CMAKE_PREFIX_PATH}/lib/aarch64-unknown-linux-gnu/")
set(CMAKE_INSTALL_RPATH "$ORIGIN/;${CMAKE_PREFIX_PATH}/lib;${CMAKE_PREFIX_PATH}/lib/x86_64-unknown-linux-gnu/;${CMAKE_PREFIX_PATH}/lib/aarch64-unknown-linux-gnu/")
if(CMAKE_PREFIX_PATH)
# append paths for cmake to check libs
set(ENV{LD_LIBRARY_PATH}
"${CMAKE_PREFIX_PATH}/lib:${CMAKE_PREFIX_PATH}/lib/x86_64-unknown-linux-gnu/:${CMAKE_PREFIX_PATH}/lib/aarch64-unknown-linux-gnu/")
endif()

message (STATUS "Using CXX=${CMAKE_CXX_COMPILER}, ver=${CMAKE_CXX_COMPILER_VERSION};CC=${CMAKE_C_COMPILER}, ver=${CMAKE_C_COMPILER_VERSION}")
Expand Down Expand Up @@ -142,6 +140,22 @@ set (COMPILER_FLAGS "${COMPILER_FLAGS} ${CXX11_ABI_FLAGS}")

string(REGEX MATCH "-?[0-9]+(.[0-9]+)?$" COMPILER_POSTFIX ${CMAKE_CXX_COMPILER})

find_program (LLVM_OBJCOPY_PATH NAMES "llvm-objcopy${COMPILER_POSTFIX}" "llvm-objcopy")
find_program (LLVM_AR_PATH NAMES "llvm-ar${COMPILER_POSTFIX}" "llvm-ar")
find_program (LLVM_RANLIB_PATH NAMES "llvm-ranlib${COMPILER_POSTFIX}" "llvm-ranlib")

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND LLVM_OBJCOPY_PATH)
set(CMAKE_OBJCOPY ${LLVM_OBJCOPY_PATH})
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND LLVM_AR_PATH)
set(CMAKE_AR ${LLVM_AR_PATH})
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND LLVM_RANLIB_PATH)
set(CMAKE_RANLIB ${LLVM_RANLIB_PATH})
endif()

find_program (LLD_PATH NAMES "lld${COMPILER_POSTFIX}" "lld")
find_program (GOLD_PATH NAMES "gold")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Normally a CMake-based IDE, e.g., Clion and VSCode, should be able to open TiFla
If your toolchain is set up using [TiFlash Env](#tiflash-env), and you may not want to add those libs to your system loader config, you can pass the following CMake options to your IDE:

```
-DTIFLASH_ENABLE_LLVM_DEVELOPMENT=ON -DCMAKE_PREFIX_PATH=$TIFLASH_ENV
-DCMAKE_PREFIX_PATH=$TIFLASH_ENV
```

Remember that `$TIFLASH_ENV` is a placeholder mentioned in [TiFlash Env](#tiflash-env).
Expand Down
11 changes: 11 additions & 0 deletions cmake/tiflash_linux_post_install.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
find_program (OBJCOPY_PATH NAMES "objcopy")
find_program (LLVM_OBJCOPY_PATH NAMES "llvm-objcopy${COMPILER_POSTFIX}" "llvm-objcopy")

if (LLVM_OBJCOPY_PATH)
set(CMAKE_OBJCOPY ${LLVM_OBJCOPY_PATH})
else ()
set(CMAKE_OBJCOPY ${OBJCOPY_PATH})
endif ()

message(STATUS "executing: ${CMAKE_OBJCOPY} --compress-debug-sections=zlib-gnu ${CMAKE_INSTALL_PREFIX}/tiflash")
execute_process(COMMAND ${CMAKE_OBJCOPY} --compress-debug-sections=zlib-gnu ${CMAKE_INSTALL_PREFIX}/tiflash)
2 changes: 1 addition & 1 deletion contrib/tiflash-proxy
Submodule tiflash-proxy updated 1 files
+5 −0 build.sh
15 changes: 1 addition & 14 deletions contrib/tiflash-proxy-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ set(_TIFLASH_PROXY_LIBRARY "${_TIFLASH_PROXY_SOURCE_DIR}/target/release/${CMAKE_
file(GLOB_RECURSE _TIFLASH_PROXY_SRCS "${_TIFLASH_PROXY_SOURCE_DIR}/*.rs")
list(FILTER _TIFLASH_PROXY_SRCS EXCLUDE REGEX ${_TIFLASH_PROXY_SOURCE_DIR}/target/.*)

option(ENABLE_PROXY_RUNTIME_RELATIVE_PATH "Make proxy dynamic lib to be linked by relative path" OFF)

if(TIFLASH_LLVM_TOOLCHAIN AND USE_LIBCXX)
set(TIFLASH_RUST_LINKER ${CMAKE_CURRENT_BINARY_DIR}/tiflash-linker)
set(TIFLASH_RUST_LINKER_TMP ${CMAKE_CURRENT_BINARY_DIR}/tmp/tiflash-linker)
Expand All @@ -13,16 +11,13 @@ if(TIFLASH_LLVM_TOOLCHAIN AND USE_LIBCXX)
file(COPY ${TIFLASH_RUST_LINKER_TMP}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE)
set(TIFLASH_RUSTFLAGS "-C linker=${TIFLASH_RUST_LINKER}")
set(TIFLASH_RUSTFLAGS "-C rpath=yes -C linker=${TIFLASH_RUST_LINKER}")
if(ARCH_AARCH64)
set(TIFLASH_RUSTFLAGS "-C link-arg=-Wl,-Bsymbolic ${TIFLASH_RUSTFLAGS}")
endif()
if(LINKER_NAME)
set(TIFLASH_RUSTFLAGS "-C link-arg=-fuse-ld=${LINKER_NAME} ${TIFLASH_RUSTFLAGS}")
endif()
if(TIFLASH_ENABLE_RUNTIME_RPATH)
set(TIFLASH_RUSTFLAGS "-C rpath=yes ${TIFLASH_RUSTFLAGS}")
endif()
set(TIFLASH_RUST_ENV CXXSTDLIB=c++ CMAKE=${CMAKE_COMMAND} CFLAGS=-w CXXFLAGS=-w RUSTFLAGS=${TIFLASH_RUSTFLAGS})
message(STATUS "enforce LLVM toolchain for rust: ${TIFLASH_RUST_ENV}")
endif()
Expand All @@ -35,14 +30,6 @@ add_custom_command(OUTPUT ${_TIFLASH_PROXY_LIBRARY}
WORKING_DIRECTORY ${_TIFLASH_PROXY_SOURCE_DIR}
DEPENDS "${_TIFLASH_PROXY_SRCS}" "${_TIFLASH_PROXY_SOURCE_DIR}/Cargo.lock" "${_TIFLASH_PROXY_SOURCE_DIR}/rust-toolchain")

if(APPLE AND ENABLE_PROXY_RUNTIME_RELATIVE_PATH)
# For MacOS, we need to explicitly adjust install name for tiflash_proxy library, as it is copied from raftstore_proxy library and the install name is kept as "raftstore_proxy".
add_custom_command(OUTPUT ${_TIFLASH_PROXY_LIBRARY}
APPEND
COMMENT "Changing install name for tiflash proxy library on MacOS"
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -id @executable_path/libtiflash_proxy.dylib ${_TIFLASH_PROXY_LIBRARY})
endif()

add_custom_target(tiflash_proxy ALL
DEPENDS ${_TIFLASH_PROXY_LIBRARY})

Expand Down
63 changes: 54 additions & 9 deletions dbms/src/Server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,59 @@ if (CLICKHOUSE_SPLIT_BINARY)
add_custom_target (tiflash ALL DEPENDS clickhouse-bundle)
else ()
add_executable (tiflash main.cpp)

if (USE_INTERNAL_TIFLASH_PROXY)
set(PROXY_LIBRARY_DIR "${TiFlash_SOURCE_DIR}/contrib/tiflash-proxy/target/release")
else()
set(PROXY_LIBRARY_DIR "${PREBUILT_LIBS_ROOT}")
endif()

if (ARCH_LINUX)
if (ARCH_AARCH64)
set(TIFLASH_ARCH_NAME "aarch64")
elseif(ARCH_AMD64)
set(TIFLASH_ARCH_NAME "x86_64")
endif()

if (CMAKE_PREFIX_PATH)
set(TIFLASH_DEPENDENCY_DIR_HINT
"${CMAKE_PREFIX_PATH}/lib;${CMAKE_PREFIX_PATH}/lib/${TIFLASH_ARCH_NAME}-unknown-linux-gnu/;")
set(TIFLASH_DEPENDENCY_DIRECTORIES
"${CMAKE_PREFIX_PATH}/lib"
"${CMAKE_PREFIX_PATH}/lib/${TIFLASH_ARCH_NAME}-unknown-linux-gnu/")
else()
set(TIFLASH_DEPENDENCY_DIR_HINT "")
set(TIFLASH_DEPENDENCY_DIRECTORIES "")
endif()

# set build rpaths, so that executables are be directly called in build tree (easy to debug)
set_target_properties(tiflash PROPERTIES BUILD_RPATH "${PROXY_LIBRARY_DIR};${TIFLASH_DEPENDENCY_DIR_HINT}")
set_target_properties(tiflash PROPERTIES INSTALL_RPATH "$ORIGIN/")
install (TARGETS tiflash
COMPONENT tiflash-release
DESTINATION "."
RUNTIME_DEPENDENCY_SET tiflash-release-dependency)
install (RUNTIME_DEPENDENCY_SET tiflash-release-dependency
COMPONENT tiflash-release
DESTINATION "."
POST_EXCLUDE_REGEXES "libdl.*" "libc-.*" "libc\\..*" "libgcc_s.*" "librt.*" "libm.*" "ld-.*" "libpthread.*"# exclude libc dependencies
DIRECTORIES ${TIFLASH_DEPENDENCY_DIRECTORIES})

install (SCRIPT ${TiFlash_SOURCE_DIR}/cmake/tiflash_linux_post_install.cmake COMPONENT tiflash-release)
elseif(APPLE)
# set build rpaths, so that executables are be directly called in build tree (easy to debug)
set_target_properties(tiflash PROPERTIES BUILD_RPATH "${PROXY_LIBRARY_DIR}")
set_target_properties(tiflash PROPERTIES INSTALL_RPATH "@executable_path/")
install (TARGETS tiflash
COMPONENT tiflash-release
DESTINATION "."
RUNTIME_DEPENDENCY_SET tiflash-release-dependency)
install (RUNTIME_DEPENDENCY_SET tiflash-release-dependency
COMPONENT tiflash-release
DESTINATION "."
PRE_INCLUDE_REGEXES ".*proxy.*")
endif ()

target_link_libraries (tiflash clickhouse_common_io)
target_include_directories (tiflash BEFORE PRIVATE ${COMMON_INCLUDE_DIR})
target_include_directories (tiflash PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Expand Down Expand Up @@ -192,16 +245,8 @@ else ()
add_custom_target (clickhouse-clang ALL COMMAND ${CMAKE_COMMAND} -E create_symlink tiflash clickhouse-clang DEPENDS tiflash)
add_custom_target (clickhouse-lld ALL COMMAND ${CMAKE_COMMAND} -E create_symlink tiflash clickhouse-lld DEPENDS tiflash)
list(APPEND CLICKHOUSE_BUNDLE clickhouse-clang clickhouse-lld)

if (TIFLASH_ENABLE_RUNTIME_RPATH)
set_target_properties(tiflash PROPERTIES
BUILD_RPATH_USE_ORIGIN TRUE
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH "$ORIGIN/"
)
endif()
install (TARGETS tiflash RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tiflash)

install (FILES
${CMAKE_CURRENT_BINARY_DIR}/clickhouse-clang
${CMAKE_CURRENT_BINARY_DIR}/clickhouse-lld
Expand Down
20 changes: 2 additions & 18 deletions release-centos7-llvm/scripts/build-tiflash-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,11 @@ cmake "${SRCPATH}" ${DEFINE_CMAKE_PREFIX_PATH} \
-DENABLE_TESTS=OFF \
-Wno-dev \
-DUSE_CCACHE=OFF \
-DLINKER_NAME=lld \
-DUSE_LIBCXX=ON \
-DUSE_LLVM_LIBUNWIND=OFF \
-DUSE_LLVM_COMPILER_RT=OFF \
-DTIFLASH_ENABLE_RUNTIME_RPATH=ON \
-DRUN_HAVE_STD_REGEX=0 \
-DCMAKE_AR="/usr/local/bin/llvm-ar" \
-DCMAKE_RANLIB="/usr/local/bin/llvm-ranlib" \
-GNinja

ninja tiflash

source ${SCRIPTPATH}/utils/vendor_dependency.sh

# compress debug symbols
llvm-objcopy --compress-debug-sections=zlib-gnu "${BUILD_DIR}/dbms/src/Server/tiflash" "${INSTALL_DIR}/tiflash"

vendor_dependency "${INSTALL_DIR}/tiflash" libc++.so "${INSTALL_DIR}/"
vendor_dependency "${INSTALL_DIR}/tiflash" libc++abi.so "${INSTALL_DIR}/"

cp -f "${SRCPATH}/contrib/tiflash-proxy/target/release/libtiflash_proxy.so" "${INSTALL_DIR}/libtiflash_proxy.so"
cmake --build . --target tiflash --parallel
cmake --install . --component=tiflash-release --prefix="${INSTALL_DIR}"

# unset LD_LIBRARY_PATH before test
unset LD_LIBRARY_PATH
Expand Down
7 changes: 0 additions & 7 deletions release-centos7-llvm/scripts/static-analysis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ if [[ "${ENABLE_CLANG_TIDY_CHECK}" == "true" ]]; then
-DCMAKE_BUILD_TYPE=RELWITHDEBINFO \
-DUSE_CCACHE=OFF \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DLINKER_NAME=lld \
-DUSE_LIBCXX=ON \
-DUSE_LLVM_LIBUNWIND=OFF \
-DUSE_LLVM_COMPILER_RT=OFF \
-DTIFLASH_ENABLE_RUNTIME_RPATH=ON \
-DRUN_HAVE_STD_REGEX=0 \
-DCMAKE_AR="/usr/local/bin/llvm-ar" \
-DCMAKE_RANLIB="/usr/local/bin/llvm-ranlib" \
-GNinja
python3 ${SRCPATH}/release-centos7-llvm/scripts/fix_compile_commands.py \
--file_path=${BUILD_DIR}/compile_commands.json \
Expand Down
20 changes: 2 additions & 18 deletions release-centos7-llvm/scripts/tiflash-ci-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,13 @@ cmake "$SRCPATH" ${CMAKE_PREBUILT_LIBS_ROOT_ARG} \
-DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \
-DUSE_CCACHE=${USE_CCACHE} \
-DDEBUG_WITHOUT_DEBUG_INFO=ON \
-DLINKER_NAME=lld \
-DUSE_LIBCXX=ON \
-DUSE_LLVM_LIBUNWIND=OFF \
-DUSE_LLVM_COMPILER_RT=OFF \
-DTIFLASH_ENABLE_RUNTIME_RPATH=ON \
-DCMAKE_PREFIX_PATH="/usr/local" \
-DCMAKE_AR="/usr/local/bin/llvm-ar" \
-DRUN_HAVE_STD_REGEX=0 \
-DCMAKE_RANLIB="/usr/local/bin/llvm-ranlib" \
-DUSE_INTERNAL_TIFLASH_PROXY=${BUILD_TIFLASH_PROXY} \
-GNinja

ninja tiflash
cmake --build . --target tiflash --parallel
cmake --install . --component=tiflash-release --prefix="${INSTALL_DIR}"

if [[ "${CMAKE_BUILD_TYPE}" == "Debug" && ${ENABLE_TESTS} -ne 0 ]]; then
ninja page_ctl
Expand All @@ -75,16 +69,6 @@ fi

ccache -s

source ${SCRIPTPATH}/utils/vendor_dependency.sh

# Reduce binary size by compressing.
llvm-objcopy --compress-debug-sections=zlib-gnu "${BUILD_DIR}/dbms/src/Server/tiflash" "${INSTALL_DIR}/tiflash"

vendor_dependency "${INSTALL_DIR}/tiflash" libc++.so "${INSTALL_DIR}/"
vendor_dependency "${INSTALL_DIR}/tiflash" libc++abi.so "${INSTALL_DIR}/"

cp -f "${SRCPATH}/contrib/tiflash-proxy/target/release/libtiflash_proxy.so" "${INSTALL_DIR}/libtiflash_proxy.so"

# unset LD_LIBRARY_PATH before test
unset LD_LIBRARY_PATH
readelf -d "${INSTALL_DIR}/tiflash"
Expand Down
5 changes: 0 additions & 5 deletions release-centos7-llvm/scripts/tiflash-ut-coverage-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ cmake "${SRCPATH}" \
-DUSE_CCACHE=ON \
-DTEST_LLVM_COVERAGE=ON \
-DDEBUG_WITHOUT_DEBUG_INFO=ON \
-DLINKER_NAME=lld \
-DUSE_LIBCXX=ON \
-DUSE_LLVM_LIBUNWIND=OFF \
-DRUN_HAVE_STD_REGEX=0 \
-DUSE_LLVM_COMPILER_RT=OFF \
-DTIFLASH_ENABLE_RUNTIME_RPATH=ON \
-DCMAKE_PREFIX_PATH="/usr/local" \
-GNinja

Expand Down
6 changes: 0 additions & 6 deletions release-centos7-llvm/scripts/utils/vendor_dependency.sh

This file was deleted.

7 changes: 2 additions & 5 deletions release-darwin/build/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ rm -rf $build_dir && mkdir -p $build_dir && cd $build_dir
cmake "$SRCPATH" \
-DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \
-Wno-dev \
-DENABLE_PROXY_RUNTIME_RELATIVE_PATH=ON \
-DNO_WERROR=ON

make -j $NPROC tiflash

cp -f "$build_dir/dbms/src/Server/tiflash" "$install_dir/tiflash"
cp -f "${SRCPATH}/contrib/tiflash-proxy/target/release/libtiflash_proxy.dylib" "$install_dir/libtiflash_proxy.dylib"
cmake --build . --target tiflash --parallel $NPROC
cmake --install . --component=tiflash-release --prefix="$install_dir"

FILE="$install_dir/tiflash"
otool -L "$FILE"
Expand Down

0 comments on commit 0d541dd

Please sign in to comment.