11 #include <boost/filesystem.hpp>
18 #include <vcsn/config.hh>
33 :
std::runtime_error(what)
41 xgetenv(
const std::string& var,
const std::string& val =
"")
43 const char* cp = getenv(var.c_str());
47 #define XGETENV(Name) xgetenv(#Name, Name)
51 std::string expand_tilda(std::string res)
53 if (!res.empty() && res[0] ==
'~')
55 assert(res.size() == 1 || res[1] ==
'/');
56 auto home = xgetenv(
"HOME", xgetenv(
"USERPROFILE"));
57 char const *hdrive = getenv(
"HOMEDRIVE");
58 char const *hres = getenv(
"HOMERES");
60 res.replace(0, 1, home);
61 else if (hdrive && hres)
62 res.replace(0, 1, std::string(hdrive) + hres);
64 res.replace(0, 1, xgetenv(
"VCSN_TMPDIR",
"/tmp"));
70 void ensure_parent_directory(
const std::string& path)
72 boost::filesystem::path p(path);
73 boost::filesystem::create_directories(p.parent_path());
77 std::string tmpname(std::string res)
97 void print(
const std::string& base)
99 ensure_parent_directory(base);
102 auto tmp = tmpname(base);
104 std::ofstream o{tmp +
".cc"};
107 boost::filesystem::rename(tmp +
".cc", base +
".cc");
116 auto ast =
parser_.parse_context();
121 void print_type(
const std::string&
type)
132 void throw_compiler_errors(std::string cmd,
133 const std::string& err)
148 static std::regex r1(
"error: static assertion failed: (.*)$",
149 std::regex::extended);
150 static std::regex r2(
"error: static_assert failed \"(.*)\"$",
151 std::regex::extended);
154 std::string assertions;
155 while (std::getline(*
is, line))
156 if (std::regex_search(line, smatch, r1)
157 || std::regex_search(line, smatch, r2))
158 assertions += std::string(smatch[1]) +
'\n';
159 if (getenv(
"VCSN_VERBOSE"))
161 cmd +=
"\n compiler error messages:\n";
164 throw jit_error(assertions,
" failed command:\n " + cmd);
171 void cxx(std::string cmd,
const std::string& tmp)
173 auto err = tmp +
".err";
174 if (getenv(
"VCSN_DEBUG"))
175 std::cerr <<
"run: " << cmd << std::endl;
176 if (system((cmd +
" 2>'" + err +
"'").c_str()))
177 throw_compiler_errors(cmd, err);
181 std::ifstream
log{err};
182 std::cerr <<
log.rdbuf();
183 boost::filesystem::remove(err);
190 void cxx_compile(
const std::string& base)
192 auto tmp = tmpname(base);
195 auto cmd = (std::string{
"LC_ALL=C"}
200 +
" -fPIC '" + base +
".cc' -c"
201 +
" -o '" + tmp +
".o'");
208 void cxx_link(
const std::string& base)
210 auto tmp = tmpname(base);
211 auto cmd = (std::string{
"LC_ALL=C"}
215 +
" -fPIC -lvcsn '" + tmp +
".o' -shared"
216 +
" -o '" + tmp +
".so'"
222 std::string plugindir()
const
224 auto res = xgetenv(
"VCSN_PLUGINDIR",
225 xgetenv(
"VCSN_HOME",
"~/.vcsn") +
"/plugins");
226 res = expand_tilda(res);
232 std::string
split(
const std::string& s)
const
235 const size_t size = 200;
236 for (
unsigned i = 0; i < s.length(); i +=
size)
240 res += s.substr(i, size);
253 void jit(
const std::string& base)
255 auto tmp = tmpname(base);
257 namespace chr = std::chrono;
258 using clock = chr::steady_clock;
259 auto start = clock::now();
260 static bool no_python = !!getenv(
"VCSN_NO_PYTHON");
265 boost::filesystem::rename(tmp +
".so", base +
".so");
270 if (!getenv(
"VCSN_DEBUG"))
271 boost::filesystem::remove(tmp +
".o");
275 auto linkflags =
printer_.linkflags();
276 if (!linkflags.empty())
277 linkflags =
" --extra-ldflags='" + linkflags +
"'";
278 cxx(
"vcsn compile -shared '" + base +
".cc'" + linkflags,
282 = chr::duration_cast<chr::milliseconds>(clock::now() - start);
283 if (getenv(
"VCSN_TIME"))
285 std::ofstream{
"/tmp/vcsn-compile.log",
286 std::ofstream::out | std::ofstream::app}
288 << (no_python ?
"C++, " :
"Py, ")
289 <<
'\'' << base.substr(plugindir().
size()) <<
'\''
291 if (getenv(
"VCSN_TIME2"))
292 std::cerr << d.count() <<
"ms: " << base <<
'\n';
295 static bool first =
true;
301 lt_dlhandle lib = lt_dlopen((base +
".so").c_str());
302 require(lib,
"cannot load lib: ", base,
".so: ", lt_dlerror());
306 void compile(
const std::string& ctx)
308 printer_.header(
"vcsn/ctx/instantiate.hh");
310 plugindir() +
"contexts/" +
split(ctx);
318 " VCSN_CTX_INSTANTIATE(ctx_t);\n"
328 printer_.header(
"vcsn/misc/attributes.hh");
329 printer_.header(
"vcsn/misc/name.hh");
330 printer_.header(
"vcsn/dyn/registries.hh");
331 for (
const auto& algo: algos)
335 for (
const auto& algo: algos)
338 <<
"// " << algo.first <<
'.';
341 for (
const auto& s: algo.second)
348 types += (first ?
"" :
", ") + t;
354 "static bool " << algo.first <<
" ATTRIBUTE_USED =" <<
incendl
355 <<
"vcsn::dyn::detail::" << algo.first <<
"_register(" <<
incendl
356 <<
"vcsn::ssignature<" << types <<
">()," <<
iendl
357 <<
"vcsn::dyn::detail::" << algo.first <<
"<" << types <<
">" <<
decendl
363 std::string base = (plugindir()
365 + begin(algos)->first +
"/"
366 +
split(begin(algos)->second.to_string()));
372 std::istringstream
is;
374 std::ostringstream
os;
382 translation translate;
383 translate.compile(ctx);
388 translation translate;
389 std::set<std::pair<std::string, signature>> algos{{algo, sig}};
390 if (algo ==
"delay_automaton"
391 || algo ==
"is_synchronized")
393 algos.emplace(
"delay_automaton", sig);
394 algos.emplace(
"is_synchronized", sig);
396 translate.compile(algos);
ast::context_parser parser_
std::string type(const automaton &a)
The implementation type of a.
std::ostream & print(const automaton &aut, std::ostream &out, const std::string &format="default")
Print automaton a on o using format format.
std::istringstream is
The input stream: the specification to translate.
std::string to_string(identities i)
Wrapper around operator<<.
ast::context_printer printer_
std::ostream & iendl(std::ostream &o)
Print an end of line, then set the indentation.
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.
jit_error(const std::string &assert, const std::string &what)
static dyn::context ctx(const driver &d)
Get the context of the driver.
void compile(const std::string &ctx)
Compile, and load, a DSO with instantiations for ctx.
polynomial split(const expression &exp)
Break exp.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Signature of a function call.
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.
Indentation relative functions.
weightset_mixin< detail::log_impl > log
std::ostream & decendl(std::ostream &o)
Decrement the indentation, print an end of line, and set the indentation.