Commit 4714416a authored by jan.koester's avatar jan.koester
Browse files

test

parent 22d668c7
Loading
Loading
Loading
Loading
+53 −9
Changes for main.cpp: 53 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ int main(int argc, char* argv[]) {
    bool keep_going = false;
    bool silent = false;
    bool question = false;
    bool no_print_directory = false;
    std::string directory;

    // Parse command-line variables and options
@@ -134,13 +135,32 @@ int main(int argc, char* argv[]) {
        if (arg == "-k" || arg == "--keep-going") { keep_going = true; continue; }
        if (arg == "-s" || arg == "--silent") { silent = true; continue; }
        if (arg == "-q" || arg == "--question") { question = true; continue; }
        if (arg == "--no-print-directory") { no_print_directory = true; continue; }
        if (arg == "-w" || arg == "--print-directory") { no_print_directory = false; continue; }
        // Flags that makeplus accepts but does not need to act on. Without this
        // they would be misinterpreted as build targets (e.g. Kbuild passes
        // -rR --no-print-directory through MAKEFLAGS to sub-makes).
        if (arg == "-r" || arg == "--no-builtin-rules" ||
            arg == "-R" || arg == "--no-builtin-variables" ||
            arg == "-d" || arg == "--debug" ||
            arg == "--no-keep-going" ||
            arg.rfind("--debug=", 0) == 0 ||
            arg.rfind("-O", 0) == 0 || arg.rfind("--output-sync", 0) == 0 ||
            arg.rfind("--jobserver", 0) == 0) {
            continue;
        }
        if (arg.rfind("-I", 0) == 0) { // include dir (with or without arg)
            if (arg == "-I" && i + 1 < argc) ++i;
            continue;
        }

        // Check for combined flags like -ksB
        if (arg.size() > 1 && arg[0] == '-' && arg[1] != '-') {
            bool is_flags = true;
            for (size_t j = 1; j < arg.size(); j++) {
                if (arg[j] != 'n' && arg[j] != 'B' && arg[j] != 'k' &&
                    arg[j] != 's' && arg[j] != 'q') {
                    arg[j] != 's' && arg[j] != 'q' && arg[j] != 'r' &&
                    arg[j] != 'R' && arg[j] != 'w') {
                    is_flags = false;
                    break;
                }
@@ -152,11 +172,19 @@ int main(int argc, char* argv[]) {
                    if (arg[j] == 'k') keep_going = true;
                    if (arg[j] == 's') silent = true;
                    if (arg[j] == 'q') question = true;
                    if (arg[j] == 'w') no_print_directory = false;
                    // 'r' and 'R' (no builtin rules/vars) are accepted, no-op.
                }
                continue;
            }
        }

        // Ignore any other unrecognised long option rather than treating it as
        // a target (GNU make would error, but tolerance keeps Kbuild moving).
        if (arg.size() > 2 && arg[0] == '-' && arg[1] == '-') {
            continue;
        }

        // Check for VAR=value on command line
        size_t eq = arg.find('=');
        if (eq != std::string::npos && eq > 0) {
@@ -199,21 +227,37 @@ int main(int argc, char* argv[]) {
        mf.variables[name] = {value, makeplus::VarFlavor::Simple, true};
    }

    // Parse makefile
    if (!parser.parse_file(makefile_path, mf)) {
        std::cerr << "makeplus: " << parser.error() << std::endl;
        return 2;
    // MAKECMDGOALS and MAKEFLAGS must exist BEFORE parsing: build systems like
    // Kbuild inspect them at parse time (e.g. to generate per-goal rules and to
    // decide whether a sub-make recursion is still needed).
    {
        std::string goals;
        for (size_t i = 0; i < targets.size(); ++i) {
            if (i) goals += ' ';
            goals += targets[i];
        }
        mf.variables["MAKECMDGOALS"] = {goals, makeplus::VarFlavor::Simple, false};
    }

    // Build MAKEFLAGS
    {
        std::string flags;
        if (dry_run) flags += "n";
        if (always_make) flags += "B";
        if (keep_going) flags += "k";
        if (silent) flags += "s";
        if (jobs > 1) flags += " -j" + std::to_string(jobs);
        mf.variables["MAKEFLAGS"] = {flags, makeplus::VarFlavor::Simple, false};
        // Long options follow a space-separated word list per GNU make.
        std::string words;
        if (no_print_directory) words += " --no-print-directory";
        std::string combined = flags;
        if (!words.empty()) combined += words;
        if (jobs > 1) combined += " -j" + std::to_string(jobs);
        mf.variables["MAKEFLAGS"] = {makeplus::Parser::trim(combined),
                                     makeplus::VarFlavor::Simple, false};
    }

    // Parse makefile
    if (!parser.parse_file(makefile_path, mf)) {
        std::cerr << "makeplus: " << parser.error() << std::endl;
        return 2;
    }

    // Execute