Skip to content

Commit 04ad78a

Browse files
committed
Fix CI failure by restoring original Makefile and adding package management
- Restore original Makefile to root for CI compatibility - Add Makefile.packages for building both pg17-full and pg17 packages - Update README to document dual package structure and build process - Fixes 'Build WASM 🔧' CI failure caused by yarn not found in emscripten container Co-Authored-By: Dan Lynch <[email protected]>
1 parent eb29a7d commit 04ad78a

File tree

3 files changed

+198
-35
lines changed

3 files changed

+198
-35
lines changed

Makefile

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,88 @@
1-
.PHONY: install build clean test pg17-full pg17
1+
WASM_OUT_DIR := wasm
2+
WASM_OUT_NAME := libpg-query
3+
WASM_MODULE_NAME := PgQueryModule
4+
LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git
5+
LIBPG_QUERY_TAG := 17-6.1.0
26

3-
# Default target
4-
all: install build
7+
CACHE_DIR := .cache
58

6-
# Install dependencies in both packages
7-
install:
8-
cd pg17-full && yarn install
9-
cd pg17 && yarn install
9+
OS ?= $(shell uname -s)
10+
ARCH ?= $(shell uname -m)
1011

11-
# Build both packages
12-
build:
13-
cd pg17-full && yarn build
14-
cd pg17 && yarn build
12+
ifdef EMSCRIPTEN
13+
PLATFORM := emscripten
14+
else ifeq ($(OS),Darwin)
15+
PLATFORM := darwin
16+
else ifeq ($(OS),Linux)
17+
PLATFORM := linux
18+
else
19+
$(error Unsupported platform: $(OS))
20+
endif
1521

16-
# Clean both packages
17-
clean:
18-
cd pg17-full && yarn clean || true
19-
cd pg17 && yarn clean || true
22+
ifdef EMSCRIPTEN
23+
ARCH := wasm
24+
endif
25+
26+
PLATFORM_ARCH := $(PLATFORM)-$(ARCH)
27+
SRC_FILES := src/wasm_wrapper.c
28+
LIBPG_QUERY_DIR := $(CACHE_DIR)/$(PLATFORM_ARCH)/libpg_query/$(LIBPG_QUERY_TAG)
29+
LIBPG_QUERY_ARCHIVE := $(LIBPG_QUERY_DIR)/libpg_query.a
30+
LIBPG_QUERY_HEADER := $(LIBPG_QUERY_DIR)/pg_query.h
31+
CXXFLAGS := -O3
32+
33+
ifdef EMSCRIPTEN
34+
OUT_FILES := $(foreach EXT,.js .wasm,$(WASM_OUT_DIR)/$(WASM_OUT_NAME)$(EXT))
35+
else
36+
$(error Native builds are no longer supported. Use EMSCRIPTEN=1 for WASM builds only.)
37+
endif
38+
39+
# Clone libpg_query source (lives in CACHE_DIR)
40+
$(LIBPG_QUERY_DIR):
41+
mkdir -p $(CACHE_DIR)
42+
git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR)
43+
44+
$(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR)
45+
46+
# Build libpg_query
47+
$(LIBPG_QUERY_ARCHIVE): $(LIBPG_QUERY_DIR)
48+
cd $(LIBPG_QUERY_DIR); $(MAKE) build
49+
50+
# Build libpg-query-node WASM module
51+
$(OUT_FILES): $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER) $(SRC_FILES)
52+
ifdef EMSCRIPTEN
53+
$(CC) \
54+
-v \
55+
$(CXXFLAGS) \
56+
-I$(LIBPG_QUERY_DIR) \
57+
-I$(LIBPG_QUERY_DIR)/vendor \
58+
-L$(LIBPG_QUERY_DIR) \
59+
-sEXPORTED_FUNCTIONS="['_malloc','_free','_wasm_parse_query','_wasm_parse_query_protobuf','_wasm_get_protobuf_len','_wasm_deparse_protobuf','_wasm_parse_plpgsql','_wasm_fingerprint','_wasm_normalize_query','_wasm_scan','_wasm_parse_query_detailed','_wasm_free_detailed_result','_wasm_free_string']" \
60+
-sEXPORTED_RUNTIME_METHODS="['lengthBytesUTF8','stringToUTF8','UTF8ToString','HEAPU8','HEAPU32']" \
61+
-sEXPORT_NAME="$(WASM_MODULE_NAME)" \
62+
-sENVIRONMENT="web,node" \
63+
-sMODULARIZE=1 \
64+
-sEXPORT_ES6=1 \
65+
-sALLOW_MEMORY_GROWTH=1 \
66+
-lpg_query \
67+
-o $@ \
68+
$(SRC_FILES)
69+
else
70+
$(error Native builds are no longer supported. Use EMSCRIPTEN=1 for WASM builds only.)
71+
endif
2072

21-
# Test both packages
22-
test:
23-
cd pg17-full && yarn test
24-
cd pg17 && yarn test
73+
# Commands
74+
build: $(OUT_FILES)
75+
76+
build-cache: $(LIBPG_QUERY_ARCHIVE) $(LIBPG_QUERY_HEADER)
77+
78+
rebuild: clean build
79+
80+
rebuild-cache: clean-cache build-cache
81+
82+
clean:
83+
-@ rm -r $(OUT_FILES) > /dev/null 2>&1
2584

26-
# Individual package targets
27-
pg17-full:
28-
cd pg17-full && yarn && yarn build
85+
clean-cache:
86+
-@ rm -rf $(LIBPG_QUERY_DIR)
2987

30-
pg17:
31-
cd pg17 && yarn && yarn build
88+
.PHONY: build build-cache rebuild rebuild-cache clean clean-cache

Makefile.packages

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.PHONY: install build clean test pg17-full pg17
2+
3+
# Package management Makefile for pg17-full and pg17 packages
4+
# Use this to build both packages: make -f Makefile.packages build
5+
6+
# Default target
7+
all: install build
8+
9+
# Install dependencies in both packages
10+
install:
11+
cd pg17-full && yarn install
12+
cd pg17 && yarn install
13+
14+
# Build both packages using their individual build scripts
15+
build:
16+
cd pg17-full && yarn build
17+
cd pg17 && yarn build
18+
19+
# Clean both packages
20+
clean:
21+
cd pg17-full && yarn clean || true
22+
cd pg17 && yarn clean || true
23+
24+
# Test both packages
25+
test:
26+
cd pg17-full && yarn test
27+
cd pg17 && yarn test
28+
29+
# Individual package targets
30+
pg17-full:
31+
cd pg17-full && yarn && yarn build
32+
33+
pg17:
34+
cd pg17 && yarn && yarn build

README.md

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# libpg-query
1+
# libpg-query-node
22

33
<p align="center" width="100%">
44
<img src="https://github.com/launchql/libpg-query-node/assets/545047/5fd420cc-cdc6-4211-9b0f-0eca8321ba72" alt="webincubator" width="100">
@@ -17,28 +17,100 @@
1717

1818
The real PostgreSQL parser for Node.js, powered by **WebAssembly (WASM)** for true cross-platform compatibility.
1919

20-
A WASM-based PostgreSQL query parser that provides the same functionality as the native PostgreSQL parser without requiring native compilation or platform-specific binaries. Primarily used for the node.js parser and deparser [pgsql-parser](https://github.com/pyramation/pgsql-parser).
20+
This repository provides two distinct packages:
21+
22+
- **pg17-full**: Complete PostgreSQL parser with all functionality including scan, deparse, and protobuf support
23+
- **pg17**: Minimal PostgreSQL parser focused on core parsing, fingerprinting, and normalization (excludes scan/deparse functionality)
24+
25+
Both packages are WASM-based PostgreSQL query parsers that provide the same core functionality as the native PostgreSQL parser without requiring native compilation or platform-specific binaries.
2126

2227

2328
## Table of Contents
2429

25-
1. [Installation](#installation)
26-
2. [Usage](#usage)
27-
3. [Build Instructions](#build-instructions)
28-
4. [Testing](#testing)
29-
5. [Versions](#versions)
30-
6. [Related Projects](#related-projects)
31-
7. [Credit](#credit)
30+
1. [Package Structure](#package-structure)
31+
2. [Installation](#installation)
32+
3. [Building Both Packages](#building-both-packages)
33+
4. [Usage](#usage)
34+
5. [Build Instructions](#build-instructions)
35+
6. [Testing](#testing)
36+
7. [Versions](#versions)
37+
8. [Related Projects](#related-projects)
38+
9. [Credit](#credit)
39+
40+
## Package Structure
41+
42+
This repository contains two packages:
3243

44+
### pg17-full (Complete Package)
45+
- Full PostgreSQL parsing functionality
46+
- Includes scan, deparse, and protobuf support
47+
- Larger package size due to complete feature set
48+
- Located in `./pg17-full/`
49+
50+
### pg17 (Minimal Package)
51+
- Core PostgreSQL parsing functionality
52+
- Excludes scan, deparse, and protobuf features
53+
- Smaller package size optimized for basic parsing needs
54+
- Located in `./pg17/`
3355

3456
## Installation
3557

36-
```sh
37-
npm install libpg-query
58+
Choose the package that best fits your needs:
59+
60+
**For full functionality:**
61+
```bash
62+
cd pg17-full
63+
npm install
64+
```
65+
66+
**For minimal parsing:**
67+
```bash
68+
cd pg17
69+
npm install
70+
```
71+
72+
## Building Both Packages
73+
74+
Use the package management Makefile to build both packages:
75+
76+
```bash
77+
# Install dependencies for both packages
78+
make -f Makefile.packages install
79+
80+
# Build both packages
81+
make -f Makefile.packages build
82+
83+
# Clean both packages
84+
make -f Makefile.packages clean
85+
86+
# Test both packages
87+
make -f Makefile.packages test
3888
```
3989

4090
## Usage
4191

92+
### Basic Examples
93+
94+
**pg17-full (Complete Package):**
95+
```typescript
96+
import { parse, deparse, scan } from './pg17-full/src/index.js';
97+
98+
const result = await parse('SELECT * FROM users WHERE active = true');
99+
const sql = await deparse(result);
100+
const tokens = await scan('SELECT * FROM users');
101+
console.log(JSON.stringify(result, null, 2));
102+
```
103+
104+
**pg17 (Minimal Package):**
105+
```typescript
106+
import { parse, fingerprint, normalize } from './pg17/src/index.js';
107+
108+
const result = await parse('SELECT * FROM users WHERE active = true');
109+
const fp = await fingerprint('SELECT * FROM users');
110+
const norm = await normalize('SELECT * FROM users');
111+
console.log(JSON.stringify(result, null, 2));
112+
```
113+
42114
### `parse(query: string): Promise<ParseResult>`
43115

44116
Parses the SQL and returns a Promise for the parse tree. May reject with a parse error.

0 commit comments

Comments
 (0)