-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
137 lines (123 loc) · 5.44 KB
/
build.zig
File metadata and controls
137 lines (123 loc) · 5.44 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const target_os = target.result.os.tag;
const location_mod = b.createModule(.{
.root_source_file = b.path("src/location.zig"),
.target = target,
.optimize = optimize,
});
// When cross-compiling for macOS (e.g. -Dtarget=x86_64-macos on an aarch64
// host), Zig doesn't auto-discover the SDK paths. Pass -Dmacos-sdk=/path/to/sdk
// to provide them. (We can't use --sysroot because Zig prepends the sysroot
// to -L paths, doubling them — see github.com/ziglang/zig/issues/24368.)
const is_native = target.query.isNativeOs() and target.query.isNativeCpu();
if (!is_native and target_os == .macos) {
const macos_sdk = b.option([]const u8, "macos-sdk", "Path to macOS SDK for cross-compilation");
if (macos_sdk) |sdk| {
location_mod.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/usr/lib", .{sdk}) });
location_mod.addFrameworkPath(.{ .cwd_relative = b.fmt("{s}/System/Library/Frameworks", .{sdk}) });
}
}
switch (target_os) {
.macos => {
location_mod.linkSystemLibrary("objc", .{});
location_mod.linkFramework("CoreLocation", .{});
location_mod.linkFramework("Foundation", .{});
},
.windows => {
// WinRT API sets (combase.dll) for RoInitialize, RoActivateInstance, etc.
location_mod.linkSystemLibrary("api-ms-win-core-winrt-l1-1-0", .{});
location_mod.linkSystemLibrary("api-ms-win-core-winrt-string-l1-1-0", .{});
},
.linux => {
location_mod.linkSystemLibrary("dbus-1", .{});
location_mod.linkSystemLibrary("c", .{});
},
else => {},
}
const exe = b.addExecutable(.{
.name = "whereami",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "location", .module = location_mod },
},
}),
});
b.installArtifact(exe);
// On Linux, install the .desktop file so GeoClue2 allows location access
if (target_os == .linux) {
b.installFile("assets/whereami.desktop", "share/applications/whereami.desktop");
}
const run_step = b.step("run", "Run whereami");
if (target_os == .macos) {
// macOS .app bundle for location permissions
const bundle_step = b.step("bundle", "Create macOS .app bundle with ad-hoc signing");
// Create .app directory structure and Info.plist
const mkdir_and_plist = b.addSystemCommand(&.{
"sh", "-c",
\\mkdir -p zig-out/whereami.app/Contents/MacOS && \
\\cat > zig-out/whereami.app/Contents/Info.plist << 'PLIST'
\\<?xml version="1.0" encoding="UTF-8"?>
\\<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
\\<plist version="1.0">
\\<dict>
\\ <key>CFBundleIdentifier</key>
\\ <string>com.whereami.cli</string>
\\ <key>CFBundleExecutable</key>
\\ <string>whereami</string>
\\ <key>CFBundleName</key>
\\ <string>whereami</string>
\\ <key>NSLocationUsageDescription</key>
\\ <string>whereami needs your location to display coordinates.</string>
\\ <key>NSLocationWhenInUseUsageDescription</key>
\\ <string>whereami needs your location to display coordinates.</string>
\\</dict>
\\</plist>
\\PLIST
});
mkdir_and_plist.step.dependOn(b.getInstallStep());
// Copy binary into bundle
const copy_binary = b.addSystemCommand(&.{
"cp", "zig-out/bin/whereami", "zig-out/whereami.app/Contents/MacOS/whereami",
});
copy_binary.step.dependOn(&mkdir_and_plist.step);
// Ad-hoc sign (requires Xcode Command Line Tools; fails with clear error if missing)
const codesign = b.addSystemCommand(&.{
"codesign", "--force", "--sign", "-", "zig-out/whereami.app",
});
codesign.step.dependOn(©_binary.step);
// Replace zig-out/bin/whereami with a symlink into the bundle,
// so running the binary directly picks up the .app's Info.plist
// and gets location permissions from macOS.
const symlink = b.addSystemCommand(&.{
"sh", "-c",
\\rm -f zig-out/bin/whereami && \
\\ln -s ../whereami.app/Contents/MacOS/whereami zig-out/bin/whereami
});
symlink.step.dependOn(&codesign.step);
bundle_step.dependOn(&symlink.step);
// zig build run uses the bundle binary on macOS
const bundle_run = b.addSystemCommand(&.{
"zig-out/whereami.app/Contents/MacOS/whereami",
});
bundle_run.step.dependOn(&symlink.step);
if (b.args) |args| {
for (args) |arg| {
bundle_run.addArg(arg);
}
}
run_step.dependOn(&bundle_run.step);
} else {
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
}
}