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 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..fcf5e2d 100644 --- a/config.sh +++ b/config.sh @@ -1,19 +1,16 @@ # 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.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 +# under $HOME/.rustup/toolchains/rust-$RUST_TOOLCHAIN -RUST_TOOLCHAIN="1.43.0" +RUST_TOOLCHAIN="ios-arm64-1.46.0" diff --git a/dist.sh b/dist.sh index add656a..4ca69cf 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 be 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),