Skip to content

Commit

Permalink
wip: no headers-only as object-modules
Browse files Browse the repository at this point in the history
- boost.json, boost.cobalt, boost.container added

```bash
Project-Specific Options:
  -Dtarget=[string]            The CPU architecture, OS, and ABI to build for
  -Dcpu=[string]               Target CPU features to add or subtract
  -Ddynamic-linker=[string]    Path to interpreter on the target system
  -Doptimize=[enum]            Prioritize performance, safety, or binary size
                                 Supported Values:
                                   Debug
                                   ReleaseSafe
                                   ReleaseFast
                                   ReleaseSmall
  -Dheaders-only=[bool]        Build header-only libraries (default: true)
  -Dcobalt=[bool]              Build cobalt library (default: false)
  -Dcontext=[bool]             Build context library (default: false)
  -Djson=[bool]                Build json library (default: false)
  -Dcontainer=[bool]           Build container library (default: false)
  -Dfilesystem=[bool]          Build filesystem library (default: false)
  -Dcoroutine2=[bool]          Build coroutine2 library (default: false)
```
  • Loading branch information
kassane committed Sep 2, 2024
1 parent ad3b78a commit 3da0c47
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 24 deletions.
176 changes: 153 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ pub fn build(b: *std.Build) !void {
const boost = boostLibraries(b, .{
.target = target,
.optimize = optimize,
.header_only = b.option(bool, "headers-only", "Build header-only libraries (default: true)") orelse true,
.header_only = b.option(bool, "headers-only", "Build headers-only libraries (default: true)") orelse true,
.module = .{
.cobalt = b.option(bool, "cobalt", "Build cobalt library (default: false)") orelse false,
.context = b.option(bool, "context", "Build context library (default: false)") orelse false,
.json = b.option(bool, "json", "Build json library (default: false)") orelse false,
.container = b.option(bool, "container", "Build container library (default: false)") orelse false,
.filesystem = b.option(bool, "filesystem", "Build filesystem library (default: false)") orelse false,
.coroutine2 = b.option(bool, "coroutine2", "Build coroutine2 library (default: false)") orelse false,
},
});
b.installArtifact(boost);
}
Expand All @@ -127,7 +135,7 @@ const cxxFlags: []const []const u8 = &.{
"-Wformat",
};

const boost_version: std.SemanticVersion = .{ .major = 0, .minor = 86, .patch = 0 };
const boost_version: std.SemanticVersion = .{ .major = 1, .minor = 86, .patch = 0 };

pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
Expand All @@ -154,27 +162,35 @@ pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
.flags = cxxFlags,
});
} else {
// WIP
const boostJson = b.dependency("json", .{}).path("");
const boostContainer = b.dependency("container", .{}).path("");
lib.addCSourceFiles(.{
.root = boostContainer,
.files = &.{
"src/pool_resource.cpp",
"src/monotonic_buffer_resource.cpp",
"src/synchronized_pool_resource.cpp",
"src/unsynchronized_pool_resource.cpp",
"src/global_resource.cpp",
},
.flags = cxxFlags,
});
lib.addCSourceFiles(.{
.root = boostJson,
.files = &.{
"src/src.cpp",
},
.flags = cxxFlags,
});
if (config.module) |module| {
if (module.cobalt) {
const boostCobalt = buildCobalt(b, .{
.header_only = config.header_only,
.target = config.target,
.optimize = config.optimize,
.include_dirs = lib.root_module.include_dirs,
});
lib.addObject(boostCobalt);
}
if (module.container) {
const boostContainer = buildContainer(b, .{
.header_only = config.header_only,
.target = config.target,
.optimize = config.optimize,
.include_dirs = lib.root_module.include_dirs,
});
lib.addObject(boostContainer);
}
if (module.json) {
const boostJson = buildJson(b, .{
.header_only = config.header_only,
.target = config.target,
.optimize = config.optimize,
.include_dirs = lib.root_module.include_dirs,
});
lib.addObject(boostJson);
}
}
}
if (lib.rootModuleTarget().abi == .msvc)
lib.linkLibC()
Expand All @@ -189,4 +205,118 @@ pub const Config = struct {
header_only: bool,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
module: ?boostLibrariesModules = null,
include_dirs: ?std.ArrayListUnmanaged(std.Build.Module.IncludeDir) = null,
};

// No header-only libraries
const boostLibrariesModules = struct {
coroutine2: bool = false,
context: bool = false,
json: bool = false,
container: bool = false,
filesystem: bool = false,
cobalt: bool = false,
};

fn buildCobalt(b: *std.Build, config: Config) *std.Build.Step.Compile {
const cobaltPath = b.dependency("cobalt", .{}).path("");
const obj = b.addObject(.{
.name = "cobalt",
.target = config.target,
.optimize = config.optimize,
});

if (config.include_dirs) |include_dirs| {
for (include_dirs.items) |include| {
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
}
}
obj.defineCMacro("BOOST_COBALT_SOURCE", null);
obj.defineCMacro("BOOST_COBALT_USE_BOOST_CONTAINER_PMR", null);
obj.addCSourceFiles(.{
.root = cobaltPath,
.files = &.{
"src/channel.cpp",
"src/detail/exception.cpp",
"src/detail/util.cpp",
"src/error.cpp",
"src/main.cpp",
"src/this_thread.cpp",
"src/thread.cpp",
},
.flags = cxxFlags ++ &[_][]const u8{"-std=c++20"},
});
if (obj.rootModuleTarget().abi == .msvc)
obj.linkLibC()
else {
obj.defineCMacro("_GNU_SOURCE", null);
obj.linkLibCpp();
}

return obj;
}

fn buildContainer(b: *std.Build, config: Config) *std.Build.Step.Compile {
const containerPath = b.dependency("container", .{}).path("");
const obj = b.addObject(.{
.name = "container",
.target = config.target,
.optimize = config.optimize,
});

if (config.include_dirs) |include_dirs| {
for (include_dirs.items) |include| {
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
}
}
obj.addCSourceFiles(.{
.root = containerPath,
.files = &.{
"src/pool_resource.cpp",
"src/monotonic_buffer_resource.cpp",
"src/synchronized_pool_resource.cpp",
"src/unsynchronized_pool_resource.cpp",
"src/global_resource.cpp",
},
.flags = cxxFlags,
});
if (obj.rootModuleTarget().abi == .msvc)
obj.linkLibC()
else {
obj.defineCMacro("_GNU_SOURCE", null);
obj.linkLibCpp();
}

return obj;
}

fn buildJson(b: *std.Build, config: Config) *std.Build.Step.Compile {
const jsonPath = b.dependency("json", .{}).path("");
const obj = b.addObject(.{
.name = "json",
.target = config.target,
.optimize = config.optimize,
});

if (config.include_dirs) |include_dirs| {
for (include_dirs.items) |include| {
obj.root_module.include_dirs.append(b.allocator, include) catch unreachable;
}
}
obj.addCSourceFiles(.{
.root = jsonPath,
.files = &.{
"src/src.cpp",
},
.flags = cxxFlags,
});
if (obj.rootModuleTarget().abi == .msvc)
obj.linkLibC()
else {
obj.defineCMacro("_GNU_SOURCE", null);
obj.linkLibCpp();
}

return obj;
}
10 changes: 9 additions & 1 deletion tests/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
// .@"headers-only" = false, // need link-library artifact

// need disable header-only
.cobalt = true,
.container = true,
.context = false,
.coroutine2 = false,
.filesystem = false,
.json = true,
});

inline for (&.{
Expand Down Expand Up @@ -41,7 +49,7 @@ fn buildTests(b: *std.Build, options: struct {
exe.root_module.include_dirs.append(b.allocator, include_dir) catch unreachable;
}
// if not header-only, link library
// exe.linkLibrary(artifact);
exe.linkLibrary(artifact);
}

for (options.files) |file| {
Expand Down

0 comments on commit 3da0c47

Please sign in to comment.