Skip to content

Commit

Permalink
fix framework private path for macos (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 authored Dec 28, 2024
1 parent c7c38c6 commit b852325
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 79 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ on:
- '**.zig'
- '.github/workflows/CI.yml'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
timeout-minutes: 10
Expand All @@ -35,14 +39,15 @@ jobs:
if: matrix.os == 'windows-latest'
run: |
zig.exe fmt --check .
zig.exe build test -Dis_ci=true
zig.exe build -Dis_ci=true
zig.exe build test
zig.exe build
- name: fmt and test(unix)
if: matrix.os != 'windows-latest'
run: |
zig fmt --check .
zig build test -Dis_ci=true
zig build -Dis_ci=true
zig build test
zig build
find zig-out
cross-compile:
timeout-minutes: 10
Expand All @@ -59,4 +64,4 @@ jobs:
version: 0.13.0
- name: Build
run: |
zig build -Dtarget=${{ matrix.targets }} -Dis_ci
zig build -Dtarget=${{ matrix.targets }}
2 changes: 1 addition & 1 deletion .github/workflows/binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
run: |
zig build -Dtarget=${{ matrix.targets }} -Doptimize=ReleaseSafe \
-Dgit_commit=${{ github.head_ref }}-${{ github.sha }} \
-Dbuild_date=${{ env.BUILD_DATE }} -Dis_ci=true
-Dbuild_date=${{ env.BUILD_DATE }}
tar -cvf zigcli.tar zig-out/bin/
- name: Upload
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/simargs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Memory leak detect
run: |
sudo apt update && sudo apt install -y valgrind
zig build -Dcpu=baseline --verbose -Dis_ci
zig build -Dcpu=baseline --verbose
TEST_BINARY=./zig-out/bin/simargs-demo
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${TEST_BINARY} --output a.out \
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zig 0.13.0
# zig master
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

build:
zig build -Doptimize=ReleaseFast \
-Dis_ci \
-Dbuild_date=$(shell date +"%Y-%m-%dT%H:%M:%S%z") \
-Dgit_commit=$(shell git rev-parse --short HEAD) \
--summary all
Expand Down
34 changes: 13 additions & 21 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const Build = std.Build;

const macos_private_framework = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/PrivateFrameworks/";

pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
Expand Down Expand Up @@ -85,7 +87,7 @@ fn buildExamples(
"simargs-demo",
"pretty-table-demo",
}) |name| {
try buildBinary(b, .{ .ex = name }, optimize, target, false, all_tests);
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests);
}
}

Expand All @@ -95,8 +97,6 @@ fn buildBinaries(
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
) !void {
const is_ci = b.option(bool, "is_ci", "Build in CI") orelse false;

inline for (.{
"tree",
"loc",
Expand All @@ -107,7 +107,7 @@ fn buildBinaries(
"repeat",
"tcp-proxy",
}) |name| {
try buildBinary(b, .{ .bin = name }, optimize, target, is_ci, all_tests);
try buildBinary(b, .{ .bin = name }, optimize, target, all_tests);
}

// TODO: move util out of `bin`
Expand All @@ -119,10 +119,9 @@ fn buildBinary(
comptime source: Source,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
is_ci: bool,
all_tests: *std.ArrayList(*Build.Step),
) !void {
if (makeCompileStep(b, source, optimize, target, is_ci)) |exe| {
if (makeCompileStep(b, source, optimize, target)) |exe| {
var deps = b.modules.iterator();
while (deps.next()) |dep| {
exe.root_module.addImport(dep.key_ptr.*, dep.value_ptr.*);
Expand Down Expand Up @@ -165,25 +164,18 @@ fn makeCompileStep(
comptime source: Source,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
is_ci: bool,
) ?*Build.Step.Compile {
const name = comptime source.name();
const path = comptime source.path();
const is_darwin = target.result.isDarwin();
if (std.mem.eql(u8, name, "night-shift") or std.mem.eql(u8, name, "dark-mode")) {
// if (target.getOsTag() != .macos) {
if (is_ci) {
// zig build -Dtarget=aarch64-macos will throw error
// error: warning(link): library not found for '-lobjc'
// warning(link): Library search paths:
// warning(link): framework not found for '-framework CoreBrightness'
// warning(link): Framework search paths:
// warning(link): /System/Library/PrivateFrameworks
// so disable this in CI environment.
// We canit use `target.result.isDarwin()` here
// Since when cross compile to darwin, there is no framework in the host!
const is_darwin = @import("builtin").os.tag == .macos;

if (!is_darwin) {
if (std.mem.eql(u8, name, "night-shift") or std.mem.eql(u8, name, "dark-mode")) {
return null;
}
}

const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path(path ++ "/" ++ name ++ ".zig"),
Expand All @@ -193,10 +185,10 @@ fn makeCompileStep(

if (std.mem.eql(u8, name, "night-shift")) {
exe.linkSystemLibrary("objc");
exe.addFrameworkPath(.{ .cwd_relative = "/System/Library/PrivateFrameworks" });
exe.addFrameworkPath(.{ .cwd_relative = macos_private_framework });
exe.linkFramework("CoreBrightness");
} else if (std.mem.eql(u8, name, "dark-mode")) {
exe.addFrameworkPath(.{ .cwd_relative = "/System/Library/PrivateFrameworks" });
exe.addFrameworkPath(.{ .cwd_relative = macos_private_framework });
exe.linkFramework("SkyLight");
} else if (std.mem.eql(u8, name, "tcp-proxy")) {
exe.linkLibC();
Expand Down
55 changes: 20 additions & 35 deletions src/bin/dark-mode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ const util = @import("util.zig");
extern "c" fn SLSSetAppearanceThemeLegacy(bool) void;
extern "c" fn SLSGetAppearanceThemeLegacy() bool;

const Command = enum {
Status,
On,
Off,
Toggle,

const FromString = std.ComptimeStringMap(@This(), .{
.{ "status", .Status },
.{ "on", .On },
.{ "off", .Off },
.{ "toggle", .Toggle },
});
};

pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
Expand All @@ -31,48 +17,47 @@ pub fn main() !void {
version: bool = false,
help: bool = false,

__commands__: union(enum) {
on: struct {},
off: struct {},
toggle: struct {},
status: struct {},

pub const __messages__ = .{
.on = "Turn dark mode on",
.off = "Turn dark mode off",
.toggle = "Toggle dark mode",
.status = "View dark mode status (default)",
};
} = .{ .status = .{} },

pub const __shorts__ = .{
.version = .v,
.help = .h,
};

pub const __messages__ = .{
.help = "Print help information",
.version = "Print version",
};
},
\\<command>
\\
\\ Available commands:
\\ status View dark mode status
\\ on Turn dark mode on
\\ off Turn dark mode off
\\ toggle Toggle dark mode
, util.get_build_info());
}, null, util.get_build_info());
defer opt.deinit();

var args_iter = util.SliceIter([]const u8).init(opt.positional_args.items);
const cmd: Command = if (args_iter.next()) |v|
Command.FromString.get(v) orelse return error.UnknownCommand
else
.Status;

switch (cmd) {
.Status => {
switch (opt.args.__commands__) {
.status => {
const is_dark = SLSGetAppearanceThemeLegacy();
if (is_dark) {
std.debug.print("on", .{});
} else {
std.debug.print("off", .{});
}
},
.On => {
.on => {
SLSSetAppearanceThemeLegacy(true);
},
.Off => {
.off => {
SLSSetAppearanceThemeLegacy(false);
},
.Toggle => {
.toggle => {
const is_dark = SLSGetAppearanceThemeLegacy();
SLSSetAppearanceThemeLegacy(!is_dark);
},
Expand Down
6 changes: 3 additions & 3 deletions src/bin/night-shift.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const Command = enum {
Temp,
Schedule,

const FromString = std.ComptimeStringMap(@This(), .{
const FromString = std.StaticStringMap(Command).initComptime(.{
.{ "status", .Status },
.{ "on", .On },
.{ "off", .Off },
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn main() !void {
, util.get_build_info());
defer opt.deinit();

var args_iter = util.SliceIter([]const u8).init(opt.positional_args.items);
var args_iter = util.SliceIter([]const u8).init(opt.positional_args);
const cmd: Command = if (args_iter.next()) |v|
Command.FromString.get(v) orelse return error.UnknownCommand
else
Expand Down Expand Up @@ -295,7 +295,7 @@ pub fn main() !void {
} else {
const from = sub_cmd;
const to = args_iter.next() orelse return error.MissingTo;
const schedule = .{ .Custom = .{
const schedule = Schedule{ .Custom = .{
.from_time = try Time.fromString(from),
.to_time = try Time.fromString(to),
} };
Expand Down
23 changes: 11 additions & 12 deletions src/mod/simargs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,13 @@ fn OptionParser(
) !Args {
var args: Args = undefined;
var parser = CommandParser(Args){};
var sub_cmd_set = false;
inline for (std.meta.fields(Args)) |fld| {
if (comptime std.mem.eql(u8, fld.name, COMMAND_FIELD_NAME)) {
if (fld.default_value) |v| {
sub_cmd_set = true;
@field(args, fld.name) = @as(*align(1) const fld.type, @ptrCast(v)).*;
}
continue;
}

Expand All @@ -578,7 +583,6 @@ fn OptionParser(

var state = ParseState.start;
var current_opt: ?*OptionField = null;
var sub_cmd_set = false;
outer: while (arg_idx.* < input_args.len) {
const arg = input_args[arg_idx.*];
// Point to the next argument
Expand Down Expand Up @@ -842,11 +846,8 @@ test "parse/valid option values" {
.@"user-agent" = "firefox",
}, opt.args);

var expected = [_][]const u8{ "hello", "world" };
try std.testing.expectEqualDeep(
opt.positional_args,
&expected,
);
const expected = args[args.len - 2 ..];
try std.testing.expectEqualDeep(opt.positional_args, expected);

var help_msg = std.ArrayList(u8).init(allocator);
defer help_msg.deinit();
Expand Down Expand Up @@ -896,12 +897,10 @@ test "parse/bool value" {
defer opt.deinit();

try std.testing.expect(opt.args.help);
var expected = [_][]const u8{
"true",
};
const expected = args[args.len - 1 ..];
try std.testing.expectEqualDeep(
opt.positional_args,
&expected,
expected,
);
}
}
Expand Down Expand Up @@ -1079,8 +1078,8 @@ test "parse/positional arguments" {
defer opt.deinit();

try std.testing.expectEqualDeep(opt.args.a, 1);
var expected = [_][]const u8{ "-a", "2" };
try std.testing.expectEqualDeep(opt.positional_args, &expected);
const expected = args[args.len - 2 ..];
try std.testing.expectEqualDeep(opt.positional_args, expected);

var help_msg = std.ArrayList(u8).init(allocator);
defer help_msg.deinit();
Expand Down

0 comments on commit b852325

Please sign in to comment.