| <!-- Part of the Crubit project, under the Apache License v2.0 with LLVM --> | |
| <!-- Exceptions. See /LICENSE for license information. --> | |
| <!-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception --> | |
| # Building the tool | |
| > [!WARNING] Building `rs_bindings_from_cc` from source is experimental. Further | |
| > support is coming soon. | |
| ## Cargo | |
| ### Prerequisites | |
| * Requires LLVM and Clang libraries to be built and installed. | |
| * They must be built with support for compression (zlib), which is the | |
| default build config. | |
| * Requires Abseil libraries to be built and installed. | |
| * Requires Protobuf libraries and protoc compiler (v27.1+) to be installed. | |
| * Requires zlib (e.g. libz.so) to be available in the system include and lib | |
| paths. | |
| * An up-to-date stable Rust toolchain. | |
| Linux-specific setup: | |
| ```sh | |
| # Choice of compiler is optional. | |
| export CC=/path/to/clang | |
| export CXX=/path/to/clang++ | |
| # We must use `lld` linker via clang. It must be in the PATH. | |
| export PATH="$PATH:/dir/containing/lld" | |
| export RUSTFLAGS="$RUSTFLAGS -Clinker=/path/to/clang" | |
| export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-fuse-ld=lld" | |
| # If you want to use a sysroot. | |
| # SYSROOT_FLAG=--sysroot=$SYSROOT | |
| # export CXXFLAGS="$CXXFLAGS $SYSROOT_FLAG" | |
| # export RUSTFLAGS="$RUSTFLAGS -Clink-arg=$SYSROOT_FLAG" | |
| ``` | |
| MacOS-specific setup: | |
| ```sh | |
| export CC=clang | |
| export CXX=clang++ | |
| export RUSTFLAGS="$RUSTFLAGS -Clinker=clang" | |
| export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-fuse-ld=lld" | |
| # Point to the Xcode sysroot. | |
| export CXXFLAGS="$CXXFLAGS -isysroot $(xcrun --show-sdk-path)" | |
| export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-isysroot -Clink-arg=$(xcrun --show-sdk-path)" | |
| ``` | |
| Windows-specific setup: | |
| * **Windows is currently unsupported**, and the APIs generated by Crubit may | |
| not compile and will change over time. | |
| * All commands must be run from a development shell, where MSVC environment | |
| variables are set up. | |
| ```sh | |
| # We use clang compiler (clang-cl); MSVC may work too but is unsupported. | |
| export CC=clang-cl | |
| export CXX=clang-cl | |
| # We must use lld to link, which is spelt lld-link. So user-specified linker | |
| # flags must be in MSVC format. | |
| export RUSTFLAGS="$RUSTFLAGS -Clinker=/path/to/lld-link" | |
| # LLVM was built with Zlib support. Point Crubit to the same library. | |
| export CXXFLAGS="$CXXFLAGS /I/path/to/zlib" | |
| export RUSTFLAGS="$RUSTFLAGS -Clink-arg=/LIBPATH:/path/to/zlib" | |
| # Avoid deprecation warnings. | |
| export CXXFLAGS="$CXXFLAGS /D_CRT_SECURE_NO_DEPRECATE" | |
| # If LLVM (-DCMAKE_MSVC_RUNTIME_LIBRARY) and Abseil (-DABSL_MSVC_STATIC_RUNTIME) | |
| # are built against static CRT, then Rust needs to match, or vice-versa. | |
| # export RUSTFLAGS="$RUSTFLAGS -Ctarget-feature=+crt-static" | |
| ``` | |
| Run the build step via cargo: | |
| ```sh | |
| # Paths for Crubit's cargo to use. | |
| ## This path contains clang/ and llvm/ dirs with their respective headers. | |
| export CLANG_INCLUDE_PATH=/path/to/llvm/and/clang/headers | |
| ## This path contains libLLVM*.a and libclang*.a. On Windows it may also hold the | |
| ## import libraries of LLVM's DLLs (LLVM-C.lib and friends); those are recognised by | |
| ## their contents and skipped, so the whole `lib` dir can be given as-is. | |
| export CLANG_LIB_STATIC_PATH=/path/to/llvm/and/clang/libs | |
| ## This path contains absl/ dir with all the includes. | |
| export ABSL_INCLUDE_PATH=/path/to/absl/include/dir | |
| ## This path contains libabsl_* | |
| export ABSL_LIB_STATIC_PATH=/path/to/absl/libs | |
| ## This path contains google/protobuf/ dir with all the includes. | |
| export PROTOBUF_INCLUDE_PATH=/path/to/protobuf/include/dir | |
| ## This path contains libprotobuf.a and libutf8_validity.a. Other archives that a | |
| ## protobuf install tree holds (libutf8_range.a, libprotobuf-lite.a, libprotoc.a, | |
| ## libupb.a) are ignored; the first two would collide with the ones above. | |
| export PROTOBUF_LIB_STATIC_PATH=/path/to/protobuf/libs | |
| cargo build --bin rs_bindings_from_cc | |
| ``` | |
| ## Bazel | |
| ```sh | |
| apt install clang lld bazel | |
| git clone git@github.com:google/crubit.git | |
| cd crubit | |
| bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl | |
| ``` | |
| ### Using a prebuilt LLVM tree | |
| ```sh | |
| git clone https://github.com/llvm/llvm-project | |
| cd llvm-project | |
| CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install | |
| cmake --build build -j | |
| # wait... | |
| cmake --install build | |
| cd ../crubit | |
| LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl | |
| ``` |