Skip to content

Commit

Permalink
create build.zig for flex v2.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
leroycep committed Dec 10, 2024
0 parents commit 1b6c19d
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Continuous Integration

on:
push:
branches: [main]

pull_request:
branches: [main]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

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

- name: Set up Zig
uses: mlugg/setup-zig@v1

- name: Run `build`
run: zig build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.zig-cache/
zig-out/
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright © 2024 LeRoyce Pearson

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# FLex

[![CI][ci-shd]][ci-url]
[![License][lc-shd]][lc-url]

## Zig build [FLex](https://github.com/westes/flex)

Uses the release tarball from https://github.com/westes/flex/releases/tag/v2.6.4

Does not currently try to build flex from source, [as building flex requires flex][issue-458].

If you want to build flex from source, the following resources may be helpful:

- https://bootstrapping.miraheze.org/wiki/Live-bootstrap#flex_2.5.11
- https://github.com/fosslinux/live-bootstrap/tree/master/steps/flex-2.5.11
- https://en.wikipedia.org/wiki/Flex_(lexical_analyser_generator)
- https://tomassetti.me/why-you-should-not-use-flex-yacc-and-bison/
- https://pubs.opengroup.org/onlinepubs/9799919799/utilities/lex.html
- https://pubs.opengroup.org/onlinepubs/9799919799/utilities/yacc.html

### Usage

```sh
git clone https://github.com/allyourcodebase/flex.git
cd flex/
zig build
```

### License

- This build script is licensed under a BSD 2-Clause license
- [flex itself is licensed under a modified BSD 2-Clause license][flex-copying]

<!-- MARKDOWN LINKS -->

[issue-458]: https://github.com/westes/flex/issues/458
[flex-copying]: https://github.com/westes/flex/blob/v2.6.4/COPYING

[ci-shd]: https://img.shields.io/github/actions/workflow/status/leroycep/flex/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black
[ci-url]: https://github.com/allyourcodebase/leroycep/blob/main/.github/workflows/ci.yaml
[lc-shd]: https://img.shields.io/github/license/leroycep/flex.svg?style=for-the-badge&labelColor=black
[lc-url]: https://github.com/allyourcodebase/leroycep/blob/main/LICENSE
91 changes: 91 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const std = @import("std");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const flex = b.dependency("flex-generated-src", .{});

const exe = b.addExecutable(.{
.name = "flex",
.target = target,
.optimize = optimize,
});

exe.root_module.addCMacro("VERSION", "\"v2.6.4\""); // flex version
exe.root_module.addCMacro("HAVE_ASSERT_H", "");
exe.root_module.addCMacro("HAVE_LIMITS_H", "");
exe.root_module.addCMacro("HAVE_NETINET_IN_H", "");
exe.root_module.addCMacro("M4", "m4"); // path to default m4 binary

exe.root_module.addIncludePath(flex.path("src"));
exe.root_module.addCSourceFile(.{ .file = flex.path("src/buf.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/ccl.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/dfa.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/ecs.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/filter.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/gen.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/main.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/misc.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/nfa.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/options.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/regex.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/scan.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/scanflags.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/scanopt.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/skel.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/sym.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/tables.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/tables_shared.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/tblcmp.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/yylex.c") });
exe.root_module.addCSourceFile(.{ .file = flex.path("src/parse.c") });
exe.root_module.link_libc = true;

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);

// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);

// // By making the run step depend on the install step, it will be run from the
// // installation directory rather than directly from within the cache directory.
// // This is not necessary, however, if the application depends on other installed
// // files, this ensures they will be present and in the expected location.
// run_cmd.step.dependOn(b.getInstallStep());

// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}
17 changes: 17 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.{
.name = "flex",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "2.6.4",
.dependencies = .{
.@"flex-generated-src" = .{
.url = "https://github.com/westes/flex/releases/download/v2.6.4/flex-2.6.4.tar.gz",
.hash = "122070327bfea3c08a1cb2dc29e24ea613e9b3a167186cb2a63813393bd6c7994998",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"LICENSE.md",
},
}

0 comments on commit 1b6c19d

Please sign in to comment.