Skip to content

Commit dfe0085

Browse files
authored
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
1 parent 830e09b commit dfe0085

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

build.rs

+38-38
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,25 @@ fn hash_changed(
7070

7171
#[cfg(feature = "asm")]
7272
fn build_nasm_files() {
73-
use std::fs::File;
74-
use std::io::Write;
75-
let out_dir = env::var("OUT_DIR").unwrap();
73+
let mut config = "
74+
%pragma preproc sane_empty_expansion true
75+
%define private_prefix rav1e
76+
%define ARCH_X86_32 0
77+
%define ARCH_X86_64 1
78+
%define PIC 1
79+
%define STACK_ALIGNMENT 16
80+
%define HAVE_AVX512ICL 1
81+
"
82+
.to_owned();
7683

77-
let dest_path = Path::new(&out_dir).join("config.asm");
78-
let mut config_file = File::create(&dest_path).unwrap();
79-
config_file.write(b" %define private_prefix rav1e\n").unwrap();
80-
config_file.write(b" %define ARCH_X86_32 0\n").unwrap();
81-
config_file.write(b" %define ARCH_X86_64 1\n").unwrap();
82-
config_file.write(b" %define PIC 1\n").unwrap();
83-
config_file.write(b" %define STACK_ALIGNMENT 16\n").unwrap();
84-
config_file.write(b" %define HAVE_AVX512ICL 1\n").unwrap();
8584
if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
86-
config_file.write(b" %define PREFIX 1\n").unwrap();
85+
config += "%define PREFIX 1\n";
8786
}
8887

88+
let out_dir = env::var("OUT_DIR").unwrap();
89+
let dest_path = Path::new(&out_dir).join("config.asm");
90+
std::fs::write(&dest_path, config).expect("can write config.asm");
91+
8992
let asm_files = &[
9093
"src/x86/cdef_avx2.asm",
9194
"src/x86/cdef_avx512.asm",
@@ -132,20 +135,17 @@ fn build_nasm_files() {
132135
if let Some((hash, hash_path)) =
133136
hash_changed(asm_files, &out_dir, &dest_path)
134137
{
135-
let mut config_include_arg = String::from("-I");
136-
config_include_arg.push_str(&out_dir);
137-
config_include_arg.push('/');
138-
let mut nasm = nasm_rs::Build::new();
139-
nasm.min_version(2, 14, 0);
140-
for file in asm_files {
141-
nasm.file(file);
142-
}
143-
nasm.flag(&config_include_arg);
144-
nasm.flag("-Isrc/");
145-
let obj = nasm.compile_objects().unwrap_or_else(|e| {
146-
println!("cargo:warning={e}");
147-
panic!("NASM build failed. Make sure you have nasm installed or disable the \"asm\" feature.\n\
148-
You can get NASM from https://nasm.us or your system's package manager.\n\nerror: {e}");
138+
let obj = nasm_rs::Build::new()
139+
.min_version(2, 15, 0)
140+
.include(&out_dir)
141+
.include("src")
142+
.files(asm_files)
143+
.compile_objects()
144+
.unwrap_or_else(|e| {
145+
panic!("NASM build failed. Make sure you have nasm installed or disable the \"asm\" feature.\n\
146+
You can get NASM from https://nasm.us or your system's package manager.\n\
147+
\n\
148+
error: {e}");
149149
});
150150

151151
// cc is better at finding the correct archiver
@@ -196,21 +196,21 @@ fn strip_command() -> Option<String> {
196196

197197
#[cfg(feature = "asm")]
198198
fn build_asm_files() {
199-
use std::fs::File;
200-
use std::io::Write;
201-
let out_dir = env::var("OUT_DIR").unwrap();
199+
let mut config = "
200+
#define PRIVATE_PREFIX rav1e_
201+
#define ARCH_AARCH64 1
202+
#define ARCH_ARM 0
203+
#define CONFIG_LOG 1
204+
#define HAVE_ASM 1
205+
"
206+
.to_owned();
202207

203-
let dest_path = Path::new(&out_dir).join("config.h");
204-
let mut config_file = File::create(&dest_path).unwrap();
205208
if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
206-
config_file.write(b" #define PREFIX 1\n").unwrap();
209+
config += "#define PREFIX 1\n";
207210
}
208-
config_file.write(b" #define PRIVATE_PREFIX rav1e_\n").unwrap();
209-
config_file.write(b" #define ARCH_AARCH64 1\n").unwrap();
210-
config_file.write(b" #define ARCH_ARM 0\n").unwrap();
211-
config_file.write(b" #define CONFIG_LOG 1 \n").unwrap();
212-
config_file.write(b" #define HAVE_ASM 1\n").unwrap();
213-
config_file.sync_all().unwrap();
211+
let out_dir = env::var("OUT_DIR").unwrap();
212+
let dest_path = Path::new(&out_dir).join("config.h");
213+
std::fs::write(&dest_path, config).expect("can write config.h");
214214

215215
let asm_files = &[
216216
"src/arm/64/cdef.S",

0 commit comments

Comments
 (0)