Skip to content

Commit

Permalink
Merge pull request #685 from psarna/wasmwasiwal
Browse files Browse the repository at this point in the history
wasm: add `make wasi` command
  • Loading branch information
psarna committed Nov 23, 2023
2 parents d78fac7 + cc9bcb1 commit e0d98e9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
1 change: 1 addition & 0 deletions libsql-sqlite3/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ libsql
/src-verify
/crates/target/
/has_tclsh*
/libsql.wasm
22 changes: 22 additions & 0 deletions libsql-sqlite3/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,7 @@ clean:
rm -f src-verify
rm -f custom.rws
rm -f has_tclsh84 has_tclsh85
rm -f libsql.wasm

distclean: clean
rm -f sqlite_cfg.h config.log config.status libtool Makefile sqlite3.pc libsql.pc sqlite3session.h \
Expand Down Expand Up @@ -1680,6 +1681,27 @@ sqlite3.dll: $(REAL_LIBOBJ) sqlite3.def
fiddle: sqlite3.c shell.c
make -C ext/wasm fiddle emcc_opt=-Os

#
# Wasm/WASI module
# TODO: hardcode less parameters
# TODO: detect if wasi sysroot exists: /usr/share/wasi-sysroot
#
wasi: sqlite3.c sqlite3.h
clang --target=wasm32-unknown-wasi \
-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_UTF16 \
-DSQLITE_OMIT_SHARED_CACHE -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_SHARED_MEM=1 \
-DSQLITE_TEMP_STORE=2 \
-DSQLITE_USE_URI=1 \
-DSQLITE_DEFAULT_UNIX_VFS=\"unix-none\" \
--sysroot /usr/share/wasi-sysroot \
-nostartfiles \
-Wl,--no-entry \
-Wl,--export-all \
-o libsql.wasm \
-v \
sqlite3.c


#
# Spell-checking for source comments
# The sources checked are either C sources or C source templates.
Expand Down
24 changes: 12 additions & 12 deletions libsql-sqlite3/src/os_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
#include <time.h>
#include <sys/time.h> /* amalgamator: keep */
#include <errno.h>
#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
#if (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0) \
&& !defined(SQLITE_WASI)
# include <sys/mman.h>
#endif
Expand Down Expand Up @@ -535,30 +535,30 @@ static struct unix_syscall {
#endif
#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)

#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
#if (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0) \
&& !defined(SQLITE_WASI)
{ "mmap", (sqlite3_syscall_ptr)mmap, 0 },
#else
{ "mmap", (sqlite3_syscall_ptr)0, 0 },
#endif
#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)

#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \
#if (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0) \
&& !defined(SQLITE_WASI)
{ "munmap", (sqlite3_syscall_ptr)munmap, 0 },
#else
{ "munmap", (sqlite3_syscall_ptr)0, 0 },
#endif
#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)

#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
#if HAVE_MREMAP && (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0)
{ "mremap", (sqlite3_syscall_ptr)mremap, 0 },
#else
{ "mremap", (sqlite3_syscall_ptr)0, 0 },
#endif
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)

#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#if !defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0
{ "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
#else
{ "getpagesize", (sqlite3_syscall_ptr)0, 0 },
Expand Down Expand Up @@ -3983,7 +3983,7 @@ static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){

/* Forward declaration */
static int unixGetTempname(int nBuf, char *zBuf);
#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
static int unixFcntlExternalReader(unixFile*, int*);
#endif

Expand Down Expand Up @@ -4104,7 +4104,7 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */

case SQLITE_FCNTL_EXTERNAL_READER: {
#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
#else
*(int*)pArg = 0;
Expand Down Expand Up @@ -4257,7 +4257,7 @@ static int unixDeviceCharacteristics(sqlite3_file *id){
return pFd->deviceCharacteristics;
}

#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#if !defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0

/*
** Return the system page size.
Expand All @@ -4275,9 +4275,9 @@ static int unixGetpagesize(void){
#endif
}

#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
#endif /* !defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0 */

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM

/*
** Object used to represent an shared memory buffer.
Expand Down Expand Up @@ -5143,7 +5143,7 @@ static int unixShmUnmap(
# define unixShmLock 0
# define unixShmBarrier 0
# define unixShmUnmap 0
#endif /* #ifndef SQLITE_OMIT_WAL */
#endif /* #ifndef SQLITE_OMIT_SHARED_MEM */

#if SQLITE_MAX_MMAP_SIZE>0
/*
Expand Down Expand Up @@ -8105,7 +8105,7 @@ int sqlite3_os_init(void){
#endif
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
/* Validate lock assumptions */
assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
assert( UNIX_SHM_BASE==120 ); /* Start of locking area */
Expand Down
32 changes: 16 additions & 16 deletions libsql-sqlite3/src/os_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
** Compiling and using WAL mode requires several APIs that are only
** available in Windows platforms based on the NT kernel.
*/
#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_SHARED_MEM)
# error "WAL mode requires support from the Windows NT kernel, compile\
with SQLITE_OMIT_WAL."
with SQLITE_OMIT_SHARED_MEM."
#endif

#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
Expand Down Expand Up @@ -189,7 +189,7 @@
** CE SDK; however, they are not present in the header file)?
*/
#if SQLITE_WIN32_FILEMAPPING_API && \
(!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
(!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0)
/*
** Two of the file mapping APIs are different under WinRT. Figure out which
** set we need.
Expand Down Expand Up @@ -236,7 +236,7 @@ WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
#endif

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
/* Forward references to structures used for WAL */
typedef struct winShm winShm; /* A connection to shared-memory */
typedef struct winShmNode winShmNode; /* A region of shared-memory */
Expand Down Expand Up @@ -268,7 +268,7 @@ struct winFile {
short sharedLockByte; /* Randomly chosen byte used as a shared lock */
u8 ctrlFlags; /* Flags. See WINFILE_* below */
DWORD lastErrno; /* The Windows errno from the last I/O error */
#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
winShm *pShm; /* Instance of shared memory on this file */
#endif
const char *zPath; /* Full pathname of this file */
Expand Down Expand Up @@ -556,7 +556,7 @@ static struct win_syscall {
LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)

#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
(!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
(!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0) && \
SQLITE_WIN32_CREATEFILEMAPPINGA
{ "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
#else
Expand All @@ -567,7 +567,7 @@ static struct win_syscall {
DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)

#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
(!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
(!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0))
{ "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
#else
{ "CreateFileMappingW", (SYSCALL)0, 0 },
Expand Down Expand Up @@ -907,7 +907,7 @@ static struct win_syscall {
#endif

#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
(!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
(!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0))
{ "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
#else
{ "MapViewOfFile", (SYSCALL)0, 0 },
Expand Down Expand Up @@ -977,7 +977,7 @@ static struct win_syscall {
#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
LPOVERLAPPED))aSyscall[58].pCurrent)

#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0
{ "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
#else
{ "UnmapViewOfFile", (SYSCALL)0, 0 },
Expand Down Expand Up @@ -1040,7 +1040,7 @@ static struct win_syscall {
#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)

#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0)
{ "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
#else
{ "MapViewOfFileFromApp", (SYSCALL)0, 0 },
Expand Down Expand Up @@ -1104,7 +1104,7 @@ static struct win_syscall {

#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)

#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_SHARED_MEM) || SQLITE_MAX_MMAP_SIZE>0)
{ "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
#else
{ "CreateFileMappingFromApp", (SYSCALL)0, 0 },
Expand Down Expand Up @@ -2666,7 +2666,7 @@ static int winClose(sqlite3_file *id){
winFile *pFile = (winFile*)id;

assert( id!=0 );
#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
assert( pFile->pShm==0 );
#endif
assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
Expand Down Expand Up @@ -3672,7 +3672,7 @@ static int winDeviceCharacteristics(sqlite3_file *id){
*/
static SYSTEM_INFO winSysInfo;

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM

/*
** Helper functions to obtain and relinquish the global mutex. The
Expand Down Expand Up @@ -4357,7 +4357,7 @@ static int winShmMap(
# define winShmLock 0
# define winShmBarrier 0
# define winShmUnmap 0
#endif /* #ifndef SQLITE_OMIT_WAL */
#endif /* #ifndef SQLITE_OMIT_SHARED_MEM */

/*
** Cleans up the mapped region of the specified file, if any.
Expand Down Expand Up @@ -6181,7 +6181,7 @@ int sqlite3_os_init(void){
sqlite3_vfs_register(&winLongPathNolockVfs, 0);
#endif

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
#endif

Expand All @@ -6196,7 +6196,7 @@ int sqlite3_os_end(void){
}
#endif

#ifndef SQLITE_OMIT_WAL
#ifndef SQLITE_OMIT_SHARED_MEM
winBigLock = 0;
#endif

Expand Down
5 changes: 3 additions & 2 deletions libsql-sqlite3/src/sqlite.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -10855,8 +10855,9 @@ int sqlite3_deserialize(
#if defined(__wasi__)
# undef SQLITE_WASI
# define SQLITE_WASI 1
# undef SQLITE_OMIT_WAL
# define SQLITE_OMIT_WAL 1/* because it requires shared memory APIs */
// NOTICE: we do support WAL mode, we just don't support shared memory
//# undef SQLITE_OMIT_WAL
//# define SQLITE_OMIT_WAL 1/* because it requires shared memory APIs */
# ifndef SQLITE_OMIT_LOAD_EXTENSION
# define SQLITE_OMIT_LOAD_EXTENSION
# endif
Expand Down
6 changes: 6 additions & 0 deletions libsql-sqlite3/src/sqliteInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
# define SQLITE_TCLAPI
#endif

// NOTICE: libSQL extension: disabled WAL also implies we don't want shared memory via mmap
#ifdef SQLITE_OMIT_WAL
# undef SQLITE_OMIT_SHARED_MEM
# define SQLITE_OMIT_SHARED_MEM 1
#endif

/*
** Include the header file used to customize the compiler options for MSVC.
** This should be done first so that it can successfully prevent spurious
Expand Down

0 comments on commit e0d98e9

Please sign in to comment.