-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from gregnr/feat/wasm
Adds browser (WASM) support
- Loading branch information
Showing
12 changed files
with
2,384 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ libs/ | |
npm-debug.log | ||
libpg_query/**/*.a | ||
libpg_query/**/*.h | ||
wasm/libpg-query.js | ||
*.wasm | ||
.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
WASM_OUT_DIR := wasm | ||
WASM_OUT_NAME := libpg-query | ||
WASM_MODULE_NAME := PgQueryModule | ||
LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git | ||
LIBPG_QUERY_TAG := 15-4.2.4 | ||
CACHE_DIR := .cache | ||
|
||
OS ?= $(shell uname -s) | ||
ARCH ?= $(shell uname -m) | ||
|
||
ifdef EMSCRIPTEN | ||
PLATFORM := emscripten | ||
else ifeq ($(OS),Darwin) | ||
PLATFORM := darwin | ||
else ifeq ($(OS),Linux) | ||
PLATFORM := linux | ||
else | ||
$(error Unsupported platform: $(OS)) | ||
endif | ||
|
||
ifdef EMSCRIPTEN | ||
ARCH := wasm | ||
endif | ||
|
||
PLATFORM_ARCH := $(PLATFORM)-$(ARCH) | ||
SRC_FILES := $(wildcard src/*.cc) | ||
LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG) | ||
LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a | ||
LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h | ||
CXXFLAGS := -O3 | ||
|
||
ifdef EMSCRIPTEN | ||
OUT_FILES := $(foreach EXT,.js .wasm,$(WASM_OUT_DIR)/$(WASM_OUT_NAME)$(EXT)) | ||
else | ||
OUT_FILES := build/Release/queryparser.node $(wildcard build/*) | ||
endif | ||
|
||
# Clone libpg_query source (lives in CACHE_DIR) | ||
$(LIBPG_QUERY_DIR): | ||
mkdir -p $(CACHE_DIR) | ||
git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR) | ||
|
||
$(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR) | ||
|
||
# Build libpg_query | ||
$(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR) | ||
cd $(LIBPG_QUERY_DIR); $(MAKE) build | ||
|
||
# Build libpg-query-node (based on platform) | ||
$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES) | ||
ifdef EMSCRIPTEN | ||
@ $(CXX) \ | ||
$(CXXFLAGS) \ | ||
-DNAPI_HAS_THREADS \ | ||
-I$(LIBPG_QUERY_DIR) \ | ||
-I./node_modules/emnapi/include \ | ||
-I./node_modules/node-addon-api \ | ||
-L./node_modules/emnapi/lib/wasm32-emscripten \ | ||
-L$(LIBPG_QUERY_DIR) \ | ||
--js-library=./node_modules/emnapi/dist/library_napi.js \ | ||
-sEXPORTED_FUNCTIONS="['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']" \ | ||
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \ | ||
-sENVIRONMENT="web" \ | ||
-sMODULARIZE=1 \ | ||
-sEXPORT_ES6=1 \ | ||
-fexceptions \ | ||
-lpg_query \ | ||
-lemnapi-basic \ | ||
-o $@ \ | ||
$(SRC_FILES) | ||
else | ||
# if not wasm, defer to node-gyp | ||
yarn rebuild | ||
endif | ||
|
||
# Commands | ||
build: $(OUT_FILES) | ||
|
||
build-cache: $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) | ||
|
||
rebuild: clean build | ||
|
||
rebuild-cache: clean-cache build-cache | ||
|
||
clean: | ||
-@ rm -r $(OUT_FILES) > /dev/null 2>&1 | ||
|
||
clean-cache: | ||
-@ rm -rf $(LIBPG_QUERY_DIR) | ||
|
||
.PHONY: build build-cache rebuild rebuild-cache clean clean-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>libpg-query web</title> | ||
</head> | ||
|
||
<body> | ||
<script src="main.js"></script> | ||
<h2>Check the console for the parsed SQL.</h2> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "webpack-test", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "webpack serve --open", | ||
"build": "webpack" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^5.89.0", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-dev-server": "^4.15.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { parseQuery } from '../../../wasm'; | ||
|
||
const sql = 'select * from customers;'; | ||
const result = await parseQuery(sql); | ||
|
||
console.log(sql); | ||
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: './src/index.js', | ||
output: { | ||
filename: 'main.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
}, | ||
devServer: { | ||
static: './dist', | ||
client: { | ||
overlay: false, | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.