From e0ce61ab1480bf095630466dcf4ec88cac13ce02 Mon Sep 17 00:00:00 2001 From: Noah Swerhun Date: Thu, 14 Dec 2023 13:18:10 -0600 Subject: [PATCH] added library (static and dynamic) capability --- src/build_rule.rs | 12 ++++++++++-- src/main.rs | 15 +++++++++++++-- src/rule_list.rs | 16 +++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/build_rule.rs b/src/build_rule.rs index 4ad55b8..95575ce 100644 --- a/src/build_rule.rs +++ b/src/build_rule.rs @@ -1,10 +1,10 @@ use std::{path::PathBuf, fs}; -use crate::ARGS; use path_clean::PathClean; use regex::Regex; use crate::makefile::*; +use crate::ARGS; #[derive(Debug)] #[allow(dead_code)] @@ -106,7 +106,15 @@ impl ToString for BuildRuleKind { command_prefix = "\t@"; let verb = match self { BuildRuleKind::Object(_) => "Building", - BuildRuleKind::Target(_) => "Linking", + BuildRuleKind::Target(_) => { + if args.static_library { + "Creating static library" + } else if args.dynamic_library { + "Creating dynamic library" + } else { + "Linking" + } + }, BuildRuleKind::Directory(_) => "Creating directory", BuildRuleKind::Convenience(_) => { special = true; diff --git a/src/main.rs b/src/main.rs index e4300e8..597a30d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,14 @@ struct Args { thread_local! {static ARGS: Args = Args::parse()} fn main() { - + ARGS.with(|args| { + if args.dynamic_library && args.static_library { + panic!("error: you must either a static library or a dynamic library, not both"); + } + if args.run && (args.dynamic_library || args.static_library) { + panic!("error: you cannot run a library"); + } + }); let mut makefile_name = String::new(); ARGS.with(|args| { makefile_name.push_str(&args.makefile); @@ -72,7 +79,11 @@ fn main() { generated_content.push_str("\n"); } - + ARGS.with(|args| { + if args.dynamic_library { + generated_content.push_str("# dynamic library\nCFLAGS += -fPIC\nLDFLAGS += -shared\n\n"); + } else {} + }); rule_list.add_target_rule(); diff --git a/src/rule_list.rs b/src/rule_list.rs index b3879a5..6e85d8b 100644 --- a/src/rule_list.rs +++ b/src/rule_list.rs @@ -3,6 +3,7 @@ use std::fs::DirEntry; use crate::build_rule::*; use crate::makefile::*; +use crate::ARGS; #[derive(Debug)] pub struct RuleList(pub Vec>); @@ -59,9 +60,18 @@ impl RuleList { pub fn add_target_rule(&mut self) { let mut objects = self.get_obj_targets(); - let target_comm = String::from( - format!("$(CC) $(LDFLAGS) $(INCS) -o $(BUILDDIR)/$(TARGET) {} $(LDLIBS)", objects.join(" ")) - ); + let mut target_comm = String::new(); + ARGS.with(|args| { + if args.static_library { + target_comm = String::from( + format!("$(AR) $(ARFLAGS) $(BUILDDIR)/$(TARGET) {}", objects.join(" ")) + ); + } else { + target_comm = String::from( + format!("$(CC) $(LDFLAGS) $(INCS) -o $(BUILDDIR)/$(TARGET) {} $(LDLIBS)", objects.join(" ")) + ); + } + }); objects.push(String::from("| $(OBJDIR) $(BUILDDIR)")); let prereqs = objects;