From 39e834fc18b41786250c17e72f156616ac8933bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 2 Nov 2023 09:33:13 +0100 Subject: [PATCH] Add initial CMake support --- .github/workflows/ci.yml | 65 ++++++++++++++++++++ .gitignore | 2 +- CMakeLists.txt | 128 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ff1bef..e48ea68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,20 @@ jobs: - name: test run: | make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test + linux-cmake: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: build + run: | + mkdir build + cd build + cmake .. + cd .. + cmake --build build -j$(getconf _NPROCESSORS_ONLN) + - name: stats + run: | + ./build/qjs -qd macos: runs-on: macos-latest @@ -77,6 +91,20 @@ jobs: - name: test run: | make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_UBSAN=y UBSAN_OPTIONS="halt_on_error=1" test + macos-cmake: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: build + run: | + mkdir build + cd build + cmake .. + cd .. + cmake --build build -j$(getconf _NPROCESSORS_ONLN) + - name: stats + run: | + ./build/qjs -qd windows-mingw: runs-on: windows-latest @@ -112,3 +140,40 @@ jobs: - name: test run: | make -j$(getconf _NPROCESSORS_ONLN) CONFIG_WERROR=y CONFIG_MINGW=y test + + windows-mingw-cmake: + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + sys: + - mingw64 + - ucrt64 + defaults: + run: + shell: msys2 {0} + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Setup MSYS2 + uses: msys2/setup-msys2@v2 + with: + msystem: ${{matrix.sys}} + install: >- + git + make + pacboy: >- + cmake:p + ninja:p + toolchain:p + - name: build + run: | + mkdir build + cd build + cmake .. + cd .. + cmake --build build -j$(getconf _NPROCESSORS_ONLN) + - name: stats + run: | + ./build/qjs -qd diff --git a/.gitignore b/.gitignore index ea4286f..e1d6b40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ - *.a *.orig .obj/ +build/ examples/hello examples/hello_module examples/point.so diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7fba4ba --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,128 @@ +cmake_minimum_required(VERSION 3.9) + +project(quickjs LANGUAGES C) + +# TODO: +# - LTO +# - Support cross-compilation +# - ASAN / MSAN / UBSAN +# - Install targets +# - Shared library target + +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS ON) +set(CMAKE_C_STANDARD 11) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror") +if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang") + 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") +else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds -Wno-format-truncation -Wno-unused-variable -Wno-unused-but-set-variable") +endif() + +if(NOT CMAKE_BUILD_TYPE) + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE "Release") +endif() + +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0 -fno-omit-frame-pointer") +string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") + +message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") +message(STATUS "Building with ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} on ${CMAKE_SYSTEM}") +if(CMAKE_BUILD_TYPE MATCHES "Debug") + message(STATUS "Building with flags ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_DEBUG}") +else() + message(STATUS "Building with flags ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE}") +endif() + +set(CMAKE_VERBOSE_MAKEFILE TRUE) + + +# QuickJS library +# + +set(qjs_sources + cutils.c + libregexp.c + libunicode.c + quickjs.c +) + +list(APPEND qjs_defines _GNU_SOURCE) + +file(STRINGS "VERSION" QJS_VERSION_STR) +list(APPEND qjs_defines CONFIG_VERSION="${QJS_VERSION_STR}") + +option(CONFIG_BIGNUM "Enable BigNum extensions" ON) +if(CONFIG_BIGNUM) + list(APPEND qjs_defines CONFIG_BIGNUM=1) + list(APPEND qjs_sources libbf.c) +endif() + +add_library(qjs STATIC ${qjs_sources}) +target_compile_definitions(qjs PUBLIC + QJS_VERSION_STR="${QJS_VERSION_STR}" +) +target_compile_definitions(qjs PRIVATE ${qjs_defines}) +if (CMAKE_BUILD_TYPE MATCHES Debug) + target_compile_definitions(qjs PRIVATE + DUMP_LEAKS + ) +endif() + + +# QuickJS bytecode compiler +# + +add_custom_command( + OUTPUT repl.c + COMMAND "${CMAKE_BINARY_DIR}/qjsc" -c -o ./repl.c -m ${CMAKE_CURRENT_SOURCE_DIR}/repl.js + DEPENDS qjsc + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Compile repl.js to bytecode" + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/repl.js +) + +add_executable(qjsc + qjsc.c + quickjs-libc.c +) +target_compile_definitions(qjsc PRIVATE ${qjs_defines}) +target_link_libraries(qjsc qjs m pthread) +if(NOT MINGW) + target_link_libraries(qjsc dl) +endif() + + +# QuickJS CLI +# + +add_executable(qjs_exe + qjs.c + quickjs-libc.c + ${CMAKE_BINARY_DIR}/repl.c +) +set_target_properties(qjs_exe PROPERTIES + OUTPUT_NAME "qjs" +) +target_compile_definitions(qjs_exe PRIVATE ${qjs_defines}) +target_link_libraries(qjs_exe qjs m pthread) +if(NOT MINGW) + target_link_libraries(qjs_exe dl) +endif() + + +# Test262 runner +# + +add_executable(run-test262 + quickjs-libc.c + run-test262.c +) +target_compile_definitions(run-test262 PRIVATE ${qjs_defines}) +target_link_libraries(run-test262 qjs m pthread) +if(NOT MINGW) + target_link_libraries(run-test262 dl) +endif()