makewiz/
lib.rs

1//! MakeWiz is a command line tool that generates a Makefile based on the files in your directory.
2//!
3//! Run `makewiz` to generate a Makefile for your C/C++ project.
4//!
5//! Run `makewiz java` to generate a Makefile for your Java project.
6//!
7//! For a more detailed usage guide and installation guide, visit [MakeWiz's GitHub page](https://github.com/kallazz/MakeWiz).
8
9pub mod cli;
10pub mod build_data;
11pub mod user_config;
12
13use std::fmt;
14
15use crate::build_data::BuildData;
16
17/// Represents a vector of strings.
18#[derive(PartialEq, Debug)]
19pub struct StringVector(Vec<String>);
20
21impl StringVector {
22    /// Creates a new empty `StringVector`.
23    pub fn new() -> StringVector {
24        StringVector(Vec::new())
25    }
26}
27
28impl fmt::Display for StringVector {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        write!(f, "{}", self.0.join(" "))?;
31        Ok(())
32    }
33}
34
35/// Generates a Makefile for a C/C++ project based on the provided `BuildData`.
36///
37/// # Arguments
38///
39/// * `file_names` - A `BuildData` struct containing file names and compiler options.
40///
41/// # Returns
42///
43/// A `String` containing the generated Makefile.
44pub fn generate_makefile(file_names: &BuildData) -> String {
45    let makefile = format!("\
46# Compiler and flags
47CC = {}
48FLAGS = -g -c -Wall
49LFLAGS = {}
50
51# Source files and object files
52OBJS = {}
53SOURCE = {}
54HEADER = {}
55OUT = {}
56
57# Libraries
58LDLIBS = {}
59
60# Default target
61all: $(OUT)
62
63# Linking rules
64$(OUT): $(OBJS)
65\t$(CC) -g $(OBJS) -o $(OUT) $(LFLAGS) $(LDLIBS)
66
67# Compilation rules
68%.o: %.cpp $(HEADER)
69\t$(CC) $(FLAGS) -o $@ $<
70
71# Clean rule
72clean:
73\trm -f $(OBJS) $(OUT)\n",
74file_names.compiler, file_names.lflags,
75file_names.get_cpp_compiled_files(), file_names.get_cpp_source_files(),
76file_names.header_files, file_names.executable, file_names.ldlibs);
77
78    makefile
79}
80
81/// Generates a Makefile for a Java project based on the provided `BuildData`.
82///
83/// # Arguments
84///
85/// * `file_names` - A `BuildData` struct containing relevant file names.
86///
87/// # Returns
88///
89/// A `String` containing the generated Makefile.
90pub fn generate_java_makefile(file_names: &BuildData) -> String {
91    let makefile = format!("\
92# Compiler and flags
93JC = javac
94JFLAGS = -g
95
96# Source files and compiled classes
97SOURCE = {}
98CLASSES = {}
99
100# Default target
101default: $(CLASSES)
102
103# Compilation rule
104%.class: %.java
105\t$(JC) $(JFLAGS) $<
106
107# Clean rule to remove generated .class files
108clean:
109\trm -f $(CLASSES)\n",
110file_names.get_java_source_files(), file_names.get_java_compiled_files());
111
112    makefile
113}