Skip to content

Commit 5849aeb

Browse files
authored
Merge pull request ClickHouse#62297 from Algunenano/rust_vendor
Vendor rust dependencies
2 parents a3dcb19 + e65dc63 commit 5849aeb

File tree

28 files changed

+361
-430
lines changed

28 files changed

+361
-430
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@
363363
[submodule "contrib/idna"]
364364
path = contrib/idna
365365
url = https://github.com/ada-url/idna.git
366+
[submodule "contrib/rust_vendor"]
367+
path = contrib/rust_vendor
368+
url = https://github.com/ClickHouse/rust_vendor.git
366369
[submodule "contrib/openssl"]
367370
path = contrib/openssl
368371
url = https://github.com/ClickHouse/openssl.git

contrib/corrosion

Submodule corrosion updated 213 files

contrib/rust_vendor

Submodule rust_vendor added at 08e82ca

contrib/sysroot

Submodule sysroot updated 2109 files

docker/packager/binary-builder/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ ENV CARGO_HOME=/rust/cargo
1313
ENV PATH="/rust/cargo/bin:${PATH}"
1414
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y && \
1515
chmod 777 -R /rust && \
16-
rustup toolchain install nightly-2023-07-04 && \
17-
rustup default nightly-2023-07-04 && \
16+
rustup toolchain install nightly-2024-04-01 && \
17+
rustup default nightly-2024-04-01 && \
1818
rustup toolchain remove stable && \
1919
rustup component add rust-src && \
2020
rustup target add x86_64-unknown-linux-gnu && \

docs/en/development/build.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ export CC=clang-18
5858
export CXX=clang++-18
5959
```
6060

61+
### Install Rust compiler
62+
63+
First follow the steps in the official [rust documentation](https://www.rust-lang.org/tools/install) to install `rustup`.
64+
65+
As with C++ dependencies, ClickHouse uses vendoring to control exactly what's installed and avoid depending on third
66+
party services (like the `crates.io` registry).
67+
68+
Although in release mode any rust modern rustup toolchain version should work with this dependencies, if you plan to
69+
enable sanitizers you must use a version that matches the exact same `std` as the one used in CI (for which we vendor
70+
the crates):
71+
72+
```bash
73+
rustup toolchain install nightly-2024-04-01
74+
rustup default nightly-2024-04-01
75+
rustup component add rust-src
76+
```
77+
6178
### Checkout ClickHouse Sources {#checkout-clickhouse-sources}
6279

6380
``` bash
@@ -97,6 +114,7 @@ The build requires the following components:
97114
- Ninja
98115
- Yasm
99116
- Gawk
117+
- rustc
100118

101119
If all the components are installed, you may build it in the same way as the steps above.
102120

rust/.cargo/config.toml.in

Lines changed: 0 additions & 11 deletions
This file was deleted.

rust/CMakeLists.txt

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
if (OS_FREEBSD)
2+
# Right nix/libc requires fspacectl and it had been added only since FreeBSD14.
3+
# And since sysroot has older libraries you will got undefined reference for clickhouse binary.
4+
#
5+
# But likely everything should work without this syscall, however it is not
6+
# possible right now to gently override libraries versions for dependencies,
7+
# and forking rust modules is a little bit too much for this thing.
8+
#
9+
# You can take a look at the details in the following issue [1].
10+
#
11+
# [1]: https://github.com/rust-lang/cargo/issues/5640
12+
#
13+
# Update 2024-04: Now prql also requires getrandom() via std::sys::pal::unix::rand::imp::getrandom_fill_bytes
14+
message(STATUS "Rust build is disabled for FreeBSD because we use old sysroot files")
15+
return()
16+
endif()
17+
18+
119
# NOTE: should be macro to export RUST_CXXFLAGS/RUST_CFLAGS for subfolders
220
macro(configure_rustc)
321
# NOTE: this can also be done by overriding rustc, but it not trivial with rustup.
@@ -52,66 +70,19 @@ macro(configure_rustc)
5270
message(STATUS "RUSTFLAGS: ${RUSTFLAGS}")
5371
message(STATUS "RUST_CARGO_BUILD_STD: ${RUST_CARGO_BUILD_STD}")
5472

55-
# NOTE: requires RW access for the source dir
56-
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config.toml.in" "${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config.toml" @ONLY)
73+
set(RUST_VENDOR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../contrib/rust_vendor")
5774
endmacro()
5875
configure_rustc()
5976

60-
function(clickhouse_import_crate)
61-
# This is a workaround for Corrosion case sensitive build type matching in
62-
# _generator_add_cargo_targets(), that leads to different paths in
63-
# IMPORTED_LOCATION and real path of the library.
64-
#
65-
# It uses CMAKE_CONFIGURATION_TYPES and $<CONFIG>, so here we preserve the
66-
# case of ${CMAKE_BUILD_TYPE} in ${CMAKE_CONFIGURATION_TYPES}.
67-
if ("${CMAKE_BUILD_TYPE_UC}" STREQUAL "DEBUG")
68-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};release")
69-
else()
70-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE};debug")
71-
endif()
72-
73-
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
74-
set(profile "")
75-
else()
76-
if (ENABLE_THINLTO)
77-
set(profile "release-thinlto")
78-
else()
79-
set(profile "release")
80-
endif()
81-
endif()
82-
83-
# Note, here --offline is not used, since on CI vendor archive is used, and
84-
# passing --offline here will be inconvenient for local development.
85-
corrosion_import_crate(NO_STD ${ARGN} PROFILE ${profile})
86-
endfunction()
87-
8877
# Add crate from the build directory.
89-
#
90-
# Our crates has configuration files:
91-
# - config for cargo (see config.toml.in)
92-
# - and possibly config for build (build.rs.in)
93-
#
94-
# And to avoid overlaps different builds for one source directory, crate will
78+
# To avoid overlaps different builds for one source directory, crate will
9579
# be copied from source directory to the binary directory.
96-
file(COPY ".cargo" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
9780
function(add_rust_subdirectory src)
9881
set(dst "${CMAKE_CURRENT_BINARY_DIR}/${src}")
9982
message(STATUS "Copy ${src} to ${dst}")
10083
file(COPY "${src}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}"
10184
PATTERN target EXCLUDE)
10285

103-
# Check is Rust available or not.
104-
#
105-
# `cargo update --dry-run` will not update anything, but will check the internet connectivity.
106-
execute_process(COMMAND ${Rust_CARGO_CACHED} update --dry-run
107-
WORKING_DIRECTORY "${dst}"
108-
RESULT_VARIABLE CARGO_UPDATE_RESULT
109-
OUTPUT_VARIABLE CARGO_UPDATE_STDOUT
110-
ERROR_VARIABLE CARGO_UPDATE_STDERR)
111-
if (CARGO_UPDATE_RESULT)
112-
message(FATAL_ERROR "Rust (${Rust_CARGO_CACHED}) support is not available (likely there is no internet connectivity):\n${CARGO_UPDATE_STDERR}\nYou can disable Rust support with -DENABLE_RUST=OFF")
113-
endif()
114-
11586
add_subdirectory("${dst}" "${dst}")
11687

11788
# cmake -E copy* do now know how to exclude files
@@ -126,5 +97,4 @@ function(add_rust_subdirectory src)
12697
VERBATIM)
12798
endfunction()
12899

129-
add_rust_subdirectory (skim)
130-
add_rust_subdirectory (prql)
100+
add_rust_subdirectory (workspace)

rust/Cargo.toml

Lines changed: 0 additions & 11 deletions
This file was deleted.

rust/VENDOR.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
As we have multiple projects we use a workspace to manage them (it's way simpler and leads to less issues). In order
2+
to vendor all the dependencies we need to store both the registry and the packages themselves.
3+
4+
Note that this includes the exact `std` dependencies for the rustc version used in CI (currently nightly-2024-04-01),
5+
so you need to install `rustup component add rust-src` for the specific version.
6+
7+
* First step: (Re)-generate the Cargo.lock file (run under `workspace/`).
8+
9+
```bash
10+
cargo generate-lockfile
11+
```
12+
13+
* Generate the local registry:
14+
15+
Note that we use both commands to vendor both registry and crates. No idea why both are necessary.
16+
17+
* First we need to install the tool if you don't already have it:
18+
```bash
19+
cargo install --version 0.2.6 cargo-local-registry
20+
```
21+
22+
* Now add the local packages:
23+
24+
```bash
25+
export CH_TOP_DIR=$(git rev-parse --show-toplevel)
26+
export RUSTC_ROOT=$(rustc --print=sysroot)
27+
28+
cd "$CH_TOP_DIR"/rust/workspace
29+
30+
cargo local-registry --git --sync Cargo.lock "$CH_TOP_DIR"/contrib/rust_vendor
31+
cp "$RUSTC_ROOT"/lib/rustlib/src/rust/Cargo.lock "$RUSTC_ROOT"/lib/rustlib/src/rust/library/std/
32+
cargo local-registry --no-delete --git --sync "$RUSTC_ROOT"/lib/rustlib/src/rust/library/std/Cargo.lock "$CH_TOP_DIR"/contrib/rust_vendor
33+
cp "$RUSTC_ROOT"/lib/rustlib/src/rust/Cargo.lock "$RUSTC_ROOT"/lib/rustlib/src/rust/library/test/
34+
cargo local-registry --no-delete --git --sync "$RUSTC_ROOT"/lib/rustlib/src/rust/library/test/Cargo.lock "$CH_TOP_DIR"/contrib/rust_vendor
35+
36+
cargo vendor --no-delete --locked "$CH_TOP_DIR"/contrib/rust_vendor
37+
cd "$RUSTC_ROOT"/lib/rustlib/src/rust/library/std/
38+
cargo vendor --no-delete "$CH_TOP_DIR"/contrib/rust_vendor
39+
cd "$RUSTC_ROOT"/lib/rustlib/src/rust/library/test/
40+
cargo vendor --no-delete "$CH_TOP_DIR"/contrib/rust_vendor
41+
42+
# Remove windows only dependencies (which are really heavy and we don't want in the repo)
43+
rm -rf "$CH_TOP_DIR"/contrib/rust_vendor/winapi* "$CH_TOP_DIR"/contrib/rust_vendor/windows*
44+
45+
# Cleanup the lock files we copied
46+
rm "$RUSTC_ROOT"/lib/rustlib/src/rust/library/std/Cargo.lock "$RUSTC_ROOT"/lib/rustlib/src/rust/library/test/Cargo.lock
47+
cd "$CH_TOP_DIR"/rust/workspace
48+
```
49+
50+
The `rustc --print=sysroot` part includes `std` dependencies, required to build with sanitizer flags. It must be kept
51+
in sync with the rustc version used in CI.

0 commit comments

Comments
 (0)