build fix
parent
18e35cb054
commit
55b5180a54
106
main.cc
106
main.cc
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
#include "tpool.hh"
|
#include "tpool.hh"
|
||||||
|
|
||||||
using ostd::ConstCharRange;
|
using ostd::string_range;
|
||||||
using ostd::slice_until;
|
using ostd::slice_until;
|
||||||
|
|
||||||
using cscript::cs_state;
|
using cscript::cs_state;
|
||||||
|
@ -31,11 +31,11 @@ using cscript::cs_bcode;
|
||||||
/* glob matching code */
|
/* glob matching code */
|
||||||
|
|
||||||
static void ob_get_path_parts(
|
static void ob_get_path_parts(
|
||||||
std::vector<ConstCharRange> &parts, ConstCharRange elem
|
std::vector<string_range> &parts, string_range elem
|
||||||
) {
|
) {
|
||||||
ConstCharRange star = ostd::find(elem, '*');
|
string_range star = ostd::find(elem, '*');
|
||||||
while (!star.empty()) {
|
while (!star.empty()) {
|
||||||
ConstCharRange ep = slice_until(elem, star);
|
string_range ep = slice_until(elem, star);
|
||||||
if (!ep.empty()) {
|
if (!ep.empty()) {
|
||||||
parts.push_back(ep);
|
parts.push_back(ep);
|
||||||
}
|
}
|
||||||
|
@ -50,10 +50,10 @@ static void ob_get_path_parts(
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ob_path_matches(
|
static bool ob_path_matches(
|
||||||
ConstCharRange fn, std::vector<ConstCharRange> const &parts
|
string_range fn, std::vector<string_range> const &parts
|
||||||
) {
|
) {
|
||||||
for (auto it = ostd::iter(parts); !it.empty(); ++it) {
|
for (auto it = ostd::iter(parts); !it.empty(); ++it) {
|
||||||
ConstCharRange elem = it.front();
|
string_range elem = it.front();
|
||||||
if (elem == "*") {
|
if (elem == "*") {
|
||||||
++it;
|
++it;
|
||||||
/* skip multiple stars if present */
|
/* skip multiple stars if present */
|
||||||
|
@ -86,12 +86,12 @@ static bool ob_path_matches(
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ob_expand_glob(
|
static bool ob_expand_glob(
|
||||||
std::string &ret, ConstCharRange src, bool ne = false
|
std::string &ret, string_range src, bool ne = false
|
||||||
);
|
);
|
||||||
|
|
||||||
static bool ob_expand_dir(
|
static bool ob_expand_dir(
|
||||||
std::string &ret, ConstCharRange dir,
|
std::string &ret, string_range dir,
|
||||||
std::vector<ConstCharRange> const &parts, ConstCharRange slash
|
std::vector<string_range> const &parts, string_range slash
|
||||||
) {
|
) {
|
||||||
ostd::DirectoryStream d(dir);
|
ostd::DirectoryStream d(dir);
|
||||||
bool appended = false;
|
bool appended = false;
|
||||||
|
@ -99,7 +99,7 @@ static bool ob_expand_dir(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (auto fi: d.iter()) {
|
for (auto fi: d.iter()) {
|
||||||
ConstCharRange fn = fi.filename();
|
string_range fn = fi.filename();
|
||||||
/* check if filename matches */
|
/* check if filename matches */
|
||||||
if (!ob_path_matches(fn, parts)) {
|
if (!ob_path_matches(fn, parts)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -109,7 +109,7 @@ static bool ob_expand_dir(
|
||||||
/* if we reach this, we match; try recursively matching */
|
/* if we reach this, we match; try recursively matching */
|
||||||
if (!slash.empty()) {
|
if (!slash.empty()) {
|
||||||
afn.append(slash);
|
afn.append(slash);
|
||||||
ConstCharRange psl = slash + 1;
|
string_range psl = slash + 1;
|
||||||
if (!ostd::find(psl, '*').empty()) {
|
if (!ostd::find(psl, '*').empty()) {
|
||||||
if (!appended) {
|
if (!appended) {
|
||||||
appended = ob_expand_glob(ret, ostd::iter(afn));
|
appended = ob_expand_glob(ret, ostd::iter(afn));
|
||||||
|
@ -136,8 +136,8 @@ static bool ob_expand_dir(
|
||||||
return appended;
|
return appended;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ob_expand_glob(std::string &ret, ConstCharRange src, bool ne) {
|
static bool ob_expand_glob(std::string &ret, string_range src, bool ne) {
|
||||||
ConstCharRange star = ostd::find(src, '*');
|
string_range star = ostd::find(src, '*');
|
||||||
/* no star use as-is */
|
/* no star use as-is */
|
||||||
if (star.empty()) {
|
if (star.empty()) {
|
||||||
if (ne) return false;
|
if (ne) return false;
|
||||||
|
@ -148,28 +148,28 @@ static bool ob_expand_glob(std::string &ret, ConstCharRange src, bool ne) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/* part before star */
|
/* part before star */
|
||||||
ConstCharRange prestar = slice_until(src, star);
|
string_range prestar = slice_until(src, star);
|
||||||
/* try finding slash before star */
|
/* try finding slash before star */
|
||||||
ConstCharRange slash = ostd::find_last(prestar, '/');
|
string_range slash = ostd::find_last(prestar, '/');
|
||||||
/* directory to scan */
|
/* directory to scan */
|
||||||
ConstCharRange dir = ".";
|
string_range dir = ".";
|
||||||
/* part of name before star */
|
/* part of name before star */
|
||||||
ConstCharRange fnpre = prestar;
|
string_range fnpre = prestar;
|
||||||
if (!slash.empty()) {
|
if (!slash.empty()) {
|
||||||
/* there was slash, adjust directory + prefix accordingly */
|
/* there was slash, adjust directory + prefix accordingly */
|
||||||
dir = slice_until(src, slash);
|
dir = slice_until(src, slash);
|
||||||
fnpre = slash + 1;
|
fnpre = slash + 1;
|
||||||
}
|
}
|
||||||
/* part after star */
|
/* part after star */
|
||||||
ConstCharRange fnpost = star + 1;
|
string_range fnpost = star + 1;
|
||||||
/* if a slash follows, adjust */
|
/* if a slash follows, adjust */
|
||||||
ConstCharRange nslash = ostd::find(fnpost, '/');
|
string_range nslash = ostd::find(fnpost, '/');
|
||||||
if (!nslash.empty()) {
|
if (!nslash.empty()) {
|
||||||
fnpost = slice_until(fnpost, nslash);
|
fnpost = slice_until(fnpost, nslash);
|
||||||
}
|
}
|
||||||
/* retrieve the single element with whatever stars in it, chop it up */
|
/* retrieve the single element with whatever stars in it, chop it up */
|
||||||
std::vector<ConstCharRange> parts;
|
std::vector<string_range> parts;
|
||||||
ob_get_path_parts(parts, ConstCharRange(&fnpre[0], &fnpost[fnpost.size()]));
|
ob_get_path_parts(parts, string_range(&fnpre[0], &fnpost[fnpost.size()]));
|
||||||
/* do a directory scan and match */
|
/* do a directory scan and match */
|
||||||
if (!ob_expand_dir(ret, dir, parts, nslash)) {
|
if (!ob_expand_dir(ret, dir, parts, nslash)) {
|
||||||
if (ne) {
|
if (ne) {
|
||||||
|
@ -187,9 +187,9 @@ static bool ob_expand_glob(std::string &ret, ConstCharRange src, bool ne) {
|
||||||
/* check funcs */
|
/* check funcs */
|
||||||
|
|
||||||
static bool ob_check_ts(
|
static bool ob_check_ts(
|
||||||
ConstCharRange tname, std::vector<std::string> const &deps
|
string_range tname, std::vector<std::string> const &deps
|
||||||
) {
|
) {
|
||||||
auto get_ts = [](ConstCharRange fname) {
|
auto get_ts = [](string_range fname) {
|
||||||
ostd::FileInfo fi(fname);
|
ostd::FileInfo fi(fname);
|
||||||
if (fi.type() != ostd::FileType::regular) {
|
if (fi.type() != ostd::FileType::regular) {
|
||||||
return time_t(0);
|
return time_t(0);
|
||||||
|
@ -209,12 +209,12 @@ static bool ob_check_ts(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ob_check_file(ConstCharRange fname) {
|
static bool ob_check_file(string_range fname) {
|
||||||
return ostd::FileStream(fname, ostd::StreamMode::read).is_open();
|
return ostd::FileStream(fname, ostd::StreamMode::read).is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ob_check_exec(
|
static bool ob_check_exec(
|
||||||
ConstCharRange tname, std::vector<std::string> const &deps
|
string_range tname, std::vector<std::string> const &deps
|
||||||
) {
|
) {
|
||||||
if (!ob_check_file(tname)) {
|
if (!ob_check_file(tname)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -228,8 +228,8 @@ static bool ob_check_exec(
|
||||||
}
|
}
|
||||||
|
|
||||||
/* this lets us properly match % patterns in target names */
|
/* this lets us properly match % patterns in target names */
|
||||||
static ConstCharRange ob_compare_subst(
|
static string_range ob_compare_subst(
|
||||||
ConstCharRange expanded, ConstCharRange toexpand
|
string_range expanded, string_range toexpand
|
||||||
) {
|
) {
|
||||||
auto rep = ostd::find(toexpand, '%');
|
auto rep = ostd::find(toexpand, '%');
|
||||||
/* no subst found */
|
/* no subst found */
|
||||||
|
@ -267,7 +267,7 @@ static ConstCharRange ob_compare_subst(
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ObState: cs_state {
|
struct ObState: cs_state {
|
||||||
ConstCharRange progname;
|
string_range progname;
|
||||||
bool ignore_env = false;
|
bool ignore_env = false;
|
||||||
|
|
||||||
/* represents a rule definition, possibly with a function */
|
/* represents a rule definition, possibly with a function */
|
||||||
|
@ -286,11 +286,11 @@ struct ObState: cs_state {
|
||||||
std::vector<Rule> rules;
|
std::vector<Rule> rules;
|
||||||
|
|
||||||
struct SubRule {
|
struct SubRule {
|
||||||
ConstCharRange sub;
|
string_range sub;
|
||||||
Rule *rule;
|
Rule *rule;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unordered_map<ConstCharRange, std::vector<SubRule>> cache;
|
std::unordered_map<string_range, std::vector<SubRule>> cache;
|
||||||
|
|
||||||
struct RuleCounter {
|
struct RuleCounter {
|
||||||
RuleCounter(): p_cond(), p_mtx(), p_counter(0), p_result(0) {}
|
RuleCounter(): p_cond(), p_mtx(), p_counter(0), p_result(0) {}
|
||||||
|
@ -340,7 +340,7 @@ struct ObState: cs_state {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ...A>
|
template<typename ...A>
|
||||||
int error(int retcode, ConstCharRange fmt, A &&...args) {
|
int error(int retcode, string_range fmt, A &&...args) {
|
||||||
ostd::err.write(progname, ": ");
|
ostd::err.write(progname, ": ");
|
||||||
ostd::err.writefln(fmt, std::forward<A>(args)...);
|
ostd::err.writefln(fmt, std::forward<A>(args)...);
|
||||||
return retcode;
|
return retcode;
|
||||||
|
@ -348,12 +348,12 @@ struct ObState: cs_state {
|
||||||
|
|
||||||
int exec_list(
|
int exec_list(
|
||||||
std::vector<SubRule> const &rlist, std::vector<std::string> &subdeps,
|
std::vector<SubRule> const &rlist, std::vector<std::string> &subdeps,
|
||||||
ConstCharRange tname
|
string_range tname
|
||||||
) {
|
) {
|
||||||
std::string repd;
|
std::string repd;
|
||||||
for (auto &sr: rlist) {
|
for (auto &sr: rlist) {
|
||||||
for (auto &target: sr.rule->deps) {
|
for (auto &target: sr.rule->deps) {
|
||||||
ConstCharRange atgt = ostd::iter(target);
|
string_range atgt = ostd::iter(target);
|
||||||
repd.clear();
|
repd.clear();
|
||||||
auto lp = ostd::find(atgt, '%');
|
auto lp = ostd::find(atgt, '%');
|
||||||
if (!lp.empty()) {
|
if (!lp.empty()) {
|
||||||
|
@ -375,7 +375,7 @@ struct ObState: cs_state {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int exec_func(ConstCharRange tname, std::vector<SubRule> const &rlist) {
|
int exec_func(string_range tname, std::vector<SubRule> const &rlist) {
|
||||||
std::vector<std::string> subdeps;
|
std::vector<std::string> subdeps;
|
||||||
int ret = wait_result([&rlist, &subdeps, &tname, this]() {
|
int ret = wait_result([&rlist, &subdeps, &tname, this]() {
|
||||||
return exec_list(rlist, subdeps, tname);
|
return exec_list(rlist, subdeps, tname);
|
||||||
|
@ -424,14 +424,14 @@ struct ObState: cs_state {
|
||||||
return run_int(rule->func);
|
return run_int(rule->func);
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_rules(ConstCharRange target, std::vector<SubRule> &rlist) {
|
int find_rules(string_range target, std::vector<SubRule> &rlist) {
|
||||||
if (!rlist.empty()) {
|
if (!rlist.empty()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
SubRule *frule = nullptr;
|
SubRule *frule = nullptr;
|
||||||
bool exact = false;
|
bool exact = false;
|
||||||
for (auto &rule: rules) {
|
for (auto &rule: rules) {
|
||||||
if (target == rule.target) {
|
if (target == string_range{rule.target}) {
|
||||||
rlist.emplace_back();
|
rlist.emplace_back();
|
||||||
rlist.back().rule = &rule;
|
rlist.back().rule = &rule;
|
||||||
if (rule.func) {
|
if (rule.func) {
|
||||||
|
@ -451,7 +451,7 @@ struct ObState: cs_state {
|
||||||
if (exact || !rule.func) {
|
if (exact || !rule.func) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ConstCharRange sub = ob_compare_subst(target, rule.target);
|
string_range sub = ob_compare_subst(target, rule.target);
|
||||||
if (!sub.empty()) {
|
if (!sub.empty()) {
|
||||||
rlist.emplace_back();
|
rlist.emplace_back();
|
||||||
SubRule &sr = rlist.back();
|
SubRule &sr = rlist.back();
|
||||||
|
@ -473,7 +473,7 @@ struct ObState: cs_state {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int exec_rule(ConstCharRange target, ConstCharRange from = nullptr) {
|
int exec_rule(string_range target, string_range from = nullptr) {
|
||||||
std::vector<SubRule> &rlist = cache[target];
|
std::vector<SubRule> &rlist = cache[target];
|
||||||
int fret = find_rules(target, rlist);
|
int fret = find_rules(target, rlist);
|
||||||
if (fret) {
|
if (fret) {
|
||||||
|
@ -496,12 +496,12 @@ struct ObState: cs_state {
|
||||||
return exec_func(target, rlist);
|
return exec_func(target, rlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
int exec_main(ConstCharRange target) {
|
int exec_main(string_range target) {
|
||||||
return wait_result([&target, this]() { return exec_rule(target); });
|
return wait_result([&target, this]() { return exec_rule(target); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void rule_add(
|
void rule_add(
|
||||||
cs_state &cs, ConstCharRange tgt, ConstCharRange dep, cs_bcode *body,
|
cs_state &cs, string_range tgt, string_range dep, cs_bcode *body,
|
||||||
bool action = false
|
bool action = false
|
||||||
) {
|
) {
|
||||||
cscript::util::ListParser p{cs, tgt};
|
cscript::util::ListParser p{cs, tgt};
|
||||||
|
@ -519,12 +519,12 @@ struct ObState: cs_state {
|
||||||
}
|
}
|
||||||
|
|
||||||
void rule_dup(
|
void rule_dup(
|
||||||
cs_state &cs, ConstCharRange tgt, ConstCharRange ptgt,
|
cs_state &cs, string_range tgt, string_range ptgt,
|
||||||
ConstCharRange dep, bool inherit_deps
|
string_range dep, bool inherit_deps
|
||||||
) {
|
) {
|
||||||
Rule *oldr = nullptr;
|
Rule *oldr = nullptr;
|
||||||
for (auto &rule: rules) {
|
for (auto &rule: rules) {
|
||||||
if (ptgt == rule.target) {
|
if (ptgt == string_range{rule.target}) {
|
||||||
oldr = &rule;
|
oldr = &rule;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -572,7 +572,7 @@ struct ObState: cs_state {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int print_help(bool is_error, ConstCharRange deffile) {
|
int print_help(bool is_error, string_range deffile) {
|
||||||
ostd::Stream &os = is_error ? ostd::err : ostd::out;
|
ostd::Stream &os = is_error ? ostd::err : ostd::out;
|
||||||
os.writeln(
|
os.writeln(
|
||||||
"Usage: ", progname, " [options] [action]\n",
|
"Usage: ", progname, " [options] [action]\n",
|
||||||
|
@ -590,8 +590,8 @@ struct ObState: cs_state {
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
ObState os;
|
ObState os;
|
||||||
ConstCharRange pn = argv[0];
|
string_range pn = argv[0];
|
||||||
ConstCharRange lslash = ostd::find_last(pn, '/');
|
string_range lslash = ostd::find_last(pn, '/');
|
||||||
os.progname = lslash.empty() ? pn : (lslash + 1);
|
os.progname = lslash.empty() ? pn : (lslash + 1);
|
||||||
|
|
||||||
os.init_libs();
|
os.init_libs();
|
||||||
|
@ -599,8 +599,8 @@ int main(int argc, char **argv) {
|
||||||
int ncpus = std::thread::hardware_concurrency();
|
int ncpus = std::thread::hardware_concurrency();
|
||||||
os.new_ivar("numcpus", 4096, 1, ncpus);
|
os.new_ivar("numcpus", 4096, 1, ncpus);
|
||||||
|
|
||||||
ConstCharRange fcont;
|
string_range fcont;
|
||||||
ConstCharRange deffile = "obuild.cfg";
|
string_range deffile = "obuild.cfg";
|
||||||
|
|
||||||
int jobs = 1, posarg = argc;
|
int jobs = 1, posarg = argc;
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
@ -612,7 +612,7 @@ int main(int argc, char **argv) {
|
||||||
} else if ((argn == 'h') || (!argv[i][2] && ((i + 1) >= argc))) {
|
} else if ((argn == 'h') || (!argv[i][2] && ((i + 1) >= argc))) {
|
||||||
return os.print_help(argn != 'h', deffile);
|
return os.print_help(argn != 'h', deffile);
|
||||||
}
|
}
|
||||||
ConstCharRange val = (argv[i][2] == '\0') ? argv[++i] : &argv[i][2];
|
string_range val = (argv[i][2] == '\0') ? argv[++i] : &argv[i][2];
|
||||||
switch (argn) {
|
switch (argn) {
|
||||||
case 'C':
|
case 'C':
|
||||||
if (!ostd::directory_change(val)) {
|
if (!ostd::directory_change(val)) {
|
||||||
|
@ -676,9 +676,9 @@ int main(int argc, char **argv) {
|
||||||
});
|
});
|
||||||
|
|
||||||
os.new_command("extreplace", "sss", [](auto &cs, auto args, auto &res) {
|
os.new_command("extreplace", "sss", [](auto &cs, auto args, auto &res) {
|
||||||
ConstCharRange lst = args[0].get_strr();
|
string_range lst = args[0].get_strr();
|
||||||
ConstCharRange oldext = args[1].get_strr();
|
string_range oldext = args[1].get_strr();
|
||||||
ConstCharRange newext = args[2].get_strr();
|
string_range newext = args[2].get_strr();
|
||||||
std::string ret;
|
std::string ret;
|
||||||
if (oldext.front() == '.') {
|
if (oldext.front() == '.') {
|
||||||
oldext.pop_front();
|
oldext.pop_front();
|
||||||
|
@ -689,7 +689,7 @@ int main(int argc, char **argv) {
|
||||||
cscript::util::ListParser p{cs, lst};
|
cscript::util::ListParser p{cs, lst};
|
||||||
while (p.parse()) {
|
while (p.parse()) {
|
||||||
auto elem = p.get_item();
|
auto elem = p.get_item();
|
||||||
ConstCharRange it = ostd::iter(elem);
|
string_range it = ostd::iter(elem);
|
||||||
if (!ret.empty()) {
|
if (!ret.empty()) {
|
||||||
ret += ' ';
|
ret += ' ';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue