]> git.kaiwu.me - quickjs.git/commitdiff
native cosmopolitan build
authorFabrice Bellard <fabrice@bellard.org>
Thu, 11 Jan 2024 14:29:19 +0000 (15:29 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Thu, 11 Jan 2024 14:29:19 +0000 (15:29 +0100)
Makefile
qjs.c
quickjs.c

index 9b003f01d2053f74dc8d6157d7fc9b16063ae5bb..632b1578915e759d894c202ddf61451bc3e936f1 100644 (file)
--- 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 c2d63e9d5a71e67919c6a80abebe8a5234841aac..77b5cfbc13a3793c8db6d67d4e933992d513a321 100644 (file)
--- 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"
index 37201fc33a6fda131fa79a6787209602854c529a..ce035ec81e11441b4ac24db117be40033aa8a70a 100644 (file)
--- 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)