From 9a4379daf69b68b3196613be11c0ef4e19203ef3 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Thu, 11 Jan 2024 15:29:19 +0100 Subject: [PATCH] native cosmopolitan build --- Makefile | 43 ++++++++++++++++++++++++++++++++----------- qjs.c | 21 +++++---------------- quickjs.c | 21 +++++---------------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 9b003f0..632b157 100644 --- a/Makefile +++ b/Makefile @@ -33,12 +33,8 @@ CONFIG_LTO=y #CONFIG_WERROR=y # force 32 bit build for some utilities #CONFIG_M32=y - -ifdef CONFIG_DARWIN -# use clang instead of gcc -CONFIG_CLANG=y -CONFIG_DEFAULT_AR=y -endif +# cosmopolitan build (see https://github.com/jart/cosmopolitan) +#CONFIG_COSMO=y # installation directory prefix=/usr/local @@ -52,6 +48,12 @@ CONFIG_BIGNUM=y OBJDIR=.obj +ifdef CONFIG_DARWIN +# use clang instead of gcc +CONFIG_CLANG=y +CONFIG_DEFAULT_AR=y +endif + ifdef CONFIG_WIN32 ifdef CONFIG_M32 CROSS_PREFIX=i686-w64-mingw32- @@ -63,6 +65,7 @@ else CROSS_PREFIX= EXE= endif + ifdef CONFIG_CLANG HOST_CC=clang CC=$(CROSS_PREFIX)clang @@ -84,6 +87,14 @@ ifdef CONFIG_CLANG AR=$(CROSS_PREFIX)ar endif endif +else ifdef CONFIG_COSMO + CONFIG_LTO= + HOST_CC=gcc + CC=cosmocc + # cosmocc does not correct support -MF + CFLAGS=-g -Wall #-MMD -MF $(OBJDIR)/$(@F).d + CFLAGS += -Wno-array-bounds -Wno-format-truncation + AR=cosmoar else HOST_CC=gcc CC=$(CROSS_PREFIX)gcc @@ -113,7 +124,11 @@ CFLAGS_DEBUG=$(CFLAGS) -O0 CFLAGS_SMALL=$(CFLAGS) -Os CFLAGS_OPT=$(CFLAGS) -O2 CFLAGS_NOLTO:=$(CFLAGS_OPT) +ifdef CONFIG_COSMO +LDFLAGS=-s # better to strip by default +else LDFLAGS=-g +endif ifdef CONFIG_LTO CFLAGS_SMALL+=-flto CFLAGS_OPT+=-flto @@ -133,6 +148,12 @@ else LDEXPORT=-rdynamic endif +ifndef CONFIG_COSMO +ifndef CONFIG_DARWIN +CONFIG_SHARED_LIBS=y # building shared libraries is supported +endif +endif + PROGS=qjs$(EXE) qjsc$(EXE) run-test262 ifneq ($(CROSS_PREFIX),) QJSC_CC=gcc @@ -157,10 +178,10 @@ endif ifeq ($(CROSS_PREFIX),) PROGS+=examples/hello ifndef CONFIG_ASAN -PROGS+=examples/hello_module examples/test_fib -ifndef CONFIG_DARWIN -PROGS+=examples/fib.so examples/point.so +PROGS+=examples/hello_module endif +ifdef CONFIG_SHARED_LIBS +PROGS+=examples/test_fib examples/fib.so examples/point.so endif endif @@ -373,7 +394,7 @@ doc/%.html: doc/%.html.pre ############################################################################### # tests -ifndef CONFIG_DARWIN +ifdef CONFIG_SHARED_LIBS test: tests/bjson.so examples/point.so endif ifdef CONFIG_M32 @@ -387,7 +408,7 @@ test: qjs ./qjs tests/test_loop.js ./qjs tests/test_std.js ./qjs tests/test_worker.js -ifndef CONFIG_DARWIN +ifdef CONFIG_SHARED_LIBS ifdef CONFIG_BIGNUM ./qjs --bignum tests/test_bjson.js else diff --git a/qjs.c b/qjs.c index c2d63e9..77b5cfb 100644 --- a/qjs.c +++ b/qjs.c @@ -140,19 +140,19 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr, } /* default memory allocation functions with memory limitation */ -static inline size_t js_trace_malloc_usable_size(void *ptr) +static size_t js_trace_malloc_usable_size(const void *ptr) { #if defined(__APPLE__) return malloc_size(ptr); #elif defined(_WIN32) - return _msize(ptr); + return _msize((void *)ptr); #elif defined(EMSCRIPTEN) return 0; #elif defined(__linux__) - return malloc_usable_size(ptr); + return malloc_usable_size((void *)ptr); #else /* change this to `return 0;` if compilation fails */ - return malloc_usable_size(ptr); + return malloc_usable_size((void *)ptr); #endif } @@ -264,18 +264,7 @@ static const JSMallocFunctions trace_mf = { js_trace_malloc, js_trace_free, js_trace_realloc, -#if defined(__APPLE__) - malloc_size, -#elif defined(_WIN32) - (size_t (*)(const void *))_msize, -#elif defined(EMSCRIPTEN) - NULL, -#elif defined(__linux__) - (size_t (*)(const void *))malloc_usable_size, -#else - /* change this to `NULL,` if compilation fails */ - malloc_usable_size, -#endif + js_trace_malloc_usable_size, }; #define PROG_NAME "qjs" diff --git a/quickjs.c b/quickjs.c index 37201fc..ce035ec 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1692,19 +1692,19 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque) } /* default memory allocation functions with memory limitation */ -static inline size_t js_def_malloc_usable_size(void *ptr) +static size_t js_def_malloc_usable_size(const void *ptr) { #if defined(__APPLE__) return malloc_size(ptr); #elif defined(_WIN32) - return _msize(ptr); + return _msize((void *)ptr); #elif defined(EMSCRIPTEN) return 0; #elif defined(__linux__) - return malloc_usable_size(ptr); + return malloc_usable_size((void *)ptr); #else /* change this to `return 0;` if compilation fails */ - return malloc_usable_size(ptr); + return malloc_usable_size((void *)ptr); #endif } @@ -1768,18 +1768,7 @@ static const JSMallocFunctions def_malloc_funcs = { js_def_malloc, js_def_free, js_def_realloc, -#if defined(__APPLE__) - malloc_size, -#elif defined(_WIN32) - (size_t (*)(const void *))_msize, -#elif defined(EMSCRIPTEN) - NULL, -#elif defined(__linux__) - (size_t (*)(const void *))malloc_usable_size, -#else - /* change this to `NULL,` if compilation fails */ - malloc_usable_size, -#endif + js_def_malloc_usable_size, }; JSRuntime *JS_NewRuntime(void)