mirror of
https://github.com/dexoron/dcr.git
synced 2026-10-21 09:18:29 +00:00
196 lines
5.3 KiB
Rust
196 lines
5.3 KiB
Rust
use crate::core::builder::BuildContext;
|
|
use crate::core::builder::common;
|
|
use crate::platform;
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use std::process::Command;
|
|
use std::time::Instant;
|
|
|
|
pub fn build(ctx: &BuildContext) -> Result<f64, String> {
|
|
let lang = ctx.language.to_lowercase();
|
|
if lang.split('+').any(|p| p.trim() != "asm") {
|
|
return Err("NASM backend requires build.language = \"asm\"".to_string());
|
|
}
|
|
let assembler = if ctx.compiler.is_empty() {
|
|
"nasm"
|
|
} else {
|
|
ctx.compiler
|
|
};
|
|
let start_time = Instant::now();
|
|
let sources = collect_sources(ctx)?;
|
|
let obj_dir = Path::new("./target").join(ctx.profile).join("obj");
|
|
let objects = build_objects(assembler, &sources, &obj_dir, ctx, "o")?;
|
|
|
|
if ctx.kind == "staticlib" {
|
|
let lib_path = platform::lib_path(ctx.profile, ctx.project_name, ctx.target_dir);
|
|
let archiver = ctx.archiver.unwrap_or(if cfg!(target_os = "windows") {
|
|
"lib"
|
|
} else {
|
|
"ar"
|
|
});
|
|
let mut cmd = Command::new(archiver);
|
|
if cfg!(target_os = "windows") && archiver == "lib" {
|
|
cmd.arg("/nologo").arg(format!("/OUT:{lib_path}"));
|
|
} else {
|
|
cmd.arg("rcs").arg(&lib_path);
|
|
}
|
|
for obj in &objects {
|
|
cmd.arg(obj);
|
|
}
|
|
if std::env::var("DCR_DEBUG").is_ok() {
|
|
eprintln!("[dcr] {:?}", cmd);
|
|
}
|
|
match cmd.status() {
|
|
Ok(status) if status.success() => {
|
|
let elapsed = ((start_time.elapsed().as_secs_f64() * 100.0).trunc()) / 100.0;
|
|
return Ok(elapsed);
|
|
}
|
|
Ok(_) => return Err("Build failed".to_string()),
|
|
Err(err) => return Err(format!("Build failed: {err}")),
|
|
}
|
|
}
|
|
|
|
let mut cmd = Command::new(ctx.linker.unwrap_or("cc"));
|
|
if ctx.kind == "sharedlib" {
|
|
if cfg!(target_os = "macos") {
|
|
cmd.arg("-dynamiclib");
|
|
} else {
|
|
cmd.arg("-shared");
|
|
}
|
|
}
|
|
for obj in &objects {
|
|
cmd.arg(obj);
|
|
}
|
|
for dir in ctx.lib_dirs {
|
|
cmd.arg(format!("-L{dir}"));
|
|
}
|
|
for lib in ctx.libs {
|
|
cmd.arg(format!("-l{lib}"));
|
|
}
|
|
for flag in ctx.ldflags {
|
|
cmd.arg(flag);
|
|
}
|
|
let out_path = if ctx.kind == "sharedlib" {
|
|
platform::shared_lib_path(ctx.profile, ctx.project_name, ctx.target_dir)
|
|
} else {
|
|
platform::bin_path(ctx.profile, ctx.project_name, ctx.target_dir)
|
|
};
|
|
cmd.arg("-o").arg(out_path);
|
|
|
|
if std::env::var("DCR_DEBUG").is_ok() {
|
|
eprintln!("[dcr] {:?}", cmd);
|
|
}
|
|
match cmd.status() {
|
|
Ok(status) if status.success() => {
|
|
let elapsed = ((start_time.elapsed().as_secs_f64() * 100.0).trunc()) / 100.0;
|
|
Ok(elapsed)
|
|
}
|
|
Ok(_) => Err("Build failed".to_string()),
|
|
Err(err) => Err(format!("Build failed: {err}")),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn collect_sources(ctx: &BuildContext) -> Result<Vec<String>, String> {
|
|
// NASM handles lowercase .s and .asm only (not .S which is GCC preprocessed)
|
|
common::collect_sources(
|
|
ctx.source_roots,
|
|
&["asm", "s"],
|
|
ctx.exclude_dirs,
|
|
ctx.include_paths,
|
|
)
|
|
}
|
|
|
|
fn build_objects(
|
|
assembler: &str,
|
|
sources: &[String],
|
|
obj_dir: &Path,
|
|
ctx: &BuildContext,
|
|
obj_ext: &str,
|
|
) -> Result<Vec<String>, String> {
|
|
let objects: Vec<String> = sources
|
|
.iter()
|
|
.map(|s| common::object_path(obj_dir, s, obj_ext))
|
|
.collect();
|
|
|
|
let format = nasm_format(ctx.platform);
|
|
|
|
common::parallel_build(sources.len(), |i| {
|
|
build_object(assembler, &sources[i], &objects[i], format, ctx)
|
|
})?;
|
|
|
|
Ok(objects)
|
|
}
|
|
|
|
fn build_object(
|
|
assembler: &str,
|
|
source: &str,
|
|
obj_path: &str,
|
|
format: &str,
|
|
ctx: &BuildContext,
|
|
) -> Result<(), String> {
|
|
if let Some(parent) = Path::new(obj_path).parent() {
|
|
fs::create_dir_all(parent).map_err(|err| format!("obj dir error: {err}"))?;
|
|
}
|
|
|
|
if !common::needs_rebuild(source, obj_path) {
|
|
return Ok(());
|
|
}
|
|
|
|
let mut cmd = Command::new(assembler);
|
|
cmd.arg("-f")
|
|
.arg(format)
|
|
.arg(source)
|
|
.arg("-o")
|
|
.arg(obj_path);
|
|
|
|
for flag in ctx.cflags {
|
|
cmd.arg(flag);
|
|
}
|
|
|
|
if std::env::var("DCR_DEBUG").is_ok() {
|
|
eprintln!("[dcr] {:?}", cmd);
|
|
}
|
|
|
|
match cmd.status() {
|
|
Ok(status) if status.success() => Ok(()),
|
|
Ok(_) => Err("Build failed".to_string()),
|
|
Err(err) => Err(format!("Build failed: {err}")),
|
|
}
|
|
}
|
|
|
|
fn nasm_format(platform: Option<&str>) -> &'static str {
|
|
if let Some(p) = platform {
|
|
let p = p.to_lowercase().replace('-', "_");
|
|
if p == "x86" || (p.starts_with('i') && p.ends_with("86") && p.len() == 4) {
|
|
#[cfg(target_os = "macos")]
|
|
{
|
|
return "macho32";
|
|
}
|
|
#[cfg(target_os = "windows")]
|
|
{
|
|
return "win32";
|
|
}
|
|
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
|
{
|
|
return "elf32";
|
|
}
|
|
}
|
|
}
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
"elf64"
|
|
}
|
|
#[cfg(target_os = "macos")]
|
|
{
|
|
"macho64"
|
|
}
|
|
#[cfg(target_os = "windows")]
|
|
{
|
|
"win64"
|
|
}
|
|
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
|
|
{
|
|
"elf64"
|
|
}
|
|
}
|