Skip to content

Commit 3d2ac24

Browse files
committed
NASM draft
1 parent 4b14c60 commit 3d2ac24

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

aws-lc-sys/builder/cc_builder.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod universal;
1515
mod win_aarch64;
1616
mod win_x86_64;
1717

18+
use crate::nasm_builder::NasmBuilder;
1819
use crate::{
1920
cargo_env, emit_warning, env_var_to_bool, execute_command, get_crate_cflags, is_no_asm,
2021
optional_env_optional_crate_target, optional_env_target, out_dir, requested_c_std,
@@ -137,6 +138,15 @@ impl BuildOption {
137138
}
138139
}
139140
}
141+
142+
pub(crate) fn apply_nasm<'a>(&self, nasm_builder: &'a mut NasmBuilder) -> &'a mut NasmBuilder {
143+
match self {
144+
BuildOption::FLAG(val) => nasm_builder.flag(val),
145+
BuildOption::DEFINE(key, val) => nasm_builder.define(key, Some(val.as_str())),
146+
BuildOption::INCLUDE(path) => nasm_builder.include(path.as_path()),
147+
_ => nasm_builder, // STD ignored for NASM
148+
}
149+
}
140150
}
141151

142152
impl CcBuilder {
@@ -436,6 +446,14 @@ impl CcBuilder {
436446
.display()
437447
));
438448

449+
let mut build_options = vec![];
450+
self.add_includes(&mut build_options);
451+
let mut nasm_builder = NasmBuilder::new(self.out_dir.clone());
452+
453+
for option in &build_options {
454+
option.apply_nasm(&mut nasm_builder);
455+
}
456+
439457
let s2n_bignum_source_feature_map = Self::build_s2n_bignum_source_feature_map();
440458
let compiler_features = self.compiler_features.take();
441459
for source in sources {
@@ -470,6 +488,8 @@ impl CcBuilder {
470488
}
471489
} else if is_jitter_entropy {
472490
jitter_entropy_builder.file(source_path);
491+
} else if source_path.extension() == Some("asm".as_ref()) {
492+
nasm_builder.file(source_path);
473493
} else {
474494
cc_build.file(source_path);
475495
}
@@ -483,6 +503,10 @@ impl CcBuilder {
483503
for object in jitter_entropy_object_files {
484504
cc_build.object(object);
485505
}
506+
let nasm_object_files = nasm_builder.compile_intermediates();
507+
for object in nasm_object_files {
508+
cc_build.object(object);
509+
}
486510
cc_build.file(PathBuf::from_str("rust_wrapper.c").unwrap());
487511
}
488512

aws-lc-sys/builder/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ bindgen_available!(
104104
);
105105
mod cc_builder;
106106
mod cmake_builder;
107+
mod nasm_builder;
107108

108109
pub(crate) fn get_aws_lc_include_path(manifest_dir: &Path) -> PathBuf {
109110
manifest_dir.join("aws-lc").join("include")

aws-lc-sys/builder/nasm_builder.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::ffi::OsString;
2+
use std::path::{Path, PathBuf};
3+
4+
use crate::execute_command;
5+
6+
#[derive(Debug)]
7+
pub(crate) struct NasmBuilder {
8+
files: Vec<PathBuf>,
9+
includes: Vec<PathBuf>,
10+
defines: Vec<(String, Option<String>)>,
11+
flags: Vec<String>,
12+
out_dir: PathBuf,
13+
}
14+
15+
impl NasmBuilder {
16+
pub(crate) fn new(out_dir: PathBuf) -> Self {
17+
Self {
18+
files: Vec::new(),
19+
includes: Vec::new(),
20+
defines: Vec::new(),
21+
flags: Vec::new(),
22+
out_dir,
23+
}
24+
}
25+
26+
pub(crate) fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
27+
self.files.push(p.as_ref().to_path_buf());
28+
self
29+
}
30+
31+
pub(crate) fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
32+
self.includes.push(dir.as_ref().to_path_buf());
33+
self
34+
}
35+
36+
pub(crate) fn define(&mut self, key: &str, val: Option<&str>) -> &mut Self {
37+
self.defines
38+
.push((key.to_string(), val.map(|s| s.to_string())));
39+
self
40+
}
41+
42+
pub(crate) fn flag(&mut self, flag: &str) -> &mut Self {
43+
self.flags.push(flag.to_string());
44+
self
45+
}
46+
47+
pub(crate) fn compile_intermediates(&self) -> Vec<PathBuf> {
48+
let mut objects = Vec::new();
49+
50+
for src in &self.files {
51+
let obj_ext = "obj";
52+
let obj_name = src
53+
.file_name()
54+
.unwrap()
55+
.to_str()
56+
.unwrap()
57+
.replace(".asm", &format!(".{}", obj_ext));
58+
let obj_path = self.out_dir.join(obj_name);
59+
60+
let mut args: Vec<OsString> = vec![
61+
"-f".into(),
62+
"win64".into(),
63+
"-o".into(),
64+
obj_path.as_os_str().into(),
65+
];
66+
67+
for inc in &self.includes {
68+
args.push("-I".into());
69+
args.push(inc.as_os_str().into());
70+
}
71+
72+
for (key, val) in &self.defines {
73+
let def = if let Some(v) = val {
74+
format!("-D{}={}", key, v)
75+
} else {
76+
format!("-D{}", key)
77+
};
78+
args.push(def.into());
79+
}
80+
81+
args.extend(self.flags.iter().map(|s| s.into()));
82+
83+
args.push(src.as_os_str().into());
84+
85+
let result = execute_command(
86+
"nasm".as_ref(),
87+
&args.iter().map(|s| s.as_os_str()).collect::<Vec<_>>(),
88+
);
89+
if !result.status {
90+
panic!("NASM failed for {}: {}", src.display(), result.stderr);
91+
}
92+
93+
objects.push(obj_path);
94+
}
95+
96+
objects
97+
}
98+
}

0 commit comments

Comments
 (0)