Skip to content

Commit

Permalink
build.rs: Clarify logic regarding nasm vs. non-nasm assembly.
Browse files Browse the repository at this point in the history
Simplify `AsmTarget` and clarify the logic regarding when and how
to use nasm.
  • Loading branch information
briansmith committed Feb 28, 2024
1 parent b873b0d commit 4f5276e
Showing 1 changed file with 20 additions and 43 deletions.
63 changes: 20 additions & 43 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,64 +158,46 @@ const ASM_TARGETS: &[AsmTarget] = &[
oss: LINUX_ABI,
arch: AARCH64,
perlasm_format: "linux64",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: ARM,
perlasm_format: "linux32",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: X86,
perlasm_format: "elf",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: LINUX_ABI,
arch: X86_64,
perlasm_format: "elf",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: MACOS_ABI,
arch: AARCH64,
perlasm_format: "ios64",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: MACOS_ABI,
arch: X86_64,
perlasm_format: "macosx",
asm_extension: "S",
preassemble: false,
},
AsmTarget {
oss: &[WINDOWS],
arch: X86,
perlasm_format: "win32n",
asm_extension: "asm",
preassemble: true,
perlasm_format: WIN32N,
},
AsmTarget {
oss: &[WINDOWS],
arch: X86_64,
perlasm_format: "nasm",
asm_extension: "asm",
preassemble: true,
perlasm_format: NASM,
},
AsmTarget {
oss: &[WINDOWS],
arch: AARCH64,
perlasm_format: "win64",
asm_extension: "S",
preassemble: false,
},
];

Expand All @@ -228,16 +210,12 @@ struct AsmTarget {

/// The PerlAsm format name.
perlasm_format: &'static str,
}

/// The filename extension for assembly files.
asm_extension: &'static str,

/// Whether pre-assembled object files should be included in the Cargo
/// package instead of the asm sources. This way, the user doesn't need
/// to install an assembler for the target. This is particularly important
/// for x86/x86_64 Windows since an assembler doesn't come with the C
/// compiler.
preassemble: bool,
impl AsmTarget {
fn use_nasm(&self) -> bool {
[WIN32N, NASM].contains(&self.perlasm_format)
}
}

/// Operating systems that have the same ABI as Linux on every architecture
Expand All @@ -256,6 +234,9 @@ const LINUX_ABI: &[&str] = &[
"solaris",
];

const WIN32N: &str = "win32n";
const NASM: &str = "nasm";

/// Operating systems that have the same ABI as macOS on every architecture
/// mentioned in `ASM_TARGETS`.
const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
Expand Down Expand Up @@ -362,16 +343,14 @@ fn generate_sources_and_preassemble<'a>(
let perl_exe = get_perl_exe();

for asm_target in asm_targets {
// For Windows, package pregenerated object files in addition to
// pregenerated assembly language source files, so that the user
// doesn't need to install the assembler.

let perlasm_src_dsts = perlasm_src_dsts(out_dir, asm_target);
perlasm(&perl_exe, &perlasm_src_dsts, asm_target);

if asm_target.preassemble {
if asm_target.use_nasm() {
// Package pregenerated object files in addition to pregenerated
// assembly language source files, so that the user doesn't need
// to install the assembler.
let srcs = asm_srcs(perlasm_src_dsts);

for src in srcs {
nasm(&src, asm_target.arch, out_dir, out_dir);
}
Expand Down Expand Up @@ -407,9 +386,9 @@ fn build_c_code(

let asm_srcs = asm_srcs(perlasm_src_dsts);

// For Windows we also pregenerate the object files for non-Git builds so
// the user doesn't need to install the assembler.
if asm_target.preassemble {
if asm_target.use_nasm() {
// Nasm was already used to generate the object files, so use them instead of
// assembling.
let obj_srcs = asm_srcs
.iter()
.map(|src| obj_path(generated_dir, src.as_path()))
Expand Down Expand Up @@ -657,11 +636,9 @@ fn asm_path(out_dir: &Path, src: &Path, asm_target: &AsmTarget) -> PathBuf {
let src_stem = src.file_stem().expect("source file without basename");

let dst_stem = src_stem.to_str().unwrap();
let dst_filename = format!(
"{}-{}.{}",
dst_stem, asm_target.perlasm_format, asm_target.asm_extension
);
out_dir.join(dst_filename)
let dst_filename = format!("{}-{}", dst_stem, asm_target.perlasm_format);
let extension = if asm_target.use_nasm() { "asm" } else { "S" };
out_dir.join(dst_filename).with_extension(extension)
}

fn perlasm(perl_exe: &Path, src_dst: &[(PathBuf, PathBuf)], asm_target: &AsmTarget) {
Expand Down

0 comments on commit 4f5276e

Please sign in to comment.