Skip to content

Commit

Permalink
[C++] Handle .POSIX at eval time
Browse files Browse the repository at this point in the history
.POSIX pseudo target should change the behavior of $(shell).
This also implements .POSIX for ckati's non-ninja mode.
  • Loading branch information
shinh committed Apr 27, 2016
1 parent 03fa345 commit 2941ea0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 17 deletions.
4 changes: 0 additions & 4 deletions dep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ class DepBuilder {
for (Symbol t : targets)
phony_.insert(t);
}
if (GetRuleInputs(Intern(".POSIX"), &targets, &loc)) {
// .POSIX: enables bash -e command line option globally
g_flags.posix_shell = true;
}
if (GetRuleInputs(Intern(".KATI_RESTAT"), &targets, &loc)) {
for (Symbol t : targets)
restat_.insert(t);
Expand Down
25 changes: 24 additions & 1 deletion eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Evaluator::Evaluator()
: last_rule_(NULL),
current_scope_(NULL),
avoid_io_(false),
eval_depth_(0) {
eval_depth_(0),
posix_sym_(Intern(".POSIX")),
is_posix_(false) {
}

Evaluator::~Evaluator() {
Expand Down Expand Up @@ -126,6 +128,11 @@ void Evaluator::EvalRule(const RuleStmt* stmt) {
rule->cmds.push_back(stmt->after_term);
}

for (Symbol o : rule->outputs) {
if (o == posix_sym_)
is_posix_ = true;
}

LOG("Rule: %s", rule->DebugString().c_str());
rules_.push_back(rule);
last_rule_ = rule;
Expand Down Expand Up @@ -311,6 +318,22 @@ string Evaluator::EvalVar(Symbol name) {
return LookupVar(name)->Eval(this);
}

string Evaluator::GetShell() {
return EvalVar(kShellSym);
}

string Evaluator::GetShellFlag() {
// TODO: Handle $(.SHELLFLAGS)
return is_posix_ ? "-ec" : "-c";
}

string Evaluator::GetShellAndFlag() {
string shell = GetShell();
shell += ' ';
shell += GetShellFlag();
return shell;
}

void Evaluator::Error(const string& msg) {
ERROR("%s:%d: %s", LOCF(loc_), msg.c_str());
}
Expand Down
7 changes: 7 additions & 0 deletions eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class Evaluator {
eval_depth_--;
}

string GetShell();
string GetShellFlag();
string GetShellAndFlag();

private:
Var* EvalRHS(Symbol lhs, Value* rhs, StringPiece orig_rhs, AssignOp op,
bool is_override = false);
Expand All @@ -115,6 +119,9 @@ class Evaluator {
// error).
vector<string> delayed_output_commands_;

Symbol posix_sym_;
bool is_posix_;

static unordered_set<Symbol> used_undefined_vars_;
};

Expand Down
2 changes: 1 addition & 1 deletion exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Executor {
public:
explicit Executor(Evaluator* ev)
: ce_(ev) {
shell_ = ev->EvalVar(kShellSym);
shell_ = ev->GetShellAndFlag();
}

double ExecNode(DepNode* n, DepNode* needed_by) {
Expand Down
2 changes: 1 addition & 1 deletion fileutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int RunCommand(const string& shell, const string& cmd,
string* s) {
string cmd_escaped = cmd;
EscapeShell(&cmd_escaped);
string cmd_with_shell = shell + " -c \"" + cmd_escaped + "\"";
string cmd_with_shell = shell + " \"" + cmd_escaped + "\"";
const char* argv[] = {
"/bin/sh", "-c", cmd_with_shell.c_str(), NULL
};
Expand Down
1 change: 0 additions & 1 deletion flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct Flags {
bool is_dry_run;
bool is_silent_mode;
bool is_syntax_check_only;
bool posix_shell;
bool regen;
bool regen_ignoring_kati_binary;
bool use_find_emulator;
Expand Down
2 changes: 1 addition & 1 deletion func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
return;
}

const string&& shell = ev->EvalVar(kShellSym);
const string&& shell = ev->GetShellAndFlag();

string out;
FindCommand* fc = NULL;
Expand Down
7 changes: 4 additions & 3 deletions ninja.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class NinjaGenerator {
start_time_(start_time),
default_target_(NULL) {
ev_->set_avoid_io(true);
shell_ = EscapeNinja(ev->EvalVar(kShellSym));
shell_flags_ = g_flags.posix_shell ? "ec" : "c";
shell_ = EscapeNinja(ev->GetShell());
shell_flags_ = EscapeNinja(ev->GetShellFlag());
const string use_goma_str = ev->EvalVar(Intern("USE_GOMA"));
use_goma_ = !(use_goma_str.empty() || use_goma_str == "false");
if (g_flags.goma_dir)
Expand Down Expand Up @@ -502,7 +502,8 @@ class NinjaGenerator {
*o << " command = " << shell_ << " $out.rsp\n";
} else {
EscapeShell(&cmd_buf);
*o << " command = " << shell_ << " -" << shell_flags_ << " \"" << cmd_buf << "\"\n";
*o << " command = " << shell_ << ' ' << shell_flags_
<< " \"" << cmd_buf << "\"\n";
}
if (node->is_restat) {
*o << " restat = 1\n";
Expand Down
9 changes: 4 additions & 5 deletions testcase/posix_var.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TODO(go|c-exec): Fix
# TODO(go): Fix

MAKEVER:=$(shell make --version | ruby -n0e 'puts $$_[/Make (\d)/,1]')

Expand All @@ -10,12 +10,11 @@ test:

else

# TODO: Fix
#$(info $(shell echo foo))
$(info $(shell echo foo))
SHELL := echo
#$(info $(shell echo bar))
$(info $(shell echo bar))
.POSIX:
#$(info $(shell echo baz))
$(info $(shell echo baz))
test:
foobar

Expand Down

0 comments on commit 2941ea0

Please sign in to comment.