diff --git a/ext/duk_config.h b/ext/duk_config.h index c88180d..a8362ec 100644 --- a/ext/duk_config.h +++ b/ext/duk_config.h @@ -2900,6 +2900,27 @@ typedef struct duk_hthread duk_context; #undef DUK_USE_COMPILER_STRING #define DUK_USE_COMPILER_STRING "crystal/llvm" +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) +int gettimeofday(struct timeval *tp, struct timezone *tzp) +{ + static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); + + SYSTEMTIME system_time; + FILETIME file_time; + uint64_t time; + + GetSystemTime(&system_time); + SystemTimeToFileTime(&system_time, &file_time); + time = ((uint64_t) file_time.dwLowDateTime); + time += ((uint64_t) file_time.dwHighDateTime) << 32; + + tp->tv_sec = (long) ((time - EPOCH) / 10000000L); + tp->tv_usec = (long) (system_time.wMilliseconds * 1000); + + return 0; +} +#endif + #define DUK_USE_EXEC_TIMEOUT_CHECK duk_cr_timeout struct timeout_data { struct timeval start; diff --git a/ext/justfile b/ext/justfile new file mode 100644 index 0000000..9a4f3f6 --- /dev/null +++ b/ext/justfile @@ -0,0 +1,49 @@ +set shell := ["bash", "-uc"] +set windows-shell := ["cmd.exe", "/c"] + +cc := if os() == "windows" { + if `where gcc 2>NUL || exit 0` == "" { + if `where clang 2>NUL || exit 0` == "" { + error("cannot find C compiler") + } else { + trim(replace(`where clang`, "\\", "\\\\")) + } + } else { + replace(`where gcc`, "\\", "\\\\") + } +} else { + "cc" +} + +cflags := "-pedantic -c -std=c99 -O2 -fstrict-aliasing -fomit-frame-pointer" +current := justfile_directory() +output := join(current, "..", "src", ".build") + +[unix] +build: + mkdir -p {{output}} + {{cc}} -o {{output}}/libduktape.o {{current}}/duktape.c {{cflags}} + + mkdir -p {{output}}/lib + ar rcs {{output}}/lib/libduktape.a {{output}}/libduktape.o + + mkdir -p {{output}}/include + cp {{current}}/duktape.h {{current}}/duk_config.h {{output}}/include + +[windows] +build compiler="{{cc}}": + mkdir {{output}}\lib + {{compiler}} -o {{output}}\lib\duktape.lib {{current}}\duktape.c {{cflags}} -fuse-ld=llvm-lib + + mkdir {{output}}\include + copy {{current}}\duktape.h + {{current}}\duk_config.h {{output}}\include + +[unix] +clean: + -rm -rf {{output}} + -rm -rf {{current}}/.crystal + +[windows] +clean: + -rmdir /q /s {{output}} + -rmdir /q /s {{current}}\.crystal diff --git a/justfile b/justfile new file mode 100644 index 0000000..a56b925 --- /dev/null +++ b/justfile @@ -0,0 +1,49 @@ +set shell := ["bash", "-uc"] +set windows-shell := ["cmd.exe", "/c"] + +current := invocation_directory() +crystal := if os() == "windows" { trim(`where crystal`) } else { trim(`which crystal`) } + +default: + @just build + +[unix] +all_spec output="{{current}}\\.build": + -mkdir -p {{current}}/.build + {{crystal}} build -o {{output}} spec/all_spec.cr --warnings all + +[windows] +all_spec output="{{current}}/.build": + -mkdir {{current}}\.build + {{crystal}} build -o {{output}} spec\all_spec.cr --warnings all + +[unix] +build output=".build/duktape": + -mkdir -p {{current}}/.build + {{crystal}} build -o {{output}} src/duktape.cr --warnings all + +[windows] +build output=".build\\duktape": + -mkdir {{current}}\.build + {{crystal}} build -o {{output}} src\duktape.cr --warnings all + +[unix] +clean: + -rm -rf {{current}}/.build + -rm -rf {{current}}/.crystal + +[windows] +clean: + -rmdir /q /s {{current}}\.build + -rmdir /q /s {{current}}\.crystal + +cleanlib: + @just -f {{current}}/ext/justfile clean + make -C {{current}}/ext -f Makefile.internal clean + +libduktape: + cd ext + @just -f ext/justfile build + +update: + make -C {{current}}/ext -f Makefile.internal update-duktape diff --git a/shard.yml b/shard.yml index da25acc..f384fcd 100644 --- a/shard.yml +++ b/shard.yml @@ -5,11 +5,11 @@ authors: - Jesse Doyle scripts: - postinstall: "make -C ext clean libduktape" + postinstall: make -C clean libduktape development_dependencies: ameba: - github: veelenga/ameba + github: crystal-ameba/ameba version: '~> 1.4' crystal: '>= 0.35.1' diff --git a/src/lib_duktape.cr b/src/lib_duktape.cr index f5cc8e2..8994146 100644 --- a/src/lib_duktape.cr +++ b/src/lib_duktape.cr @@ -4,7 +4,24 @@ # # This is free software. Please see LICENSE for details. -@[Link(ldflags: "-L#{__DIR__}/.build/lib -L#{__DIR__}/.build/include -lduktape -lm")] +{% if flag?(:win32) %} + lib LibC + alias SusecondsT = LongLong + + struct Timeval + tv_sec : TimeT + tv_usec : SusecondsT + end + + fun gettimeofday(tp : Timeval*, timezone : Void*) : Int + end +{% end %} + +{% if flag?(:win32) %} + @[Link(ldflags: "#{__DIR__}\\.build\\lib\\duktape.lib")] +{% else %} + @[Link(ldflags: "-L#{__DIR__}/.build/lib -L#{__DIR__}/.build/include -lduktape -lm")] +{% end %} lib LibDUK alias Context = Void* alias Size = LibC::SizeT