default val arg for getenv, add extreplace for extension subst

master
Daniel Kolesa 2015-11-06 19:38:37 +00:00
parent d1bb66976e
commit adf3c0f068
3 changed files with 38 additions and 12 deletions

View File

@ -1,11 +1,6 @@
CC = (getenv CC)
if (=s $CC "") [
CC = "cc"
]
CC = (getenv CC cc)
rule default test
OBJ = (strreplace (glob "*.c") ".c" ".o")
OBJ = (extreplace (glob *.c) .c .o)
rule test $OBJ [
echo " LD" $target
@ -36,4 +31,8 @@ action test_invoke [
depend foo.o foo.h
depend bar.o bar.h
depend test.o [foo.h bar.h]
depend test.o [foo.h bar.h]
// default rule
rule default test

View File

@ -159,4 +159,27 @@ void cs_register_globs(cscript::CsState &cs) {
auto fnames = cscript::util::list_explode(lst);
cs.result->set_str(ob_expand_globs(fnames).disown());
});
cs.add_command("extreplace", "sss", [](cscript::CsState &cs,
const char *lst,
const char *oldext,
const char *newext) {
String ret;
if (oldext[0] == '.') ++oldext;
if (newext[0] == '.') ++newext;
auto fnames = cscript::util::list_explode(lst);
for (ConstCharRange it: fnames.iter()) {
if (!ret.empty()) ret += ' ';
auto dot = ostd::find_last(it, '.');
if (!dot.empty() && ((dot + 1) == oldext)) {
ret += ostd::slice_until(it, dot);
ret += '.';
ret += newext;
} else {
ret += it;
}
}
cs.result->set_str(ret.iter());
ret.disown();
});
}

12
main.cc
View File

@ -438,16 +438,20 @@ int main(int argc, char **argv) {
((ObState &)cs).rule_add(file, deps, nullptr);
});
os.cs.add_commandn("getenv", "s", [](CsState &cs, TvalRange args) {
os.cs.add_commandn("getenv", "ss", [](CsState &cs, TvalRange args) {
if (((ObState &)cs).ignore_env) {
cs.result->set_cstr("");
return;
}
auto ret = ob_get_env(args[0].get_str());
if (ret.empty())
cs.result->set_cstr("");
else
if (ret.empty()) {
if (!args[1].get_str().empty())
cs.result->set_str_dup(args[1].get_str());
else
cs.result->set_cstr("");
} else {
cs.result->set_str_dup(ret);
}
});
os.cs.add_command("invoke", "s", [](CsState &cs, const char *name) {