Commit af5049e1 authored by jan.koester's avatar jan.koester
Browse files

bugfix

parent 7dfe5d8c
Loading
Loading
Loading
Loading
+18 −1
Changes for parser.cpp: 18 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1035,7 +1035,24 @@ void Parser::handle_include(const std::string& args, Makefile& mf, bool optional
    auto files = split_words(expanded);
    for (const auto& f : files) {
        std::string path = f;
        if (!fs::path(path).is_absolute() && !mf.base_dir.empty()) {
        // GNU make always resolves a relative include path against the
        // process's current working directory, never against the directory
        // of the makefile containing the include -- unlike C's #include,
        // make has no notion of "relative to this file" unless a makefile
        // builds that path itself (e.g. $(dir $(lastword $(MAKEFILE_LIST)))).
        // Falling back to base_dir only when the CWD-relative path doesn't
        // exist is a convenience for genuinely makefile-relative sibling
        // files; trying it FIRST is wrong and dangerous, since it lets an
        // unrelated same-named file elsewhere in the tree silently shadow
        // the real one (confirmed: tools/build/Makefile.build's own
        // "-include $(dir)/Build" -- dir set to "." via the recursive
        // per-directory $(build)=NAME convention -- picked up tools/build/
        // Build, the Makefile.build's own directory, instead of the real
        // CWD-relative tools/lib/subcmd/Build, because tools/build/Build
        // happens to also exist (it builds the unrelated fixdep host tool)
        // -- silently starving tools/lib/subcmd's obj-y and corrupting the
        // objtool build).
        if (!fs::exists(path) && !fs::path(path).is_absolute() && !mf.base_dir.empty()) {
            std::string candidate = mf.base_dir + "/" + path;
            if (fs::exists(candidate)) path = candidate;
        }