Skip to content

Commit

Permalink
Add new NASM pragma regarding macro parameter expansion (#3361)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FreezyLemon authored Mar 11, 2024
1 parent 830e09b commit dfe0085
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,21 +196,21 @@ fn strip_command() -> Option<String> {

#[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",
Expand Down

0 comments on commit dfe0085

Please sign in to comment.