|
| 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