From 94414aef0750d235c58bca7926f0baaba56b6598 Mon Sep 17 00:00:00 2001 From: Tom Karpiniec Date: Tue, 13 Oct 2020 17:46:53 +1100 Subject: [PATCH 1/3] Update from Rust 1.43 to 1.46 and improve packaging in minor ways. In particular, toolchains are now installed against their specific version number, so they can coexist. This is particularly useful in CI environments where different branches may be on different versions. --- build.sh | 5 ++--- config.sh | 7 +++--- dist.sh | 26 ++++++++++++++------- install.sh | 8 +++++-- patches/llvm-system-libs.patch | 40 +++++++++++++++++++++++++++++++++ patches/rust_embed_cmdline.diff | 15 ------------- 6 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 patches/llvm-system-libs.patch delete mode 100644 patches/rust_embed_cmdline.diff diff --git a/build.sh b/build.sh index 90e136f..2b59972 100755 --- a/build.sh +++ b/build.sh @@ -28,6 +28,7 @@ cd "$WORKING_DIR/llvm-project" git reset --hard git clean -f git checkout "$LLVM_BRANCH" +git apply ../../patches/llvm-system-libs.patch cd .. mkdir -p llvm-build @@ -44,11 +45,9 @@ cd rust git reset --hard git clean -f git checkout "$RUST_BRANCH" -git apply ../../patches/rust_embed_cmdline.diff cd .. mkdir -p rust-build cd rust-build -../rust/configure --llvm-config="$WORKING_DIR/llvm-root/bin/llvm-config" --target=aarch64-apple-ios --enable-extended --tools=cargo -export RUSTFLAGS_NOT_BOOTSTRAP=-Zembed-bitcode +../rust/configure --llvm-config="$WORKING_DIR/llvm-root/bin/llvm-config" --target=aarch64-apple-ios --enable-extended --tools=cargo --release-channel=stable export CFLAGS_aarch64_apple_ios=-fembed-bitcode python "$WORKING_DIR/rust/x.py" build diff --git a/config.sh b/config.sh index 4246cd2..544937e 100644 --- a/config.sh +++ b/config.sh @@ -5,15 +5,14 @@ # Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) # Target: x86_64-apple-darwin19.3.0 -LLVM_BRANCH="tags/swift-5.2.3-RELEASE" +LLVM_BRANCH="tags/swift-5.3-RELEASE" # 2. Select the best branch, tag or commit hash from https://github.com/rust-lang/rust -# The stable 1.40.0 version of Rust seems to work -RUST_BRANCH="tags/1.43.0" +RUST_BRANCH="tags/1.46.0" # 3. Select a name for the toolchain you want to install as. The toolchain will be installed # under $HOME/.rust-ios-arm64/toolchain-$RUST_TOOLCHAIN -RUST_TOOLCHAIN="1.43.0" +RUST_TOOLCHAIN="ios-arm64-1.46.0" diff --git a/dist.sh b/dist.sh index add656a..07d0dad 100755 --- a/dist.sh +++ b/dist.sh @@ -2,25 +2,35 @@ set -euxo source config.sh +# The built toolchain that we are going to package WORKING_DIR="$(pwd)/build" -DEST="$(pwd)/dist/rust-ios-arm64-${RUST_TOOLCHAIN}" -TOOLCHAIN_DEST="${DEST}/toolchain-${RUST_TOOLCHAIN}" -rm -rf "$TOOLCHAIN_DEST" +# The directory which will added to the final zip file +DEST="$(pwd)/dist/rust-${RUST_TOOLCHAIN}" + +# The actual toolchain inside that, which will be installed to ~/.rustup/... +TOOLCHAIN_DEST="${DEST}/${RUST_TOOLCHAIN}" + +rm -rf "$DEST" mkdir -p "$TOOLCHAIN_DEST" + +# Remove unneeded files from output +rm -rf "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2/lib/rustlib/src" + +# Copy in toolchain artifacts cp -r "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2"/* "$TOOLCHAIN_DEST" cp -r "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2-tools/x86_64-apple-darwin/release/cargo" "$TOOLCHAIN_DEST/bin" +# Copy in static files that need to be included in the distribution cp LICENSE* README.md "$DEST" -rm -rf "$DEST/install.sh" echo "#!/bin/bash" >> "$DEST/install.sh" -echo "DEST_TOOLCHAIN=\"\$HOME/.rust-ios-arm64/toolchain-$RUST_TOOLCHAIN\"" >> "$DEST/install.sh" +echo "DEST_TOOLCHAIN=\"\$HOME/.rustup/toolchains/$RUST_TOOLCHAIN\"" >> "$DEST/install.sh" echo "mkdir -p \"\$DEST_TOOLCHAIN\"" >> $DEST/install.sh -echo "cp -r \"toolchain-$RUST_TOOLCHAIN\"/* \"\$DEST_TOOLCHAIN\"" >> "$DEST/install.sh" -echo "rustup toolchain link ios-arm64 \"\$DEST_TOOLCHAIN\"" >> "$DEST/install.sh" +echo "cp -r \"$RUST_TOOLCHAIN\"/* \"\$DEST_TOOLCHAIN\"" >> "$DEST/install.sh" chmod +x "$DEST/install.sh" cd dist -zip -r "rust-ios-arm64-${RUST_TOOLCHAIN}.zip" "rust-ios-arm64-${RUST_TOOLCHAIN}" +rm -f "rust-${RUST_TOOLCHAIN}.zip" +zip -r "rust-${RUST_TOOLCHAIN}.zip" "rust-${RUST_TOOLCHAIN}" cd .. diff --git a/install.sh b/install.sh index 04ccf5c..8af381c 100755 --- a/install.sh +++ b/install.sh @@ -3,10 +3,14 @@ set -euxo source config.sh WORKING_DIR="$(pwd)/build" -DEST_TOOLCHAIN="$HOME/.rust-ios-arm64/toolchain-$RUST_TOOLCHAIN" +DEST_TOOLCHAIN="$HOME/.rustup/toolchains/$RUST_TOOLCHAIN" +# Remove unneeded files from output +rm -rf "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2/lib/rustlib/src" + +rm -rf "$DEST_TOOLCHAIN" mkdir -p "$DEST_TOOLCHAIN" cp -r "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2"/* "$DEST_TOOLCHAIN" cp -r "$WORKING_DIR/rust-build/build/x86_64-apple-darwin/stage2-tools/x86_64-apple-darwin/release/cargo" "$DEST_TOOLCHAIN/bin" -rustup toolchain link ios-arm64 "$DEST_TOOLCHAIN" +echo "Installed bitcode-enabled Rust toolchain. Use with: +$RUST_TOOLCHAIN" diff --git a/patches/llvm-system-libs.patch b/patches/llvm-system-libs.patch new file mode 100644 index 0000000..1dee27b --- /dev/null +++ b/patches/llvm-system-libs.patch @@ -0,0 +1,40 @@ +diff --git a/llvm/tools/llvm-config/llvm-config.cpp b/llvm/tools/llvm-config/llvm-config.cpp +index 6c31df3e173b..69aaaf6ca78e 100644 +--- a/llvm/tools/llvm-config/llvm-config.cpp ++++ b/llvm/tools/llvm-config/llvm-config.cpp +@@ -707,7 +707,34 @@ int main(int argc, char **argv) { + // Output system libraries only if linking against a static + // library (since the shared library links to all system libs + // already) +- OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n'; ++ ++ // ========== BEGIN HACK ========== ++ ++ // There is a bug where "llvm-config --system-libs" will output long absolute paths. ++ // This ends up with rust's cc doing bad things like "-llibz.tbd" or "-llibxml2.dylib". ++ // Some attempts have been made to patch this but none of them have actually landed ++ // in either LLVM 10 (which Apple is currently using) nor in LLVM 11. ++ // ++ // References: ++ // https://discourse.brew.sh/t/llvm-config-10-0-1-advertise-libxml2-tbd-as-system-libs/8593/4 ++ // https://reviews.llvm.org/D86134 ++ // https://reviews.llvm.org/D87590 ++ // ++ // It appears that the right way to fix this involves LLVM's CMakeLists.txt where ++ // these system libraries are calculated. Unfortunately I can't work out how to apply ++ // the proposed fixes to apple's fork of LLVM as the CMake stuff is all a bit different. ++ // ++ // Rather than spend all week understanding LLVM's build system, I'm going to make the ++ // bold assumption that everybody who's building this bitcode toolchain is running on ++ // basically similar Macs, so if I just hardcode the right list of libraries here then ++ // we'll all be okay. ++ // ++ // This can be removed when the appropriate upstream fix reaches Xcode's LLVM. ++ ++ //OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n'; ++ OS << (LinkMode == LinkModeStatic ? "-lz -lcurses -lm -lxml2" : "") << '\n'; ++ ++ // ========== END HACK ========== + } + } else if (!Components.empty()) { + WithColor::error(errs(), "llvm-config") diff --git a/patches/rust_embed_cmdline.diff b/patches/rust_embed_cmdline.diff deleted file mode 100644 index 46cdafe..0000000 --- a/patches/rust_embed_cmdline.diff +++ /dev/null @@ -1,15 +0,0 @@ -diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs -index 52f3a1cbb5c..6014a1d8ade 100644 ---- a/src/librustc_codegen_llvm/back/write.rs -+++ b/src/librustc_codegen_llvm/back/write.rs -@@ -686,7 +686,9 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext, - llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage); - llvm::LLVMSetGlobalConstant(llglobal, llvm::True); - -- let llconst = common::bytes_in_context(llcx, &[]); -+ let args = "-triple\0arm64-apple-ios11.0.0\0-emit-obj\0\ -+ -disable-llvm-passes\0-target-abi\0darwinpcs\0-Os\0"; -+ let llconst = common::bytes_in_context(llcx, args.as_bytes()); - let llglobal = llvm::LLVMAddGlobal( - llmod, - common::val_ty(llconst), From 3842dcd96d3ac06d6505a8e4d2da281d4cb1a6f5 Mon Sep 17 00:00:00 2001 From: Tom Karpiniec Date: Tue, 13 Oct 2020 17:50:50 +1100 Subject: [PATCH 2/3] Update README --- README.md | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e3a3dac..31c4421 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,7 @@ create an issue if you notice any problems. Visit the [releases page](https://github.com/getditto/rust-bitcode/releases) and download a zip file. It will have a name of the form -`rust-ios-arm64-20xx-xx-xx.zip` where the date is the Rust nightly that it is -based on. +`rust-ios-arm64-xxx.zip`. Unzip the file and open a terminal to the extracted directory. Run the installation script: @@ -31,12 +30,7 @@ installation script: ./install.sh ``` -This will: -1. Install the toolchain in `~/.rust-ios-arm64/toolchain-YYYY-MM-DD` -2. Configure `rustup` with a custom toolchain under the name `ios-arm64`. - -You can also install and add the toolchain yourself if you don't like these -defaults. +This will install the toolchain in `~/.rustup/toolchains/ios-arm64-xxx`. ## Build from source @@ -48,15 +42,14 @@ defaults. repositories under `build/` and compile them. The toolchain will end up at `build/rust-build/build/x86_64-apple-darwin/stage2`. 5. Run `./install.sh`. This will install the toolchain in - `~/.rust-ios-arm64/toolchain-YYYY-MM-DD` and add it to rustup, the same as - for pre-compiled releases. + `~/.rustup/toolchains/rust-ios-arm64-1.46.0`, making it available in rustup. ## Using the toolchain Build your library like this: ``` -RUSTFLAGS="-Z embed-bitcode" cargo +ios-arm64 build --target aarch64-apple-ios --release --lib +cargo +ios-arm64-1.46.0 build --target aarch64-apple-ios --release --lib ``` ## License From 6135d82647107d578e16657d671422e6824f6317 Mon Sep 17 00:00:00 2001 From: Tom Karpiniec Date: Wed, 14 Oct 2020 10:01:34 +1100 Subject: [PATCH 3/3] Fix out-of-date remarks and typos in comments --- config.sh | 6 ++---- dist.sh | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config.sh b/config.sh index 544937e..fcf5e2d 100644 --- a/config.sh +++ b/config.sh @@ -1,9 +1,7 @@ # 1. Select the best branch, tag or commit hash from https://github.com/apple/llvm-project # The recommended approach is to use the tagged release that matches the Swift version -# returned by the command below (at this time running Xcode Version 11.3.1 (11C504)) +# returned by the command below: # $ xcrun -sdk iphoneos swiftc --version -# Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) -# Target: x86_64-apple-darwin19.3.0 LLVM_BRANCH="tags/swift-5.3-RELEASE" @@ -12,7 +10,7 @@ LLVM_BRANCH="tags/swift-5.3-RELEASE" RUST_BRANCH="tags/1.46.0" # 3. Select a name for the toolchain you want to install as. The toolchain will be installed -# under $HOME/.rust-ios-arm64/toolchain-$RUST_TOOLCHAIN +# under $HOME/.rustup/toolchains/rust-$RUST_TOOLCHAIN RUST_TOOLCHAIN="ios-arm64-1.46.0" diff --git a/dist.sh b/dist.sh index 07d0dad..4ca69cf 100755 --- a/dist.sh +++ b/dist.sh @@ -5,7 +5,7 @@ source config.sh # The built toolchain that we are going to package WORKING_DIR="$(pwd)/build" -# The directory which will added to the final zip file +# The directory which will be added to the final zip file DEST="$(pwd)/dist/rust-${RUST_TOOLCHAIN}" # The actual toolchain inside that, which will be installed to ~/.rustup/...