From 134e761bdc1ed10a5fa2423e5d61ba50ca5f86c9 Mon Sep 17 00:00:00 2001 From: kylcho Date: Thu, 30 Aug 2018 22:54:50 -0700 Subject: [PATCH] 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