1 set(LIBA_JAVASCRIPT_SUFFIX ".js" CACHE STRING "The suffix for JavaScript output files.")
2 set_property(CACHE LIBA_JAVASCRIPT_SUFFIX PROPERTY STRINGS ".js" ".mjs" ".html" ".wasm")
3 set(CMAKE_EXECUTABLE_SUFFIX ${LIBA_JAVASCRIPT_SUFFIX})
6 set(CMAKE_C_STANDARD_LIBRARIES "")
7 set(CMAKE_CXX_STANDARD_LIBRARIES "")
8 set(CMAKE_C_COMPILER ${EMCC_EXECUTABLE})
9 set(CMAKE_CXX_COMPILER ${EMXX_EXECUTABLE})
10 set(CMAKE_C_COMPILER_AR ${EMAR_EXECUTABLE})
11 set(CMAKE_CXX_COMPILER_AR ${EMAR_EXECUTABLE})
12 set(CMAKE_C_COMPILER_RANLIB ${EMRANLIB_EXECUTABLE})
13 set(CMAKE_CXX_COMPILER_RANLIB ${EMRANLIB_EXECUTABLE})
14 set(CMAKE_CROSSCOMPILING_EMULATOR ${NODE_JS_EXECUTABLE})
16 list(FIND CMAKE_C_COMPILER_PREDEFINES_COMMAND -m64 emcc)
19 unset(CMAKE_C_COMPILER_ARG${emcc} CACHE)
20 unset(CMAKE_C_COMPILER_ARG${emcc})
23 list(FIND CMAKE_CXX_COMPILER_PREDEFINES_COMMAND -m64 emxx)
26 unset(CMAKE_CXX_COMPILER_ARG${emxx} CACHE)
27 unset(CMAKE_CXX_COMPILER_ARG${emxx})
31 if(NOT DEFINED EMSDK_COMPILER_VERSION)
32 set(EMSDK_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION})
36 set(OBJECTS $<TARGET_OBJECTS:a>)
38 get_property(OBJECTS TARGET a PROPERTY SOURCES)
41 set(CMAKE_POSITION_INDEPENDENT_CODE ${LIBA_PIE})
42 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ${LIBA_IPO})
43 file_scaner(SOURCES RECURSE src EXT c h cc hh cpp hpp)
45 # https://emscripten.org/docs/tools_reference/emcc.html
46 set(LIBA_EMCC_FLAGS "" CACHE STRING "Flags used by the emcc")
47 string_append(CMAKE_C_FLAGS ${LIBA_EMCC_FLAGS})
48 set(LIBA_EMXX_FLAGS "" CACHE STRING "Flags used by the em++")
49 string_append(CMAKE_CXX_FLAGS ${LIBA_EMXX_FLAGS})
50 set(LIBA_WASM_FLAGS "" CACHE STRING "Flags used by the linker")
52 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> --bind")
53 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> --bind")
54 option(LIBA_JAVASCRIPT_TSGEN "Enable/Disable generation of TypeScript definition files" 0)
55 option(LIBA_JAVASCRIPT_ASYNC "Enable/Disable compilation of wasm async" 0)
56 option(LIBA_JAVASCRIPT_DEBUG "Enable/Disable debug with sanitizers" 0)
57 option(LIBA_JAVASCRIPT_WASM "Enable/Disable JavaScript WebAssembly" 1)
58 option(LIBA_JAVASCRIPT_BIGINT "Enable/Disable JavaScript BigInt" 0)
60 if(LIBA_JAVASCRIPT_DEBUG)
61 string_append(CMAKE_C_LINK_EXECUTABLE -fsanitize=undefined,address,leak -sALLOW_MEMORY_GROWTH)
62 string_append(CMAKE_CXX_LINK_EXECUTABLE -fsanitize=undefined,address,leak -sALLOW_MEMORY_GROWTH)
65 if(LIBA_JAVASCRIPT_TSGEN)
66 string_append(CMAKE_C_LINK_EXECUTABLE --embind-emit-tsd liba.gen.d.ts)
67 string_append(CMAKE_CXX_LINK_EXECUTABLE --embind-emit-tsd liba.gen.d.ts)
70 if(NOT LIBA_JAVASCRIPT_ASYNC)
71 string_append(CMAKE_C_LINK_EXECUTABLE -sWASM_ASYNC_COMPILATION=0)
72 string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM_ASYNC_COMPILATION=0)
75 if(LIBA_JAVASCRIPT_BIGINT)
76 string_append(CMAKE_C_LINK_EXECUTABLE -sWASM_BIGINT)
77 string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM_BIGINT)
78 set_property(SOURCE ${SOURCES} APPEND PROPERTY
79 COMPILE_DEFINITIONS WASM_BIGINT
83 if(NOT LIBA_JAVASCRIPT_WASM)
84 string_append(CMAKE_C_LINK_EXECUTABLE -sWASM=0)
85 string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM=0)
88 string_append(CMAKE_C_LINK_EXECUTABLE ${LIBA_WASM_FLAGS})
89 string_append(CMAKE_CXX_LINK_EXECUTABLE ${LIBA_WASM_FLAGS})
91 add_executable(ajs ${OBJECTS} ${SOURCES})
92 target_compile_definitions(ajs PRIVATE $<TARGET_PROPERTY:a,COMPILE_DEFINITIONS>)
93 target_include_directories(ajs PRIVATE $<TARGET_PROPERTY:a,INCLUDE_DIRECTORIES>)
94 target_include_directories(ajs PRIVATE ${EMSCRIPTEN_SYSROOT}/include)
95 set_target_properties(ajs PROPERTIES OUTPUT_NAME ${PROJECT_NAME}
96 RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_LIST_DIR}
97 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_CURRENT_LIST_DIR}
98 RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_LIST_DIR}
99 RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_LIST_DIR}
102 if(LIBA_JAVASCRIPT_DEBUG)
103 target_compile_options(ajs PRIVATE -fsanitize=undefined,address,leak)
106 if(NOT CMAKE_VERSION VERSION_LESS 3.3 AND LIBA_WARNINGS AND UNIX)
107 target_compile_options(ajs PRIVATE -Weverything -Wno-documentation
108 -Wno-documentation-unknown-command -Wno-used-but-marked-unused
109 $<$<COMPILE_LANGUAGE:C>:-Wno-declaration-after-statement>
110 $<$<COMPILE_LANGUAGE:CXX>:-Wno-c++98-compat-pedantic>
113 if(NOT EMSDK_COMPILER_VERSION VERSION_LESS 16)
114 target_compile_options(ajs PRIVATE -Wno-unsafe-buffer-usage)
118 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/test/CMakeLists.txt AND CMAKE_CROSSCOMPILING_EMULATOR)
119 add_subdirectory(test)