Commit 038685ef authored by jan.koester's avatar jan.koester
Browse files

Initial commit: GNU Make compatible build system in C++17 with threading support

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+2 −0
Original line number Diff line number Diff line
makeplus
*.o

LICENSE

0 → 100644
+28 −0
Original line number Diff line number Diff line
BSD 3-Clause License

Copyright (c) 2026, Jan Koester

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

0 → 100644
+25 −0
Original line number Diff line number Diff line
CXX = g++
CXXFLAGS = -std=c++17 -O2 -Wall -Wextra -Wpedantic -pthread
SOURCES = main.cpp parser.cpp executor.cpp
OBJECTS = $(SOURCES:.cpp=.o)
TARGET = makeplus

all: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

main.o: main.cpp parser.h executor.h
parser.o: parser.cpp parser.h
executor.o: executor.cpp executor.h parser.h

clean:
	$(RM) $(OBJECTS) $(TARGET)

install: $(TARGET)
	install -m 755 $(TARGET) /usr/local/bin/

.PHONY: all clean install

boostrap.sh

0 → 100755
+14 −0
Original line number Diff line number Diff line
#!/bin/bash
set -e

CXX="${CXX:-g++}"
CXXFLAGS="-std=c++17 -O2 -Wall -Wextra -Wpedantic -pthread"
SOURCES="main.cpp parser.cpp executor.cpp"
TARGET="makeplus"

echo "=== Bootstrapping $TARGET ==="
echo "$CXX $CXXFLAGS -o $TARGET $SOURCES"
$CXX $CXXFLAGS -o $TARGET $SOURCES
echo "=== Done! ==="
echo "Run ./$TARGET to build using Makefile."
echo "Run ./$TARGET -j\$(nproc) for parallel build."

executor.cpp

0 → 100644
+388 −0
Original line number Diff line number Diff line
#include "executor.h"
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <cstdlib>
#include <sys/wait.h>

namespace fs = std::filesystem;
using namespace makeplus;

Executor::Executor(Makefile& mf, Parser& parser) : mf_(mf), parser_(parser) {}

// ======================== Rule Lookup ========================

const Rule* Executor::find_rule(const std::string& target, std::string& stem) {
    const Rule* r = mf_.find_explicit_rule(target);
    if (r && !r->recipes.empty()) { stem.clear(); return r; }

    // Explicit rule has no recipe - try pattern/implicit rules for recipe
    const Rule* pattern = mf_.find_pattern_rule(target, stem);
    if (pattern) return pattern;

    // Built-in implicit rules
    static std::vector<Rule> builtins;
    if (builtins.empty()) {
        // %.o: %.c
        builtins.push_back({{"%.o"}, {"%.c"}, {}, {"$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@"}, true});
        // %.o: %.cpp
        builtins.push_back({{"%.o"}, {"%.cpp"}, {}, {"$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@"}, true});
        // %.o: %.cc
        builtins.push_back({{"%.o"}, {"%.cc"}, {}, {"$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@"}, true});
    }

    for (const auto& rule : builtins) {
        for (const auto& t : rule.targets) {
            size_t pct = t.find('%');
            if (pct == std::string::npos) continue;
            std::string prefix = t.substr(0, pct);
            std::string suffix = t.substr(pct + 1);
            if (target.size() >= prefix.size() + suffix.size() &&
                target.substr(0, prefix.size()) == prefix &&
                (suffix.empty() || target.substr(target.size() - suffix.size()) == suffix)) {
                stem = target.substr(prefix.size(),
                                      target.size() - prefix.size() - suffix.size());
                // Check if prerequisite exists
                for (const auto& p : rule.prerequisites) {
                    std::string prereq = p;
                    size_t ppct = prereq.find('%');
                    if (ppct != std::string::npos) {
                        prereq = prereq.substr(0, ppct) + stem + prereq.substr(ppct + 1);
                    }
                    if (file_exists(prereq)) return &rule;
                }
            }
        }
    }

    // If we had an explicit rule without recipe, return it (it has prereqs only)
    if (r) { stem.clear(); return r; }

    return nullptr;
}

std::vector<std::string> Executor::expand_prereqs(const Rule* rule, const std::string& stem) {
    std::vector<std::string> prereqs;

    // Collect prerequisites from the given rule
    for (const auto& p : rule->prerequisites) {
        std::string ep = p;
        if (rule->is_pattern) {
            size_t pct = ep.find('%');
            while (pct != std::string::npos) {
                ep = ep.substr(0, pct) + stem + ep.substr(pct + 1);
                pct = ep.find('%', pct + stem.size());
            }
        }
        std::string expanded = parser_.expand(ep, mf_);
        auto words = Parser::split_words(expanded);
        prereqs.insert(prereqs.end(), words.begin(), words.end());
    }

    // If rule is a pattern/implicit rule, also collect prereqs from explicit rules
    // (GNU Make merges prerequisites from all matching rules)
    if (rule->is_pattern && !rule->targets.empty()) {
        std::string target_pattern = rule->targets[0];
        // Find what target this was matched for - reconstruct from stem
        size_t pct = target_pattern.find('%');
        if (pct != std::string::npos) {
            std::string actual_target = target_pattern.substr(0, pct) + stem + target_pattern.substr(pct + 1);
            for (const auto& r : mf_.rules) {
                if (r.is_pattern) continue;
                if (&r == rule) continue;
                for (const auto& t : r.targets) {
                    if (t == actual_target) {
                        for (const auto& p : r.prerequisites) {
                            std::string expanded = parser_.expand(p, mf_);
                            auto words = Parser::split_words(expanded);
                            prereqs.insert(prereqs.end(), words.begin(), words.end());
                        }
                    }
                }
            }
        }
    }
    return prereqs;
}

// ======================== File Operations ========================

bool Executor::file_exists(const std::string& path) {
    std::error_code ec;
    return fs::exists(path, ec);
}

long long Executor::file_mtime(const std::string& path) {
    std::error_code ec;
    auto ftime = fs::last_write_time(path, ec);
    if (ec) return 0;
    return ftime.time_since_epoch().count();
}

bool Executor::needs_rebuild(const std::string& target, const std::vector<std::string>& prereqs) {
    if (always_make_) return true;
    if (mf_.phony_targets.count(target)) return true;
    if (!file_exists(target)) return true;

    long long target_mtime = file_mtime(target);
    for (const auto& p : prereqs) {
        if (file_exists(p) && file_mtime(p) > target_mtime) return true;
    }
    return false;
}

// ======================== Command Execution ========================

int Executor::run_command(const std::string& cmd) {
    int status = std::system(cmd.c_str());
    if (WIFEXITED(status)) return WEXITSTATUS(status);
    if (WIFSIGNALED(status)) return 128 + WTERMSIG(status);
    return 1;
}

int Executor::execute_rule(const std::string& target, const Rule* rule,
                            const std::string& stem, const std::vector<std::string>& prereqs) {
    std::string first_prereq = prereqs.empty() ? "" : prereqs[0];

    std::vector<std::string> newer;
    if (file_exists(target)) {
        long long target_mtime = file_mtime(target);
        for (const auto& p : prereqs) {
            if (file_exists(p) && file_mtime(p) > target_mtime) newer.push_back(p);
        }
    } else {
        newer = prereqs;
    }

    for (const auto& recipe : rule->recipes) {
        std::string cmd = parser_.expand_with_automatic(
            recipe, mf_, target, first_prereq, prereqs, newer, stem);

        bool is_silent = silent_;
        bool ignore_error = false;
        bool force_exec = false;

        size_t start = 0;
        while (start < cmd.size()) {
            if (cmd[start] == '@') { is_silent = true; start++; }
            else if (cmd[start] == '-') { ignore_error = true; start++; }
            else if (cmd[start] == '+') { force_exec = true; start++; }
            else if (cmd[start] == ' ' || cmd[start] == '\t') { start++; }
            else break;
        }
        cmd = cmd.substr(start);

        if (cmd.empty()) continue;

        if (!is_silent) {
            std::cout << cmd << std::endl;
        }

        if (question_) return 1; // question mode: return 1 if would rebuild

        if (!dry_run_ || force_exec) {
            int rc = run_command(cmd);
            if (rc != 0 && !ignore_error) {
                std::cerr << "makeplus: *** [" << target << "] Error " << rc << std::endl;
                return rc;
            }
        }
    }
    return 0;
}

// ======================== Sequential Build ========================

int Executor::build_sequential(const std::string& target) {
    auto it = visited_.find(target);
    if (it != visited_.end()) return it->second;

    visited_[target] = 0;

    std::string stem;
    const Rule* rule = find_rule(target, stem);

    if (!rule) {
        if (file_exists(target)) return 0;
        std::cerr << "makeplus: *** No rule to make target '" << target << "'. Stop." << std::endl;
        return 2;
    }

    auto prereqs = expand_prereqs(rule, stem);

    // Build all prerequisites
    for (const auto& prereq : prereqs) {
        int rc = build_sequential(prereq);
        if (rc != 0) {
            if (!keep_going_) {
                visited_[target] = rc;
                return rc;
            }
        }
    }

    if (!needs_rebuild(target, prereqs)) {
        visited_[target] = 0;
        return 0;
    }

    int rc = execute_rule(target, rule, stem, prereqs);
    if (rc == 0) rebuilt_.insert(target);
    visited_[target] = rc;
    return rc;
}

// ======================== Parallel Build ========================

void Executor::add_to_dag(const std::string& target, std::set<std::string>& visited) {
    if (visited.count(target)) return;
    visited.insert(target);

    if (dag_.count(target)) return;

    DagNode node;
    node.target = target;
    node.rule = find_rule(target, node.stem);

    if (node.rule) {
        node.prereqs = expand_prereqs(node.rule, node.stem);
        node.unresolved_deps = 0;

        for (const auto& p : node.prereqs) {
            add_to_dag(p, visited);
            node.unresolved_deps++;
        }
    }

    dag_[target] = std::move(node);

    // Add reverse edges
    for (const auto& p : dag_[target].prereqs) {
        if (dag_.count(p)) {
            dag_[p].dependents.push_back(target);
        }
    }
}

void Executor::worker_thread() {
    while (true) {
        std::string target;
        {
            std::unique_lock<std::mutex> lock(mutex_);
            cv_.wait(lock, [this] {
                return !ready_queue_.empty() || remaining_ == 0 || failed_;
            });

            if ((remaining_ == 0 || failed_) && ready_queue_.empty()) return;
            if (ready_queue_.empty()) continue;

            target = ready_queue_.front();
            ready_queue_.pop();
            dag_[target].state = DagNode::State::Running;
        }

        // Process node (no lock held)
        auto& node = dag_[target];
        bool success = true;

        if (!node.rule) {
            if (!file_exists(target)) {
                std::lock_guard<std::mutex> lock(mutex_);
                std::cerr << "makeplus: *** No rule to make target '" << target << "'. Stop." << std::endl;
                success = false;
            }
        } else {
            if (needs_rebuild(target, node.prereqs)) {
                int rc = execute_rule(target, node.rule, node.stem, node.prereqs);
                if (rc != 0) success = false;
            }
        }

        {
            std::lock_guard<std::mutex> lock(mutex_);
            if (success) {
                node.state = DagNode::State::Done;
            } else {
                node.state = DagNode::State::Failed;
                if (!keep_going_) failed_ = true;
            }

            // Update dependents
            for (const auto& dep : node.dependents) {
                auto& dep_node = dag_[dep];
                dep_node.unresolved_deps--;
                if (dep_node.unresolved_deps == 0 && dep_node.state == DagNode::State::Pending) {
                    dep_node.state = DagNode::State::Ready;
                    ready_queue_.push(dep);
                }
            }

            remaining_--;
        }
        cv_.notify_all();
    }
}

int Executor::build_parallel(const std::vector<std::string>& targets) {
    // Phase 1: Build DAG
    std::set<std::string> visited;
    for (const auto& t : targets) {
        add_to_dag(t, visited);
    }

    // Phase 2: Find initially ready nodes
    remaining_ = static_cast<int>(dag_.size());
    for (auto& [name, node] : dag_) {
        if (node.unresolved_deps == 0) {
            node.state = DagNode::State::Ready;
            ready_queue_.push(name);
        }
    }

    if (dag_.empty()) return 0;

    // Phase 3: Start workers
    int actual_jobs = std::min(num_jobs_, remaining_.load());
    std::vector<std::thread> workers;
    workers.reserve(actual_jobs);
    for (int i = 0; i < actual_jobs; i++) {
        workers.emplace_back(&Executor::worker_thread, this);
    }

    // Phase 4: Wait
    for (auto& w : workers) w.join();

    // Check results
    for (const auto& t : targets) {
        if (dag_.count(t) && dag_[t].state == DagNode::State::Failed) return 2;
    }

    return failed_ ? 2 : 0;
}

// ======================== Main Build Entry ========================

int Executor::build(const std::vector<std::string>& targets) {
    std::vector<std::string> actual_targets = targets;
    if (actual_targets.empty()) {
        if (mf_.default_target.empty()) {
            std::cerr << "makeplus: *** No targets. Stop." << std::endl;
            return 2;
        }
        actual_targets.push_back(mf_.default_target);
    }

    if (num_jobs_ <= 1) {
        int result = 0;
        for (const auto& t : actual_targets) {
            int rc = build_sequential(t);
            if (rc != 0) {
                if (!keep_going_) return rc;
                result = rc;
            }
        }
        return result;
    } else {
        return build_parallel(actual_targets);
    }
}
Loading