Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

3 changed files with 110 additions and 125 deletions

View File

@ -1,7 +1,14 @@
Copyright (c) 2015-2020 Daniel "q66" Kolesa. All rights reserved. OctaBuild is licensed under the University of Illinois/NCSA Open Source License,
a permissive, non-copyleft, BSD style license. The license text goes as follows:
Redistribution and use in source and binary forms, with or without modification, Copyright (c) 2015 Daniel "q66" Kolesa. All rights reserved.
are permitted provided that the following conditions are met:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
* Redistributions of source code must retain the above copyright notice, * Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers. this list of conditions and the following disclaimers.
@ -10,14 +17,14 @@ are permitted provided that the following conditions are met:
this list of conditions and the following disclaimers in the this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
**THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * Neither the names of OctaSTD developers nor any contributors may be
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE used to endorse or promote products derived from this Software without
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE specific prior written permission.
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR **THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
THE POSSIBILITY OF SUCH DAMAGE.** SOFTWARE.**

198
main.cc
View File

@ -11,124 +11,130 @@
#include <ostd/argparse.hh> #include <ostd/argparse.hh>
#include <ostd/build/make.hh> #include <ostd/build/make.hh>
#include <ostd/build/make_coroutine.hh>
#include <cubescript/cubescript.hh> #include <cubescript/cubescript.hh>
namespace cs = cubescript; using ostd::string_range;
using ostd::path;
using cscript::cs_state;
using cscript::cs_value_r;
using cscript::cs_value;
using cscript::cs_stacked_value;
using cscript::cs_bcode_ref;
using cscript::cs_bcode;
using cscript::util::list_parser;
namespace fs = ostd::fs; namespace fs = ostd::fs;
namespace build = ostd::build; namespace build = ostd::build;
static void rule_add( static void rule_add(
cs::state &cs, build::make &mk, cs_state &cs, build::make &mk,
std::string_view target, std::string_view depends, string_range target, string_range depends,
cs::bcode_ref body, bool action = false cs_bcode *body, bool action = false
) { ) {
build::make_rule::body_func bodyf{}; build::make_rule::body_func bodyf{};
if (!body.empty()) { if (!cscript::cs_code_is_empty(body)) {
bodyf = [body, &cs](auto tgt, auto srcs) { bodyf = [body = cs_bcode_ref(body), &cs](auto tgt, auto srcs) {
auto ts = cs.new_thread(); cs_stacked_value targetv, sourcev, sourcesv;
cs::alias_local target{ts, "target"};
cs::alias_local source{ts, "source"};
cs::alias_local sources{ts, "sources"};
cs::any_value idv{}; if (!targetv.set_alias(cs.new_ident("target"))) {
idv.set_string(tgt, ts); throw build::make_error{
target.set(std::move(idv)); "internal error: could not set alias 'target'"
};
}
targetv.set_cstr(tgt);
targetv.push();
if (!srcs.empty()) { if (!srcs.empty()) {
if (!sourcev.set_alias(cs.new_ident("source"))) {
throw build::make_error{
"internal error: could not set alias 'source'"
};
}
if (!sourcesv.set_alias(cs.new_ident("sources"))) {
throw build::make_error{
"internal error: could not set alias 'sources'"
};
}
idv.set_string(srcs[0], ts); sourcev.set_cstr(srcs[0]);
source.set(std::move(idv)); sourcev.push();
auto dsv = ostd::appender<std::string>(); auto dsv = ostd::appender<std::string>();
ostd::format(dsv, "%(%s %)", srcs); ostd::format(dsv, "%(%s %)", srcs);
idv.set_string(dsv.get(), cs); sourcesv.set_str(std::move(dsv.get()));
sources.set(std::move(idv)); sourcesv.push();
} }
try { try {
body.call(ts); cs.run(body);
} catch (cs::error const &e) { } catch (cscript::cs_error const &e) {
throw build::make_error{e.what()}; throw build::make_error{e.what()};
} }
}; };
} }
cs::list_parser p{cs, target}; list_parser p{cs, target};
while (p.parse()) { for (auto tr: p.iter()) {
cs::list_parser lp{cs, depends}; list_parser lp{cs, depends};
auto &r = mk.rule( mk.rule(tr).action(action).body(bodyf).depend(lp.iter());
std::string_view{p.get_item()}
).action(action).body(bodyf);
while (lp.parse()) {
r.depend(std::string_view{lp.get_item()});
}
} }
} }
static void init_rulelib(cs::state &s, build::make &mk) { static void init_rulelib(cs_state &cs, build::make &mk) {
s.new_command("rule", "ssb", [&mk](auto &css, auto args, auto &) { cs.new_command("rule", "sse", [&cs, &mk](auto &, auto args, auto &) {
rule_add( rule_add(
css, mk, args[0].get_string(css), cs, mk, args[0].get_strr(), args[1].get_strr(), args[2].get_code()
args[1].get_string(css), args[2].get_code()
); );
}); });
s.new_command("action", "sb", [&mk](auto &css, auto args, auto &) { cs.new_command("action", "se", [&cs, &mk](auto &, auto args, auto &) {
rule_add( rule_add(
css, mk, args[0].get_string(css), std::string_view{}, cs, mk, args[0].get_strr(), nullptr, args[1].get_code(), true
args[1].get_code(), true
); );
}); });
s.new_command("depend", "ss", [&mk](auto &css, auto args, auto &) { cs.new_command("depend", "ss", [&cs, &mk](auto &, auto args, auto &) {
rule_add( rule_add(cs, mk, args[0].get_strr(), args[1].get_strr(), nullptr);
css, mk, args[0].get_string(css), args[1].get_string(css),
cs::bcode_ref{}
);
}); });
} }
static void init_baselib(cs::state &s, build::make &mk, bool ignore_env) { static void init_baselib(cs_state &cs, build::make &mk, bool ignore_env) {
s.new_command("echo", "...", [](auto &css, auto args, auto &) { cs.new_command("echo", "C", [](auto &, auto args, auto &) {
ostd::writeln(cs::concat_values(css, args, " ").view()); writeln(args[0].get_strr());
}); });
s.new_command("shell", "...", [&mk](auto &css, auto args, auto &) { cs.new_command("shell", "C", [&mk](auto &, auto args, auto &) {
mk.push_task([ mk.push_task([ds = std::string(args[0].get_strr())]() {
ds = std::string{cs::concat_values(css, args, " ").view()}
]() {
if (system(ds.data())) { if (system(ds.data())) {
throw build::make_error{""}; throw build::make_error{""};
} }
}); });
}); });
s.new_command("getenv", "ss", [ignore_env](auto &css, auto args, auto &res) { cs.new_command("getenv", "ss", [ignore_env](auto &, auto args, auto &res) {
if (ignore_env) { if (ignore_env) {
res.set_string("", css); res.set_cstr("");
return; return;
} }
res.set_string(ostd::env_get( res.set_str(std::move(
std::string_view{args[0].get_string(css)} ostd::env_get(args[0].get_str()).value_or(args[1].get_str())
).value_or( ));
std::string{std::string_view{args[1].get_string(css)}}
), css);
}); });
s.new_command("invoke", "s", [&mk](auto &css, auto args, auto &) { cs.new_command("invoke", "s", [&mk](auto &, auto args, auto &) {
mk.exec(std::string_view{args[0].get_string(css)}); mk.exec(args[0].get_strr());
}); });
} }
static void init_pathlib(cs::state &s) { static void init_pathlib(cs_state &cs) {
s.new_command("extreplace", "sss", [](auto &css, auto args, auto &res) { cs.new_command("extreplace", "sss", [](auto &cs, auto args, auto &res) {
ostd::string_range oldext = std::string_view{args[1].get_string(css)}; string_range oldext = args[1].get_strr();
ostd::string_range newext = std::string_view{args[2].get_string(css)}; string_range newext = args[2].get_strr();
std::string ret; std::string ret;
cs::list_parser p{css, args[0].get_string(css)}; list_parser p{cs, args[0].get_strr()};
while (p.parse()) { for (auto ps: p.iter()) {
ostd::path np{std::string_view{p.get_item()}}; ostd::path np{ps};
if (!ret.empty()) { if (!ret.empty()) {
ret += ' '; ret += ' ';
} }
@ -136,51 +142,25 @@ static void init_pathlib(cs::state &s) {
(np.suffixes() == oldext) ? np.with_suffixes(newext) : np (np.suffixes() == oldext) ? np.with_suffixes(newext) : np
).string(); ).string();
} }
res.set_string(ret, css); res.set_str(std::move(ret));
}); });
s.new_command("glob", "...", [](auto &css, auto args, auto &res) { cs.new_command("glob", "C", [](auto &cs, auto args, auto &res) {
auto app = ostd::appender<std::vector<ostd::path>>(); auto app = ostd::appender<std::vector<path>>();
cs::list_parser p{css, cs::concat_values(css, args, " ")}; list_parser p{cs, args[0].get_strr()};
while (p.parse()) { for (auto ps: p.iter()) {
auto it = p.get_item(); fs::glob_match(app, ps);
fs::glob_match(app, std::string_view{it});
} }
res.set_string(ostd::format( res.set_str(ostd::format(
ostd::appender<std::string>(), "%(%s %)", app.get() ostd::appender<std::string>(), "%(%s %)", app.get()
).get(), css); ).get());
}); });
} }
static bool do_run_file(cs::state &s, std::string_view fname) {
ostd::file_stream f{fname};
if (!f.is_open()) {
return false;
}
f.seek(0, ostd::stream_seek::END);
auto len = f.tell();
f.seek(0);
auto buf = std::make_unique<char[]>(len + 1);
if (!buf) {
return false;
}
if (f.read_bytes(buf.get(), len) != std::size_t(len)) {
return false;
}
buf[len] = '\0';
s.compile(std::string_view{buf.get(), std::size_t(len)}, fname).call(s);
return true;
}
void do_main(int argc, char **argv) { void do_main(int argc, char **argv) {
/* cubescript interpreter */ /* cubescript interpreter */
cs::state s; cs_state cs;
cs::std_init_all(s); cs.init_libs();
/* arg values */ /* arg values */
std::string action = "default"; std::string action = "default";
@ -241,8 +221,8 @@ void do_main(int argc, char **argv) {
jobs = std::max(1, jobs ? jobs : ncpus); jobs = std::max(1, jobs ? jobs : ncpus);
/* core cubescript variables */ /* core cubescript variables */
s.new_var("numcpus", ncpus, true); cs.new_ivar("numcpus", 4096, 1, ncpus);
s.new_var("numjobs", jobs, true); cs.new_ivar("numjobs", 4096, 1, jobs);
/* switch to target directory */ /* switch to target directory */
try { try {
@ -255,18 +235,16 @@ void do_main(int argc, char **argv) {
}; };
} }
/* init buildsystem, use coroutine tasks */ /* init buildsystem, use simple tasks, cubescript cannot into coroutines */
build::make mk{build::make_task_coroutine, jobs}; build::make mk{build::make_task_simple, jobs};
/* octabuild cubescript libs */ /* octabuild cubescript libs */
init_rulelib(s, mk); init_rulelib(cs, mk);
init_baselib(s, mk, ignore_env); init_baselib(cs, mk, ignore_env);
init_pathlib(s); init_pathlib(cs);
/* parse rules */ /* parse rules */
if (( if ((!fcont.empty() && !cs.run_bool(fcont)) || !cs.run_file(deffile)) {
!fcont.empty() && !s.compile(fcont).call(s).get_bool()
) || !do_run_file(s, deffile)) {
throw build::make_error{"failed creating rules"}; throw build::make_error{"failed creating rules"};
} }

View File

@ -24,6 +24,6 @@ action clean [
shell rm -f $FILES obuild_ob shell rm -f $FILES obuild_ob
] ]
depend main_ob.o [@CS_PATH/include/cubescript/cubescript.hh] depend main_ob.o [tpool.hh @CS_PATH/include/cubescript/cubescript.hh]
rule default obuild rule default obuild