2020-05-03 17:59:11 +02:00
|
|
|
module executable
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import dl
|
|
|
|
import strconv
|
|
|
|
import live
|
|
|
|
|
2020-07-01 00:53:53 +02:00
|
|
|
pub const (
|
|
|
|
is_used = 1
|
|
|
|
)
|
|
|
|
|
2020-05-03 17:59:11 +02:00
|
|
|
// The live reloader code is implemented here.
|
|
|
|
|
|
|
|
// NB: new_live_reload_info will be called by generated C code inside main()
|
|
|
|
pub fn new_live_reload_info(original string, vexe string, vopts string, live_fn_mutex voidptr, live_linkfn live.FNLinkLiveSymbols) &live.LiveReloadInfo {
|
|
|
|
file_base := os.file_name(original).replace('.v', '')
|
|
|
|
so_dir := os.cache_dir()
|
2020-08-25 20:40:32 +02:00
|
|
|
mut so_extension := dl.dl_ext
|
|
|
|
$if macos {
|
|
|
|
so_extension = '.dylib'
|
|
|
|
}
|
2020-05-29 04:36:08 +02:00
|
|
|
// $if msvc { so_extension = '.dll' } $else { so_extension = '.so' }
|
2020-05-03 17:59:11 +02:00
|
|
|
return &live.LiveReloadInfo{
|
|
|
|
original: original
|
|
|
|
vexe: vexe
|
|
|
|
vopts: vopts
|
|
|
|
live_fn_mutex: live_fn_mutex
|
|
|
|
live_linkfn: live_linkfn
|
|
|
|
so_extension: so_extension
|
|
|
|
so_name_template: '${so_dir}/tmp.%d.${file_base}'
|
|
|
|
live_lib: 0
|
|
|
|
reloads: 0
|
|
|
|
reload_time_ms: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: start_reloader will be called by generated code inside main(), to start
|
|
|
|
// the hot code reloader thread. start_reloader is executed in the context of
|
|
|
|
// the original main thread.
|
2020-06-04 10:35:40 +02:00
|
|
|
pub fn start_reloader(mut r live.LiveReloadInfo) {
|
2020-05-03 17:59:11 +02:00
|
|
|
// The shared library should be loaded once in the main thread
|
|
|
|
// If that fails, the program would crash anyway, just provide
|
|
|
|
// an error message to the user and exit:
|
|
|
|
r.reloads++
|
2020-06-08 00:47:04 +02:00
|
|
|
compile_and_reload_shared_lib(mut r) or {
|
2020-05-03 17:59:11 +02:00
|
|
|
eprintln( err )
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-05-29 04:36:08 +02:00
|
|
|
go reloader(mut r)
|
2020-05-03 17:59:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[if debuglive]
|
2020-05-29 04:42:02 +02:00
|
|
|
fn elog(r &live.LiveReloadInfo, s string){
|
2020-05-03 17:59:11 +02:00
|
|
|
eprintln(s)
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn compile_and_reload_shared_lib(mut r live.LiveReloadInfo) ?bool {
|
2020-05-30 09:20:54 +02:00
|
|
|
sw := time.new_stopwatch({})
|
2020-05-29 04:36:08 +02:00
|
|
|
new_lib_path := compile_lib(mut r) or {
|
2020-05-03 17:59:11 +02:00
|
|
|
return error('errors while compiling $r.original')
|
|
|
|
}
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'> compile_and_reload_shared_lib compiled: ${new_lib_path}')
|
2020-05-29 04:36:08 +02:00
|
|
|
load_lib(mut r, new_lib_path )
|
2020-05-28 02:20:55 +02:00
|
|
|
r.reload_time_ms = int(sw.elapsed().milliseconds())
|
2020-05-03 17:59:11 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn compile_lib(mut r live.LiveReloadInfo) ?string {
|
2020-05-29 04:36:08 +02:00
|
|
|
new_lib_path, new_lib_path_with_extension := current_shared_library_path(mut r)
|
2020-05-03 17:59:11 +02:00
|
|
|
cmd := '$r.vexe $r.vopts -o $new_lib_path $r.original'
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'> compilation cmd: $cmd')
|
2020-05-30 09:20:54 +02:00
|
|
|
cwatch := time.new_stopwatch({})
|
2020-05-03 17:59:11 +02:00
|
|
|
recompilation_result := os.exec( cmd ) or {
|
|
|
|
eprintln('recompilation failed')
|
|
|
|
return none
|
|
|
|
}
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'compilation took: ${cwatch.elapsed().milliseconds()}ms')
|
2020-05-03 17:59:11 +02:00
|
|
|
if recompilation_result.exit_code != 0 {
|
|
|
|
eprintln('recompilation error:')
|
|
|
|
eprintln( recompilation_result.output )
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
if !os.exists( new_lib_path_with_extension ) {
|
|
|
|
eprintln('new_lib_path: $new_lib_path_with_extension does not exist')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return new_lib_path_with_extension
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn current_shared_library_path(mut r live.LiveReloadInfo) (string, string) {
|
2020-05-03 17:59:11 +02:00
|
|
|
lib_path := strconv.v_sprintf(r.so_name_template.replace('\\', '\\\\'), r.reloads)
|
|
|
|
lib_path_with_extension := lib_path + r.so_extension
|
|
|
|
return lib_path, lib_path_with_extension
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn load_lib(mut r live.LiveReloadInfo, new_lib_path string) {
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'live mutex locking...')
|
2020-05-03 17:59:11 +02:00
|
|
|
C.pthread_mutex_lock(r.live_fn_mutex)
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'live mutex locked')
|
2020-05-03 17:59:11 +02:00
|
|
|
//
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_locked_before != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_locked_before( r )
|
|
|
|
}
|
|
|
|
//
|
2020-05-29 04:36:08 +02:00
|
|
|
protected_load_lib(mut r, new_lib_path)
|
2020-05-03 17:59:11 +02:00
|
|
|
//
|
|
|
|
r.reloads_ok++
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_locked_after != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_locked_after( r )
|
|
|
|
}
|
|
|
|
//
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'live mutex unlocking...')
|
2020-05-03 17:59:11 +02:00
|
|
|
C.pthread_mutex_unlock(r.live_fn_mutex)
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'live mutex unlocked')
|
2020-05-03 17:59:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn protected_load_lib(mut r live.LiveReloadInfo, new_lib_path string) {
|
2020-05-03 17:59:11 +02:00
|
|
|
if r.live_lib != 0 {
|
|
|
|
dl.close( r.live_lib )
|
|
|
|
r.live_lib = 0
|
|
|
|
}
|
2020-05-22 17:36:09 +02:00
|
|
|
r.live_lib = dl.open(new_lib_path, dl.rtld_lazy)
|
2020-05-03 17:59:11 +02:00
|
|
|
if r.live_lib == 0 {
|
|
|
|
eprintln('opening $new_lib_path failed')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
r.live_linkfn( r.live_lib )
|
2020-05-29 04:42:02 +02:00
|
|
|
elog(r,'> load_lib OK, new live_lib: $r.live_lib')
|
2020-05-22 17:36:09 +02:00
|
|
|
// removing the .so file from the filesystem after dlopen-ing
|
2020-05-03 17:59:11 +02:00
|
|
|
// it is safe, since it will still be mapped in memory
|
|
|
|
os.rm( new_lib_path )
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: r.reloader() is executed in a new, independent thread
|
2020-06-04 10:35:40 +02:00
|
|
|
fn reloader(mut r live.LiveReloadInfo) {
|
2020-05-07 18:18:48 +02:00
|
|
|
// elog(r,'reloader, r: $r')
|
2020-05-03 17:59:11 +02:00
|
|
|
mut last_ts := os.file_last_mod_unix( r.original )
|
|
|
|
for {
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_recheck != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_recheck( r )
|
|
|
|
}
|
|
|
|
now_ts := os.file_last_mod_unix( r.original )
|
|
|
|
if last_ts != now_ts {
|
|
|
|
r.reloads++
|
|
|
|
last_ts = now_ts
|
|
|
|
r.last_mod_ts = last_ts
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_before != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_before( r )
|
|
|
|
}
|
2020-05-29 04:36:08 +02:00
|
|
|
compile_and_reload_shared_lib(mut r) or {
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_compile_failed != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_compile_failed( r )
|
|
|
|
}
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_after != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_after( r )
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-05-28 02:20:55 +02:00
|
|
|
if r.cb_after != voidptr(0) {
|
2020-05-03 17:59:11 +02:00
|
|
|
r.cb_after( r )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.recheck_period_ms > 0 {
|
|
|
|
time.sleep_ms(r.recheck_period_ms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|