@@ -29,7 +29,7 @@ typedef HMODULE RCRL_Dynlib;
2929
3030#include < dlfcn.h>
3131typedef void * RCRL_Dynlib;
32- #define RDRL_LoadDynlib (lib ) dlopen(lib, RTLD_LAZY | RTLD_GLOBAL)
32+ #define RDRL_LoadDynlib (lib ) dlopen(lib, RTLD_NOW | RTLD_GLOBAL)
3333#define RCRL_CloseDynlib dlclose
3434#endif
3535
@@ -53,7 +53,9 @@ auto CopyFileToString(const string& f_name) {
5353 return out;
5454}
5555Plugin::Plugin (fs::path file, std::vector<string> flags)
56- : is_compiling_(false ), parser_(file.string() + " .cpp" , flags) {
56+ : is_compiling_(false ),
57+ parser_ (file.string() + ".cpp", flags),
58+ abort_(false ) {
5759 auto header = parser_.get_file ().replace_extension (" .hpp" );
5860 std::ofstream f (header, std::fstream::trunc | std::fstream::out);
5961 f << " #pragma once\n " ;
@@ -181,15 +183,29 @@ bool Plugin::CompileCode(string code) {
181183 return lambda_impl (ec, size, lambda_impl);
182184 };
183185 ap.async_read_some (output_buffer, OnStdout);
184- ios.run ();
185- c.join ();
186+ // support aborting compiling
187+ while (!ios.stopped () && !abort_) {
188+ ios.run_for (std::chrono::milliseconds (500 ));
189+ }
190+ if (abort_) {
191+ ios.stop ();
192+ c.terminate ();
193+ abort_ = false ;
194+ } else {
195+ c.join ();
196+ }
186197 is_compiling_ = false ;
187198 return c.exit_code ();
188199 });
189200 return true ;
190201}
191202
192203bool Plugin::IsCompiling () { return is_compiling_; }
204+ void Plugin::AbortCompiling () {
205+ if (IsCompiling ()) {
206+ abort_ = true ;
207+ }
208+ }
193209
194210bool Plugin::TryGetExitStatusFromCompile (int & exit_code) {
195211 if (compiler_process_.valid () && !IsCompiling ()) {
@@ -231,18 +247,18 @@ string Plugin::CopyAndLoadNewPlugin(bool redirect_stdout) {
231247 fd = dup (fileno (stdout));
232248 freopen (kRcrlOutputFile .c_str (), " w" , stdout);
233249 }
250+
251+ string out;
252+
234253 // load the plugin
235254 auto plugin = RDRL_LoadDynlib (name_copied.c_str ());
255+
236256 if (!plugin) {
237- fprintf (stderr, " %s\n " , dlerror ());
238- exit (EXIT_FAILURE);
257+ compiler_output_ = string (" Library loading error: " ) + dlerror ();
258+ } else {
259+ // add the plugin to the list of loaded ones - for later unloading
260+ plugins_.push_back ({name_copied, plugin});
239261 }
240- assert (plugin);
241-
242- // add the plugin to the list of loaded ones - for later unloading
243- plugins_.push_back ({name_copied, plugin});
244-
245- string out;
246262
247263 if (redirect_stdout) {
248264 // Flush stdout so any buffered messages are delivered
0 commit comments