Skip to content

Commit

Permalink
no-ho: add boost.context
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Sep 3, 2024
1 parent 9c296e4 commit e4b1cbb
Showing 1 changed file with 190 additions and 3 deletions.
193 changes: 190 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const boost_libs = [_][]const u8{
"utility",
"endian",
"regex",
"asio",
"asio", // stackless coroutine (stl) and stackful coroutine (need boost.context)
"align",
"system",
"intrusive",
Expand Down Expand Up @@ -81,7 +81,7 @@ const boost_libs = [_][]const u8{
"function",
"spirit",
"function_types",
"cobalt", // need boost.context & boost.asio (no header-only)
"cobalt", // need boost.asio (no header-only)
"phoenix",
"nowide",
"locale",
Expand Down Expand Up @@ -181,6 +181,15 @@ pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
});
lib.addObject(boostContainer);
}
if (module.context) {
const boostContext = buildContext(b, .{
.header_only = config.header_only,
.target = config.target,
.optimize = config.optimize,
.include_dirs = lib.root_module.include_dirs,
});
lib.addObject(boostContext);
}
if (module.json) {
const boostJson = buildJson(b, .{
.header_only = config.header_only,
Expand All @@ -202,7 +211,7 @@ pub fn boostLibraries(b: *std.Build, config: Config) *std.Build.Step.Compile {
}

pub const Config = struct {
header_only: bool,
header_only: bool = false,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
module: ?boostLibrariesModules = null,
Expand Down Expand Up @@ -320,3 +329,181 @@ fn buildJson(b: *std.Build, config: Config) *std.Build.Step.Compile {

return obj;
}

fn buildContext(b: *std.Build, config: Config) *std.Build.Step.Compile {
const contextPath = b.dependency("context", .{}).path("");
const obj = b.addObject(.{
.name = "context",
.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;
}
}
const ctxPath = contextPath.getPath(b);
obj.addIncludePath(.{
.cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm" }),
}); // common.h
obj.addCSourceFiles(.{
.root = contextPath,
.files = &.{
"src/continuation.cpp",
"src/fiber.cpp",
},
.flags = cxxFlags,
});

obj.addCSourceFile(.{
.file = switch (obj.rootModuleTarget().os.tag) {
.windows => .{
.cwd_relative = b.pathJoin(&.{ ctxPath, "src/windows/stack_traits.cpp" }),
},
else => .{
.cwd_relative = b.pathJoin(&.{ ctxPath, "src/posix/stack_traits.cpp" }),
},
},
.flags = cxxFlags,
});
if (obj.rootModuleTarget().os.tag == .windows) {
obj.defineCMacro("BOOST_USE_WINFIB", null);
obj.want_lto = false;
} else {
obj.defineCMacro("BOOST_USE_UCONTEXT", null);
}
switch (obj.rootModuleTarget().cpu.arch) {
.arm => switch (obj.rootModuleTarget().os.tag) {
.windows => {
if (obj.rootModuleTarget().abi == .msvc) {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm_aapcs_pe_armasm.asm" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm_aapcs_pe_armasm.asm" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm_aapcs_pe_armasm.asm" }) });
}
},
.macos => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm_aapcs_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm_aapcs_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm_aapcs_macho_gas.S" }) });
},
else => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm_aapcs_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm_aapcs_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm_aapcs_elf_gas.S" }) });
},
},
.aarch64 => switch (obj.rootModuleTarget().os.tag) {
.windows => {
if (obj.rootModuleTarget().abi == .msvc) {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm64_aapcs_pe_armasm.asm" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm64_aapcs_pe_armasm.asm" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm64_aapcs_pe_armasm.asm" }) });
}
},
.macos => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm64_aapcs_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm64_aapcs_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm64_aapcs_macho_gas.S" }) });
},
else => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_arm64_aapcs_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_arm64_aapcs_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_arm64_aapcs_elf_gas.S" }) });
},
},
.riscv64 => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_riscv64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_riscv64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_riscv64_sysv_elf_gas.S" }) });
},
.x86 => switch (obj.rootModuleTarget().os.tag) {
.windows => {
// @panic("undefined symbol:{j/m/o}-fcontext");
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_ms_pe_clang_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_ms_pe_clang_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_ms_pe_clang_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_ms_pe_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_ms_pe_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_ms_pe_gas.S" }) });
},
.macos => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_x86_64_sysv_macho_gas.S" }) });
},
else => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_sysv_elf_gas.S" }) });
},
},
.x86_64 => switch (obj.rootModuleTarget().os.tag) {
.windows => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_x86_64_ms_pe_clang_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_x86_64_ms_pe_clang_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_x86_64_ms_pe_clang_gas.S" }) });
},
.macos => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_i386_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_i386_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_x86_64_sysv_macho_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_i386_x86_64_sysv_macho_gas.S" }) });
},
else => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_x86_64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_x86_64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_x86_64_sysv_elf_gas.S" }) });
},
},
.s390x => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_s390x_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_s390x_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_s390x_sysv_elf_gas.S" }) });
},
.mips, .mipsel => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_mips32_o32_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_mips32_o32_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_mips32_o32_elf_gas.S" }) });
},
.mips64, .mips64el => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_mips64_n64_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_mips64_n64_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_mips64_n64_elf_gas.S" }) });
},
.loongarch64 => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_loongarch64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_loongarch64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_loongarch64_sysv_elf_gas.S" }) });
},
.powerpc => {
obj.addCSourceFile(.{
.file = .{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/tail_ontop_ppc32_sysv.cpp" }) },
.flags = cxxFlags,
});
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_ppc32_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_ppc32_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_ppc32_sysv_elf_gas.S" }) });
},
.powerpc64 => {
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/jump_ppc64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/make_ppc64_sysv_elf_gas.S" }) });
obj.addAssemblyFile(.{ .cwd_relative = b.pathJoin(&.{ ctxPath, "src/asm/ontop_ppc64_sysv_elf_gas.S" }) });
},
else => @panic("Invalid arch"),
}

if (obj.rootModuleTarget().abi == .msvc)
obj.linkLibC()
else {
obj.defineCMacro("_GNU_SOURCE", null);
obj.linkLibCpp();
}

return obj;
}

0 comments on commit e4b1cbb

Please sign in to comment.