1
- cmake_minimum_required (VERSION 3.14)
1
+ cmake_minimum_required (VERSION 3.24)
2
+
2
3
set (PROJECT_NAME "sqlite3_web" )
3
4
project (${PROJECT_NAME} LANGUAGES C)
4
5
6
+ set (triple wasm32-unknown-wasi)
7
+ set (wasi_sysroot "/usr/share/wasi-sysroot" CACHE PATH "Path to wasi sysroot" )
8
+ set (clang "clang" CACHE FILEPATH "Path to wasm-capable clang executable" )
9
+
5
10
include (FetchContent)
6
- if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0" )
7
- # cmake 3.24.0 added the `DOWNLOAD_EXTRACT_TIMESTAMP` and prints an ugly warning when
8
- # the default is used, so override it to the recommended behavior.
9
- # We can't really ask users to use a cmake that recent, so there's this if here.
10
- FetchContent_Declare(
11
+
12
+ FetchContent_Declare(
11
13
sqlite3
12
- URL https://sqlite.org/2023 /sqlite-autoconf-3440000 .tar.gz
14
+ URL https://sqlite.org/2024 /sqlite-autoconf-3460000 .tar.gz
13
15
DOWNLOAD_EXTRACT_TIMESTAMP NEW
14
- )
15
- else ()
16
- FetchContent_Declare(
17
- sqlite3
18
- URL https://sqlite.org/2023/sqlite-autoconf-3440000.tar.gz
19
- )
20
- endif ()
16
+ )
21
17
22
18
FetchContent_MakeAvailable(sqlite3)
23
19
24
- set (wasm_visibility "__attribute__((visibility( \" default \" ))) " )
20
+ file (DOWNLOAD https://raw.githubusercontent.com/sqlite/sqlite/master/src/test_vfstrace.c " ${CMAKE_BINARY_DIR} /vfstrace.c " )
25
21
26
22
get_filename_component (RS_LIB_DIR "${CMAKE_BINARY_DIR} /../../powersync-sqlite-core/" ABSOLUTE )
27
23
set (RS_LIB "powersync" )
@@ -31,28 +27,74 @@ set(RS_WASM_TGT_DIR "${RS_LIB_DIR}/target/${RS_WASM_TGT}")
31
27
set (RS_RELEASE_OUT "${RS_WASM_TGT_DIR} /wasm/" )
32
28
set (RS_RELEASE_OUT_DEPS "${RS_WASM_TGT_DIR} /wasm/deps" )
33
29
set (RS_RELEASE_EXTENSION_OUT "${RS_RELEASE_OUT} /powersync-extension.o" )
34
- set (RS_DEBUG_BC "${RS_WASM_TGT_DIR} /debug/deps/${RS_LIB} .bc" )
35
- set (RS_BUILD_COMMAND "cargo build -p powersync_loadable --profile wasm --no-default-features --features \" powersync_core/static powersync_core/omit_load_extension sqlite_nostd/static sqlite_nostd/omit_load_extension\" -Z build-std=panic_abort,core,alloc --target ${RS_WASM_TGT} " )
36
30
37
- file (GLOB BYTECODE_FILES "${RS_WASM_TGT_DIR} /wasm/deps/*.bc" )
38
- file (GLOB OBJ_FILES "${RS_WASM_TGT_DIR} /wasm/deps/*.o" CONFIGURE_DEPENDS "*.o" )
31
+ # Generate symbols we need to export from the sqlite3.wasm build
32
+ add_custom_command (
33
+ OUTPUT required_symbols.txt
34
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} /../../
35
+ COMMAND dart run tool/wasm_symbols.dart ${CMAKE_CURRENT_BINARY_DIR} /required_symbols.txt
36
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR} /../../tool/wasm_symbols.dart
37
+ VERBATIM
38
+ )
39
+ add_custom_target (required_symbols DEPENDS required_symbols.txt)
39
40
40
- macro (base_sqlite3_target name )
41
- add_executable (${name}
42
- "${sqlite3_SOURCE_DIR} /sqlite3.c"
43
- os_web.c
44
- helpers.c
45
- )
41
+ macro (base_sqlite3_target name debug)
42
+ set (clang_output ${name} .clang.wasm)
43
+ set (init_output ${name} .init.wasm)
44
+ set (output ${init_output} )
46
45
47
- target_link_libraries (${name} ${RS_RELEASE_EXTENSION_OUT} )
48
- target_link_options (${name} PRIVATE -nostartfiles -Wl,--import-memory -Wl,--no -entry -Wl,--export-dynamic)
49
- target_include_directories (${name} PRIVATE "${PROJECT_SOURCE_DIR} /" )
50
- target_include_directories (${name} PRIVATE ${sqlite3_SOURCE_DIR} )
51
- target_compile_definitions (${name} PRIVATE
52
- _HAVE_SQLITE_CONFIG_H
53
- SQLITE_API=${wasm_visibility}
46
+ set (sources
47
+ ${CMAKE_CURRENT_SOURCE_DIR} /os_web.c
48
+ ${CMAKE_CURRENT_SOURCE_DIR} /helpers.c
49
+ ${sqlite3_SOURCE_DIR} /sqlite3.c
50
+ ${RS_RELEASE_EXTENSION_OUT}
51
+ )
52
+ set (flags -Wall -Wextra -Wno-unused-parameter -Wno-unused-function)
53
+
54
+ if (${debug} )
55
+ list (APPEND sources "${CMAKE_BINARY_DIR} /vfstrace.c" )
56
+ list (APPEND flags "-g" "-DDEBUG" )
57
+ else ()
58
+ list (APPEND flags "-Oz" "-DNDEBUG" "-flto" )
59
+ endif ()
60
+
61
+ add_custom_command (
62
+ OUTPUT ${clang_output}
63
+ COMMAND ${clang} --target =${triple} -std=c23
64
+ ${flags}
65
+ -o ${clang_output}
66
+ -I ${PROJECT_SOURCE_DIR} -I ${sqlite3_SOURCE_DIR}
67
+ -D_HAVE_SQLITE_CONFIG_H
68
+ -mcpu=generic
69
+ -mexec-model=reactor
70
+ -fno-stack-protector -fno-stack-clash-protection
71
+ -Wl,--import-memory
72
+ --sysroot ${wasi_sysroot}
73
+ ${sources}
74
+ @${CMAKE_CURRENT_BINARY_DIR} /required_symbols.txt
75
+ DEPENDS ${sources} required_symbols
76
+ VERBATIM
77
+ )
78
+
79
+ add_custom_command (
80
+ OUTPUT ${init_output}
81
+ COMMAND wasm-ctor-eval -c _initialize ${clang_output} -o ${init_output}
82
+ VERBATIM
83
+ DEPENDS ${clang_output}
84
+ )
85
+
86
+ if (NOT ${debug} )
87
+ set (output ${name} .wasm)
88
+
89
+ add_custom_command (
90
+ OUTPUT ${output}
91
+ COMMAND wasm-opt --strip --strip-producers -c -O4 ${init_output} -o ${output}
92
+ VERBATIM
93
+ DEPENDS ${init_output}
54
94
)
55
- set_property (TARGET ${name} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
95
+ endif ()
96
+
97
+ add_custom_target (${name} DEPENDS ${output} )
56
98
endmacro ()
57
99
58
100
# Script to use llc to get object code from bytecode
@@ -73,26 +115,13 @@ add_custom_target(
73
115
COMMAND sh ${objectcode_script}
74
116
)
75
117
76
- base_sqlite3_target(sqlite3_debug)
77
- file (DOWNLOAD https://raw.githubusercontent.com/sqlite/sqlite/version -3.44.0/src/test_vfstrace.c "${CMAKE_BINARY_DIR} /vfstrace.c" )
78
- target_sources (sqlite3_debug PRIVATE "${CMAKE_BINARY_DIR} /vfstrace.c" )
79
- target_compile_options (sqlite3_debug PRIVATE -g)
80
- target_compile_definitions (sqlite3_debug PRIVATE SQLITE_ENABLE_VFSTRACE SQLITE_ENABLE_API_ARMOR)
81
- set_target_properties (sqlite3_debug PROPERTIES OUTPUT_NAME "sqlite3" SUFFIX ".debug.wasm" )
82
-
83
- base_sqlite3_target(sqlite3_opt)
84
- target_compile_options (sqlite3_opt PRIVATE -Oz)
85
- set_target_properties (sqlite3_opt PROPERTIES OUTPUT_NAME "sqlite3" SUFFIX ".tmp.wasm" )
86
- add_custom_command (TARGET sqlite3_opt POST_BUILD
87
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} /../../
88
- COMMAND dart run tool/wasm_dce.dart ${CMAKE_CURRENT_BINARY_DIR} /sqlite3.tmp.wasm ${CMAKE_CURRENT_BINARY_DIR} /sqlite3.dce.wasm
89
- COMMAND wasm-opt ${CMAKE_CURRENT_BINARY_DIR} /sqlite3.dce.wasm -O4 -o ${CMAKE_CURRENT_BINARY_DIR} /sqlite3.wasm
90
- )
118
+ base_sqlite3_target(sqlite3_debug true )
119
+ base_sqlite3_target(sqlite3_opt false )
91
120
92
121
add_dependencies (sqlite3_opt powersync_core_bytecode)
93
122
add_dependencies (sqlite3_debug powersync_core_bytecode)
94
123
95
124
add_custom_target (output )
96
- add_custom_command (TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR} /sqlite3 .wasm ${PROJECT_SOURCE_DIR} /../../example/web/sqlite3.wasm DEPENDS )
97
- add_custom_command (TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR} /sqlite3.debug .wasm ${PROJECT_SOURCE_DIR} /../../example/web/sqlite3.debug.wasm)
98
- add_dependencies (output sqlite3_debug sqlite3_opt powersync_core_bytecode)
125
+ add_custom_command (TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR} /sqlite3_opt .wasm ${PROJECT_SOURCE_DIR} /../../example/web/sqlite3.wasm)
126
+ add_custom_command (TARGET output COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR} /sqlite3_debug.init .wasm ${PROJECT_SOURCE_DIR} /../../example/web/sqlite3.debug.wasm)
127
+ add_dependencies (output sqlite3_debug sqlite3_opt powersync_core_bytecode)
0 commit comments