2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-08-19 00:18:07 +02:00
|
|
|
|
2019-12-23 11:09:22 +01:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
time
|
|
|
|
)
|
2019-08-19 00:18:07 +02:00
|
|
|
|
2019-09-17 12:09:58 +02:00
|
|
|
fn (v &V) generate_hotcode_reloading_compiler_flags() []string {
|
2019-09-28 19:42:29 +02:00
|
|
|
mut a := []string
|
2019-08-19 00:18:07 +02:00
|
|
|
if v.pref.is_live || v.pref.is_so {
|
|
|
|
// See 'man dlopen', and test running a GUI program compiled with -live
|
2020-03-27 14:57:19 +01:00
|
|
|
if v.pref.os == .linux || os.user_os() == 'linux' {
|
2019-08-19 00:18:07 +02:00
|
|
|
a << '-rdynamic'
|
|
|
|
}
|
2020-03-27 14:57:19 +01:00
|
|
|
if v.pref.os == .mac || os.user_os() == 'mac' {
|
2019-08-19 00:18:07 +02:00
|
|
|
a << '-flat_namespace'
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 19:42:29 +02:00
|
|
|
return a
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:09:58 +02:00
|
|
|
fn (v &V) generate_hotcode_reloading_declarations() {
|
2019-09-28 19:42:29 +02:00
|
|
|
mut cgen := v.cgen
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os != .windows {
|
2019-08-19 00:18:07 +02:00
|
|
|
if v.pref.is_so {
|
|
|
|
cgen.genln('pthread_mutex_t live_fn_mutex;')
|
|
|
|
}
|
|
|
|
if v.pref.is_live {
|
|
|
|
cgen.genln('pthread_mutex_t live_fn_mutex = PTHREAD_MUTEX_INITIALIZER;')
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-08-19 00:18:07 +02:00
|
|
|
if v.pref.is_so {
|
|
|
|
cgen.genln('HANDLE live_fn_mutex;')
|
2019-10-21 03:15:43 +02:00
|
|
|
cgen.genln('
|
|
|
|
void pthread_mutex_lock(HANDLE *m) {
|
|
|
|
WaitForSingleObject(*m, INFINITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pthread_mutex_unlock(HANDLE *m) {
|
|
|
|
ReleaseMutex(*m);
|
|
|
|
}')
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
|
|
|
if v.pref.is_live {
|
|
|
|
cgen.genln('HANDLE live_fn_mutex = 0;')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 19:42:29 +02:00
|
|
|
fn (v &V) generate_hotcode_reloading_main_caller() {
|
2019-12-19 22:29:37 +01:00
|
|
|
if !v.pref.is_live {
|
|
|
|
return
|
|
|
|
}
|
2019-09-28 19:42:29 +02:00
|
|
|
// We are in live code reload mode, so start the .so loader in the background
|
|
|
|
mut cgen := v.cgen
|
|
|
|
cgen.genln('')
|
2020-03-19 15:49:07 +01:00
|
|
|
file_base := os.file_name(v.pref.path).replace('.v', '')
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os != .windows {
|
2019-09-28 19:42:29 +02:00
|
|
|
// unix:
|
|
|
|
so_name := file_base + '.so'
|
|
|
|
cgen.genln(' char *live_library_name = "$so_name";')
|
|
|
|
cgen.genln(' load_so(live_library_name);')
|
|
|
|
cgen.genln(' pthread_t _thread_so;')
|
2019-11-07 22:58:49 +01:00
|
|
|
cgen.genln(' pthread_create(&_thread_so , NULL, (void *)&reload_so, live_library_name);')
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-09-28 19:42:29 +02:00
|
|
|
// windows:
|
2019-12-19 22:29:37 +01:00
|
|
|
so_name := file_base + if v.pref.ccompiler == 'msvc' { '.dll' } else { '.so' }
|
2019-09-28 19:42:29 +02:00
|
|
|
cgen.genln(' char *live_library_name = "$so_name";')
|
|
|
|
cgen.genln(' live_fn_mutex = CreateMutexA(0, 0, 0);')
|
|
|
|
cgen.genln(' load_so(live_library_name);')
|
|
|
|
cgen.genln(' unsigned long _thread_so;')
|
|
|
|
cgen.genln(' _thread_so = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&reload_so, 0, 0, 0);')
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
|
2019-09-28 19:42:29 +02:00
|
|
|
fn (v &V) generate_hot_reload_code() {
|
|
|
|
mut cgen := v.cgen
|
2019-08-19 00:18:07 +02:00
|
|
|
// Hot code reloading
|
|
|
|
if v.pref.is_live {
|
2020-03-20 16:41:18 +01:00
|
|
|
mut file := os.real_path(v.pref.path)
|
2020-03-19 15:49:07 +01:00
|
|
|
file_base := os.file_name(file).replace('.v', '')
|
2019-08-19 00:18:07 +02:00
|
|
|
so_name := file_base + '.so'
|
|
|
|
// Need to build .so file before building the live application
|
|
|
|
// The live app needs to load this .so file on initialization.
|
|
|
|
mut vexe := os.args[0]
|
|
|
|
if os.user_os() == 'windows' {
|
2019-10-11 23:04:42 +02:00
|
|
|
vexe = cescaped_path(vexe)
|
|
|
|
file = cescaped_path(file)
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
|
|
|
mut msvc := ''
|
2019-10-14 07:41:46 +02:00
|
|
|
if v.pref.ccompiler == 'msvc' {
|
|
|
|
msvc = '-cc msvc'
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
2019-12-05 12:40:14 +01:00
|
|
|
so_debug_flag := if v.pref.is_debug { '-g' } else { '' }
|
2019-12-09 11:48:41 +01:00
|
|
|
cmd_compile_shared_library := '$vexe $msvc $so_debug_flag -o $file_base -solive -shared $file'
|
2020-03-06 18:53:29 +01:00
|
|
|
if v.pref.verbosity.is_higher_or_equal(.level_one) {
|
2019-09-28 19:42:29 +02:00
|
|
|
println(cmd_compile_shared_library)
|
|
|
|
}
|
|
|
|
ticks := time.ticks()
|
2020-03-19 14:50:37 +01:00
|
|
|
so_compilation_result := os.system(cmd_compile_shared_library)
|
2020-03-06 18:53:29 +01:00
|
|
|
if v.pref.verbosity.is_higher_or_equal(.level_two) {
|
2020-01-14 18:58:46 +01:00
|
|
|
diff := time.ticks() - ticks
|
|
|
|
println('compiling shared library took $diff ms')
|
|
|
|
println('=========\n')
|
|
|
|
}
|
2020-03-19 14:50:37 +01:00
|
|
|
if so_compilation_result != 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-08-19 00:18:07 +02:00
|
|
|
cgen.genln('
|
|
|
|
|
|
|
|
void lfnmutex_print(char *s){
|
2020-03-21 06:37:58 +01:00
|
|
|
#if 0
|
|
|
|
fflush(stderr);
|
|
|
|
fprintf(stderr,">> live_fn_mutex: %p | %s\\n", &live_fn_mutex, s);
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
|
|
|
')
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os != .windows {
|
2019-08-19 00:18:07 +02:00
|
|
|
cgen.genln('
|
2020-03-21 06:37:58 +01:00
|
|
|
#define _POSIX_C_SOURCE 1
|
|
|
|
#include <limits.h>
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void* live_lib = 0;
|
|
|
|
|
2019-08-19 00:18:07 +02:00
|
|
|
int load_so(byteptr path) {
|
2020-03-21 06:37:58 +01:00
|
|
|
char cpath[PATH_MAX] = {0};
|
|
|
|
int res = snprintf(cpath, sizeof (cpath), "./%s", path);
|
|
|
|
if (res >= sizeof (cpath)) {
|
|
|
|
fprintf (stderr, "path is too long");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-19 00:18:07 +02:00
|
|
|
//printf("load_so %s\\n", cpath);
|
|
|
|
if (live_lib) dlclose(live_lib);
|
|
|
|
live_lib = dlopen(cpath, RTLD_LAZY);
|
|
|
|
if (!live_lib) {
|
2020-03-21 06:37:58 +01:00
|
|
|
fprintf(stderr, "open failed");
|
2019-08-19 00:18:07 +02:00
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
')
|
|
|
|
for so_fn in cgen.so_fns {
|
|
|
|
cgen.genln('$so_fn = dlsym(live_lib, "$so_fn"); ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cgen.genln('
|
2020-03-21 06:37:58 +01:00
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
|
2019-10-12 07:41:41 +02:00
|
|
|
void pthread_mutex_lock(HANDLE *m) {
|
|
|
|
WaitForSingleObject(*m, INFINITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pthread_mutex_unlock(HANDLE *m) {
|
|
|
|
ReleaseMutex(*m);
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:58 +01:00
|
|
|
void* live_lib = NULL;
|
2019-08-19 00:18:07 +02:00
|
|
|
int load_so(byteptr path) {
|
2020-03-21 06:37:58 +01:00
|
|
|
char cpath[PATH_MAX];
|
|
|
|
int res = snprintf(cpath, sizeof (cpath), "./%s", path);
|
|
|
|
if (res >= sizeof(cpath)) {
|
|
|
|
puts("path is too long\\n");
|
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-19 00:18:07 +02:00
|
|
|
if (live_lib) FreeLibrary(live_lib);
|
|
|
|
live_lib = LoadLibraryA(cpath);
|
|
|
|
if (!live_lib) {
|
2020-03-21 06:37:58 +01:00
|
|
|
puts("open failed\\n");
|
2019-08-19 00:18:07 +02:00
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
')
|
|
|
|
for so_fn in cgen.so_fns {
|
|
|
|
cgen.genln('$so_fn = (void *)GetProcAddress(live_lib, "$so_fn"); ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cgen.genln('return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _live_reloads = 0;
|
|
|
|
void reload_so() {
|
2020-03-21 06:37:58 +01:00
|
|
|
char new_so_base[PATH_MAX] = {0};
|
|
|
|
char new_so_name[PATH_MAX] = {0};
|
|
|
|
char compile_cmd[PATH_MAX] = {0};
|
2019-08-19 00:18:07 +02:00
|
|
|
int last = os__file_last_mod_unix(tos2("$file"));
|
|
|
|
while (1) {
|
|
|
|
// TODO use inotify
|
|
|
|
int now = os__file_last_mod_unix(tos2("$file"));
|
|
|
|
if (now != last) {
|
|
|
|
last = now;
|
|
|
|
_live_reloads++;
|
|
|
|
|
|
|
|
//v -o bounce -shared bounce.v
|
2020-03-21 06:37:58 +01:00
|
|
|
snprintf(new_so_base, sizeof (new_so_base), ".tmp.%d.${file_base}", _live_reloads);
|
2019-08-19 00:18:07 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
// We have to make this directory becuase windows WILL NOT
|
|
|
|
// do it for us
|
|
|
|
os__mkdir(string_all_before_last(tos2(new_so_base), tos2("/")));
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
2020-03-21 06:37:58 +01:00
|
|
|
snprintf(new_so_name, sizeof (new_so_name), "%s.dll", new_so_base);
|
2019-08-19 00:18:07 +02:00
|
|
|
#else
|
2020-03-21 06:37:58 +01:00
|
|
|
snprintf(new_so_name, sizeof (new_so_name), "%s.so", new_so_base);
|
2019-08-19 00:18:07 +02:00
|
|
|
#endif
|
2020-03-21 06:37:58 +01:00
|
|
|
snprintf(compile_cmd, sizeof (compile_cmd), "$vexe $msvc -o %s -solive -shared $file", new_so_base);
|
2019-08-19 00:18:07 +02:00
|
|
|
os__system(tos2(compile_cmd));
|
|
|
|
|
2019-12-04 21:03:12 +01:00
|
|
|
if( !os__exists(tos2(new_so_name)) ) {
|
2020-03-21 06:37:58 +01:00
|
|
|
puts("Errors while compiling $file\\n");
|
2019-08-19 00:18:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lfnmutex_print("reload_so locking...");
|
|
|
|
pthread_mutex_lock(&live_fn_mutex);
|
|
|
|
lfnmutex_print("reload_so locked");
|
|
|
|
|
|
|
|
load_so(new_so_name);
|
|
|
|
#ifndef _WIN32
|
|
|
|
unlink(new_so_name); // removing the .so file from the filesystem after dlopen-ing it is safe, since it will still be mapped in memory.
|
|
|
|
#else
|
|
|
|
_unlink(new_so_name);
|
|
|
|
#endif
|
|
|
|
//if(0 == rename(new_so_name, "${so_name}")){
|
|
|
|
// load_so("${so_name}");
|
|
|
|
//}
|
|
|
|
|
|
|
|
lfnmutex_print("reload_so unlocking...");
|
|
|
|
pthread_mutex_unlock(&live_fn_mutex);
|
|
|
|
lfnmutex_print("reload_so unlocked");
|
|
|
|
|
|
|
|
}
|
|
|
|
time__sleep_ms(100);
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
')
|
2019-08-19 00:18:07 +02:00
|
|
|
}
|
|
|
|
if v.pref.is_so {
|
|
|
|
cgen.genln(' int load_so(byteptr path) { return 0; }')
|
|
|
|
}
|
|
|
|
}
|