forked from marler8997/ziglibc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathziglibcbuild.zig
More file actions
173 lines (166 loc) · 5.46 KB
/
ziglibcbuild.zig
File metadata and controls
173 lines (166 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const std = @import("std");
const build = std.Build;
const CompileStep = build.Step.Compile;
pub const LinkKind = enum { static, shared };
pub const LibVariant = enum {
only_std,
only_posix,
only_linux,
only_gnu,
full,
};
pub const Start = enum {
ziglibc,
glibc,
};
pub const ZigLibcOptions = struct {
variant: LibVariant,
link: LinkKind,
start: Start,
trace: bool,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
};
fn relpath(builder: *build, src_path: []const u8) std.Build.LazyPath {
return builder.path(src_path);
}
/// Provides a _start symbol that will call C main
pub fn addZigStart(
builder: *build,
target: std.Build.ResolvedTarget,
optimize: anytype,
) *CompileStep {
const lib = addStaticLibraryCompat(builder, .{
.name = "start",
.root_source_file = relpath(builder, "src" ++ std.fs.path.sep_str ++ "start.zig"),
.target = target,
.optimize = optimize,
.pic = true,
});
return lib;
}
// Returns ziglibc as a CompileStep
// Caller will also need to add the include path to get the C headers
pub fn addLibc(builder: *std.Build, opt: ZigLibcOptions) *CompileStep {
const name = switch (opt.variant) {
.only_std => "c-only-std",
.only_posix => "c-only-posix",
.only_linux => "c-only-linux",
.only_gnu => "c-only-gnu",
//.full => "c",
.full => "cguana", // use cguana to avoid passing in '-lc' to zig which will
// cause it to add the system libc headers
};
const trace_options = builder.addOptions();
trace_options.addOption(bool, "enabled", opt.trace);
const modules_options = builder.addOptions();
modules_options.addOption(bool, "glibcstart", switch (opt.start) {
.glibc => true,
else => false,
});
const index = relpath(builder, "src" ++ std.fs.path.sep_str ++ "lib.zig");
const lib = switch (opt.link) {
.static => addStaticLibraryCompat(builder, .{
.name = name,
.root_source_file = index,
.target = opt.target,
.optimize = opt.optimize,
.pic = true,
}),
.shared => addSharedLibraryCompat(builder, .{
.name = name,
.root_source_file = index,
.target = opt.target,
.optimize = opt.optimize,
.pic = true,
.version = switch (opt.variant) {
.full => .{ .major = 6, .minor = 0, .patch = 0 },
else => null,
},
}),
};
lib.root_module.addOptions("modules", modules_options);
lib.root_module.addOptions("trace_options", trace_options);
const c_flags = [_][]const u8{
"-std=c11",
};
const include_cstd = switch (opt.variant) {
.only_std, .full => true,
else => false,
};
modules_options.addOption(bool, "cstd", include_cstd);
if (include_cstd) {
lib.addCSourceFile(.{ .file = relpath(builder, "src" ++ std.fs.path.sep_str ++ "printf.c"), .flags = &c_flags });
lib.addCSourceFile(.{ .file = relpath(builder, "src" ++ std.fs.path.sep_str ++ "scanf.c"), .flags = &c_flags });
}
const include_posix = switch (opt.variant) {
.only_posix, .full => true,
else => false,
};
modules_options.addOption(bool, "posix", include_posix);
if (include_posix) {
lib.addCSourceFile(.{ .file = relpath(builder, "src" ++ std.fs.path.sep_str ++ "posix.c"), .flags = &c_flags });
}
const include_linux = switch (opt.variant) {
.only_linux, .full => true,
else => false,
};
modules_options.addOption(bool, "linux", include_linux);
if (include_cstd or include_posix) {
lib.addIncludePath(relpath(builder, "inc" ++ std.fs.path.sep_str ++ "libc"));
lib.addIncludePath(relpath(builder, "inc" ++ std.fs.path.sep_str ++ "posix"));
}
const include_gnu = switch (opt.variant) {
.only_gnu, .full => true,
else => false,
};
modules_options.addOption(bool, "gnu", include_gnu);
if (include_gnu) {
lib.addIncludePath(relpath(builder, "inc" ++ std.fs.path.sep_str ++ "gnu"));
}
return lib;
}
fn addStaticLibraryCompat(
b: *std.Build,
opt: struct {
name: []const u8,
root_source_file: ?std.Build.LazyPath = null,
target: ?std.Build.ResolvedTarget = null,
optimize: ?std.builtin.OptimizeMode = null,
pic: ?bool = null,
},
) *CompileStep {
return b.addLibrary(.{
.linkage = .static,
.name = opt.name,
.root_module = b.createModule(.{
.root_source_file = opt.root_source_file,
.target = opt.target orelse b.graph.host,
.optimize = opt.optimize orelse .Debug,
.pic = opt.pic,
}),
});
}
fn addSharedLibraryCompat(
b: *std.Build,
opt: struct {
name: []const u8,
root_source_file: ?std.Build.LazyPath = null,
target: ?std.Build.ResolvedTarget = null,
optimize: ?std.builtin.OptimizeMode = null,
version: ?std.SemanticVersion = null,
pic: ?bool = null,
},
) *CompileStep {
return b.addLibrary(.{
.linkage = .dynamic,
.name = opt.name,
.root_module = b.createModule(.{
.root_source_file = opt.root_source_file,
.target = opt.target orelse b.graph.host,
.optimize = opt.optimize orelse .Debug,
.pic = opt.pic,
}),
.version = opt.version,
});
}