Skip to content

Commit 39e834f

Browse files
committed
Add initial CMake support
1 parent e449cb0 commit 39e834f

File tree

3 files changed

+194
-1
lines changed

3 files changed

+194
-1
lines changed

.github/workflows/ci.yml

+65
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ jobs:
4848
- name: test
4949
run: |
5050
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test
51+
linux-cmake:
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: actions/checkout@v3
55+
- name: build
56+
run: |
57+
mkdir build
58+
cd build
59+
cmake ..
60+
cd ..
61+
cmake --build build -j$(getconf _NPROCESSORS_ONLN)
62+
- name: stats
63+
run: |
64+
./build/qjs -qd
5165
5266
macos:
5367
runs-on: macos-latest
@@ -77,6 +91,20 @@ jobs:
7791
- name: test
7892
run: |
7993
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test
94+
macos-cmake:
95+
runs-on: macos-latest
96+
steps:
97+
- uses: actions/checkout@v3
98+
- name: build
99+
run: |
100+
mkdir build
101+
cd build
102+
cmake ..
103+
cd ..
104+
cmake --build build -j$(getconf _NPROCESSORS_ONLN)
105+
- name: stats
106+
run: |
107+
./build/qjs -qd
80108
81109
windows-mingw:
82110
runs-on: windows-latest
@@ -112,3 +140,40 @@ jobs:
112140
- name: test
113141
run: |
114142
make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_MINGW=y test
143+
144+
windows-mingw-cmake:
145+
runs-on: windows-latest
146+
strategy:
147+
fail-fast: false
148+
matrix:
149+
sys:
150+
- mingw64
151+
- ucrt64
152+
defaults:
153+
run:
154+
shell: msys2 {0}
155+
steps:
156+
- uses: actions/checkout@v3
157+
with:
158+
submodules: true
159+
- name: Setup MSYS2
160+
uses: msys2/setup-msys2@v2
161+
with:
162+
msystem: ${{matrix.sys}}
163+
install: >-
164+
git
165+
make
166+
pacboy: >-
167+
cmake:p
168+
ninja:p
169+
toolchain:p
170+
- name: build
171+
run: |
172+
mkdir build
173+
cd build
174+
cmake ..
175+
cd ..
176+
cmake --build build -j$(getconf _NPROCESSORS_ONLN)
177+
- name: stats
178+
run: |
179+
./build/qjs -qd

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
*.a
32
*.orig
43
.obj/
4+
build/
55
examples/hello
66
examples/hello_module
77
examples/point.so

CMakeLists.txt

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
3+
project(quickjs LANGUAGES C)
4+
5+
# TODO:
6+
# - LTO
7+
# - Support cross-compilation
8+
# - ASAN / MSAN / UBSAN
9+
# - Install targets
10+
# - Shared library target
11+
12+
set(CMAKE_C_STANDARD_REQUIRED ON)
13+
set(CMAKE_C_EXTENSIONS ON)
14+
set(CMAKE_C_STANDARD 11)
15+
16+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")
17+
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
18+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-but-set-variable -funsigned-char")
19+
else()
20+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds -Wno-format-truncation -Wno-unused-variable -Wno-unused-but-set-variable")
21+
endif()
22+
23+
if(NOT CMAKE_BUILD_TYPE)
24+
message(STATUS "No build type selected, default to Release")
25+
set(CMAKE_BUILD_TYPE "Release")
26+
endif()
27+
28+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g")
29+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0 -fno-omit-frame-pointer")
30+
string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
31+
32+
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode")
33+
message(STATUS "Building with ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} on ${CMAKE_SYSTEM}")
34+
if(CMAKE_BUILD_TYPE MATCHES "Debug")
35+
message(STATUS "Building with flags ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_DEBUG}")
36+
else()
37+
message(STATUS "Building with flags ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE}")
38+
endif()
39+
40+
set(CMAKE_VERBOSE_MAKEFILE TRUE)
41+
42+
43+
# QuickJS library
44+
#
45+
46+
set(qjs_sources
47+
cutils.c
48+
libregexp.c
49+
libunicode.c
50+
quickjs.c
51+
)
52+
53+
list(APPEND qjs_defines _GNU_SOURCE)
54+
55+
file(STRINGS "VERSION" QJS_VERSION_STR)
56+
list(APPEND qjs_defines CONFIG_VERSION="${QJS_VERSION_STR}")
57+
58+
option(CONFIG_BIGNUM "Enable BigNum extensions" ON)
59+
if(CONFIG_BIGNUM)
60+
list(APPEND qjs_defines CONFIG_BIGNUM=1)
61+
list(APPEND qjs_sources libbf.c)
62+
endif()
63+
64+
add_library(qjs STATIC ${qjs_sources})
65+
target_compile_definitions(qjs PUBLIC
66+
QJS_VERSION_STR="${QJS_VERSION_STR}"
67+
)
68+
target_compile_definitions(qjs PRIVATE ${qjs_defines})
69+
if (CMAKE_BUILD_TYPE MATCHES Debug)
70+
target_compile_definitions(qjs PRIVATE
71+
DUMP_LEAKS
72+
)
73+
endif()
74+
75+
76+
# QuickJS bytecode compiler
77+
#
78+
79+
add_custom_command(
80+
OUTPUT repl.c
81+
COMMAND "${CMAKE_BINARY_DIR}/qjsc" -c -o ./repl.c -m ${CMAKE_CURRENT_SOURCE_DIR}/repl.js
82+
DEPENDS qjsc
83+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
84+
COMMENT "Compile repl.js to bytecode"
85+
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/repl.js
86+
)
87+
88+
add_executable(qjsc
89+
qjsc.c
90+
quickjs-libc.c
91+
)
92+
target_compile_definitions(qjsc PRIVATE ${qjs_defines})
93+
target_link_libraries(qjsc qjs m pthread)
94+
if(NOT MINGW)
95+
target_link_libraries(qjsc dl)
96+
endif()
97+
98+
99+
# QuickJS CLI
100+
#
101+
102+
add_executable(qjs_exe
103+
qjs.c
104+
quickjs-libc.c
105+
${CMAKE_BINARY_DIR}/repl.c
106+
)
107+
set_target_properties(qjs_exe PROPERTIES
108+
OUTPUT_NAME "qjs"
109+
)
110+
target_compile_definitions(qjs_exe PRIVATE ${qjs_defines})
111+
target_link_libraries(qjs_exe qjs m pthread)
112+
if(NOT MINGW)
113+
target_link_libraries(qjs_exe dl)
114+
endif()
115+
116+
117+
# Test262 runner
118+
#
119+
120+
add_executable(run-test262
121+
quickjs-libc.c
122+
run-test262.c
123+
)
124+
target_compile_definitions(run-test262 PRIVATE ${qjs_defines})
125+
target_link_libraries(run-test262 qjs m pthread)
126+
if(NOT MINGW)
127+
target_link_libraries(run-test262 dl)
128+
endif()

0 commit comments

Comments
 (0)