From 134e761bdc1ed10a5fa2423e5d61ba50ca5f86c9 Mon Sep 17 00:00:00 2001 From: kylcho Date: Thu, 30 Aug 2018 22:54:50 -0700 Subject: [PATCH 1/2] Add Ionut's --include-dir and -C option support --- fileutil.cc | 12 ++++++++++++ flags.cc | 5 +++++ flags.h | 1 + testcase/include_dir.sh | 13 +++++++++++++ 4 files changed, 31 insertions(+) create mode 100755 testcase/include_dir.sh diff --git a/fileutil.cc b/fileutil.cc index 7ebb8ec2..865733bb 100644 --- a/fileutil.cc +++ b/fileutil.cc @@ -33,6 +33,7 @@ #include "log.h" #include "strutil.h" +#include "flags.h" bool Exists(StringPiece filename) { CHECK(filename.size() < PATH_MAX); @@ -174,6 +175,17 @@ class GlobCache { } else { if (Exists(pat)) files->push_back(pat); + else { + for (auto inc_path : g_flags.include_dirs) { + auto to_check = (inc_path + '/' + pat); + LOG("searching for %s in : %s", pat, inc_path.c_str()); + LOG("checking for Exists(|%s|)", to_check.c_str()); + if (Exists(to_check.c_str())) { + LOG("found %s in : %s", pat, inc_path.c_str()); + files->push_back(to_check); + } + } + } } } *files = p.first->second; diff --git a/flags.cc b/flags.cc index 54828e56..1179bef7 100644 --- a/flags.cc +++ b/flags.cc @@ -70,6 +70,9 @@ void Flags::Parse(int argc, char** argv) { should_propagate = false; } else if (!strcmp(arg, "-c")) { is_syntax_check_only = true; + } else if (!strcmp(arg, "-C")) { + if (chdir(argv[++i]) != 0) + PERROR("chdir failed"); } else if (!strcmp(arg, "-i")) { is_dry_run = true; } else if (!strcmp(arg, "-s")) { @@ -159,6 +162,8 @@ void Flags::Parse(int argc, char** argv) { } else if (ParseCommandLineOptionWithArg("--writable", argv, &i, &writable_str)) { writable.push_back(writable_str); + } else if (!strncmp(arg, "--include-dir=", 14)) { + include_dirs.push_back(string(&arg[14])); } else if (arg[0] == '-') { ERROR("Unknown flag: %s", arg); } else { diff --git a/flags.h b/flags.h index 62865a35..a93d6160 100644 --- a/flags.h +++ b/flags.h @@ -68,6 +68,7 @@ struct Flags { vector targets; vector cl_vars; vector writable; + vector include_dirs; void Parse(int argc, char** argv); }; diff --git a/testcase/include_dir.sh b/testcase/include_dir.sh new file mode 100755 index 00000000..a7491d74 --- /dev/null +++ b/testcase/include_dir.sh @@ -0,0 +1,13 @@ +set -e + +mk="$@" + +cat < Makefile +test: test2 + echo PASS +include myfile.mk +EOF + +mkdir -p test_dir +echo -e "test2:\n\techo \$@" > test_dir/myfile.mk +${mk} --include-dir=test_dir 2> /dev/null From 518e8d3f53c205925db424bad68a268e82076810 Mon Sep 17 00:00:00 2001 From: kylcho Date: Tue, 6 Nov 2018 15:39:32 -0800 Subject: [PATCH 2/2] Update based on reviews of public kati PR157 * Move code to the function evaluating include directive and fix test case --- eval.cc | 9 +++++++++ fileutil.cc | 12 ------------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/eval.cc b/eval.cc index 8a71d228..eee14ca1 100644 --- a/eval.cc +++ b/eval.cc @@ -426,6 +426,15 @@ void Evaluator::EvalInclude(const IncludeStmt* stmt) { ScopedTerminator st(pat); vector* files; Glob(pat.data(), &files); + if (files->size() == 0) { + for (auto inc_path : g_flags.include_dirs) { + auto to_check = (inc_path + '/' + pat.data()); + LOG("searching for %s in : %s", pat.data(), inc_path.c_str()); + Glob(to_check.c_str(), &files); + if (files->size() > 0) + break; + } + } if (stmt->should_exist) { if (files->empty()) { diff --git a/fileutil.cc b/fileutil.cc index 865733bb..7ebb8ec2 100644 --- a/fileutil.cc +++ b/fileutil.cc @@ -33,7 +33,6 @@ #include "log.h" #include "strutil.h" -#include "flags.h" bool Exists(StringPiece filename) { CHECK(filename.size() < PATH_MAX); @@ -175,17 +174,6 @@ class GlobCache { } else { if (Exists(pat)) files->push_back(pat); - else { - for (auto inc_path : g_flags.include_dirs) { - auto to_check = (inc_path + '/' + pat); - LOG("searching for %s in : %s", pat, inc_path.c_str()); - LOG("checking for Exists(|%s|)", to_check.c_str()); - if (Exists(to_check.c_str())) { - LOG("found %s in : %s", pat, inc_path.c_str()); - files->push_back(to_check); - } - } - } } } *files = p.first->second;