[C++] ninja: allow specifying output directory for ninja files
Allow passing --ninja_dir to set the directory where build.ninja
and ninja.sh will be run from. Also remove the cd $(dirname $0) from
ninja.sh, to allow the caller to set the current directory.
diff --git a/ninja.cc b/ninja.cc
index 5e009a3..05c8455 100644
--- a/ninja.cc
+++ b/ninja.cc
@@ -155,7 +155,7 @@
class NinjaGenerator {
public:
- NinjaGenerator(const char* ninja_suffix, Evaluator* ev)
+ NinjaGenerator(const char* ninja_suffix, const char* ninja_dir, Evaluator* ev)
: ce_(ev), ev_(ev), fp_(NULL), rule_id_(0) {
ev_->set_avoid_io(true);
shell_ = ev->EvalVar(kShellSym);
@@ -164,6 +164,11 @@
if (ninja_suffix) {
ninja_suffix_ = ninja_suffix;
}
+ if (ninja_dir) {
+ ninja_dir_ = ninja_dir;
+ } else {
+ ninja_dir_ = ".";
+ }
}
~NinjaGenerator() {
@@ -491,12 +496,16 @@
fprintf(fp_, "\n");
}
+ string GetNinjaDirectory() const {
+ return ninja_dir_;
+ }
+
string GetNinjaFilename() const {
- return StringPrintf("build%s.ninja", ninja_suffix_.c_str());
+ return StringPrintf("%s/build%s.ninja", ninja_dir_.c_str(), ninja_suffix_.c_str());
}
string GetShellScriptFilename() const {
- return StringPrintf("ninja%s.sh", ninja_suffix_.c_str());
+ return StringPrintf("%s/ninja%s.sh", ninja_dir_.c_str(), ninja_suffix_.c_str());
}
void GenerateNinja(const vector<DepNode*>& nodes, bool build_all_targets) {
@@ -550,7 +559,6 @@
fprintf(fp, "#!%s\n", shell->c_str());
fprintf(fp, "# Generated by kati %s\n", kGitVersion);
fprintf(fp, "\n");
- fprintf(fp, "cd $(dirname \"$0\")\n");
for (const auto& p : ev_->exports()) {
if (p.second) {
@@ -579,14 +587,16 @@
string cmd_buf_;
string gomacc_;
string ninja_suffix_;
+ string ninja_dir_;
unordered_map<Symbol, Symbol> short_names_;
shared_ptr<string> shell_;
};
void GenerateNinja(const char* ninja_suffix,
+ const char* ninja_dir,
const vector<DepNode*>& nodes,
Evaluator* ev,
bool build_all_targets) {
- NinjaGenerator ng(ninja_suffix, ev);
+ NinjaGenerator ng(ninja_suffix, ninja_dir, ev);
ng.Generate(nodes, build_all_targets);
}