Skip to content

Commit 75eae54

Browse files
authored
Support +crt-static through cargo xwin (#938)
* Improve handling of +crt-static builds * Fix clippy * Also host cargo-xwin on Windows
1 parent 61b520d commit 75eae54

4 files changed

Lines changed: 94 additions & 29 deletions

File tree

.github/workflows/cross.yml

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,29 +246,74 @@ jobs:
246246

247247
cargo-xwin:
248248
if: github.repository_owner == 'aws'
249-
runs-on: ubuntu-latest
249+
runs-on: ${{ matrix.host }}
250+
strategy:
251+
fail-fast: false
252+
matrix:
253+
host:
254+
- ubuntu-latest
255+
- macos-latest
256+
- windows-latest
257+
- windows-11-arm
258+
target:
259+
- "x86_64-pc-windows-msvc"
260+
- "aarch64-pc-windows-msvc"
261+
crt_static:
262+
- "0"
263+
- "1"
250264
steps:
251265
- name: Install build dependencies
252-
# cargo-xwin apparently require ninja-build
266+
if: matrix.host == 'ubuntu-latest'
267+
# cargo-xwin apparently requires ninja-build
253268
run: |
254-
sudo apt-get update && sudo apt-get install --assume-yes nasm clang ninja-build llvm
269+
sudo apt-get update && sudo apt-get install --assume-yes nasm ninja-build llvm
270+
- name: Install build dependencies
271+
if: matrix.host == 'macos-latest'
272+
run: |
273+
brew install nasm ninja llvm
255274
- uses: actions/checkout@v3
256275
with:
257276
submodules: "recursive"
258277
- uses: dtolnay/rust-toolchain@master
259278
with:
260279
toolchain: stable
261-
target: x86_64-pc-windows-msvc
262-
- uses: dtolnay/rust-toolchain@master
263-
with:
264-
toolchain: stable
265-
target: aarch64-pc-windows-msvc
266-
- name: Install cargo-xwin and bindgen-cli
267-
run: cargo install --locked cargo-xwin bindgen-cli
268-
- name: cargo xwin build for `x86_64-pc-windows-msvc`
269-
run: cargo xwin build -p aws-lc-rs --release --all-targets --target x86_64-pc-windows-msvc
270-
- name: cargo xwin build for `aarch64-pc-windows-msvc`
271-
run: cargo xwin build -p aws-lc-rs --release --all-targets --target aarch64-pc-windows-msvc
280+
target: ${{ matrix.target }}
281+
- name: Install cargo-xwin
282+
run: cargo install --locked cargo-xwin
283+
- name: Set RUSTFLAGS
284+
if: contains(matrix.host, 'windows') != true
285+
run: |
286+
if [[ '${{ matrix.crt_static }}' == '1' ]]; then
287+
echo "RUSTFLAGS=-Clink-arg=/WX -Ctarget-feature=+crt-static" >> $GITHUB_ENV
288+
else
289+
echo "RUSTFLAGS=-Clink-arg=/WX" >> $GITHUB_ENV
290+
fi
291+
echo "EXTRA_FLAGS=" >> $GITHUB_ENV
292+
- name: Set RUSTFLAGS
293+
if: contains(matrix.host, 'windows')
294+
run: |
295+
if ('${{ matrix.crt_static }}' -eq '1') {
296+
echo "RUSTFLAGS=-Clink-arg=/WX -Ctarget-feature=+crt-static" | Out-File -FilePath $env:GITHUB_ENV -Append
297+
} else {
298+
echo "RUSTFLAGS=-Clink-arg=/WX" | Out-File -FilePath $env:GITHUB_ENV -Append
299+
}
300+
if ('${{ matrix.host }}' -like '*arm*') {
301+
echo "EXTRA_FLAGS=--features=prebuilt-nasm" | Out-File -FilePath $env:GITHUB_ENV -Append
302+
} else {
303+
echo "EXTRA_FLAGS=" | Out-File -FilePath $env:GITHUB_ENV -Append
304+
}
305+
- name: cargo xwin build for ${{ matrix.target }}
306+
run: cargo xwin build -p aws-lc-rs --release --all-targets --target ${{ matrix.target }} ${{ env.EXTRA_FLAGS }}
307+
- name: cargo test for x86_64-pc-windows-msvc
308+
if: matrix.host == 'windows-latest' && matrix.target == 'x86_64-pc-windows-msvc'
309+
env:
310+
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER: 'cmd.exe'
311+
run: cargo xwin test -p aws-lc-rs --release --all-targets --target x86_64-pc-windows-msvc ${{ env.EXTRA_FLAGS }}
312+
- name: cargo test for aarch64-pc-windows-msvc
313+
if: matrix.host == 'windows-11-arm' && matrix.target == 'aarch64-pc-windows-msvc'
314+
env:
315+
CARGO_TARGET_AARCH64_PC_WINDOWS_MSVC_RUNNER: 'cmd.exe'
316+
run: cargo xwin test -p aws-lc-rs --release --all-targets --target aarch64-pc-windows-msvc ${{ env.EXTRA_FLAGS }}
272317

273318
aws-lc-rs-windows-msvc:
274319
if: ${{ github.repository_owner == 'aws' }}

aws-lc-rs/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ fn main() {
3434
);
3535
};
3636

37+
// When using static CRT on Windows MSVC, ignore missing PDB file warnings
38+
// The static CRT libraries reference PDB files from Microsoft's build servers
39+
// which are not available during linking
40+
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows")
41+
&& env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc")
42+
&& env::var("CARGO_CFG_TARGET_FEATURE")
43+
.map(|features| features.contains("crt-static"))
44+
.unwrap_or(false)
45+
{
46+
println!("cargo:rustc-link-arg=/ignore:4099");
47+
}
48+
3749
export_sys_vars(sys_crate);
3850
}
3951

aws-lc-sys/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33

44
cmake_minimum_required(VERSION 3.5...3.31)
55

6-
if(CMAKE_VERSION VERSION_GREATER "3.14")
7-
# https://cmake.org/cmake/help/latest/policy/CMP0091.html
8-
# In CMake 3.14 and below, MSVC runtime library selection flags are added to the default CMAKE_<LANG>_FLAGS_<CONFIG>
9-
# cache entries by CMake automatically.
10-
cmake_policy(SET CMP0091 OLD)
11-
endif()
12-
136
project(AWS_LC_RUST NONE)
147
enable_language(C)
158

aws-lc-sys/builder/cmake_builder.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
use std::env;
1313
use std::ffi::OsString;
1414
use std::path::{Path, PathBuf};
15+
use std::str::FromStr;
1516

1617
pub(crate) struct CmakeBuilder {
1718
manifest_dir: PathBuf,
@@ -164,12 +165,29 @@ impl CmakeBuilder {
164165
// are disabled.
165166
Self::preserve_cflag_optimization_flags(&mut cmake_cfg);
166167

167-
// Allow environment to specify CMake toolchain.
168-
if let Some(toolchain) = optional_env_optional_crate_target("CMAKE_TOOLCHAIN_FILE") {
169-
set_env_for_target("CMAKE_TOOLCHAIN_FILE", toolchain);
168+
if target_os() == "windows" {
170169
if use_prebuilt_nasm() {
171170
self.configure_prebuilt_nasm(&mut cmake_cfg);
172171
}
172+
if target_env().as_str() == "msvc" {
173+
let mut msvcrt = String::from_str("MultiThreaded").unwrap();
174+
if is_crt_static() {
175+
cmake_cfg.static_crt(true);
176+
// When using static CRT on Windows MSVC, ignore missing PDB file warnings
177+
// The static CRT libraries reference PDB files from Microsoft's build servers
178+
// which are not available.
179+
println!("cargo:rustc-link-arg=/ignore:4099");
180+
} else {
181+
msvcrt.push_str("DLL");
182+
}
183+
cmake_cfg.define("CMAKE_MSVC_RUNTIME_LIBRARY", msvcrt.as_str());
184+
}
185+
}
186+
187+
// Allow environment to specify CMake toolchain.
188+
if let Some(toolchain) = optional_env_optional_crate_target("CMAKE_TOOLCHAIN_FILE") {
189+
set_env_for_target("CMAKE_TOOLCHAIN_FILE", toolchain);
190+
173191
return cmake_cfg;
174192
}
175193
// We only consider compiler CFLAGS when no cmake toolchain is set
@@ -286,6 +304,7 @@ impl CmakeBuilder {
286304
}
287305
}
288306

307+
#[allow(clippy::unused_self)]
289308
fn configure_windows(&self, cmake_cfg: &mut cmake::Config) {
290309
match (target_env().as_str(), target_arch().as_str()) {
291310
("msvc", "aarch64") => {
@@ -303,21 +322,17 @@ impl CmakeBuilder {
303322
));
304323
cmake_cfg.define("CMAKE_GENERATOR_PLATFORM", "ARM64");
305324
}
306-
cmake_cfg.static_crt(is_crt_static());
307325
cmake_cfg.define("CMAKE_SYSTEM_NAME", "Windows");
308326
cmake_cfg.define("CMAKE_SYSTEM_PROCESSOR", "ARM64");
309327
}
310328
("msvc", _) => {
311-
cmake_cfg.static_crt(is_crt_static());
329+
// No-op
312330
}
313331
(_, arch) => {
314332
cmake_cfg.define("CMAKE_SYSTEM_NAME", "Windows");
315333
cmake_cfg.define("CMAKE_SYSTEM_PROCESSOR", arch);
316334
}
317335
}
318-
if use_prebuilt_nasm() {
319-
self.configure_prebuilt_nasm(cmake_cfg);
320-
}
321336
}
322337

323338
fn configure_prebuilt_nasm(&self, cmake_cfg: &mut cmake::Config) {

0 commit comments

Comments
 (0)