| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
use std::ffi::OsString; |
| 5 |
use std::fs; |
| 6 |
use std::path::{Path, PathBuf}; |
| 7 |
|
| 8 |
use crate::{execute_command, target_arch, test_nasm_command, use_prebuilt_nasm}; |
| 9 |
|
| 10 |
#[derive(Debug)] |
| 11 |
pub(crate) struct NasmBuilder { |
| 12 |
files: Vec<PathBuf>, |
| 13 |
includes: Vec<PathBuf>, |
| 14 |
defines: Vec<(String, Option<String>)>, |
| 15 |
flags: Vec<String>, |
| 16 |
out_dir: PathBuf, |
| 17 |
manifest_dir: PathBuf, |
| 18 |
} |
| 19 |
|
| 20 |
impl NasmBuilder { |
| 21 |
pub(crate) fn new(manifest_dir: PathBuf, out_dir: PathBuf) -> Self { |
| 22 |
Self { |
| 23 |
files: Vec::new(), |
| 24 |
includes: Vec::new(), |
| 25 |
defines: Vec::new(), |
| 26 |
flags: Vec::new(), |
| 27 |
out_dir, |
| 28 |
manifest_dir, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
pub(crate) fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut Self { |
| 33 |
self.files.push(p.as_ref().to_path_buf()); |
| 34 |
self |
| 35 |
} |
| 36 |
|
| 37 |
pub(crate) fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { |
| 38 |
self.includes.push(dir.as_ref().to_path_buf()); |
| 39 |
self |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) fn define(&mut self, key: &str, val: Option<&str>) -> &mut Self { |
| 43 |
self.defines |
| 44 |
.push((key.to_string(), val.map(std::string::ToString::to_string))); |
| 45 |
self |
| 46 |
} |
| 47 |
|
| 48 |
pub(crate) fn flag(&mut self, flag: &str) -> &mut Self { |
| 49 |
self.flags.push(flag.to_string()); |
| 50 |
self |
| 51 |
} |
| 52 |
|
| 53 |
pub(crate) fn compile_intermediates(&self) -> Vec<PathBuf> { |
| 54 |
let mut objects = Vec::new(); |
| 55 |
|
| 56 |
if self.files.is_empty() { |
| 57 |
return vec![]; |
| 58 |
} |
| 59 |
|
| 60 |
if test_nasm_command() { |
| 61 |
for src in &self.files { |
| 62 |
let obj_name = src |
| 63 |
.file_name() |
| 64 |
.unwrap() |
| 65 |
.to_str() |
| 66 |
.unwrap() |
| 67 |
.replace(".asm", ".obj"); |
| 68 |
let obj_path = self.out_dir.join(obj_name); |
| 69 |
|
| 70 |
let format = match target_arch().as_str() { |
| 71 |
"x86_64" => "win64", |
| 72 |
_ => "win32", |
| 73 |
}; |
| 74 |
|
| 75 |
let mut args: Vec<OsString> = vec![ |
| 76 |
"-f".into(), |
| 77 |
format.into(), |
| 78 |
"-o".into(), |
| 79 |
obj_path.as_os_str().into(), |
| 80 |
]; |
| 81 |
|
| 82 |
for inc in &self.includes { |
| 83 |
args.push("-I".into()); |
| 84 |
args.push(inc.as_os_str().into()); |
| 85 |
} |
| 86 |
|
| 87 |
for (key, val) in &self.defines { |
| 88 |
let def = if let Some(v) = val { |
| 89 |
format!("-D{key}={v}") |
| 90 |
} else { |
| 91 |
format!("-D{key}") |
| 92 |
}; |
| 93 |
args.push(def.into()); |
| 94 |
} |
| 95 |
|
| 96 |
args.extend(self.flags.iter().map(std::convert::Into::into)); |
| 97 |
|
| 98 |
args.push(src.as_os_str().into()); |
| 99 |
|
| 100 |
let result = execute_command( |
| 101 |
"nasm".as_ref(), |
| 102 |
&args |
| 103 |
.iter() |
| 104 |
.map(std::ffi::OsString::as_os_str) |
| 105 |
.collect::<Vec<_>>(), |
| 106 |
); |
| 107 |
assert!( |
| 108 |
result.status, |
| 109 |
"NASM failed for {}:\n-----\n{}\n-----\n{}\n-----\n", |
| 110 |
src.display(), |
| 111 |
result.stdout, |
| 112 |
result.stderr |
| 113 |
); |
| 114 |
|
| 115 |
objects.push(obj_path); |
| 116 |
} |
| 117 |
} else if use_prebuilt_nasm() { |
| 118 |
let prebuilt_dir = self.manifest_dir.join("builder").join("prebuilt-nasm"); |
| 119 |
for src in &self.files { |
| 120 |
let obj_name = src |
| 121 |
.file_name() |
| 122 |
.unwrap() |
| 123 |
.to_str() |
| 124 |
.unwrap() |
| 125 |
.replace(".asm", ".obj"); |
| 126 |
let obj_path = self.out_dir.join(&obj_name); |
| 127 |
let base_name = obj_name.strip_suffix(".obj").unwrap_or(&obj_name); |
| 128 |
let prebuilt_src = prebuilt_dir.join(format!("{base_name}.obj")); |
| 129 |
if prebuilt_src.exists() { |
| 130 |
fs::copy(&prebuilt_src, &obj_path) |
| 131 |
.expect("Failed to copy prebuilt NASM object"); |
| 132 |
} else { |
| 133 |
panic!("Prebuilt NASM object not found: {}", prebuilt_src.display()); |
| 134 |
} |
| 135 |
objects.push(obj_path); |
| 136 |
} |
| 137 |
} else { |
| 138 |
panic!("NASM command not found! Build cannot continue."); |
| 139 |
} |
| 140 |
|
| 141 |
objects |
| 142 |
} |
| 143 |
} |