Skip to content

Commit 72dfdab

Browse files
committed
Initial commit
0 parents  commit 72dfdab

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
pull_request:
8+
paths:
9+
- ".github/workflows/ci.yml"
10+
- "build.zig"
11+
- "build.zig.zon"
12+
13+
workflow_dispatch:
14+
15+
jobs:
16+
build:
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
zig-version: ["master", "0.13.0"]
21+
os: [ubuntu-latest, macos-latest, windows-latest]
22+
optimize: [ReleaseSafe, ReleaseFast]
23+
24+
runs-on: ${{ matrix.os }}
25+
26+
steps:
27+
- name: Check out repository
28+
uses: actions/checkout@v4
29+
with:
30+
submodules: false
31+
32+
- name: Set up Zig
33+
uses: mlugg/setup-zig@v1
34+
with:
35+
version: ${{ matrix.zig-version }}
36+
use-cache: false
37+
38+
- name: Run `build`
39+
run: zig build ${{ matrix.build-options }} -Doptimize=${{ matrix.optimize }} --summary all
40+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.zig-cache/
2+
/zig-cache/
3+
/zig-out/
4+
gen

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Rob Blanckaert
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# QuickJS-NG
2+
3+
This is [QuickJS-NG](https://github.com/quickjs-ng/quick-js) packaged for [Zig](https://ziglang.org).

build.zig

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const std = @import("std");
2+
3+
fn addDefines(c: *std.Build.Step.Compile, b: *std.Build) void {
4+
c.root_module.addCMacro("CONFIG_BIGNUM", "1");
5+
c.root_module.addCMacro("_GNU_SOURCE", "1");
6+
_ = b;
7+
}
8+
9+
fn addStdLib(c: *std.Build.Step.Compile, cflags: []const []const u8, root: *std.Build.Dependency) void {
10+
if (c.rootModuleTarget().os.tag == .wasi) {
11+
c.root_module.addCMacro("_WASI_EMULATED_PROCESS_CLOCKS", "1");
12+
c.root_module.addCMacro("_WASI_EMULATED_SIGNAL", "1");
13+
c.linkSystemLibrary("wasi-emulated-process-clocks");
14+
c.linkSystemLibrary("wasi-emulated-signal");
15+
}
16+
c.addCSourceFiles(.{ .files = &.{"quickjs-libc.c"}, .flags = cflags, .root = root.path(".") });
17+
}
18+
19+
pub fn build(b: *std.Build) void {
20+
const target = b.standardTargetOptions(.{});
21+
const optimize = b.standardOptimizeOption(.{});
22+
const include_stdlib = b.option(bool, "stdlib", "include stdlib in library") orelse true;
23+
24+
const csrc = b.dependency("quickjs-ng", .{});
25+
26+
const cflags = &.{
27+
"-Wno-implicit-fallthrough",
28+
"-Wno-sign-compare",
29+
"-Wno-missing-field-initializers",
30+
"-Wno-unused-parameter",
31+
"-Wno-unused-but-set-variable",
32+
"-Wno-array-bounds",
33+
"-Wno-format-truncation",
34+
"-funsigned-char",
35+
"-fwrapv",
36+
};
37+
38+
const libquickjs_source = &.{
39+
"quickjs.c",
40+
"libregexp.c",
41+
"libunicode.c",
42+
"cutils.c",
43+
"libbf.c",
44+
};
45+
46+
const libquickjs = b.addStaticLibrary(.{
47+
.name = "quickjs",
48+
.target = target,
49+
.optimize = optimize,
50+
});
51+
libquickjs.addCSourceFiles(.{
52+
.files = libquickjs_source,
53+
.flags = cflags,
54+
.root = csrc.path("."),
55+
});
56+
addDefines(libquickjs, b);
57+
if (include_stdlib) {
58+
addStdLib(libquickjs, cflags, csrc);
59+
}
60+
libquickjs.linkLibC();
61+
b.installArtifact(libquickjs);
62+
63+
const qjsc = b.addExecutable(.{
64+
.name = "qjsc",
65+
.target = target,
66+
.optimize = optimize,
67+
});
68+
qjsc.addCSourceFiles(.{
69+
.files = &.{"qjsc.c"},
70+
.flags = cflags,
71+
.root = csrc.path("."),
72+
});
73+
qjsc.linkLibrary(libquickjs);
74+
addDefines(qjsc, b);
75+
if (!include_stdlib) {
76+
addStdLib(qjsc, cflags, csrc);
77+
}
78+
b.installArtifact(qjsc);
79+
80+
const qjsc_host = b.addExecutable(.{
81+
.name = "qjsc-host",
82+
.target = b.graph.host,
83+
.optimize = .Debug,
84+
});
85+
qjsc_host.addCSourceFiles(.{
86+
.files = &.{"qjsc.c"},
87+
.flags = cflags,
88+
.root = csrc.path("."),
89+
});
90+
qjsc_host.addCSourceFiles(.{
91+
.files = libquickjs_source,
92+
.flags = cflags,
93+
.root = csrc.path("."),
94+
});
95+
addStdLib(qjsc_host, cflags, csrc);
96+
addDefines(qjsc_host, b);
97+
qjsc_host.linkLibC();
98+
99+
const header = b.addTranslateC(.{
100+
.root_source_file = csrc.path("quickjs.h"),
101+
.target = target,
102+
.optimize = optimize,
103+
});
104+
_ = b.addModule("quickjs-ng", .{ .root_source_file = header.getOutput() });
105+
106+
const gen_repl = b.addRunArtifact(qjsc_host);
107+
gen_repl.addArg("-o");
108+
const gen_repl_out = gen_repl.addOutputFileArg("repl.c");
109+
gen_repl.addArg("-m");
110+
gen_repl.addFileArg(csrc.path("repl.js"));
111+
112+
const qjs = b.addExecutable(.{ .name = "qjs", .target = target, .optimize = optimize });
113+
qjs.addCSourceFiles(.{
114+
.files = &.{"qjs.c"},
115+
.flags = cflags,
116+
.root = csrc.path("."),
117+
});
118+
qjs.addCSourceFiles(.{
119+
.files = &.{"repl.c"},
120+
.root = gen_repl_out.dirname(),
121+
.flags = cflags,
122+
});
123+
if (!include_stdlib) {
124+
addStdLib(qjs, cflags, csrc);
125+
}
126+
qjs.linkLibrary(libquickjs);
127+
addDefines(qjs, b);
128+
qjs.step.dependOn(&gen_repl.step);
129+
b.installArtifact(qjs);
130+
}

build.zig.zon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.{
2+
.name = "quickjs-ng",
3+
.version = "0.7.0",
4+
.dependencies = .{
5+
.@"quickjs-ng" = .{
6+
.url = "git+https://github.com/quickjs-ng/quickjs?ref=v0.7.0#706ba05fa6fc984e07e6b8b47d028a8ac729fd13",
7+
.hash = "1220c268865c5015ce612560139b95404f3c49eef966d74c5b634647ffdc1855c9cc",
8+
},
9+
},
10+
.paths = .{
11+
"build.zig",
12+
"build.zig.zon",
13+
"LICENSE.txt",
14+
"README.md",
15+
},
16+
}

0 commit comments

Comments
 (0)