From 1180f10f27f2989d02fde24a9b86119e027922a4 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 03:26:48 +0100 Subject: [PATCH 1/9] feat: replace makefiles with justfiles --- Makefile | 43 ------------------------------------------- ext/Makefile | 34 ---------------------------------- ext/justfile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ justfile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 77 deletions(-) delete mode 100644 Makefile delete mode 100644 ext/Makefile create mode 100644 ext/justfile create mode 100644 justfile diff --git a/Makefile b/Makefile deleted file mode 100644 index 31df2d4..0000000 --- a/Makefile +++ /dev/null @@ -1,43 +0,0 @@ -.PHONY: all spec duktape libduktape update clean cleanlib - -CRYSTAL_BIN := $(shell which crystal) -CRYSTAL_LOG_LEVEL ?= NONE -CRYSTAL_LOG_SOURCES ?= * -SOURCES := $(shell find src -name '*.cr') -SPEC_SOURCES := $(shell find spec -name '*.cr') -CURRENT := $(shell pwd) -EXT := $(CURRENT)/ext -OUTPUT := $(CURRENT)/.build - -all: duktape - -duktape: $(OUTPUT)/duktape - -libduktape: - $(MAKE) -C $(EXT) libduktape - -# use the following to update the native engine: -# VERSION=X.X.X make clean cleanlib update libduktape -update: - $(MAKE) -C $(EXT) -f Makefile.internal update-duktape - -spec: all_spec - @LOG_LEVEL=$(CRYSTAL_LOG_LEVEL) LOG_SOURCES=$(CRYSTAL_LOG_SOURCES) $(OUTPUT)/all_spec - -all_spec: $(OUTPUT)/all_spec - -$(OUTPUT)/all_spec: $(SOURCES) $(SPEC_SOURCES) - @mkdir -p $(OUTPUT) - $(CRYSTAL_BIN) build -o $@ spec/all_spec.cr --warnings all - -$(OUTPUT)/duktape: $(SOURCES) - @mkdir -p $(OUTPUT) - $(CRYSTAL_BIN) build -o $@ src/duktape.cr --warnings all - -clean: - rm -rf $(OUTPUT) - rm -rf $(CURRENT)/.crystal - -cleanlib: - $(MAKE) -C $(EXT) clean - $(MAKE) -C $(EXT) -f Makefile.internal clean diff --git a/ext/Makefile b/ext/Makefile deleted file mode 100644 index 3ff2cd7..0000000 --- a/ext/Makefile +++ /dev/null @@ -1,34 +0,0 @@ -.PHONY: libduktape clean - -CURRENT := $(shell pwd) -EXT := $(CURRENT) -PREV := $(shell dirname `pwd`) -OUTPUT := $(PREV)/src/.build -LIBDIR := $(OUTPUT)/lib -INCLUDEDIR := $(OUTPUT)/include - -CFLAGS ?= \ - -pedantic \ - -c \ - -std=c99 \ - -O2 \ - -fstrict-aliasing \ - -fomit-frame-pointer - -libduktape: $(OUTPUT)/libduktape.o $(LIBDIR)/libduktape.a $(INCLUDEDIR)/duktape.h - -$(OUTPUT)/libduktape.o: $(EXT)/duktape.c - @mkdir -p $(OUTPUT) - $(CC) -o $@ $(EXT)/duktape.c $(CFLAGS) - -$(LIBDIR)/libduktape.a: - @mkdir -p $(LIBDIR) - ar rcs $(LIBDIR)/libduktape.a $(OUTPUT)/libduktape.o - -$(INCLUDEDIR)/duktape.h: - @mkdir -p $(INCLUDEDIR) - cp $(EXT)/duktape.h $(EXT)/duk_config.h $(INCLUDEDIR) - -clean: - rm -rf $(OUTPUT) - rm -rf $(CURRENT)/.crystal diff --git a/ext/justfile b/ext/justfile new file mode 100644 index 0000000..1ed92d0 --- /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 From 53d83be4d272f2fa8958c0624baf4eb166476c37 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 03:27:07 +0100 Subject: [PATCH 2/9] chore: update shard info --- shard.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index da25acc..1b20efc 100644 --- a/shard.yml +++ b/shard.yml @@ -5,11 +5,11 @@ authors: - Jesse Doyle scripts: - postinstall: "make -C ext clean libduktape" + postinstall: just clean libduktape development_dependencies: ameba: - github: veelenga/ameba + github: crystal-ameba/ameba version: '~> 1.4' crystal: '>= 0.35.1' From b2c5cf1c8babdd21b8d06983c4b8c0785443b997 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 03:29:25 +0100 Subject: [PATCH 3/9] fix: update win32 specific bindings and link --- src/lib_duktape.cr | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib_duktape.cr b/src/lib_duktape.cr index f5cc8e2..b74c225 100644 --- a/src/lib_duktape.cr +++ b/src/lib_duktape.cr @@ -4,7 +4,22 @@ # # 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 + 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 From 271b613e80a3aa6c1de493afa6280e3e000217ad Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 03:59:00 +0100 Subject: [PATCH 4/9] fix: include gettimeofday win binding --- src/lib_duktape.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib_duktape.cr b/src/lib_duktape.cr index b74c225..8994146 100644 --- a/src/lib_duktape.cr +++ b/src/lib_duktape.cr @@ -12,6 +12,8 @@ tv_sec : TimeT tv_usec : SusecondsT end + + fun gettimeofday(tp : Timeval*, timezone : Void*) : Int end {% end %} From 77be94e737ffb43b90f26cccf7db1ee77eb1c26d Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 03:59:35 +0100 Subject: [PATCH 5/9] fix(ext): use correct output location --- ext/justfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/justfile b/ext/justfile index 1ed92d0..9a4f3f6 100644 --- a/ext/justfile +++ b/ext/justfile @@ -17,7 +17,7 @@ cc := if os() == "windows" { cflags := "-pedantic -c -std=c99 -O2 -fstrict-aliasing -fomit-frame-pointer" current := justfile_directory() -output := join(current, "src", ".build") +output := join(current, "..", "src", ".build") [unix] build: From a9e0c899f672da7d62d82112fa46a803bf341431 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 04:00:47 +0100 Subject: [PATCH 6/9] feat(ext): include gettimeofday impl for windows --- ext/duk_config.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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; From d4f15937b23c4e1f542e1fb4e4973c78d1e541d1 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 21:26:23 +0100 Subject: [PATCH 7/9] chore: re-add makefiles --- Makefile | 43 +++++++++++++++++++++++++++++++++++++++++++ ext/Makefile | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 Makefile create mode 100644 ext/Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..31df2d4 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +.PHONY: all spec duktape libduktape update clean cleanlib + +CRYSTAL_BIN := $(shell which crystal) +CRYSTAL_LOG_LEVEL ?= NONE +CRYSTAL_LOG_SOURCES ?= * +SOURCES := $(shell find src -name '*.cr') +SPEC_SOURCES := $(shell find spec -name '*.cr') +CURRENT := $(shell pwd) +EXT := $(CURRENT)/ext +OUTPUT := $(CURRENT)/.build + +all: duktape + +duktape: $(OUTPUT)/duktape + +libduktape: + $(MAKE) -C $(EXT) libduktape + +# use the following to update the native engine: +# VERSION=X.X.X make clean cleanlib update libduktape +update: + $(MAKE) -C $(EXT) -f Makefile.internal update-duktape + +spec: all_spec + @LOG_LEVEL=$(CRYSTAL_LOG_LEVEL) LOG_SOURCES=$(CRYSTAL_LOG_SOURCES) $(OUTPUT)/all_spec + +all_spec: $(OUTPUT)/all_spec + +$(OUTPUT)/all_spec: $(SOURCES) $(SPEC_SOURCES) + @mkdir -p $(OUTPUT) + $(CRYSTAL_BIN) build -o $@ spec/all_spec.cr --warnings all + +$(OUTPUT)/duktape: $(SOURCES) + @mkdir -p $(OUTPUT) + $(CRYSTAL_BIN) build -o $@ src/duktape.cr --warnings all + +clean: + rm -rf $(OUTPUT) + rm -rf $(CURRENT)/.crystal + +cleanlib: + $(MAKE) -C $(EXT) clean + $(MAKE) -C $(EXT) -f Makefile.internal clean diff --git a/ext/Makefile b/ext/Makefile new file mode 100644 index 0000000..3ff2cd7 --- /dev/null +++ b/ext/Makefile @@ -0,0 +1,34 @@ +.PHONY: libduktape clean + +CURRENT := $(shell pwd) +EXT := $(CURRENT) +PREV := $(shell dirname `pwd`) +OUTPUT := $(PREV)/src/.build +LIBDIR := $(OUTPUT)/lib +INCLUDEDIR := $(OUTPUT)/include + +CFLAGS ?= \ + -pedantic \ + -c \ + -std=c99 \ + -O2 \ + -fstrict-aliasing \ + -fomit-frame-pointer + +libduktape: $(OUTPUT)/libduktape.o $(LIBDIR)/libduktape.a $(INCLUDEDIR)/duktape.h + +$(OUTPUT)/libduktape.o: $(EXT)/duktape.c + @mkdir -p $(OUTPUT) + $(CC) -o $@ $(EXT)/duktape.c $(CFLAGS) + +$(LIBDIR)/libduktape.a: + @mkdir -p $(LIBDIR) + ar rcs $(LIBDIR)/libduktape.a $(OUTPUT)/libduktape.o + +$(INCLUDEDIR)/duktape.h: + @mkdir -p $(INCLUDEDIR) + cp $(EXT)/duktape.h $(EXT)/duk_config.h $(INCLUDEDIR) + +clean: + rm -rf $(OUTPUT) + rm -rf $(CURRENT)/.crystal From 1ed85653a5efb5c9327788b42477597e640b79ef Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 21:27:46 +0100 Subject: [PATCH 8/9] revert: use make for postinstall --- shard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index 1b20efc..54239d6 100644 --- a/shard.yml +++ b/shard.yml @@ -5,7 +5,7 @@ authors: - Jesse Doyle scripts: - postinstall: just clean libduktape + postinstall: make clean libduktape development_dependencies: ameba: From 0485ad8990dc3a78bda07ab24f5308d4b8390f40 Mon Sep 17 00:00:00 2001 From: devnote-dev Date: Mon, 10 Jul 2023 21:28:37 +0100 Subject: [PATCH 9/9] fix: include dir change --- shard.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shard.yml b/shard.yml index 54239d6..f384fcd 100644 --- a/shard.yml +++ b/shard.yml @@ -5,7 +5,7 @@ authors: - Jesse Doyle scripts: - postinstall: make clean libduktape + postinstall: make -C clean libduktape development_dependencies: ameba: