11 #include <boost/algorithm/string/case_conv.hpp> 12 #include <boost/filesystem.hpp> 34 :
std::runtime_error(what)
40 #define XGETENV(Name) xgetenv(#Name, Name) 45 config(
const std::string& var)
47 auto envvar =
"VCSN_" + boost::algorithm::to_upper_copy(var);
48 if (
auto cp = getenv(envvar.c_str()))
51 return get_config()[
"configuration"][var].str();
58 if (!res.empty() && res[0] ==
'~')
60 assert(res.size() == 1 || res[1] ==
'/');
62 char const *hdrive = getenv(
"HOMEDRIVE");
63 char const *hres = getenv(
"HOMERES");
65 res.replace(0, 1, home);
66 else if (hdrive && hres)
67 res.replace(0, 1, std::string(hdrive) + hres);
69 res.replace(0, 1,
xgetenv(
"VCSN_TMPDIR",
"/tmp"));
75 void ensure_parent_directory(
const std::string& path)
77 boost::filesystem::path p(path);
78 boost::filesystem::create_directories(p.parent_path());
82 std::string tmpname(std::string res)
105 void print(
const std::string& base)
107 ensure_parent_directory(base);
110 auto tmp = tmpname(base);
112 std::ofstream o{tmp +
".cc"};
116 boost::filesystem::remove(tmp +
".cc");
118 boost::filesystem::rename(tmp +
".cc", base +
".cc");
129 void print_type(
const std::string&
type)
137 void throw_compiler_errors(std::string cmd,
138 const std::string& err)
157 static auto r1 = std::regex{
"static assertion failed: (.*)$",
158 std::regex::extended};
159 static auto r2 = std::regex{
"static_assert failed \"(.*)\"$",
160 std::regex::extended};
164 while (std::getline(*is, line))
165 if (std::regex_search(line, smatch, r1)
166 || std::regex_search(line, smatch, r2))
167 assertions += std::string(smatch[1]) +
'\n';
168 if (getenv(
"VCSN_VERBOSE"))
170 cmd +=
"\n compiler error messages:\n";
173 throw jit_error(assertions,
" failed command:\n " + cmd);
180 void cxx(std::string cmd,
const std::string& tmp)
182 auto err = tmp +
".err";
183 if (getenv(
"VCSN_DEBUG"))
184 std::cerr <<
"run: " << cmd <<
'\n';
185 if (system((cmd +
" 2>'" + err +
"'").c_str()))
186 throw_compiler_errors(cmd, err);
190 std::ifstream
log{err};
191 std::cerr <<
log.rdbuf();
196 boost::filesystem::remove(err);
203 void cxx_compile(
const std::string& base)
205 auto tmp = tmpname(base);
208 auto cmd = (std::string{
"LC_ALL=C"}
209 +
" " + config(
"ccache")
210 +
" " + config(
"cxx")
211 +
" " + config(
"cxxflags")
212 +
" " + config(
"cppflags")
213 +
" -fPIC '" + base +
".cc' -c" 214 +
" -o '" + tmp +
".o'");
221 void cxx_link(
const std::string& base)
223 auto tmp = tmpname(base);
224 auto cmd = (std::string{
"LC_ALL=C"}
225 +
" " + config(
"cxx")
226 +
" " + config(
"cxxflags")
227 +
" " + config(
"ldflags")
228 +
" -fPIC -lvcsn '" + tmp +
".o' -shared" 229 +
" -o '" + tmp +
".so'" 235 std::string plugindir()
const 237 auto res =
xgetenv(
"VCSN_PLUGINDIR",
238 xgetenv(
"VCSN_HOME",
"~/.vcsn") +
"/plugins");
245 std::string
split(
const std::string& s)
const 247 auto res = std::string{};
248 const size_t size = 150;
249 for (
unsigned i = 0; i < s.length(); i +=
size)
253 res += s.substr(i, size);
266 void jit(
const std::string& base)
268 auto tmp = tmpname(base);
270 namespace chr = std::chrono;
271 using clock = chr::steady_clock;
272 auto start = clock::now();
273 static bool no_python = !!getenv(
"VCSN_NO_PYTHON");
278 boost::filesystem::rename(tmp +
".so", base +
".so");
283 if (!getenv(
"VCSN_DEBUG"))
284 boost::filesystem::remove(tmp +
".o");
288 auto linkflags =
printer_.linkflags();
289 if (!linkflags.empty())
290 linkflags =
" LDFLAGS+='" + linkflags +
"'";
291 cxx(
"vcsn compile -shared" + linkflags +
" '" + base +
".cc'",
295 = chr::duration_cast<chr::milliseconds>(clock::now() - start);
296 if (getenv(
"VCSN_TIME"))
298 std::ofstream{
"/tmp/vcsn-compile.log",
301 << (no_python ?
"C++, " :
"Py, ")
302 <<
'\'' << base.substr(plugindir().
size()) <<
'\'' 304 if (getenv(
"VCSN_TIME2"))
305 std::cerr << d.count() <<
"ms: " << base <<
'\n';
312 void compile(
const std::string& ctx)
314 printer_.header(
"vcsn/ctx/instantiate.hh");
315 auto base = plugindir() +
"contexts/" +
split(ctx);
323 " VCSN_CTX_INSTANTIATE(ctx_t);\n" 333 printer_.header(
"vcsn/misc/attributes.hh");
334 printer_.header(
"vcsn/dyn/name.hh");
335 printer_.header(
"vcsn/dyn/registries.hh");
336 for (
const auto& algo: algos)
340 for (
const auto& algo: algos)
343 <<
"// " << algo.first <<
'.';
346 for (
const auto& s: algo.second)
353 types += (first ?
"" :
", ") + t;
359 "static bool vcsn_" << algo.first <<
" ATTRIBUTE_USED =" 361 <<
"vcsn::dyn::detail::" << algo.first <<
"_register(" 363 <<
"vcsn::ssignature<" << types <<
">()," 365 <<
"vcsn::dyn::detail::" << algo.first <<
"<" << types <<
">" 372 auto base = (plugindir()
374 + begin(algos)->first +
"/" 375 +
split(begin(algos)->second.to_string()));
381 std::ostringstream
os;
388 translation translate;
389 translate.compile(ctx);
394 translation translate;
395 std::set<std::pair<std::string, signature>> algos{{algo, sig}};
396 if (algo ==
"delay_automaton" 397 || algo ==
"is_synchronized")
399 algos.emplace(
"delay_automaton", sig);
400 algos.emplace(
"is_synchronized", sig);
402 translate.compile(algos);
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
Indentation relative functions.
std::string xgetenv(const std::string &var, const std::string &val="")
getenv(var) if defined, otherwise val.
std::shared_ptr< ast_node > parse_type(const std::string &type)
Parse a type, and return its AST.
std::string assertions
If defined, static assertions that failed (ends with a eol).
weightset_mixin< detail::log_impl > log
detail::config & get_config()
Get the configuration singleton.
std::ostream & decendl(std::ostream &o)
Decrement the indentation, print an end of line, and set the indentation.
Signature of a function call.
std::string expand_tilda(const std::string &res)
Expand initial "~" in res.
ast::context_printer printer_
std::ostream & print(const automaton &aut, std::ostream &out=std::cout, const std::string &format="default")
Print automaton a on out using format format.
polynomial split(const expression &exp)
Break exp.
void compile(const std::string &ctx)
Compile, and load, a DSO with instantiations for ctx.
bool equal_files(const std::string &fn1, const std::string &fn2)
Whether two files have exactly equal contents.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
std::ostream & print_context(const context &ctx, std::ostream &o, const std::string &fmt)
Bridge (print).
std::string get_file_contents(const std::string &file)
Return the contents of file.
xlt_handle xlt_openext(const std::string &s, bool global)
Wrapper around lt_dlopenext.
jit_error(const std::string &assert, const std::string &what)
std::string to_string(identities i)
Wrapper around operator<<.
std::ostream & iendl(std::ostream &o)
Print an end of line, then set the indentation.
std::string type(const automaton &a)
The implementation type of a.
Request the set implementation (bool weights).
std::shared_ptr< ast_node > parse_context(const std::string &ctx)
Parse a context, and return its AST.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
std::ostream & incendl(std::ostream &o)
Increment the indentation, print an end of line, and set the indentation.
std::shared_ptr< std::istream > open_input_file(const std::string &file)
Open file for reading and return its autoclosing stream.