Compare commits

...

4 Commits

Author SHA1 Message Date
Daniel Kolesa 4af8dc8e25 rebase to latest libcubescript 2021-05-04 03:16:12 +02:00
Daniel Kolesa 57fef39b5e switch to coroutine-based tasks
now that the cubescript library supports threads properly, we can
rely on this and get much better behavior when calling compiler
2021-04-05 00:56:59 +02:00
Daniel Kolesa 1a6b137935 migrate to current libcubescript api 2021-04-04 20:33:13 +02:00
Daniel Kolesa 876250f0c8 switch to bsd-2-clause 2020-04-27 14:26:36 +02:00
3 changed files with 125 additions and 110 deletions

View File

@ -1,14 +1,7 @@
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:
Copyright (c) 2015-2020 Daniel "q66" Kolesa. All rights reserved.
Copyright (c) 2015 Daniel "q66" Kolesa. All rights reserved.
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:
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.
@ -17,14 +10,14 @@ so, subject to the following conditions:
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
* Neither the names of OctaSTD developers nor any contributors may be
used to endorse or promote products derived from this Software without
specific prior written permission.
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
SOFTWARE.**
**THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.**

198
main.cc
View File

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

View File

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