Skip to content

Commit 36a02c7

Browse files
committed
refactoring and new example test added
1 parent 8847626 commit 36a02c7

File tree

4 files changed

+114
-195
lines changed

4 files changed

+114
-195
lines changed

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Build libraries
1414

1515
```bash
1616
# Build no-header-only libraries
17-
$ zig build -Doptimize=<Debug|ReleaseSafe|ReleaseFast|ReleaseSmall> -Dtarget=<triple-target> --summary <all|new> -Dcontext -Djson -Dsystem -Dcontainer -Dcobalt -Dfilesystem -Dheaders-only=false
17+
$ zig build -Doptimize=<Debug|ReleaseSafe|ReleaseFast|ReleaseSmall> -Dtarget=<triple-target> --summary <all|new> -Dcontext -Djson -Dsystem -Dcontainer -Dcobalt -Dfilesystem
1818
```
1919

2020
#### Helper
@@ -30,7 +30,6 @@ Project-Specific Options:
3030
ReleaseSafe
3131
ReleaseFast
3232
ReleaseSmall
33-
-Dheaders-only=[bool] Use headers-only libraries (default: true)
3433
-Dcobalt=[bool] Build cobalt library (default: false)
3534
-Dcontext=[bool] Build context library (default: false)
3635
-Djson=[bool] Build json library (default: false)
@@ -61,8 +60,6 @@ pub fn build(b: *std.Build) !void {
6160
const boost_dep = b.dependency("boost", .{
6261
.target = target,
6362
.optimize = optimize,
64-
// default is true (recommended)
65-
// .@"headers-only" = false,
6663
});
6764
const boost_artifact = boost_dep.artifact("boost");
6865

build.zig

+35-187
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub fn build(b: *std.Build) !void {
117117
const boost = boostLibraries(b, .{
118118
.target = target,
119119
.optimize = optimize,
120-
.headers_only = b.option(bool, "headers-only", "Use headers-only libraries (default: true)") orelse true,
121120
.module = .{
122121
.cobalt = b.option(bool, "cobalt", "Build cobalt library (default: false)") orelse false,
123122
.context = b.option(bool, "context", "Build context library (default: false)") orelse false,
@@ -160,73 +159,34 @@ pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
160159
lib.addIncludePath(boostLib);
161160
}
162161

163-
if (config.headers_only) {
164-
// zig-pkg bypass (artifact need source file)
165-
const empty = b.addWriteFile("empty.cc",
166-
\\ #include <boost/config.hpp>
167-
);
168-
lib.step.dependOn(&empty.step);
169-
lib.addCSourceFiles(.{
170-
.root = empty.getDirectory(),
171-
.files = &.{"empty.cc"},
172-
.flags = cxxFlags,
173-
});
174-
} else {
175-
if (config.module) |module| {
176-
if (module.cobalt) {
177-
const boostCobalt = buildCobalt(b, .{
178-
.headers_only = config.headers_only,
179-
.target = config.target,
180-
.optimize = config.optimize,
181-
.include_dirs = lib.root_module.include_dirs,
182-
});
183-
lib.addObject(boostCobalt);
184-
}
185-
if (module.container) {
186-
const boostContainer = buildContainer(b, .{
187-
.headers_only = config.headers_only,
188-
.target = config.target,
189-
.optimize = config.optimize,
190-
.include_dirs = lib.root_module.include_dirs,
191-
});
192-
lib.addObject(boostContainer);
193-
}
194-
if (module.context) {
195-
const boostContext = buildContext(b, .{
196-
.headers_only = config.headers_only,
197-
.target = config.target,
198-
.optimize = config.optimize,
199-
.include_dirs = lib.root_module.include_dirs,
200-
});
201-
lib.addObject(boostContext);
202-
}
203-
if (module.json) {
204-
const boostJson = buildJson(b, .{
205-
.headers_only = config.headers_only,
206-
.target = config.target,
207-
.optimize = config.optimize,
208-
.include_dirs = lib.root_module.include_dirs,
209-
});
210-
lib.addObject(boostJson);
211-
}
212-
if (module.filesystem) {
213-
const boostFileSystem = buildFileSystem(b, .{
214-
.headers_only = config.headers_only,
215-
.target = config.target,
216-
.optimize = config.optimize,
217-
.include_dirs = lib.root_module.include_dirs,
218-
});
219-
lib.addObject(boostFileSystem);
220-
}
221-
if (module.system) {
222-
const boostSystem = buildSystem(b, .{
223-
.headers_only = config.headers_only,
224-
.target = config.target,
225-
.optimize = config.optimize,
226-
.include_dirs = lib.root_module.include_dirs,
227-
});
228-
lib.addObject(boostSystem);
229-
}
162+
// zig-pkg bypass (artifact need source file to generate object file)
163+
const empty = b.addWriteFile("empty.cc",
164+
\\ #include <boost/config.hpp>
165+
);
166+
lib.step.dependOn(&empty.step);
167+
lib.addCSourceFiles(.{
168+
.root = empty.getDirectory(),
169+
.files = &.{"empty.cc"},
170+
.flags = cxxFlags,
171+
});
172+
if (config.module) |module| {
173+
if (module.cobalt) {
174+
buildCobalt(b, lib);
175+
}
176+
if (module.container) {
177+
buildContainer(b, lib);
178+
}
179+
if (module.context) {
180+
buildContext(b, lib);
181+
}
182+
if (module.json) {
183+
buildJson(b, lib);
184+
}
185+
if (module.filesystem) {
186+
buildFileSystem(b, lib);
187+
}
188+
if (module.system) {
189+
buildSystem(b, lib);
230190
}
231191
}
232192
if (lib.rootModuleTarget().abi == .msvc)
@@ -239,11 +199,9 @@ pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
239199
}
240200

241201
pub const Config = struct {
242-
headers_only: bool = false,
243202
target: std.Build.ResolvedTarget,
244203
optimize: std.builtin.OptimizeMode,
245204
module: ?boostLibrariesModules = null,
246-
include_dirs: ?std.ArrayListUnmanaged(std.Build.Module.IncludeDir) = null,
247205
};
248206

249207
// No header-only libraries
@@ -257,21 +215,10 @@ const boostLibrariesModules = struct {
257215
system: bool = false,
258216
};
259217

260-
fn buildCobalt(b: *std.Build, config: Config) *std.Build.Step.Compile {
218+
fn buildCobalt(b: *std.Build, obj: *std.Build.Step.Compile) void {
261219
const cobaltPath = b.dependency("cobalt", .{}).path("src");
262-
const obj = b.addObject(.{
263-
.name = "cobalt",
264-
.target = config.target,
265-
.optimize = config.optimize,
266-
});
267-
268-
if (config.include_dirs) |include_dirs| {
269-
for (include_dirs.items) |include| {
270-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
271-
}
272-
}
273220
obj.defineCMacro("BOOST_COBALT_SOURCE", null);
274-
obj.defineCMacro("BOOST_COBALT_USE_BOOST_CONTAINER_PMR", null);
221+
// obj.defineCMacro("BOOST_COBALT_USE_BOOST_CONTAINER_PMR", null);
275222
obj.addCSourceFiles(.{
276223
.root = cobaltPath,
277224
.files = &.{
@@ -285,29 +232,10 @@ fn buildCobalt(b: *std.Build, config: Config) *std.Build.Step.Compile {
285232
},
286233
.flags = cxxFlags ++ &[_][]const u8{"-std=c++20"},
287234
});
288-
if (obj.rootModuleTarget().abi == .msvc)
289-
obj.linkLibC()
290-
else {
291-
obj.defineCMacro("_GNU_SOURCE", null);
292-
obj.linkLibCpp();
293-
}
294-
295-
return obj;
296235
}
297236

298-
fn buildContainer(b: *std.Build, config: Config) *std.Build.Step.Compile {
237+
fn buildContainer(b: *std.Build, obj: *std.Build.Step.Compile) void {
299238
const containerPath = b.dependency("container", .{}).path("src");
300-
const obj = b.addObject(.{
301-
.name = "container",
302-
.target = config.target,
303-
.optimize = config.optimize,
304-
});
305-
306-
if (config.include_dirs) |include_dirs| {
307-
for (include_dirs.items) |include| {
308-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
309-
}
310-
}
311239
obj.addCSourceFiles(.{
312240
.root = containerPath,
313241
.files = &.{
@@ -319,89 +247,35 @@ fn buildContainer(b: *std.Build, config: Config) *std.Build.Step.Compile {
319247
},
320248
.flags = cxxFlags,
321249
});
322-
if (obj.rootModuleTarget().abi == .msvc)
323-
obj.linkLibC()
324-
else {
325-
obj.defineCMacro("_GNU_SOURCE", null);
326-
obj.linkLibCpp();
327-
}
328-
329-
return obj;
330250
}
331251

332-
fn buildJson(b: *std.Build, config: Config) *std.Build.Step.Compile {
252+
fn buildJson(b: *std.Build, obj: *std.Build.Step.Compile) void {
333253
const jsonPath = b.dependency("json", .{}).path("src");
334-
const obj = b.addObject(.{
335-
.name = "json",
336-
.target = config.target,
337-
.optimize = config.optimize,
338-
});
339254

340-
if (config.include_dirs) |include_dirs| {
341-
for (include_dirs.items) |include| {
342-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
343-
}
344-
}
345255
obj.addCSourceFiles(.{
346256
.root = jsonPath,
347257
.files = &.{
348258
"src.cpp",
349259
},
350260
.flags = cxxFlags,
351261
});
352-
if (obj.rootModuleTarget().abi == .msvc)
353-
obj.linkLibC()
354-
else {
355-
obj.defineCMacro("_GNU_SOURCE", null);
356-
obj.linkLibCpp();
357-
}
358-
359-
return obj;
360262
}
361263

362-
fn buildSystem(b: *std.Build, config: Config) *std.Build.Step.Compile {
264+
fn buildSystem(b: *std.Build, obj: *std.Build.Step.Compile) void {
363265
const systemPath = b.dependency("system", .{}).path("src");
364-
const obj = b.addObject(.{
365-
.name = "system",
366-
.target = config.target,
367-
.optimize = config.optimize,
368-
});
369266

370-
if (config.include_dirs) |include_dirs| {
371-
for (include_dirs.items) |include| {
372-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
373-
}
374-
}
375267
obj.addCSourceFiles(.{
376268
.root = systemPath,
377269
.files = &.{
378270
"error_code.cpp",
379271
},
380272
.flags = cxxFlags,
381273
});
382-
if (obj.rootModuleTarget().abi == .msvc)
383-
obj.linkLibC()
384-
else {
385-
obj.defineCMacro("_GNU_SOURCE", null);
386-
obj.linkLibCpp();
387-
}
388-
389-
return obj;
390274
}
391275

392-
fn buildFileSystem(b: *std.Build, config: Config) *std.Build.Step.Compile {
276+
fn buildFileSystem(b: *std.Build, obj: *std.Build.Step.Compile) void {
393277
const fsPath = b.dependency("filesystem", .{}).path("src");
394-
const obj = b.addObject(.{
395-
.name = "filesystem",
396-
.target = config.target,
397-
.optimize = config.optimize,
398-
});
399278

400-
if (config.include_dirs) |include_dirs| {
401-
for (include_dirs.items) |include| {
402-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
403-
}
404-
}
405279
if (obj.rootModuleTarget().os.tag == .windows) {
406280
obj.addCSourceFiles(.{
407281
.root = fsPath,
@@ -429,32 +303,15 @@ fn buildFileSystem(b: *std.Build, config: Config) *std.Build.Step.Compile {
429303
.flags = cxxFlags,
430304
});
431305
if (obj.rootModuleTarget().abi == .msvc) {
432-
obj.linkLibC();
433306
obj.defineCMacro("_SCL_SECURE_NO_WARNINGS", null);
434307
obj.defineCMacro("_SCL_SECURE_NO_DEPRECATE", null);
435308
obj.defineCMacro("_CRT_SECURE_NO_WARNINGS", null);
436309
obj.defineCMacro("_CRT_SECURE_NO_DEPRECATE", null);
437-
} else {
438-
obj.defineCMacro("_GNU_SOURCE", null);
439-
obj.linkLibCpp();
440310
}
441-
442-
return obj;
443311
}
444312

445-
fn buildContext(b: *std.Build, config: Config) *std.Build.Step.Compile {
313+
fn buildContext(b: *std.Build, obj: *std.Build.Step.Compile) void {
446314
const contextPath = b.dependency("context", .{}).path("src");
447-
const obj = b.addObject(.{
448-
.name = "context",
449-
.target = config.target,
450-
.optimize = config.optimize,
451-
});
452-
453-
if (config.include_dirs) |include_dirs| {
454-
for (include_dirs.items) |include| {
455-
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
456-
}
457-
}
458315
const ctxPath = contextPath.getPath(b);
459316
obj.addIncludePath(.{
460317
.cwd_relative = b.pathJoin(&.{ ctxPath, "asm" }),
@@ -609,13 +466,4 @@ fn buildContext(b: *std.Build, config: Config) *std.Build.Step.Compile {
609466
},
610467
else => @panic("Invalid arch"),
611468
}
612-
613-
if (obj.rootModuleTarget().abi == .msvc)
614-
obj.linkLibC()
615-
else {
616-
obj.defineCMacro("_GNU_SOURCE", null);
617-
obj.linkLibCpp();
618-
}
619-
620-
return obj;
621469
}

tests/build.zig

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ pub fn build(b: *std.Build) void {
77
const boost_dep = b.dependency("boost", .{
88
.target = target,
99
.optimize = optimize,
10-
// .@"headers-only" = false, // need link-library artifact
1110

12-
// need disable header-only
1311
.cobalt = true,
1412
.container = true,
1513
.context = false,
@@ -19,6 +17,7 @@ pub fn build(b: *std.Build) void {
1917
});
2018

2119
inline for (&.{
20+
"http_server.cc",
2221
"include_all.cc",
2322
}) |file| {
2423
buildTests(b, .{
@@ -71,9 +70,11 @@ fn buildTests(b: *std.Build, options: struct {
7170
exe.linkLibC();
7271
}
7372

74-
// for boost::asio/boost::beast
75-
if (exe.rootModuleTarget().os.tag == .windows)
73+
// for boost::asio/boost::beast/boost::cobalt
74+
if (exe.rootModuleTarget().os.tag == .windows) {
7675
exe.linkSystemLibrary("ws2_32");
76+
exe.linkSystemLibrary("mswsock");
77+
}
7778

7879
b.installArtifact(exe);
7980
const run_cmd = b.addRunArtifact(exe);

0 commit comments

Comments
 (0)