Skip to content

Commit

Permalink
fix llvm-ar as archiver for msvc targets; fix clang-cl detection; fix…
Browse files Browse the repository at this point in the history
… assembler output flag detection; add clang/clang-cl windows CI (#1015)
  • Loading branch information
russelltg committed Apr 7, 2024
1 parent be62f4a commit fd912ec
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 130 deletions.
194 changes: 116 additions & 78 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [stable, beta, nightly, linux32, macos, aarch64-macos, aarch64-ios, win32, win64, mingw32, mingw64, windows-2019]
build:
[
stable,
beta,
nightly,
linux32,
macos,
aarch64-macos,
aarch64-ios,
win32,
win64,
mingw32,
mingw64,
windows-2019,
]
include:
- build: stable
os: ubuntu-latest
Expand Down Expand Up @@ -72,29 +86,53 @@ jobs:
os: windows-2019
rust: stable-x86_64
target: x86_64-pc-windows-msvc
- build: windows-clang
os: windows-2019
rust: stable
target: x86_64-pc-windows-msvc
CC: clang
CXX: clang++
- build: windows-clang-cl
os: windows-2019
rust: stable
target: x86_64-pc-windows-msvc
CC: clang-cl
CXX: clang-cl
steps:
- uses: actions/checkout@v4
- name: Install Rust (rustup)
run: |
set -euxo pipefail
rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal --target ${{ matrix.target }}
rustup default ${{ matrix.rust }}
shell: bash
- name: Install g++-multilib
run: |
set -e
# Remove the ubuntu-toolchain-r/test PPA, which is added by default.
# Some packages were removed, and this is causing the g++multilib
# install to fail. Similar issue:
# https://github.com/scikit-learn/scikit-learn/issues/13928.
sudo add-apt-repository --remove ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-multilib
if: matrix.build == 'linux32'
- uses: Swatinem/rust-cache@v2
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }}
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --release
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --features parallel
- uses: actions/checkout@v4
- name: Install Rust (rustup)
run: |
set -euxo pipefail
rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal --target ${{ matrix.target }}
rustup default ${{ matrix.rust }}
shell: bash
- name: Install g++-multilib
run: |
set -e
# Remove the ubuntu-toolchain-r/test PPA, which is added by default.
# Some packages were removed, and this is causing the g++multilib
# install to fail. Similar issue:
# https://github.com/scikit-learn/scikit-learn/issues/13928.
sudo add-apt-repository --remove ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-multilib
if: matrix.build == 'linux32'
- name: add clang to path
if: startsWith(matrix.build, 'windows-clang')
run: |
echo "C:\msys64\mingw64\bin" >> "$GITHUB_PATH"
echo -e "AR=llvm-ar\nRUSTFLAGS=-Clinker=lld-link\nCC=${CC}\nCXX=${CXX}" >> "$GITHUB_ENV"
shell: bash
env:
CC: ${{ matrix.CC }}
CXX: ${{ matrix.CXX }}
- name: setup dev environment
uses: ilammy/msvc-dev-cmd@v1
if: startsWith(matrix.build, 'windows-clang')
- uses: Swatinem/rust-cache@v2
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }}
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --release
- run: cargo test ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --features parallel

# This is separate from the matrix above because there is no prebuilt rust-std component for these targets.
check-tvos:
Expand All @@ -120,37 +158,37 @@ jobs:
target: x86_64-apple-tvos
no_run: --no-run
steps:
- uses: actions/checkout@v4
- name: Install Rust (rustup)
run: |
set -euxo pipefail
rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal
rustup component add rust-src --toolchain ${{ matrix.rust }}
rustup default ${{ matrix.rust }}
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }}
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --release
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --features parallel
- uses: actions/checkout@v4
- name: Install Rust (rustup)
run: |
set -euxo pipefail
rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal
rustup component add rust-src --toolchain ${{ matrix.rust }}
rustup default ${{ matrix.rust }}
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }}
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --release
- run: cargo test -Z build-std=std ${{ matrix.no_run }} --workspace --target ${{ matrix.target }} --features parallel

cuda:
name: Test CUDA support
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- name: Install cuda-minimal-build-11-8
shell: bash
run: |
# https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=20.04&target_type=deb_network
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-minimal-build-11-8
- uses: Swatinem/rust-cache@v2
- name: Test 'cudart' feature
shell: bash
run: |
PATH="/usr/local/cuda/bin:$PATH" cargo test --manifest-path dev-tools/cc-test/Cargo.toml --features test_cuda
- uses: actions/checkout@v4
- name: Install cuda-minimal-build-11-8
shell: bash
run: |
# https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=20.04&target_type=deb_network
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-minimal-build-11-8
- uses: Swatinem/rust-cache@v2
- name: Test 'cudart' feature
shell: bash
run: |
PATH="/usr/local/cuda/bin:$PATH" cargo test --manifest-path dev-tools/cc-test/Cargo.toml --features test_cuda
msrv:
name: MSRV
Expand All @@ -160,42 +198,42 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install 1.53.0 --no-self-update --profile minimal
rustup toolchain install nightly --no-self-update --profile minimal
rustup default 1.53.0
shell: bash
- name: Create Cargo.lock with minimal version
run: cargo +nightly update -Zminimal-versions
- name: Cache downloaded crates since 1.53 is really slow in fetching
uses: Swatinem/rust-cache@v2
- run: cargo check --lib -p cc --locked
- run: cargo check --lib -p cc --locked --all-features
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install 1.53.0 --no-self-update --profile minimal
rustup toolchain install nightly --no-self-update --profile minimal
rustup default 1.53.0
shell: bash
- name: Create Cargo.lock with minimal version
run: cargo +nightly update -Zminimal-versions
- name: Cache downloaded crates since 1.53 is really slow in fetching
uses: Swatinem/rust-cache@v2
- run: cargo check --lib -p cc --locked
- run: cargo check --lib -p cc --locked --all-features

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install stable --no-self-update --profile minimal --component rustfmt
rustup default stable
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo clippy
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install stable --no-self-update --profile minimal --component rustfmt
rustup default stable
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo clippy

rustfmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install stable --no-self-update --profile minimal --component rustfmt
rustup default stable
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo fmt -- --check
- uses: actions/checkout@v4
- name: Install Rust
run: |
rustup toolchain install stable --no-self-update --profile minimal --component rustfmt
rustup default stable
shell: bash
- uses: Swatinem/rust-cache@v2
- run: cargo fmt -- --check
20 changes: 18 additions & 2 deletions dev-tools/cc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ fn main() {
run_forked_capture_output(&out, "warnings-on");
}

cc::Build::new()
let mut build = cc::Build::new();
build
.file("src/foo.c")
.flag_if_supported("-Wall")
.flag_if_supported("-Wfoo-bar-this-flag-does-not-exist")
.define("FOO", None)
.define("BAR", "1")
.compile("foo");

let compiler = build.get_compiler();

cc::Build::new()
.file("src/bar1.c")
.file("src/bar2.c")
Expand Down Expand Up @@ -84,6 +87,14 @@ fn main() {
// nmake which runs vanilla cl, and then also test it after we remove all
// the relevant env vars from our own process.
if target.contains("msvc") {
let cc_frontend = if compiler.is_like_msvc() {
"MSVC"
} else if compiler.is_like_clang() {
"CLANG"
} else {
unimplemented!("Unknown compiler that targets msvc but isn't clang-like or msvc-like")
};

let out = out.join("tmp");
fs::create_dir(&out).unwrap();
println!("nmake 1");
Expand All @@ -92,14 +103,18 @@ fn main() {
.env_remove("MAKEFLAGS")
.arg("/fsrc/NMakefile")
.env("OUT_DIR", &out)
.env("CC_FRONTEND", cc_frontend)
.status()
.unwrap();
assert!(status.success());

fs::remove_dir_all(&out).unwrap();
fs::create_dir(&out).unwrap();

env::remove_var("PATH");
// windows registry won't find clang in path
if !compiler.path().to_string_lossy().starts_with("clang") {
env::remove_var("PATH");
}
env::remove_var("VCINSTALLDIR");
env::remove_var("INCLUDE");
env::remove_var("LIB");
Expand All @@ -109,6 +124,7 @@ fn main() {
.env_remove("MAKEFLAGS")
.arg("/fsrc/NMakefile")
.env("OUT_DIR", &out)
.env("CC_FRONTEND", cc_frontend)
.status()
.unwrap();
assert!(status.success());
Expand Down
14 changes: 10 additions & 4 deletions dev-tools/cc-test/src/NMakefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
all: $(OUT_DIR)/msvc.lib $(OUT_DIR)/msvc.exe

!IF "$(CC_FRONTEND)" == "MSVC"
EXTRA_CFLAGS=-nologo
CFLAG_OUTPUT=-Fo
!ELSE
CFLAG_OUTPUT=-o
!ENDIF

$(OUT_DIR)/msvc.lib: $(OUT_DIR)/msvc.o
lib -nologo -out:$(OUT_DIR)/msvc.lib $(OUT_DIR)/msvc.o
rc -h

$(OUT_DIR)/msvc.o: src/msvc.c
$(CC) -nologo -c -Fo:$@ src/msvc.c -MD
$(CC) $(EXTRA_CFLAGS) -c $(CFLAG_OUTPUT)$@ src/msvc.c -MD

$(OUT_DIR)/msvc.exe: $(OUT_DIR)/msvc2.o
$(CC) -nologo -Fo:$@ $(OUT_DIR)/msvc2.o
$(CC) $(EXTRA_CFLAGS) $(CFLAG_OUTPUT)$@ $(OUT_DIR)/msvc2.o

$(OUT_DIR)/msvc2.o: src/msvc.c
$(CC) -nologo -c -Fo:$@ src/msvc.c -DMAIN -MD
$(CC) $(EXTRA_CFLAGS) -c $(CFLAG_OUTPUT)$@ src/msvc.c -DMAIN -MD
25 changes: 14 additions & 11 deletions src/command_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,20 @@ for help)"
}
}

pub(crate) fn command_add_output_file(
cmd: &mut Command,
dst: &Path,
cuda: bool,
msvc: bool,
clang: bool,
gnu: bool,
is_asm: bool,
is_arm: bool,
) {
if msvc && !clang && !gnu && !cuda && !(is_asm && is_arm) {
pub(crate) struct CmdAddOutputFileArgs {
pub(crate) cuda: bool,
pub(crate) is_assembler_msvc: bool,
pub(crate) msvc: bool,
pub(crate) clang: bool,
pub(crate) gnu: bool,
pub(crate) is_asm: bool,
pub(crate) is_arm: bool,
}

pub(crate) fn command_add_output_file(cmd: &mut Command, dst: &Path, args: CmdAddOutputFileArgs) {
if args.is_assembler_msvc
|| (args.msvc && !args.clang && !args.gnu && !args.cuda && !(args.is_asm && args.is_arm))
{
let mut s = OsString::from("-Fo");
s.push(dst);
cmd.arg(s);
Expand Down
4 changes: 0 additions & 4 deletions src/detect_compiler_family.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@
#ifdef __GNUC__
#pragma message "gcc"
#endif

#ifdef _MSC_VER
#pragma message "msvc"
#endif

0 comments on commit fd912ec

Please sign in to comment.