Skip to content

Commit 9a4379d

Browse files
author
Fabrice Bellard
committed
native cosmopolitan build
1 parent e80917b commit 9a4379d

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

Makefile

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ CONFIG_LTO=y
3333
#CONFIG_WERROR=y
3434
# force 32 bit build for some utilities
3535
#CONFIG_M32=y
36-
37-
ifdef CONFIG_DARWIN
38-
# use clang instead of gcc
39-
CONFIG_CLANG=y
40-
CONFIG_DEFAULT_AR=y
41-
endif
36+
# cosmopolitan build (see https://github.com/jart/cosmopolitan)
37+
#CONFIG_COSMO=y
4238

4339
# installation directory
4440
prefix=/usr/local
@@ -52,6 +48,12 @@ CONFIG_BIGNUM=y
5248

5349
OBJDIR=.obj
5450

51+
ifdef CONFIG_DARWIN
52+
# use clang instead of gcc
53+
CONFIG_CLANG=y
54+
CONFIG_DEFAULT_AR=y
55+
endif
56+
5557
ifdef CONFIG_WIN32
5658
ifdef CONFIG_M32
5759
CROSS_PREFIX=i686-w64-mingw32-
@@ -63,6 +65,7 @@ else
6365
CROSS_PREFIX=
6466
EXE=
6567
endif
68+
6669
ifdef CONFIG_CLANG
6770
HOST_CC=clang
6871
CC=$(CROSS_PREFIX)clang
@@ -84,6 +87,14 @@ ifdef CONFIG_CLANG
8487
AR=$(CROSS_PREFIX)ar
8588
endif
8689
endif
90+
else ifdef CONFIG_COSMO
91+
CONFIG_LTO=
92+
HOST_CC=gcc
93+
CC=cosmocc
94+
# cosmocc does not correct support -MF
95+
CFLAGS=-g -Wall #-MMD -MF $(OBJDIR)/$(@F).d
96+
CFLAGS += -Wno-array-bounds -Wno-format-truncation
97+
AR=cosmoar
8798
else
8899
HOST_CC=gcc
89100
CC=$(CROSS_PREFIX)gcc
@@ -113,7 +124,11 @@ CFLAGS_DEBUG=$(CFLAGS) -O0
113124
CFLAGS_SMALL=$(CFLAGS) -Os
114125
CFLAGS_OPT=$(CFLAGS) -O2
115126
CFLAGS_NOLTO:=$(CFLAGS_OPT)
127+
ifdef CONFIG_COSMO
128+
LDFLAGS=-s # better to strip by default
129+
else
116130
LDFLAGS=-g
131+
endif
117132
ifdef CONFIG_LTO
118133
CFLAGS_SMALL+=-flto
119134
CFLAGS_OPT+=-flto
@@ -133,6 +148,12 @@ else
133148
LDEXPORT=-rdynamic
134149
endif
135150

151+
ifndef CONFIG_COSMO
152+
ifndef CONFIG_DARWIN
153+
CONFIG_SHARED_LIBS=y # building shared libraries is supported
154+
endif
155+
endif
156+
136157
PROGS=qjs$(EXE) qjsc$(EXE) run-test262
137158
ifneq ($(CROSS_PREFIX),)
138159
QJSC_CC=gcc
@@ -157,10 +178,10 @@ endif
157178
ifeq ($(CROSS_PREFIX),)
158179
PROGS+=examples/hello
159180
ifndef CONFIG_ASAN
160-
PROGS+=examples/hello_module examples/test_fib
161-
ifndef CONFIG_DARWIN
162-
PROGS+=examples/fib.so examples/point.so
181+
PROGS+=examples/hello_module
163182
endif
183+
ifdef CONFIG_SHARED_LIBS
184+
PROGS+=examples/test_fib examples/fib.so examples/point.so
164185
endif
165186
endif
166187

@@ -373,7 +394,7 @@ doc/%.html: doc/%.html.pre
373394
###############################################################################
374395
# tests
375396

376-
ifndef CONFIG_DARWIN
397+
ifdef CONFIG_SHARED_LIBS
377398
test: tests/bjson.so examples/point.so
378399
endif
379400
ifdef CONFIG_M32
@@ -387,7 +408,7 @@ test: qjs
387408
./qjs tests/test_loop.js
388409
./qjs tests/test_std.js
389410
./qjs tests/test_worker.js
390-
ifndef CONFIG_DARWIN
411+
ifdef CONFIG_SHARED_LIBS
391412
ifdef CONFIG_BIGNUM
392413
./qjs --bignum tests/test_bjson.js
393414
else

qjs.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,19 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
140140
}
141141

142142
/* default memory allocation functions with memory limitation */
143-
static inline size_t js_trace_malloc_usable_size(void *ptr)
143+
static size_t js_trace_malloc_usable_size(const void *ptr)
144144
{
145145
#if defined(__APPLE__)
146146
return malloc_size(ptr);
147147
#elif defined(_WIN32)
148-
return _msize(ptr);
148+
return _msize((void *)ptr);
149149
#elif defined(EMSCRIPTEN)
150150
return 0;
151151
#elif defined(__linux__)
152-
return malloc_usable_size(ptr);
152+
return malloc_usable_size((void *)ptr);
153153
#else
154154
/* change this to `return 0;` if compilation fails */
155-
return malloc_usable_size(ptr);
155+
return malloc_usable_size((void *)ptr);
156156
#endif
157157
}
158158

@@ -264,18 +264,7 @@ static const JSMallocFunctions trace_mf = {
264264
js_trace_malloc,
265265
js_trace_free,
266266
js_trace_realloc,
267-
#if defined(__APPLE__)
268-
malloc_size,
269-
#elif defined(_WIN32)
270-
(size_t (*)(const void *))_msize,
271-
#elif defined(EMSCRIPTEN)
272-
NULL,
273-
#elif defined(__linux__)
274-
(size_t (*)(const void *))malloc_usable_size,
275-
#else
276-
/* change this to `NULL,` if compilation fails */
277-
malloc_usable_size,
278-
#endif
267+
js_trace_malloc_usable_size,
279268
};
280269

281270
#define PROG_NAME "qjs"

quickjs.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,19 +1692,19 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
16921692
}
16931693

16941694
/* default memory allocation functions with memory limitation */
1695-
static inline size_t js_def_malloc_usable_size(void *ptr)
1695+
static size_t js_def_malloc_usable_size(const void *ptr)
16961696
{
16971697
#if defined(__APPLE__)
16981698
return malloc_size(ptr);
16991699
#elif defined(_WIN32)
1700-
return _msize(ptr);
1700+
return _msize((void *)ptr);
17011701
#elif defined(EMSCRIPTEN)
17021702
return 0;
17031703
#elif defined(__linux__)
1704-
return malloc_usable_size(ptr);
1704+
return malloc_usable_size((void *)ptr);
17051705
#else
17061706
/* change this to `return 0;` if compilation fails */
1707-
return malloc_usable_size(ptr);
1707+
return malloc_usable_size((void *)ptr);
17081708
#endif
17091709
}
17101710

@@ -1768,18 +1768,7 @@ static const JSMallocFunctions def_malloc_funcs = {
17681768
js_def_malloc,
17691769
js_def_free,
17701770
js_def_realloc,
1771-
#if defined(__APPLE__)
1772-
malloc_size,
1773-
#elif defined(_WIN32)
1774-
(size_t (*)(const void *))_msize,
1775-
#elif defined(EMSCRIPTEN)
1776-
NULL,
1777-
#elif defined(__linux__)
1778-
(size_t (*)(const void *))malloc_usable_size,
1779-
#else
1780-
/* change this to `NULL,` if compilation fails */
1781-
malloc_usable_size,
1782-
#endif
1771+
js_def_malloc_usable_size,
17831772
};
17841773

17851774
JSRuntime *JS_NewRuntime(void)

0 commit comments

Comments
 (0)