Skip to content

Commit 5b2363b

Browse files
authored
Merge pull request ClickHouse#78590 from Algunenano/rust_build
Rust build improvements
2 parents 6fd03d9 + e45edb4 commit 5b2363b

File tree

10 files changed

+73
-75
lines changed

10 files changed

+73
-75
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ endmacro ()
3434

3535
set(CMAKE_EXPORT_COMPILE_COMMANDS 1) # Write compile_commands.json
3636
set(CMAKE_LINK_DEPENDS_NO_SHARED 1) # Do not relink all depended targets on .so
37-
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Debug;Release;MinSizeRel" CACHE STRING "" FORCE)
38-
set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Generate debug library name with a postfix.") # To be consistent with CMakeLists from contrib libs.
3937

4038
# Enable the ability to organize targets into hierarchies of "folders" for capable GUI-based IDEs.
4139
# For more info see https://cmake.org/cmake/help/latest/prop_gbl/USE_FOLDERS.html

cmake/cpu_features.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
option (ARCH_NATIVE "Add -march=native compiler flag. This makes your binaries non-portable but more performant code may be generated. This option overrides ENABLE_* options for specific instruction set. Highly not recommended to use." 0)
1010

11+
set(RUSTFLAGS)
1112
if (ARCH_NATIVE)
1213
set (COMPILER_FLAGS "${COMPILER_FLAGS} -march=native")
14+
list(APPEND RUSTFLAGS "-C" "target-feature=native")
1315

1416
# Populate the ENABLE_ option flags. This is required for the build of some third-party dependencies, specifically snappy, which
1517
# (somewhat weirdly) expects the relative SNAPPY_HAVE_ preprocessor variables to be populated, in addition to the microarchitecture
@@ -56,6 +58,7 @@ elseif (ARCH_AARCH64)
5658
# crc32 is optional in v8.0 and mandatory in v8.1. Enable it as __crc32()* is used in lot's of places and even very old ARM CPUs
5759
# support it.
5860
set (COMPILER_FLAGS "${COMPILER_FLAGS} -march=armv8+crc")
61+
list(APPEND RUSTFLAGS "-C" "target_feature=+crc,-neon")
5962
else ()
6063
# ARMv8.2 is quite ancient but the lowest common denominator supported by both Graviton 2 and 3 processors [1, 10]. In particular, it
6164
# includes LSE (made mandatory with ARMv8.1) which provides nice speedups without having to fall back to compat flag
@@ -87,6 +90,8 @@ elseif (ARCH_AARCH64)
8790
# [9] https://developer.arm.com/documentation/dui0801/g/A64-Data-Transfer-Instructions/LDAPR?lang=en
8891
# [10] https://github.com/aws/aws-graviton-getting-started/blob/main/README.md
8992
set (COMPILER_FLAGS "${COMPILER_FLAGS} -march=armv8.2-a+simd+crypto+dotprod+ssbs+rcpc+bf16")
93+
# Not adding `+v8.2a,+crypto` to rust because it complains about them being unstable
94+
list(APPEND RUSTFLAGS "-C" "target_feature=+dotprod,+ssbs,+rcpc,+bf16")
9095
endif ()
9196

9297
# Best-effort check: The build generates and executes intermediate binaries, e.g. protoc and llvm-tablegen. If we build on ARM for ARM
@@ -112,8 +117,10 @@ elseif (ARCH_PPC64LE)
112117
option (POWER9 "Build for Power 9 CPU and above" 0)
113118
if(POWER9)
114119
set (COMPILER_FLAGS "${COMPILER_FLAGS} -maltivec -mcpu=power9 -D__SSE2__=1 -DNO_WARN_X86_INTRINSICS")
120+
list(APPEND RUSTFLAGS "-C" "target_feature=+power9-altivec")
115121
else ()
116122
set (COMPILER_FLAGS "${COMPILER_FLAGS} -maltivec -mcpu=power8 -D__SSE2__=1 -DNO_WARN_X86_INTRINSICS")
123+
list(APPEND RUSTFLAGS "-C" "target_feature=+power8-altivec")
117124
endif ()
118125

119126
elseif (ARCH_AMD64)
@@ -169,48 +176,60 @@ elseif (ARCH_AMD64)
169176

170177
if (ENABLE_SSSE3)
171178
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mssse3")
179+
list(APPEND RUSTFLAGS "-C" "target_feature=+ssse3")
172180
endif ()
173181

174182
if (ENABLE_SSE41)
175183
set (COMPILER_FLAGS "${COMPILER_FLAGS} -msse4.1")
184+
list(APPEND RUSTFLAGS "-C" "target_feature=+sse4.1")
176185
endif ()
177186

178187
if (ENABLE_SSE42)
179188
set (COMPILER_FLAGS "${COMPILER_FLAGS} -msse4.2")
189+
list(APPEND RUSTFLAGS "-C" "target_feature=+sse4.2")
180190
endif ()
181191

182192
if (ENABLE_PCLMULQDQ)
183193
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mpclmul")
194+
list(APPEND RUSTFLAGS "-C" "target_feature=+pclmulqdq")
184195
endif ()
185196

186197
if (ENABLE_BMI)
187198
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mbmi")
199+
list(APPEND RUSTFLAGS "-C" "target_feature=+bmi1")
188200
endif ()
189201

190202
if (ENABLE_POPCNT)
191203
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mpopcnt")
204+
list(APPEND RUSTFLAGS "-C" "target_feature=+popcnt")
192205
endif ()
193206

194207
if (ENABLE_AVX)
195208
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mavx")
209+
list(APPEND RUSTFLAGS "-C" "target_feature=+avx")
196210
endif ()
197211

198212
if (ENABLE_AVX2)
199213
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mavx2")
214+
list(APPEND RUSTFLAGS "-C" "target_feature=+avx2")
200215
if (ENABLE_BMI2)
201216
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mbmi2")
217+
list(APPEND RUSTFLAGS "-C" "target_feature=+bmi2")
202218
endif ()
203219
endif ()
204220

205221
if (ENABLE_AVX512)
206222
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mavx512f -mavx512bw -mavx512vl")
223+
list(APPEND RUSTFLAGS "-C" "target_feature=+avx512f,+avx512bw,+avx512vl")
207224
if (ENABLE_AVX512_VBMI)
208225
set (COMPILER_FLAGS "${COMPILER_FLAGS} -mavx512vbmi")
226+
list(APPEND RUSTFLAGS "-C" "target_feature=+avx512vbmi")
209227
endif ()
210228
endif ()
211229

212230
if (ENABLE_AVX512_FOR_SPEC_OP)
213231
set (X86_INTRINSICS_FLAGS "-mbmi -mavx512f -mavx512bw -mavx512vl -mprefer-vector-width=256")
232+
list(APPEND RUSTFLAGS "-C" "target_feature=+bmi1,+avx512f,+avx512bw,+avx512vl,+prefer-256-bit")
214233
endif ()
215234

216235
else ()

contrib/corrosion-cmake/CMakeLists.txt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ list(APPEND CMAKE_MODULE_PATH "${ClickHouse_SOURCE_DIR}/contrib/corrosion/cmake"
5050
# Define function corrosion_import_crate()
5151
include ("${ClickHouse_SOURCE_DIR}/contrib/corrosion/cmake/Corrosion.cmake")
5252

53-
# NOTE: this can also be done by overriding rustc, but it not trivial with rustup.
54-
set(RUST_CFLAGS "${CMAKE_C_FLAGS}")
55-
5653
set(CXX_INCLUDE_DIR "${ClickHouse_SOURCE_DIR}/contrib/llvm-project/libcxx/include")
57-
set(RUST_CXXFLAGS "${CMAKE_CXX_FLAGS} -isystem ${CXX_INCLUDE_DIR} -nostdinc++")
54+
55+
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
56+
set(RUST_CFLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_DEBUG}")
57+
set(RUST_CXXFLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -isystem ${CXX_INCLUDE_DIR} -nostdinc++")
58+
else ()
59+
set(RUST_CFLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
60+
set(RUST_CXXFLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -isystem ${CXX_INCLUDE_DIR} -nostdinc++")
61+
endif()
5862

5963
if (CMAKE_OSX_SYSROOT)
6064
set(RUST_CXXFLAGS "${RUST_CXXFLAGS} -isysroot ${CMAKE_OSX_SYSROOT}")
@@ -80,7 +84,7 @@ else()
8084
set(RUSTCWRAPPER "")
8185
endif()
8286

83-
set(RUSTFLAGS)
87+
# Note that RUSTFLAGS is defined in cpu_features.cmake
8488
if (CMAKE_OSX_DEPLOYMENT_TARGET)
8589
list(APPEND RUSTFLAGS "-C" "link-arg=-mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
8690
endif()
@@ -116,3 +120,18 @@ set(RUST_VENDOR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../rust_vendor")
116120
# This needs to be done for each project using set_property(INTERFACE_CORROSION_CARGO_FLAGS)
117121
configure_file("config.toml.in" "config.toml" @ONLY)
118122
set (RUST_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/config.toml" CACHE INTERNAL "Rust config")
123+
124+
function(clickhouse_import_crate)
125+
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
126+
set(profile "")
127+
else()
128+
if (ENABLE_THINLTO)
129+
set(profile "release-thinlto")
130+
else()
131+
set(profile "release")
132+
endif()
133+
endif()
134+
135+
corrosion_import_crate(${ARGN} NO_STD PROFILE ${profile} CRATE_TYPES staticlib LOCKED FLAGS --offline NO_DEFAULT_FEATURES)
136+
137+
endfunction()

contrib/corrosion-cmake/config.toml.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[env]
2+
# Used by skim
23
CC = "@CMAKE_C_COMPILER@"
4+
CC_@Rust_CARGO_TARGET@ = "@CMAKE_C_COMPILER@"
35
CXX = "@CMAKE_CXX_COMPILER@"
6+
CXX_@Rust_CARGO_TARGET@ = "@CMAKE_CXX_COMPILER@"
47
CFLAGS = "@RUST_CFLAGS@"
58
CXXFLAGS = "@RUST_CXXFLAGS@"
69

@@ -12,6 +15,15 @@ rustdocflags = @RUSTFLAGS@
1215
[unstable]
1316
@RUST_CARGO_BUILD_STD@
1417

18+
[profile.release]
19+
debug = false
20+
strip = true
21+
22+
# We use LTO to slightly decrease binary size
23+
[profile.release-thinlto]
24+
inherits = "release"
25+
lto = true
26+
1527
[source.crates-io]
1628
registry = 'sparse+https://index.crates.io/'
1729
replace-with = 'local-registry'

contrib/delta-kernel-rs-cmake/CMakeLists.txt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ include(ExternalProject)
66

77
set(USE_DELTA_KERNEL_RS ${ENABLE_LIBRARIES})
88

9-
# TODO: Improve this
10-
# MSAN: ld.lld: error: undefined symbol: ring_core_0_17_9__bn_mul_mont_nohw / ld.lld: error: undefined symbol: ring_core_0_17_9__chacha20_poly1305_seal_avx2 ...
9+
# MSAN: Disabled because ring does not generate the appropriate symbols
10+
# NO_ARMV81_OR_HIGHER: Disabled because ring assumes that Neon is available with ARM
11+
# Darwin: Issues with reqwest -> system-configuration (requires other frameworks)
1112
if (NOT ENABLE_LIBRARIES OR NOT OS_LINUX OR (NOT ARCH_AMD64 AND NOT ARCH_AARCH64) OR (SANITIZE STREQUAL "memory") OR NO_ARMV81_OR_HIGHER OR NOT ENABLE_RUST)
1213
message(STATUS "Disabling delta kernel because of incompatible platform or Rust is disabled")
1314
set(USE_DELTA_KERNEL_RS 0)
@@ -19,9 +20,14 @@ if (NOT ENABLE_DELTA_KERNEL_RS)
1920
return()
2021
endif()
2122

22-
# Path for autogenerated files (`delta_kernel_ffi.hpp`)
23-
set(DELTA_KERNEL_TARGET_PATH ${DELTA_KERNEL_RS_BINARY_DIR}/include)
23+
# Ideally we would disable all features to remove dependencies but:
24+
# * default-engine: Needed for its s3 client (we can't pass our own s3 client via ffi)
25+
# * delta_kernel/cloud: s3 client
26+
clickhouse_import_crate(
27+
MANIFEST_PATH "${DELTA_KERNEL_RS_SOURCE_DIR}/ffi/Cargo.toml"
28+
FEATURES "default-engine, delta_kernel/cloud")
2429

30+
# We use our own OpenSSL headers and libs
2531
if(ARCH_AMD64)
2632
if(OS_DARWIN)
2733
set(PLATFORM_DIRECTORY darwin_x86_64)
@@ -47,30 +53,12 @@ endif()
4753
file(COPY ${ClickHouse_SOURCE_DIR}/contrib/openssl-cmake/${PLATFORM_DIRECTORY}/include/ DESTINATION ${DELTA_KERNEL_RS_BINARY_DIR}/${PLATFORM_DIRECTORY})
4854
file(COPY ${ClickHouse_SOURCE_DIR}/contrib/openssl/include/ DESTINATION ${DELTA_KERNEL_RS_BINARY_DIR}/${PLATFORM_DIRECTORY})
4955

50-
if ("${CMAKE_BUILD_TYPE_UC}" STREQUAL "DEBUG")
51-
SET(SSLNAME "ssld")
52-
SET(CRYPTONAME "cryptod")
53-
else()
54-
SET(SSLNAME "ssl")
55-
SET(CRYPTONAME "crypto")
56-
endif()
57-
58-
if ("${CMAKE_BUILD_TYPE_UC}" STREQUAL "DEBUG")
59-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};release")
60-
else()
61-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};debug")
62-
endif()
63-
64-
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
65-
set(profile "")
66-
else()
67-
set(profile "release")
68-
endif()
56+
# Path for autogenerated files (`delta_kernel_ffi.hpp`)
57+
set(DELTA_KERNEL_TARGET_PATH ${DELTA_KERNEL_RS_BINARY_DIR}/include)
6958

70-
corrosion_import_crate(MANIFEST_PATH "${DELTA_KERNEL_RS_SOURCE_DIR}/ffi/Cargo.toml" NO_STD PROFILE ${profile} CRATE_TYPES staticlib ALL_FEATURES FLAGS --offline)
7159
corrosion_set_env_vars(delta_kernel_ffi
7260
"CARGO_TARGET_DIR=${DELTA_KERNEL_TARGET_PATH}"
73-
"OPENSSL_LIBS=${SSLNAME}:${CRYPTONAME}"
61+
"OPENSSL_LIBS=ssl:crypto"
7462
"OPENSSL_STATIC=1"
7563
"OPENSSL_LIB_DIR=${CMAKE_BINARY_DIR}/contrib/openssl-cmake/"
7664
"OPENSSL_INCLUDE_DIR=${DELTA_KERNEL_RS_BINARY_DIR}/${PLATFORM_DIRECTORY}")

contrib/grpc-cmake/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ if (NOT CMAKE_HOST_SYSTEM_NAME STREQUAL CMAKE_SYSTEM_NAME
148148
"-DOPENSSL_INCLUDE_DIR=${OPENSSL_BUILD_DIR}/include"
149149
"-DZLIB_ROOT_DIR=${zlib_source_dir}"
150150
"-DgRPC_SSL_PROVIDER=package"
151+
# TODO: Bump c-ares
152+
"-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
151153
"${_gRPC_SOURCE_DIR}"
152154
WORKING_DIRECTORY "${GRPC_CPP_PLUGIN_BUILD_DIR}"
153155
COMMAND_ECHO STDOUT

contrib/openssl-cmake/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
13
# Actually, so many 3rd party libraries + unit tests need SSL that we cannot disable it
24
# without breaking the build ...
35
set(ENABLE_SSL 1 CACHE INTERNAL "")

rust/workspace/CMakeLists.txt

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,6 @@ if (OS_FREEBSD)
1515
return()
1616
endif()
1717

18-
function(clickhouse_import_crate)
19-
# This is a workaround for Corrosion case sensitive build type matching in
20-
# _generator_add_cargo_targets(), that leads to different paths in
21-
# IMPORTED_LOCATION and real path of the library.
22-
#
23-
# It uses CMAKE_CONFIGURATION_TYPES and $<CONFIG>, so here we preserve the
24-
# case of ${CMAKE_BUILD_TYPE} in ${CMAKE_CONFIGURATION_TYPES}.
25-
if ("${CMAKE_BUILD_TYPE_UC}" STREQUAL "DEBUG")
26-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};release")
27-
else()
28-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};debug")
29-
endif()
30-
31-
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
32-
set(profile "")
33-
else()
34-
if (ENABLE_THINLTO)
35-
set(profile "release-thinlto")
36-
else()
37-
set(profile "release")
38-
endif()
39-
endif()
40-
41-
corrosion_import_crate(${ARGN} NO_STD PROFILE ${profile} LOCKED FLAGS --offline)
42-
43-
endfunction()
44-
45-
# TODO: add via properties if necessary
46-
## -Wno-dollar-in-identifier-extension: cxx bridge complies names with '$'
47-
## -Wno-unused-macros: unused CXXBRIDGE1_RUST_STRING
48-
#set(CXXBRIDGE_CXXFLAGS "-Wno-dollar-in-identifier-extension -Wno-unused-macros")
49-
#set(RUST_CXXFLAGS "${RUST_CXXFLAGS} ${CXXBRIDGE_CXXFLAGS}")
50-
#message(STATUS "RUST_CXXFLAGS (for skim): ${RUST_CXXFLAGS}")
51-
5218
clickhouse_import_crate(MANIFEST_PATH Cargo.toml)
5319

5420
# Set cargo config (see contrib/delta-kernel-rs-cmake/CMakeLists.txt)

rust/workspace/Cargo.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ members = [
66
]
77
resolver = "2"
88

9-
[profile.release]
10-
debug = false
11-
12-
# We use LTO to slightly decrease binary size
13-
[profile.release-thinlto]
14-
inherits = "release"
15-
lto = true
16-
179
[patch.crates-io]
1810
# Ref: https://github.com/lotabout/tuikit/pull/51
1911
tuikit = { git = "https://github.com/azat-rust/tuikit.git", rev = "e1994c0e03ff02c49cf1471f0cc3cbf185ce0104" }

0 commit comments

Comments
 (0)