diff options
-rw-r--r-- | fileutil.cc | 23 | ||||
-rw-r--r-- | fileutil.h | 2 | ||||
-rw-r--r-- | ninja.cc | 6 |
3 files changed, 31 insertions, 0 deletions
diff --git a/fileutil.cc b/fileutil.cc index eb4fc22..4c87e9c 100644 --- a/fileutil.cc +++ b/fileutil.cc @@ -23,6 +23,9 @@ #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> +#if defined(__APPLE__) +#include <mach-o/dyld.h> +#endif #include <unordered_map> @@ -95,6 +98,26 @@ int RunCommand(const string& shell, const string& cmd, bool redirect_stderr, abort(); } +void GetExecutablePath(string* path) { +#if defined(__linux__) + char mypath[PATH_MAX + 1]; + ssize_t l = readlink("/proc/self/exe", mypath, PATH_MAX); + if (l < 0) { + PERROR("readlink for /proc/self/exe"); + } + mypath[l] = '\0'; + *path = mypath; +#elif defined(__APPLE__) + char mypath[PATH_MAX + 1]; + if (_NSGetExecutablePath(mypath, PATH_MAX) != 0) { + ERROR("_NSGetExecutablePath failed"); + } + *path = mypath; +#else +#error "Unsupported OS" +#endif +} + namespace { class GlobCache { @@ -29,6 +29,8 @@ double GetTimestamp(StringPiece f); int RunCommand(const string& shell, const string& cmd, bool redirect_stderr, string* out); +void GetExecutablePath(string* path); + void Glob(const char* pat, vector<string>** files); #endif // FILEUTIL_H_ @@ -30,6 +30,7 @@ #include "dep.h" #include "eval.h" #include "file_cache.h" +#include "fileutil.h" #include "flags.h" #include "log.h" #include "string_piece.h" @@ -181,6 +182,9 @@ class NinjaGenerator { shared_ptr<string> val = ev_->EvalVar(e); used_envs_.emplace(e.str(), *val); } + + if (g_gen_regen_rule) + GetExecutablePath(&kati_binary_); } ~NinjaGenerator() { @@ -528,6 +532,7 @@ class NinjaGenerator { for (const string& makefile : makefiles) { fprintf(fp_, " %.*s", SPF(makefile)); } + fprintf(fp_, " %s", kati_binary_.c_str()); // TODO: Add dependencies to directories read by $(wildcard) // or $(shell find). if (!used_envs_.empty()) @@ -656,6 +661,7 @@ class NinjaGenerator { unordered_map<Symbol, Symbol> short_names_; shared_ptr<string> shell_; map<string, string> used_envs_; + string kati_binary_; }; void GenerateNinja(const char* ninja_suffix, |