From a1cb1175d405ed1b022f4d7122fb0df445394052 Mon Sep 17 00:00:00 2001 From: silt Date: Mon, 5 Dec 2022 09:39:57 -0500 Subject: [PATCH] build assembly true/false with cargo --- Cargo.toml | 10 +--------- build.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++--- src/bin/false.rs | 22 ---------------------- src/bin/true.rs | 22 ---------------------- 4 files changed, 47 insertions(+), 56 deletions(-) delete mode 100644 src/bin/false.rs delete mode 100644 src/bin/true.rs diff --git a/Cargo.toml b/Cargo.toml index 9f7b25c..1ea9478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,12 +12,4 @@ authors = [ [[bin]] name = "cat" -path = "src/bin/cat.rs" - -[[bin]] -name = "false" -path = "src/bin/false.rs" - -[[bin]] -name = "true" -path = "src/bin/true.rs" +path = "src/bin/cat.rs" \ No newline at end of file diff --git a/build.rs b/build.rs index 72746a5..d98132e 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,47 @@ -fn main() { - println!("cargo:rustc-link-arg-bin=true=-nostartfiles"); - println!("cargo:rustc-link-arg-bin=false=-nostartfiles"); +use std::env; +use std::process::{Command, exit}; + +fn main() -> Result<(), Box> { + let out_dir = env::var("OUT_DIR")?; + + let mut handler_nasm_true = Command::new("nasm").args([ + "-f", "elf", + "src/asm/true.asm", + "-o", &(out_dir.clone() + "/true.o") + ]).spawn()?; + + let mut handler_nasm_false = Command::new("nasm").args([ + "-f", "elf", + "src/asm/false.asm", + "-o", &(out_dir.clone() + "/false.o") + ]).spawn()?; + + + if !handler_nasm_true.wait_with_output()?.stderr.is_empty() { + exit(1); + } + let mut handler_ld_true = Command::new("ld").args([ + "-m", "elf_i386", + "src/asm/true.asm", + "-s", + &(out_dir.clone() + "/true.o"), + "-o", &(out_dir.clone() + "/true"), + ]).spawn()?; + + if !handler_nasm_false.wait_with_output()?.stderr.is_empty() { + exit(1) + } + let mut handler_ld_false = Command::new("ld").args([ + "-m", "elf_i386", + "src/asm/false.asm", + "-s", + &(out_dir.clone() + "/false.o"), + "-o", &(out_dir.clone() + "/false"), + ]).spawn()?; + + + handler_ld_true.wait(); + handler_ld_false.wait(); + + Ok(()) } \ No newline at end of file diff --git a/src/bin/false.rs b/src/bin/false.rs deleted file mode 100644 index 24f88ec..0000000 --- a/src/bin/false.rs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2022 silt -// SPDX-License-Identifier: AGPL-3.0-or-later - -#![feature(lang_items, start)] -#![no_main] -#![no_std] - -use core::panic::PanicInfo; -use core::arch::asm; - -#[lang = "eh_personality"] extern fn rust_eh_personality() {} -#[lang = "panic_impl"] extern fn rust_begin_panic(_: &PanicInfo) -> ! { loop {} } - -#[no_mangle] -pub unsafe fn _start() -> ! { - asm!( - "syscall", - in("rax") 60, // Syscall number - in("rdi") 1, // Exit code - options(noreturn) - ) -} diff --git a/src/bin/true.rs b/src/bin/true.rs deleted file mode 100644 index 2ce4d35..0000000 --- a/src/bin/true.rs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2022 silt -// SPDX-License-Identifier: AGPL-3.0-or-later - -#![feature(lang_items, start)] -#![no_main] -#![no_std] - -use core::panic::PanicInfo; -use core::arch::asm; - -#[lang = "eh_personality"] extern fn rust_eh_personality() {} -#[lang = "panic_impl"] extern fn rust_begin_panic(_: &PanicInfo) -> ! { loop {} } - -#[no_mangle] -pub unsafe fn _start() -> ! { - asm!( - "syscall", - in("rax") 60, // Syscall number - in("rdi") 0, // Exit code - options(noreturn) - ) -}