From 933ba82266da94a812cc3052c6b91b7f8f5b4c35 Mon Sep 17 00:00:00 2001 From: msizanoen1 Date: Sat, 30 Nov 2019 19:47:59 +0700 Subject: [PATCH] RISC-V GNU/Linux as host platform --- .github/workflows/ci.yml | 3 ++ src/bootstrap/native.rs | 24 ++++++++++++-- src/ci/azure-pipelines/auto.yml | 1 + .../host-x86_64/dist-riscv64-linux/Dockerfile | 31 +++++++++++++++++++ .../build-toolchains.sh} | 8 ++--- .../crosstool-ng.sh | 1 - .../riscv64-unknown-linux-gnu.config | 2 -- .../host-x86_64/dist-various-1/Dockerfile | 16 ---------- src/ci/github-actions/ci.yml | 3 ++ src/librustc_llvm/build.rs | 5 +++ 10 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 src/ci/docker/host-x86_64/dist-riscv64-linux/Dockerfile rename src/ci/docker/host-x86_64/{dist-various-1/build-riscv-toolchain.sh => dist-riscv64-linux/build-toolchains.sh} (75%) rename src/ci/docker/host-x86_64/{dist-various-1 => dist-riscv64-linux}/crosstool-ng.sh (96%) mode change 100755 => 100644 rename src/ci/docker/host-x86_64/{dist-various-1 => dist-riscv64-linux}/riscv64-unknown-linux-gnu.config (99%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ffc8a7ad52..68cd91fee17 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -297,6 +297,9 @@ jobs: - name: dist-powerpc64le-linux os: ubuntu-latest-xl env: {} + - name: dist-riscv64-linux + os: ubuntu-latest-xl + env: {} - name: dist-s390x-linux os: ubuntu-latest-xl env: {} diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index cceb7941650..594bac873a1 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -111,6 +111,15 @@ fn make_run(run: RunConfig<'_>) { /// Compile LLVM for `target`. fn run(self, builder: &Builder<'_>) -> PathBuf { let target = self.target; + let target_native = if self.target.starts_with("riscv") { + // RISC-V target triples in Rust is not named the same as C compiler target triples. + // This converts Rust RISC-V target triples to C compiler triples. + let idx = target.find('-').unwrap(); + + format!("riscv{}{}", &target[5..7], &target[idx..]) + } else { + target.to_string() + }; let Meta { stamp, build_llvm_config, out_dir, root } = match prebuilt_llvm_config(builder, target) { @@ -164,8 +173,8 @@ fn run(self, builder: &Builder<'_>) -> PathBuf { .define("LLVM_ENABLE_BINDINGS", "OFF") .define("LLVM_ENABLE_Z3_SOLVER", "OFF") .define("LLVM_PARALLEL_COMPILE_JOBS", builder.jobs().to_string()) - .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap()) - .define("LLVM_DEFAULT_TARGET_TRIPLE", target); + .define("LLVM_TARGET_ARCH", target_native.split('-').next().unwrap()) + .define("LLVM_DEFAULT_TARGET_TRIPLE", target_native); if !target.contains("netbsd") { cfg.define("LLVM_ENABLE_ZLIB", "ON"); @@ -212,6 +221,17 @@ fn run(self, builder: &Builder<'_>) -> PathBuf { } } + if target.starts_with("riscv") { + // In RISC-V, using C++ atomics require linking to `libatomic` but the LLVM build + // system check cannot detect this. Therefore it is set manually here. + if !builder.config.llvm_tools_enabled { + cfg.define("CMAKE_EXE_LINKER_FLAGS", "-latomic"); + } else { + cfg.define("CMAKE_EXE_LINKER_FLAGS", "-latomic -static-libstdc++"); + } + cfg.define("CMAKE_SHARED_LINKER_FLAGS", "-latomic"); + } + if target.contains("msvc") { cfg.define("LLVM_USE_CRT_DEBUG", "MT"); cfg.define("LLVM_USE_CRT_RELEASE", "MT"); diff --git a/src/ci/azure-pipelines/auto.yml b/src/ci/azure-pipelines/auto.yml index 3de5a19f851..13eba269392 100644 --- a/src/ci/azure-pipelines/auto.yml +++ b/src/ci/azure-pipelines/auto.yml @@ -53,6 +53,7 @@ jobs: dist-powerpc-linux: {} dist-powerpc64-linux: {} dist-powerpc64le-linux: {} + dist-riscv64-linux: {} dist-s390x-linux: {} dist-x86_64-freebsd: {} dist-x86_64-musl: {} diff --git a/src/ci/docker/host-x86_64/dist-riscv64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-riscv64-linux/Dockerfile new file mode 100644 index 00000000000..dff7c475157 --- /dev/null +++ b/src/ci/docker/host-x86_64/dist-riscv64-linux/Dockerfile @@ -0,0 +1,31 @@ +FROM ubuntu:18.04 + +COPY scripts/cross-apt-packages.sh /scripts/ +RUN sh /scripts/cross-apt-packages.sh + +COPY host-x86_64/dist-riscv64-linux/crosstool-ng.sh /scripts/ +RUN sh /scripts/crosstool-ng.sh + +COPY scripts/rustbuild-setup.sh /scripts/ +RUN sh /scripts/rustbuild-setup.sh +USER rustbuild +WORKDIR /tmp + +COPY host-x86_64/dist-riscv64-linux/build-toolchains.sh host-x86_64/dist-riscv64-linux/riscv64-unknown-linux-gnu.config /tmp/ +RUN ./build-toolchains.sh + +USER root + +COPY scripts/sccache.sh /scripts/ +RUN sh /scripts/sccache.sh + +ENV PATH=$PATH:/x-tools/riscv64-unknown-linux-gnu/bin + +ENV CC_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-gcc \ + AR_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-ar \ + CXX_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-g++ + +ENV HOSTS=riscv64gc-unknown-linux-gnu + +ENV RUST_CONFIGURE_ARGS --enable-extended --disable-docs +ENV SCRIPT python3 ../x.py dist --target $HOSTS --host $HOSTS diff --git a/src/ci/docker/host-x86_64/dist-various-1/build-riscv-toolchain.sh b/src/ci/docker/host-x86_64/dist-riscv64-linux/build-toolchains.sh similarity index 75% rename from src/ci/docker/host-x86_64/dist-various-1/build-riscv-toolchain.sh rename to src/ci/docker/host-x86_64/dist-riscv64-linux/build-toolchains.sh index 9cb5700b3b6..6a7c022d01a 100755 --- a/src/ci/docker/host-x86_64/dist-various-1/build-riscv-toolchain.sh +++ b/src/ci/docker/host-x86_64/dist-riscv64-linux/build-toolchains.sh @@ -19,9 +19,9 @@ exit 1 set -x } -mkdir -p /tmp/build-riscv -cp riscv64-unknown-linux-gnu.config /tmp/build-riscv/.config -cd /tmp/build-riscv +mkdir build +cd build +cp ../riscv64-unknown-linux-gnu.config .config hide_output ct-ng build cd .. -rm -rf build-riscv +rm -rf build diff --git a/src/ci/docker/host-x86_64/dist-various-1/crosstool-ng.sh b/src/ci/docker/host-x86_64/dist-riscv64-linux/crosstool-ng.sh old mode 100755 new mode 100644 similarity index 96% rename from src/ci/docker/host-x86_64/dist-various-1/crosstool-ng.sh rename to src/ci/docker/host-x86_64/dist-riscv64-linux/crosstool-ng.sh index b01fdd0bf65..fb067a79a5c --- a/src/ci/docker/host-x86_64/dist-various-1/crosstool-ng.sh +++ b/src/ci/docker/host-x86_64/dist-riscv64-linux/crosstool-ng.sh @@ -1,4 +1,3 @@ -#!/bin/bash set -ex # Mirrored from https://github.com/crosstool-ng/crosstool-ng/archive/crosstool-ng-1.24.0.tar.gz diff --git a/src/ci/docker/host-x86_64/dist-various-1/riscv64-unknown-linux-gnu.config b/src/ci/docker/host-x86_64/dist-riscv64-linux/riscv64-unknown-linux-gnu.config similarity index 99% rename from src/ci/docker/host-x86_64/dist-various-1/riscv64-unknown-linux-gnu.config rename to src/ci/docker/host-x86_64/dist-riscv64-linux/riscv64-unknown-linux-gnu.config index dd06065b197..dbb4be550dd 100644 --- a/src/ci/docker/host-x86_64/dist-various-1/riscv64-unknown-linux-gnu.config +++ b/src/ci/docker/host-x86_64/dist-riscv64-linux/riscv64-unknown-linux-gnu.config @@ -17,8 +17,6 @@ CT_CONFIGURE_has_gnu_m4_1_4_12_or_newer=y CT_CONFIGURE_has_python_3_4_or_newer=y CT_CONFIGURE_has_bison_2_7_or_newer=y CT_CONFIGURE_has_python=y -CT_CONFIGURE_has_dtc=y -CT_CONFIGURE_has_svn=y CT_CONFIGURE_has_git=y CT_CONFIGURE_has_md5sum=y CT_CONFIGURE_has_sha1sum=y diff --git a/src/ci/docker/host-x86_64/dist-various-1/Dockerfile b/src/ci/docker/host-x86_64/dist-various-1/Dockerfile index 87bab78f796..ac228cfe01d 100644 --- a/src/ci/docker/host-x86_64/dist-various-1/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-various-1/Dockerfile @@ -47,18 +47,6 @@ RUN add-apt-repository ppa:team-gcc-arm-embedded/ppa && \ apt-get update && \ apt-get install -y --no-install-recommends gcc-arm-embedded -COPY scripts/rustbuild-setup.sh host-x86_64/dist-various-1/build-riscv-toolchain.sh host-x86_64/dist-various-1/riscv64-unknown-linux-gnu.config host-x86_64/dist-various-1/crosstool-ng.sh /build/ -RUN ./crosstool-ng.sh - -# Crosstool-ng will refuse to build as root -RUN sh ./rustbuild-setup.sh -USER rustbuild - -RUN ./build-riscv-toolchain.sh - -USER root -ENV PATH=/x-tools/riscv64-unknown-linux-gnu/bin:$PATH - COPY host-x86_64/dist-various-1/build-rumprun.sh /build RUN ./build-rumprun.sh @@ -158,7 +146,6 @@ ENV TARGETS=$TARGETS,riscv32imc-unknown-none-elf ENV TARGETS=$TARGETS,riscv32imac-unknown-none-elf ENV TARGETS=$TARGETS,riscv64imac-unknown-none-elf ENV TARGETS=$TARGETS,riscv64gc-unknown-none-elf -ENV TARGETS=$TARGETS,riscv64gc-unknown-linux-gnu ENV TARGETS=$TARGETS,armebv7r-none-eabi ENV TARGETS=$TARGETS,armebv7r-none-eabihf ENV TARGETS=$TARGETS,armv7r-none-eabi @@ -186,9 +173,6 @@ ENV CC_mipsel_unknown_linux_musl=mipsel-openwrt-linux-gcc \ CFLAGS_aarch64_unknown_none_softfloat=-mstrict-align -march=armv8-a+nofp+nosimd \ CC_aarch64_unknown_none=aarch64-none-elf-gcc \ CFLAGS_aarch64_unknown_none=-mstrict-align -march=armv8-a+fp+simd \ - CC_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-gcc \ - AR_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-ar \ - CXX_riscv64gc_unknown_linux_gnu=riscv64-unknown-linux-gnu-g++ \ CC_riscv32i_unknown_none_elf=false \ CC_riscv32imc_unknown_none_elf=false \ CC_riscv32imac_unknown_none_elf=false \ diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 0614d5e31d1..06cda99a3fe 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -341,6 +341,9 @@ jobs: - name: dist-powerpc64le-linux <<: *job-linux-xl + - name: dist-riscv64-linux + <<: *job-linux-xl + - name: dist-s390x-linux <<: *job-linux-xl diff --git a/src/librustc_llvm/build.rs b/src/librustc_llvm/build.rs index d25f8bd1b8c..5145b3b5e6f 100644 --- a/src/librustc_llvm/build.rs +++ b/src/librustc_llvm/build.rs @@ -275,6 +275,11 @@ fn main() { "stdc++" }; + // RISC-V requires libatomic for sub-word atomic operations + if target.starts_with("riscv") { + println!("cargo:rustc-link-lib=atomic"); + } + // C++ runtime library if !target.contains("msvc") { if let Some(s) = llvm_static_stdcpp {