Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
basicer committed Dec 19, 2024
0 parents commit 72dfdab
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on:
push:
branches: [main]

pull_request:
paths:
- ".github/workflows/ci.yml"
- "build.zig"
- "build.zig.zon"

workflow_dispatch:

jobs:
build:
strategy:
fail-fast: false
matrix:
zig-version: ["master", "0.13.0"]
os: [ubuntu-latest, macos-latest, windows-latest]
optimize: [ReleaseSafe, ReleaseFast]

runs-on: ${{ matrix.os }}

steps:
- name: Check out repository
uses: actions/checkout@v4
with:
submodules: false

- name: Set up Zig
uses: mlugg/setup-zig@v1
with:
version: ${{ matrix.zig-version }}
use-cache: false

- name: Run `build`
run: zig build ${{ matrix.build-options }} -Doptimize=${{ matrix.optimize }} --summary all

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.zig-cache/
/zig-cache/
/zig-out/
gen
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Rob Blanckaert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# QuickJS-NG

This is [QuickJS-NG](https://github.com/quickjs-ng/quick-js) packaged for [Zig](https://ziglang.org).
130 changes: 130 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const std = @import("std");

fn addDefines(c: *std.Build.Step.Compile, b: *std.Build) void {
c.root_module.addCMacro("CONFIG_BIGNUM", "1");
c.root_module.addCMacro("_GNU_SOURCE", "1");
_ = b;
}

fn addStdLib(c: *std.Build.Step.Compile, cflags: []const []const u8, root: *std.Build.Dependency) void {
if (c.rootModuleTarget().os.tag == .wasi) {
c.root_module.addCMacro("_WASI_EMULATED_PROCESS_CLOCKS", "1");
c.root_module.addCMacro("_WASI_EMULATED_SIGNAL", "1");
c.linkSystemLibrary("wasi-emulated-process-clocks");
c.linkSystemLibrary("wasi-emulated-signal");
}
c.addCSourceFiles(.{ .files = &.{"quickjs-libc.c"}, .flags = cflags, .root = root.path(".") });
}

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const include_stdlib = b.option(bool, "stdlib", "include stdlib in library") orelse true;

const csrc = b.dependency("quickjs-ng", .{});

const cflags = &.{
"-Wno-implicit-fallthrough",
"-Wno-sign-compare",
"-Wno-missing-field-initializers",
"-Wno-unused-parameter",
"-Wno-unused-but-set-variable",
"-Wno-array-bounds",
"-Wno-format-truncation",
"-funsigned-char",
"-fwrapv",
};

const libquickjs_source = &.{
"quickjs.c",
"libregexp.c",
"libunicode.c",
"cutils.c",
"libbf.c",
};

const libquickjs = b.addStaticLibrary(.{
.name = "quickjs",
.target = target,
.optimize = optimize,
});
libquickjs.addCSourceFiles(.{
.files = libquickjs_source,
.flags = cflags,
.root = csrc.path("."),
});
addDefines(libquickjs, b);
if (include_stdlib) {
addStdLib(libquickjs, cflags, csrc);
}
libquickjs.linkLibC();
b.installArtifact(libquickjs);

const qjsc = b.addExecutable(.{
.name = "qjsc",
.target = target,
.optimize = optimize,
});
qjsc.addCSourceFiles(.{
.files = &.{"qjsc.c"},
.flags = cflags,
.root = csrc.path("."),
});
qjsc.linkLibrary(libquickjs);
addDefines(qjsc, b);
if (!include_stdlib) {
addStdLib(qjsc, cflags, csrc);
}
b.installArtifact(qjsc);

const qjsc_host = b.addExecutable(.{
.name = "qjsc-host",
.target = b.graph.host,
.optimize = .Debug,
});
qjsc_host.addCSourceFiles(.{
.files = &.{"qjsc.c"},
.flags = cflags,
.root = csrc.path("."),
});
qjsc_host.addCSourceFiles(.{
.files = libquickjs_source,
.flags = cflags,
.root = csrc.path("."),
});
addStdLib(qjsc_host, cflags, csrc);
addDefines(qjsc_host, b);
qjsc_host.linkLibC();

const header = b.addTranslateC(.{
.root_source_file = csrc.path("quickjs.h"),
.target = target,
.optimize = optimize,
});
_ = b.addModule("quickjs-ng", .{ .root_source_file = header.getOutput() });

const gen_repl = b.addRunArtifact(qjsc_host);
gen_repl.addArg("-o");
const gen_repl_out = gen_repl.addOutputFileArg("repl.c");
gen_repl.addArg("-m");
gen_repl.addFileArg(csrc.path("repl.js"));

const qjs = b.addExecutable(.{ .name = "qjs", .target = target, .optimize = optimize });
qjs.addCSourceFiles(.{
.files = &.{"qjs.c"},
.flags = cflags,
.root = csrc.path("."),
});
qjs.addCSourceFiles(.{
.files = &.{"repl.c"},
.root = gen_repl_out.dirname(),
.flags = cflags,
});
if (!include_stdlib) {
addStdLib(qjs, cflags, csrc);
}
qjs.linkLibrary(libquickjs);
addDefines(qjs, b);
qjs.step.dependOn(&gen_repl.step);
b.installArtifact(qjs);
}
16 changes: 16 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.{
.name = "quickjs-ng",
.version = "0.7.0",
.dependencies = .{
.@"quickjs-ng" = .{
.url = "git+https://github.com/quickjs-ng/quickjs?ref=v0.7.0#706ba05fa6fc984e07e6b8b47d028a8ac729fd13",
.hash = "1220c268865c5015ce612560139b95404f3c49eef966d74c5b634647ffdc1855c9cc",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"LICENSE.txt",
"README.md",
},
}

0 comments on commit 72dfdab

Please sign in to comment.