fix build `luamake -C test`
[liba.git] / javascript / CMakeLists.txt
blobf085e33f4e9911a8d9b090142c3aee974e6eea3a
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})
5 if(NOT LIBA_CXX)
6   enable_language(CXX)
7 endif()
9 if(NOT EMSCRIPTEN)
10   set(CMAKE_C_STANDARD_LIBRARIES "")
11   set(CMAKE_CXX_STANDARD_LIBRARIES "")
12   set(CMAKE_C_COMPILER ${EMCC_EXECUTABLE})
13   set(CMAKE_CXX_COMPILER ${EMXX_EXECUTABLE})
14   set(CMAKE_C_COMPILER_AR ${EMAR_EXECUTABLE})
15   set(CMAKE_CXX_COMPILER_AR ${EMAR_EXECUTABLE})
16   set(CMAKE_C_COMPILER_RANLIB ${EMRANLIB_EXECUTABLE})
17   set(CMAKE_CXX_COMPILER_RANLIB ${EMRANLIB_EXECUTABLE})
18   set(CMAKE_CROSSCOMPILING_EMULATOR ${NODE_JS_EXECUTABLE})
20   list(FIND CMAKE_C_COMPILER_PREDEFINES_COMMAND -m64 emcc)
22   if(${emcc} GREATER 0)
23     unset(CMAKE_C_COMPILER_ARG${emcc} CACHE)
24     unset(CMAKE_C_COMPILER_ARG${emcc})
25   endif()
27   list(FIND CMAKE_CXX_COMPILER_PREDEFINES_COMMAND -m64 emxx)
29   if(${emxx} GREATER 0)
30     unset(CMAKE_CXX_COMPILER_ARG${emxx} CACHE)
31     unset(CMAKE_CXX_COMPILER_ARG${emxx})
32   endif()
33 endif()
35 if(NOT DEFINED EMSDK_COMPILER_VERSION)
36   set(EMSDK_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION})
37 endif()
39 if(EMSCRIPTEN)
40   set(OBJECTS $<TARGET_OBJECTS:a>)
41 else()
42   get_property(OBJECTS TARGET a PROPERTY SOURCES)
43 endif()
45 set(CMAKE_POSITION_INDEPENDENT_CODE ${LIBA_PIE})
46 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ${LIBA_IPO})
47 file_scaner(SOURCES RECURSE src EXT c h cc hh cpp hpp)
49 if(NOT LIBA_CXX)
50   file_scaner(source RECURSE ../src EXT cc hh cpp hpp)
51   list(APPEND SOURCES ${source})
52   set(source)
53 endif()
55 # https://emscripten.org/docs/tools_reference/emcc.html
56 set(LIBA_EMCC_FLAGS "" CACHE STRING "Flags used by the emcc")
57 string_append(CMAKE_C_FLAGS ${LIBA_EMCC_FLAGS})
58 set(LIBA_EMXX_FLAGS "" CACHE STRING "Flags used by the em++")
59 string_append(CMAKE_CXX_FLAGS ${LIBA_EMXX_FLAGS})
60 set(LIBA_WASM_FLAGS "" CACHE STRING "Flags used by the linker")
62 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> --bind")
63 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> --bind")
64 option(LIBA_JAVASCRIPT_TSGEN "Enable/Disable generation of TypeScript definition files" 0)
65 option(LIBA_JAVASCRIPT_ASYNC "Enable/Disable compilation of wasm async" 0)
66 option(LIBA_JAVASCRIPT_DEBUG "Enable/Disable debug with sanitizers" 0)
67 option(LIBA_JAVASCRIPT_WASM "Enable/Disable JavaScript WebAssembly" 1)
68 option(LIBA_JAVASCRIPT_BIGINT "Enable/Disable JavaScript BigInt" 0)
70 if(LIBA_JAVASCRIPT_DEBUG)
71   string_append(CMAKE_C_LINK_EXECUTABLE -fsanitize=undefined,address,leak -sALLOW_MEMORY_GROWTH)
72   string_append(CMAKE_CXX_LINK_EXECUTABLE -fsanitize=undefined,address,leak -sALLOW_MEMORY_GROWTH)
73 endif()
75 if(LIBA_JAVASCRIPT_TSGEN)
76   string_append(CMAKE_C_LINK_EXECUTABLE --embind-emit-tsd liba.gen.d.ts)
77   string_append(CMAKE_CXX_LINK_EXECUTABLE --embind-emit-tsd liba.gen.d.ts)
78 endif()
80 if(NOT LIBA_JAVASCRIPT_ASYNC)
81   string_append(CMAKE_C_LINK_EXECUTABLE -sWASM_ASYNC_COMPILATION=0)
82   string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM_ASYNC_COMPILATION=0)
83 endif()
85 if(LIBA_JAVASCRIPT_BIGINT)
86   string_append(CMAKE_C_LINK_EXECUTABLE -sWASM_BIGINT)
87   string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM_BIGINT)
88   set_property(SOURCE ${SOURCES} APPEND PROPERTY
89     COMPILE_DEFINITIONS WASM_BIGINT
90   )
91 endif()
93 if(NOT LIBA_JAVASCRIPT_WASM)
94   string_append(CMAKE_C_LINK_EXECUTABLE -sWASM=0)
95   string_append(CMAKE_CXX_LINK_EXECUTABLE -sWASM=0)
96 endif()
98 string_append(CMAKE_C_LINK_EXECUTABLE ${LIBA_WASM_FLAGS})
99 string_append(CMAKE_CXX_LINK_EXECUTABLE ${LIBA_WASM_FLAGS})
101 add_executable(ajs ${OBJECTS} ${SOURCES})
102 target_compile_definitions(ajs PRIVATE $<TARGET_PROPERTY:a,COMPILE_DEFINITIONS>)
103 target_include_directories(ajs PRIVATE $<TARGET_PROPERTY:a,INCLUDE_DIRECTORIES>)
104 target_include_directories(ajs PRIVATE ${EMSCRIPTEN_SYSROOT}/include)
105 set_target_properties(ajs PROPERTIES OUTPUT_NAME ${PROJECT_NAME}
106   RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_LIST_DIR}
107   RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_CURRENT_LIST_DIR}
108   RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_LIST_DIR}
109   RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_LIST_DIR}
112 if(LIBA_JAVASCRIPT_DEBUG)
113   target_compile_options(ajs PRIVATE -fsanitize=undefined,address,leak)
114 endif()
116 if(NOT CMAKE_VERSION VERSION_LESS 3.3 AND LIBA_WARNINGS AND UNIX)
117   target_compile_options(ajs PRIVATE -Weverything -Wno-documentation
118     -Wno-documentation-unknown-command -Wno-used-but-marked-unused
119     $<$<COMPILE_LANGUAGE:C>:-Wno-declaration-after-statement>
120     $<$<COMPILE_LANGUAGE:CXX>:-Wno-c++98-compat-pedantic>
121   )
123   if(NOT EMSDK_COMPILER_VERSION VERSION_LESS 16)
124     target_compile_options(ajs PRIVATE -Wno-unsafe-buffer-usage)
125   endif()
126 endif()
128 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/test/CMakeLists.txt AND CMAKE_CROSSCOMPILING_EMULATOR)
129   add_subdirectory(test)
130 endif()