From dfe008566abe7baf14c7ed7b3f280a9e91a0aea4 Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Mon, 11 Mar 2024 02:23:14 +0100 Subject: [PATCH] Add new NASM pragma regarding macro parameter expansion (#3361) * Add new pragma and bump NASM min_version 2.15.0 was released in June 2020. The current MSRV was released in June 2023, so this should not be a problem. * Refactor creation of config.h/config.asm * Refactor nasm-rs usage --- build.rs | 76 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/build.rs b/build.rs index 87987fcf89..0007dd1d36 100644 --- a/build.rs +++ b/build.rs @@ -70,22 +70,25 @@ fn hash_changed( #[cfg(feature = "asm")] fn build_nasm_files() { - use std::fs::File; - use std::io::Write; - let out_dir = env::var("OUT_DIR").unwrap(); + let mut config = " +%pragma preproc sane_empty_expansion true +%define private_prefix rav1e +%define ARCH_X86_32 0 +%define ARCH_X86_64 1 +%define PIC 1 +%define STACK_ALIGNMENT 16 +%define HAVE_AVX512ICL 1 +" + .to_owned(); - let dest_path = Path::new(&out_dir).join("config.asm"); - let mut config_file = File::create(&dest_path).unwrap(); - config_file.write(b" %define private_prefix rav1e\n").unwrap(); - config_file.write(b" %define ARCH_X86_32 0\n").unwrap(); - config_file.write(b" %define ARCH_X86_64 1\n").unwrap(); - config_file.write(b" %define PIC 1\n").unwrap(); - config_file.write(b" %define STACK_ALIGNMENT 16\n").unwrap(); - config_file.write(b" %define HAVE_AVX512ICL 1\n").unwrap(); if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" { - config_file.write(b" %define PREFIX 1\n").unwrap(); + config += "%define PREFIX 1\n"; } + let out_dir = env::var("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("config.asm"); + std::fs::write(&dest_path, config).expect("can write config.asm"); + let asm_files = &[ "src/x86/cdef_avx2.asm", "src/x86/cdef_avx512.asm", @@ -132,20 +135,17 @@ fn build_nasm_files() { if let Some((hash, hash_path)) = hash_changed(asm_files, &out_dir, &dest_path) { - let mut config_include_arg = String::from("-I"); - config_include_arg.push_str(&out_dir); - config_include_arg.push('/'); - let mut nasm = nasm_rs::Build::new(); - nasm.min_version(2, 14, 0); - for file in asm_files { - nasm.file(file); - } - nasm.flag(&config_include_arg); - nasm.flag("-Isrc/"); - let obj = nasm.compile_objects().unwrap_or_else(|e| { - println!("cargo:warning={e}"); - panic!("NASM build failed. Make sure you have nasm installed or disable the \"asm\" feature.\n\ - You can get NASM from https://nasm.us or your system's package manager.\n\nerror: {e}"); + let obj = nasm_rs::Build::new() + .min_version(2, 15, 0) + .include(&out_dir) + .include("src") + .files(asm_files) + .compile_objects() + .unwrap_or_else(|e| { + panic!("NASM build failed. Make sure you have nasm installed or disable the \"asm\" feature.\n\ + You can get NASM from https://nasm.us or your system's package manager.\n\ + \n\ + error: {e}"); }); // cc is better at finding the correct archiver @@ -196,21 +196,21 @@ fn strip_command() -> Option { #[cfg(feature = "asm")] fn build_asm_files() { - use std::fs::File; - use std::io::Write; - let out_dir = env::var("OUT_DIR").unwrap(); + let mut config = " +#define PRIVATE_PREFIX rav1e_ +#define ARCH_AARCH64 1 +#define ARCH_ARM 0 +#define CONFIG_LOG 1 +#define HAVE_ASM 1 +" + .to_owned(); - let dest_path = Path::new(&out_dir).join("config.h"); - let mut config_file = File::create(&dest_path).unwrap(); if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" { - config_file.write(b" #define PREFIX 1\n").unwrap(); + config += "#define PREFIX 1\n"; } - config_file.write(b" #define PRIVATE_PREFIX rav1e_\n").unwrap(); - config_file.write(b" #define ARCH_AARCH64 1\n").unwrap(); - config_file.write(b" #define ARCH_ARM 0\n").unwrap(); - config_file.write(b" #define CONFIG_LOG 1 \n").unwrap(); - config_file.write(b" #define HAVE_ASM 1\n").unwrap(); - config_file.sync_all().unwrap(); + let out_dir = env::var("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("config.h"); + std::fs::write(&dest_path, config).expect("can write config.h"); let asm_files = &[ "src/arm/64/cdef.S",