-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_geos.zig
162 lines (149 loc) · 4.8 KB
/
build_geos.zig
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
/// Credit to mattnite for https://github.com/mattnite/zig-zlib/blob/a6a72f47c0653b5757a86b453b549819a151d6c7/zlib.zig
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const endsWith = std.mem.endsWith;
pub fn addIncludePath(b:*std.Build,libOrExe:anytype,geos_dep:*std.Build.Dependency) void {
const include_dirs = [_][]const u8{
"/home/catnuko/zig-geos/src/vendor/geos/build/capi",
"/home/catnuko/zig-geos/src/vendor/geos/build/include",
"/home/catnuko/zig-geos/src/shim",
};
for (include_dirs) |d| {
// libOrExe.addIncludePath(b.path(d));
libOrExe.addIncludePath(.{ .src_path = .{
.owner = b,
.sub_path = d,
} });
}
libOrExe.addIncludePath(geos_dep.path("include"));
libOrExe.addIncludePath(geos_dep.path("src/deps"));
}
/// c args and defines were (mostly) copied from
/// src/geos/build/CMakeFiles/geos.dir/flags.make
const geos_c_args = [_][]const u8{
"-g0",
"-O",
"-DNDEBUG",
"-DDLL_EXPORT",
"-DUSE_UNSTABLE_GEOS_CPP_API",
"-DGEOS_INLINE",
"-Dgeos_EXPORTS",
"-fPIC",
"-ffp-contract=off",
"-Werror",
"-pedantic",
"-Wall",
"-Wextra",
"-Wno-long-long",
"-Wcast-align",
"-Wchar-subscripts",
"-Wdouble-promotion",
"-Wpointer-arith",
"-Wformat",
"-Wformat-security",
"-Wshadow",
"-Wuninitialized",
"-Wunused-parameter",
"-fno-common",
"-Wno-unknown-warning-option",
};
/// cpp args and defines were (mostly) copied from
/// src/geos/build/CMakeFiles/geos.dir/flags.make
const geos_cpp_args = [_][]const u8{
"-g0",
"-O",
"-DNDEBUG",
"-DDLL_EXPORT",
"-DGEOS_INLINE",
"-DUSE_UNSTABLE_GEOS_CPP_API",
"-Dgeos_EXPORTS",
"-fPIC",
"-ffp-contract=off",
"-Werror",
"-pedantic",
"-Wall",
"-Wextra",
"-Wno-long-long",
"-Wcast-align",
"-Wchar-subscripts",
"-Wdouble-promotion",
"-Wpointer-arith",
"-Wformat",
"-Wformat-security",
"-Wshadow",
"-Wuninitialized",
"-Wunused-parameter",
"-fno-common",
"-Wno-unknown-warning-option",
"-std=c++14",
};
pub const Options = struct {
import_name: ?[]const u8 = null,
};
pub fn createCore(b: *std.Build,geos_dep:*std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step.Compile {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
var core = b.addStaticLibrary(.{
.name = "geos_core",
.target = target,
.optimize = optimize,
});
addIncludePath(b,core,geos_dep);
core.linkLibCpp();
const root = geos_dep.path(".");
const src_dir = geos_dep.path("src").getPath(b);
const core_cpp_srcs = try findSources(alloc,src_dir,"src", ".cpp");
defer alloc.free(core_cpp_srcs);
core.addCSourceFiles(.{
.root = root,
.files = core_cpp_srcs,
.flags = &geos_cpp_args,
});
const core_c_srcs = try findSources(alloc, src_dir, "src", ".c");
defer alloc.free(core_c_srcs);
core.addCSourceFiles(.{
.root = root,
.files = core_c_srcs,
.flags = &geos_c_args,
});
core.addCSourceFile(.{ .file = b.path("src/shim/zig_handlers.c"), .flags = &geos_c_args });
return core;
}
pub fn createCAPI(b: *std.Build,geos_dep:*std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step.Compile {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
var c_api = b.addStaticLibrary(.{
.name = "geos_c",
.target = target,
.optimize = optimize,
});
addIncludePath(b,c_api,geos_dep);
c_api.linkLibCpp();
const root = geos_dep.path(".");
const capi_dir = geos_dep.path("capi").getPath(b);
const cpp_srcs = try findSources(alloc,capi_dir,"capi", ".cpp");
defer alloc.free(cpp_srcs);
c_api.addCSourceFiles(.{
.root = root,
.files = cpp_srcs,
.flags = &geos_cpp_args,
});
return c_api;
}
/// Walk the libgeos source tree and collect either .c and .cpp source files,
/// depending on the suffix. *Caller owns the returned memory.*
fn findSources(alloc: Allocator, path: []const u8, rel_path: []const u8, suffix: []const u8) ![]const []const u8 {
const libgeos_dir = try std.fs.openDirAbsolute(path, .{ .iterate = true });
var walker = try libgeos_dir.walk(alloc);
defer walker.deinit();
var list = ArrayList([]const u8).init(alloc);
while (try walker.next()) |entry| {
if (entry.kind != .file) continue;
if (endsWith(u8, entry.basename, suffix)) {
const abs_path = try std.fs.path.join(alloc, &.{ rel_path, entry.path });
try list.append(abs_path);
}
}
return list.toOwnedSlice();
}