Skip to content

Commit 01b64b6

Browse files
committed
Initial project
1 parent 0166f15 commit 01b64b6

File tree

5 files changed

+373
-3
lines changed

5 files changed

+373
-3
lines changed

.github/workflows/ci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Zig
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: ['*']
8+
9+
jobs:
10+
build:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
targets:
15+
- x86_64-linux-gnu
16+
- x86_64-linux-musl
17+
- x86-linux-gnu
18+
- x86-linux-musl
19+
- aarch64-linux-gnu
20+
- aarch64-linux-musl
21+
- riscv64-linux-musl
22+
- powerpc64-linux-musl
23+
- x86_64-macos
24+
- aarch64-macos
25+
- x86-windows
26+
- x86_64-windows
27+
- aarch64-windows
28+
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
with:
33+
submodules: recursive
34+
fetch-depth: 0
35+
- uses: mlugg/setup-zig@v1
36+
- name: Build Summary ${{ matrix.targets }}
37+
run: zig build -Dtests --summary all -freference-trace -Dtarget=${{ matrix.targets }}
38+
39+
zig-msvc:
40+
runs-on: windows-latest
41+
strategy:
42+
fail-fast: false
43+
steps:
44+
- uses: actions/checkout@v4
45+
- uses: mlugg/setup-zig@v1
46+
- name: Build Summary x86_64-windows-msvc
47+
run: zig build -Dtests --summary all -freference-trace -Dtarget=native-windows-msvc

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
.zig-cache/
2-
zig-out/
1+
*zig-*

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
# fmt-zig
2-
A C++ modern formatting library with build.zig
2+
3+
A modern C++ formatting library {fmt} with build.zig integration
4+
5+
This is a Zig build system wrapper for the [{fmt}](https://github.com/fmtlib/fmt) library - a modern formatting library providing a fast and safe alternative to C stdio and C++ iostreams.
6+
7+
Key features of {fmt}:
8+
- Fast and efficient formatting with type-safe format strings
9+
- Implements C++20 std::format
10+
- Supports user-defined types
11+
- Extensive formatting options and positional arguments
12+
- Modern clean API design
13+
- Header-only and compiled versions available
14+
15+
## Requirements
16+
17+
- [zig](https://ziglang.org/download) v0.14.0 or master
18+
19+
## How to build
20+
21+
Make directory and init
22+
23+
```bash
24+
$ zig init
25+
## add in 'build.zig.zon' fmt-zig package
26+
$ zig fetch --save=fmt git+https://github.com/allyourcodebase/fmt-zig
27+
```
28+
Add in **build.zig**
29+
```zig
30+
const std = @import("std");
31+
32+
pub fn build(b: *std.Build) !void {
33+
const target = b.standardTargetOptions(.{});
34+
const optimize = b.standardOptimizeOption(.{});
35+
36+
const fmt_dep = b.dependency("fmt", .{
37+
.target = target,
38+
.optimize = optimize,
39+
});
40+
const fmt_artifact = fmt_dep.artifact("fmt");
41+
42+
for(fmt_artifact.root_module.include_dirs.items) |include_dir| {
43+
try exe.root_module.include_dirs.append(b.allocator, include_dir);
44+
}
45+
exe.linkLibrary(fmt_artifact);
46+
}
47+
```

build.zig

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
// Options
8+
const shared = b.option(bool, "shared", "Build the Shared Library [default: false]") orelse false;
9+
const tests = b.option(bool, "tests", "Build tests [default: false]") orelse false;
10+
11+
const fmt_dep = b.dependency("fmt", .{
12+
.target = target,
13+
.optimize = optimize,
14+
});
15+
16+
const lib = if (shared) b.addSharedLibrary(.{
17+
.name = "fmt",
18+
.target = target,
19+
.optimize = optimize,
20+
.version = .{
21+
.major = 11,
22+
.minor = 1,
23+
.patch = 4,
24+
},
25+
}) else b.addStaticLibrary(.{
26+
.name = "fmt",
27+
.target = target,
28+
.optimize = optimize,
29+
});
30+
lib.addIncludePath(fmt_dep.path("include"));
31+
lib.addCSourceFiles(.{
32+
.root = fmt_dep.path("src"),
33+
.files = src,
34+
});
35+
if (optimize == .Debug or optimize == .ReleaseSafe)
36+
lib.bundle_compiler_rt = true
37+
else
38+
lib.root_module.strip = true;
39+
if (lib.linkage == .static)
40+
lib.pie = true;
41+
if (lib.rootModuleTarget().abi != .msvc)
42+
lib.linkLibCpp()
43+
else
44+
lib.linkLibC();
45+
lib.installHeadersDirectory(fmt_dep.path("include"), "", .{});
46+
b.installArtifact(lib);
47+
48+
if (tests) {
49+
buildTest(b, .{
50+
.optimize = optimize,
51+
.target = target,
52+
.lib = lib,
53+
.dep = fmt_dep,
54+
.path = "args-test.cc",
55+
});
56+
buildTest(b, .{
57+
.optimize = optimize,
58+
.target = target,
59+
.lib = lib,
60+
.dep = fmt_dep,
61+
.path = "base-test.cc",
62+
});
63+
buildTest(b, .{
64+
.optimize = optimize,
65+
.target = target,
66+
.lib = lib,
67+
.dep = fmt_dep,
68+
.path = "unicode-test.cc",
69+
});
70+
buildTest(b, .{
71+
.optimize = optimize,
72+
.target = target,
73+
.lib = lib,
74+
.dep = fmt_dep,
75+
.path = "assert-test.cc",
76+
});
77+
buildTest(b, .{
78+
.optimize = optimize,
79+
.target = target,
80+
.lib = lib,
81+
.dep = fmt_dep,
82+
.path = "std-test.cc",
83+
});
84+
buildTest(b, .{
85+
.optimize = optimize,
86+
.target = target,
87+
.lib = lib,
88+
.dep = fmt_dep,
89+
.path = "xchar-test.cc",
90+
});
91+
buildTest(b, .{
92+
.optimize = optimize,
93+
.target = target,
94+
.lib = lib,
95+
.dep = fmt_dep,
96+
.path = "ostream-test.cc",
97+
});
98+
buildTest(b, .{
99+
.optimize = optimize,
100+
.target = target,
101+
.lib = lib,
102+
.dep = fmt_dep,
103+
.path = "printf-test.cc",
104+
});
105+
// FIXME: duplicate symbols error
106+
// buildTest(b, .{
107+
// .optimize = optimize,
108+
// .target = target,
109+
// .lib = lib,
110+
// .dep = fmt_dep,
111+
// .path = "scan-test.cc",
112+
// });
113+
buildTest(b, .{
114+
.optimize = optimize,
115+
.target = target,
116+
.lib = lib,
117+
.dep = fmt_dep,
118+
.path = "ranges-test.cc",
119+
});
120+
buildTest(b, .{
121+
.optimize = optimize,
122+
.target = target,
123+
.lib = lib,
124+
.dep = fmt_dep,
125+
.path = "color-test.cc",
126+
});
127+
buildTest(b, .{
128+
.optimize = optimize,
129+
.target = target,
130+
.lib = lib,
131+
.dep = fmt_dep,
132+
.path = "chrono-test.cc",
133+
});
134+
buildTest(b, .{
135+
.optimize = optimize,
136+
.target = target,
137+
.lib = lib,
138+
.dep = fmt_dep,
139+
.path = "compile-test.cc",
140+
});
141+
buildTest(b, .{
142+
.optimize = optimize,
143+
.target = target,
144+
.lib = lib,
145+
.dep = fmt_dep,
146+
.path = "compile-fp-test.cc",
147+
});
148+
buildTest(b, .{
149+
.optimize = optimize,
150+
.target = target,
151+
.lib = lib,
152+
.dep = fmt_dep,
153+
.path = "format-test.cc",
154+
});
155+
buildTest(b, .{
156+
.optimize = optimize,
157+
.target = target,
158+
.lib = lib,
159+
.dep = fmt_dep,
160+
.path = "os-test.cc",
161+
});
162+
buildTest(b, .{
163+
.optimize = optimize,
164+
.target = target,
165+
.lib = lib,
166+
.dep = fmt_dep,
167+
.path = "noexception-test.cc",
168+
});
169+
buildTest(b, .{
170+
.optimize = optimize,
171+
.target = target,
172+
.lib = lib,
173+
.dep = fmt_dep,
174+
.path = "posix-mock-test.cc",
175+
});
176+
// FIXME: https://github.com/ziglang/zig/issues/15496
177+
// buildTest(b, .{
178+
// .optimize = optimize,
179+
// .target = target,
180+
// .lib = lib,
181+
// .dep = fmt_dep,
182+
// .path = "module-test.cc",
183+
// });
184+
}
185+
}
186+
187+
fn buildTest(b: *std.Build, info: BuildInfo) void {
188+
const test_exe = b.addExecutable(.{
189+
.name = info.filename(),
190+
.optimize = info.optimize,
191+
.target = info.target,
192+
});
193+
for (info.lib.root_module.include_dirs.items) |include_dir| {
194+
test_exe.root_module.include_dirs.append(b.allocator, include_dir) catch unreachable;
195+
}
196+
if (info.dep) |dep_test| {
197+
test_exe.addIncludePath(dep_test.path("test"));
198+
test_exe.addIncludePath(dep_test.path("test/gtest"));
199+
test_exe.addIncludePath(dep_test.path("test/gmock"));
200+
201+
test_exe.addCSourceFiles(.{
202+
.root = dep_test.path("test"),
203+
.files = &.{info.path},
204+
});
205+
test_exe.addCSourceFiles(.{
206+
.root = dep_test.path("test/gtest"),
207+
.files = &.{"gmock-gtest-all.cc"},
208+
});
209+
test_exe.addCSourceFiles(.{
210+
.root = dep_test.path("test"),
211+
.files = test_src,
212+
.flags = &.{
213+
"-Wall",
214+
"-Wextra",
215+
"-Wno-deprecated-declarations",
216+
},
217+
});
218+
}
219+
test_exe.root_module.addCMacro("_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING", "1");
220+
test_exe.root_module.addCMacro("GTEST_HAS_PTHREAD", "0");
221+
test_exe.linkLibrary(info.lib);
222+
if (test_exe.rootModuleTarget().abi != .msvc)
223+
test_exe.linkLibCpp()
224+
else
225+
test_exe.linkLibC();
226+
b.installArtifact(test_exe);
227+
228+
const run_cmd = b.addRunArtifact(test_exe);
229+
run_cmd.step.dependOn(b.getInstallStep());
230+
if (b.args) |args| {
231+
run_cmd.addArgs(args);
232+
}
233+
234+
const run_step = b.step(
235+
b.fmt("{s}", .{info.filename()}),
236+
b.fmt("Run the {s} test", .{info.filename()}),
237+
);
238+
run_step.dependOn(&run_cmd.step);
239+
}
240+
241+
const src: []const []const u8 = &.{
242+
"format.cc",
243+
"os.cc",
244+
};
245+
const test_src: []const []const u8 = &.{
246+
"gtest-extra.cc",
247+
"enforce-checks-test.cc",
248+
"util.cc",
249+
};
250+
251+
const BuildInfo = struct {
252+
lib: *std.Build.Step.Compile,
253+
optimize: std.builtin.OptimizeMode,
254+
target: std.Build.ResolvedTarget,
255+
path: []const u8,
256+
dep: ?*std.Build.Dependency = null,
257+
258+
fn filename(self: BuildInfo) []const u8 {
259+
var split = std.mem.splitSequence(u8, std.fs.path.basename(self.path), ".");
260+
return split.first();
261+
}
262+
};

build.zig.zon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.{
2+
.name = .fmt,
3+
.version = "0.1.0",
4+
.fingerprint = 0x3420ce4f26fee0a1,
5+
.dependencies = .{
6+
.fmt = .{
7+
.url = "https://github.com/fmtlib/fmt/releases/download/11.1.4/fmt-11.1.4.zip",
8+
.hash = "N-V-__8AAKD2VgCfm9hxSGtvOt6Ypcvvwh9EPAvMVEZjeLIW",
9+
},
10+
},
11+
.paths = .{
12+
"README.md",
13+
"LICENSE",
14+
"build.zig",
15+
"build.zig.zon",
16+
},
17+
}

0 commit comments

Comments
 (0)