1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 12:36:01 -07:00

build.zig: build both static and shared versions, copy version file

This commit is contained in:
Frank Denis 2022-04-28 18:19:02 -07:00
parent be7c15b23b
commit 94100e5920

124
build.zig
View File

@ -4,71 +4,85 @@ const fmt = std.fmt;
const fs = std.fs; const fs = std.fs;
const heap = std.heap; const heap = std.heap;
const mem = std.mem; const mem = std.mem;
const LibExeObjStep = std.build.LibExeObjStep;
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
const src_path = "src/libsodium";
const src_dir = try fs.Dir.openDir(fs.cwd(), src_path, .{ .iterate = true, .no_follow = true });
var target = b.standardTargetOptions(.{}); var target = b.standardTargetOptions(.{});
var mode = b.standardReleaseOptions(); var mode = b.standardReleaseOptions();
const libsodium = b.addStaticLibrary("sodium", null); const static = b.addStaticLibrary("sodium", null);
libsodium.setTarget(target); const shared = b.addSharedLibrary("sodium", null, .unversioned);
libsodium.setBuildMode(mode); const libs = [_]*LibExeObjStep{ static, shared };
libsodium.install();
if (mode != .Debug) {
libsodium.strip = true;
}
libsodium.linkLibC();
libsodium.addIncludeDir("src/libsodium/include/sodium"); const prebuilt_version_file_path = "builds/msvc/version.h";
libsodium.defineCMacro("CONFIGURED", "1"); const version_file_path = "include/sodium/version.h";
libsodium.defineCMacro("DEV_MODE", "1");
libsodium.defineCMacro("_GNU_SOURCE", "1");
libsodium.defineCMacro("HAVE_INLINE_ASM", "1");
libsodium.defineCMacro("HAVE_TI_MODE", "1");
libsodium.defineCMacro("HAVE_ATOMIC_OPS", "1");
switch (target.getCpuArch()) { if (src_dir.access(version_file_path, .{ .mode = .read_only })) {} else |_| {
.x86_64 => { try fs.cwd().copyFile(prebuilt_version_file_path, src_dir, version_file_path, .{});
libsodium.defineCMacro("HAVE_AMD64_ASM", "1");
libsodium.defineCMacro("HAVE_AVX_ASM", "1");
libsodium.defineCMacro("HAVE_CPUID", "1");
libsodium.defineCMacro("HAVE_MMINTRIN_H", "1");
libsodium.defineCMacro("HAVE_EMMINTRIN_H", "1");
libsodium.defineCMacro("HAVE_PMMINTRIN_H", "1");
},
.aarch64, .aarch64_be => {
libsodium.defineCMacro("HAVE_ARMCRYTO", "1");
},
.wasm32, .wasm64 => {
libsodium.defineCMacro("__wasm__", "1");
},
else => {},
} }
switch (target.getOsTag()) { for (libs) |lib| {
.wasi => { lib.setTarget(target);
libsodium.defineCMacro("__wasi__", "1"); lib.setBuildMode(mode);
}, lib.install();
else => {}, if (mode != .Debug) {
} lib.strip = true;
}
lib.linkLibC();
const base = "src/libsodium"; lib.addIncludeDir("src/libsodium/include/sodium");
const dir = try fs.Dir.openDir(fs.cwd(), base, .{ .iterate = true, .no_follow = true }); lib.defineCMacro("CONFIGURED", "1");
var allocator = heap.page_allocator; lib.defineCMacro("DEV_MODE", "1");
var walker = try dir.walk(allocator); lib.defineCMacro("_GNU_SOURCE", "1");
while (try walker.next()) |entry| { lib.defineCMacro("HAVE_INLINE_ASM", "1");
const name = entry.basename; lib.defineCMacro("HAVE_TI_MODE", "1");
if (mem.endsWith(u8, name, ".c")) { lib.defineCMacro("HAVE_ATOMIC_OPS", "1");
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ base, entry.path });
libsodium.addCSourceFiles(&.{full_path}, &.{ switch (target.getCpuArch()) {
"-fvisibility=hidden", .x86_64 => {
"-fno-strict-aliasing", lib.defineCMacro("HAVE_AMD64_ASM", "1");
"-fno-strict-overflow", lib.defineCMacro("HAVE_AVX_ASM", "1");
"-fwrapv", lib.defineCMacro("HAVE_CPUID", "1");
"-flax-vector-conversions", lib.defineCMacro("HAVE_MMINTRIN_H", "1");
}); lib.defineCMacro("HAVE_EMMINTRIN_H", "1");
} else if (mem.endsWith(u8, name, ".S")) { lib.defineCMacro("HAVE_PMMINTRIN_H", "1");
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ base, entry.path }); },
libsodium.addAssemblyFile(full_path); .aarch64, .aarch64_be => {
lib.defineCMacro("HAVE_ARMCRYTO", "1");
},
.wasm32, .wasm64 => {
lib.defineCMacro("__wasm__", "1");
},
else => {},
}
switch (target.getOsTag()) {
.wasi => {
lib.defineCMacro("__wasi__", "1");
},
else => {},
}
var allocator = heap.page_allocator;
var walker = try src_dir.walk(allocator);
while (try walker.next()) |entry| {
const name = entry.basename;
if (mem.endsWith(u8, name, ".c")) {
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
lib.addCSourceFiles(&.{full_path}, &.{
"-fvisibility=hidden",
"-fno-strict-aliasing",
"-fno-strict-overflow",
"-fwrapv",
"-flax-vector-conversions",
});
} else if (mem.endsWith(u8, name, ".S")) {
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
lib.addAssemblyFile(full_path);
}
} }
} }
} }