Skip to content

Commit

Permalink
chore: Use as provided by mingw on Windows (#387)
Browse files Browse the repository at this point in the history
Previously, we were using clang (as a preprocessor) & yasm to
transform assembly source code on Windows. On the one hand, this is
really a hack solution to build a GAS-coupled assembly source file, on
the other hand, yasm hasn't seen a proper release in years, making us
believe that yasm is slowly becoming unmaintained. This change
switches to use mingw when building the assembly source code, mingw is
actively maintained now, which will reduce any problems we might run
into.

Note that we only activate the new logic when target environment is
`msvc`, for `x86_64-pc-windows-gnu` target, current logic would
already work flawlessly.
  • Loading branch information
xxuejie authored Oct 7, 2023
1 parent 5a29e43 commit acb7767
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 62 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,16 @@ jobs:
run: make ci-asm

windows-x86-ci-asm:
# We must use windows-2019 here, otherwise we will encounter some bugs, for futher details, see
# https://github.com/yasm/yasm/issues/153
runs-on: windows-2019
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
shell: pwsh
# https://github.com/ScoopInstaller/Install#for-admin
run: |
iex "& {$(irm get.scoop.sh)} -RunAsAdmin"
scoop install llvm
scoop install yasm
scoop install mingw
- name: Run ci-asm
shell: pwsh
run: |
$env:path="C:\Users\runneradmin\scoop\apps\llvm\current;"+$env:path
$env:path="C:\Users\runneradmin\scoop\apps\yasm\current;"+$env:path
make ci-asm
67 changes: 12 additions & 55 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ fn main() {

let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
let is_windows = target_family == "windows";
let is_msvc = is_windows && (target_env == "msvc");
let is_unix = target_family == "unix";
let is_x86_64 = target_arch == "x86_64";
let is_aarch64 = target_arch == "aarch64";
Expand All @@ -28,63 +30,18 @@ fn main() {
println!("cargo:rerun-if-changed=src/machine/asm/execute_aarch64.S");
println!("cargo:rerun-if-changed=src/machine/asm/cdefinitions_generated.h");

use cc::Build;
use std::path::Path;
use std::process::Command;
let mut build = cc::Build::new();

fn run_command(mut c: Command) {
println!("Running Command[{:?}]", c);

let output = c.output().unwrap_or_else(|e| {
panic!("Error running Command[{:?}], error: {:?}", c, e);
});

if !output.status.success() {
use std::io::{self, Write};
io::stdout()
.write_all(&output.stdout)
.expect("stdout write");
io::stderr()
.write_all(&output.stderr)
.expect("stderr write");

panic!(
"Command[{:?}] exits with non-success status: {:?}",
c, output.status
);
}
}

let mut build = Build::new();

if is_windows && x64_asm {
let out_dir = env::var("OUT_DIR").unwrap();
let expand_path = Path::new(&out_dir).join("execute_x64-expanded.S");
let mut expand_command = Command::new("clang");
expand_command
.arg("-E")
.arg("src/machine/asm/execute_x64.S")
.arg("-o")
.arg(&expand_path);
run_command(expand_command);

let compile_path = Path::new(&out_dir).join("execute_x64.o");
let mut compile_command = Command::new("yasm");
compile_command
.arg("-p")
.arg("gas")
.arg("-f")
.arg("x64")
.arg("-m")
.arg("amd64")
.arg(&expand_path)
.arg("-o")
.arg(&compile_path);
run_command(compile_command);

build.object(&compile_path);
} else if x64_asm {
if x64_asm {
build.file("src/machine/asm/execute_x64.S");
if is_msvc {
// For now, only an assembly source code is required for CKB-VM, we won't
// need to build any C source file here. Hence we can use this simpler solution
// to set the default compiler to GCC. We will need to manually trigger the
// command to assemble the assembly code file, should any C source file is also
// required here.
build.compiler("gcc");
}
} else if aarch64_asm {
build.file("src/machine/asm/execute_aarch64.S");
}
Expand Down

0 comments on commit acb7767

Please sign in to comment.