Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial Zig 12 support #36

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions Sdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ fn sdkRoot() *const [sdkRootIntern().len]u8 {

// linux-x86_64
pub fn toolchainHostTag() []const u8 {
comptime {
const os = builtin.os.tag;
const arch = builtin.cpu.arch;
return @tagName(os) ++ "-" ++ @tagName(arch);
}
const os = builtin.os.tag;
const arch = builtin.cpu.arch;
return (comptime if (std.mem.eql(u8, @tagName(os), "macos")) "darwin" else @tagName(os)) ++ "-" ++ @tagName(arch);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

android uses "darwin" tag instead of "macos" as zig does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you're the first mac use to try this out! Thank you :)

}

/// This file encodes a instance of an Android SDK interface.
Expand Down Expand Up @@ -92,12 +90,12 @@ pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions
.name = "zip_add",
.root_source_file = .{ .path = sdkRoot() ++ "/tools/zip_add.zig" },
});
zip_add.addCSourceFile(sdkRoot() ++ "/vendor/kuba-zip/zip.c", &[_][]const u8{
zip_add.addCSourceFile(.{ .file = .{ .path = sdkRoot() ++ "/vendor/kuba-zip/zip.c" }, .flags = &[_][]const u8{
"-std=c99",
"-fno-sanitize=undefined",
"-D_POSIX_C_SOURCE=200112L",
});
zip_add.addIncludePath(sdkRoot() ++ "/vendor/kuba-zip");
} });
zip_add.addIncludePath(.{ .path = sdkRoot() ++ "/vendor/kuba-zip" });
zip_add.linkLibC();

break :blk HostTools{
Expand Down Expand Up @@ -503,10 +501,11 @@ pub fn createApp(
}
resource_dir_step.add(Resource{
.path = "values/strings.xml",
.content = write_xml_step.getFileSource("strings.xml").?,
// .content = write_xml_step.getFileSource("strings.xml").?,
.content = write_xml_step.addCopyFile(.{ .path = "strings.xml" }, ""),
});

const sdk_version_int = @enumToInt(app_config.target_version);
const sdk_version_int = @intFromEnum(app_config.target_version);

if (sdk_version_int < 16) @panic("Minimum supported sdk version is 16.");

Expand Down Expand Up @@ -549,7 +548,8 @@ pub fn createApp(
const unaligned_apk_file = make_unsigned_apk.addOutputFileArg(unaligned_apk_name);

make_unsigned_apk.addArg("-M"); // specify full path to AndroidManifest.xml to include in zip
make_unsigned_apk.addFileSourceArg(manifest_step.getFileSource("AndroidManifest.xml").?);
// make_unsigned_apk.addFileSourceArg(manifest_step.getFileSource("AndroidManifest.xml").?);
make_unsigned_apk.addFileSourceArg(manifest_step.addCopyFile(.{ .path = "AndroidManifest.xml" }, ""));

make_unsigned_apk.addArg("-S"); // directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence
make_unsigned_apk.addDirectorySourceArg(resource_dir_step.getOutputDirectory());
Expand Down Expand Up @@ -848,7 +848,7 @@ pub fn compileAppLibrary(
ndk_root,
toolchainHostTag(),
config.lib_dir,
@enumToInt(app_config.target_version),
@intFromEnum(app_config.target_version),
});

const include_dir = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{
Expand Down Expand Up @@ -887,7 +887,7 @@ pub fn compileAppLibrary(

// exe.addIncludePath(include_dir);

exe.addLibraryPath(lib_dir);
exe.addLibraryPath(.{ .path = lib_dir });

// exe.addIncludePath(include_dir);
// exe.addIncludePath(system_include_dir);
Expand All @@ -904,7 +904,7 @@ pub fn compileAppLibrary(
}

fn createLibCFile(sdk: *const Sdk, version: AndroidVersion, folder_name: []const u8, include_dir: []const u8, sys_include_dir: []const u8, crt_dir: []const u8) !std.build.FileSource {
const fname = sdk.b.fmt("android-{d}-{s}.conf", .{ @enumToInt(version), folder_name });
const fname = sdk.b.fmt("android-{d}-{s}.conf", .{ @intFromEnum(version), folder_name });

var contents = std.ArrayList(u8).init(sdk.b.allocator);
errdefer contents.deinit();
Expand All @@ -926,7 +926,8 @@ fn createLibCFile(sdk: *const Sdk, version: AndroidVersion, folder_name: []const
try writer.writeAll("gcc_dir=\n");

const step = sdk.b.addWriteFile(fname, contents.items);
return step.getFileSource(fname) orelse unreachable;
// return step.getFileSource(fname) orelse unreachable;
return step.addCopyFile(.{ .path = fname }, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addCopyFile isn't the function you want here. We're already writing out the file using addWriteFile, we just need to get a FileSource from it. According to the message in the deprecated getFileSource() function, we need to use step.files.items[0].getPath() here.

}

pub fn compressApk(sdk: Sdk, input_apk_file: []const u8, output_apk_file: []const u8) *Step {
Expand Down Expand Up @@ -1151,22 +1152,22 @@ const BuildOptionStep = struct {
}
return;
},
std.builtin.Version => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found this type in the standard library, so I removed it for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zig 0.11.0 release notes address this: https://ziglang.org/download/0.11.0/release-notes.html#Language-Changes

  • Replaced builtin.Version with SemanticVersion.

Since SemanticVersion is already implemented below this code, we can go ahead and delete the std.builtin.Version case entirely.

out.print(
\\pub const {}: @import("std").builtin.Version = .{{
\\ .major = {d},
\\ .minor = {d},
\\ .patch = {d},
\\}};
\\
, .{
std.zig.fmtId(name),

value.major,
value.minor,
value.patch,
}) catch unreachable;
},
// std.builtin.Version => {
// out.print(
// \\pub const {}: @import("std").builtin.Version = .{{
// \\ .major = {d},
// \\ .minor = {d},
// \\ .patch = {d},
// \\}};
// \\
// , .{
// std.zig.fmtId(name),

// value.major,
// value.minor,
// value.patch,
// }) catch unreachable;
// },
std.SemanticVersion => {
out.print(
\\pub const {}: @import("std").SemanticVersion = .{{
Expand Down
34 changes: 18 additions & 16 deletions build/auto-detect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig
// Check for a user config file.
if (std.fs.cwd().openFile(config_path, .{})) |file| {
defer file.close();
const bytes = file.readToEndAlloc(b.allocator, 1 * 1000 * 1000) catch |err| {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't figured out the new Json api, it's hard because I haven't used the previous one.

print("Unexpected error reading {s}: {s}\n", .{ config_path, @errorName(err) });
return err;
};
var stream = std.json.TokenStream.init(bytes);
if (std.json.parse(UserConfig, &stream, .{ .allocator = b.allocator })) |conf| {
config = conf;
} else |err| {
print("Could not parse {s} ({s}).\n", .{ config_path, @errorName(err) });
return err;
}
// @panic("Config file not supported yet");
std.debug.print("Config file: TODO\n", .{});
// const bytes = file.readToEndAlloc(b.allocator, 1 * 1000 * 1000) catch |err| {
// print("Unexpected error reading {s}: {s}\n", .{ config_path, @errorName(err) });
// return err;
// };
// var stream = std.json.TokenStream.init(bytes);
// if (std.json.parse(UserConfig, &stream, .{ .allocator = b.allocator })) |conf| {
// config = conf;
// } else |err| {
// print("Could not parse {s} ({s}).\n", .{ config_path, @errorName(err) });
// return err;
// }
Jomy10 marked this conversation as resolved.
Show resolved Hide resolved
} else |err| switch (err) {
error.FileNotFound => {
config_dirty = true;
Expand Down Expand Up @@ -99,10 +101,10 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig
const LSTATUS = u32;
const DWORD = u32;

// const HKEY_CLASSES_ROOT = @intToPtr(HKEY, 0x80000000);
const HKEY_CURRENT_USER = @intToPtr(HKEY, 0x80000001);
const HKEY_LOCAL_MACHINE = @intToPtr(HKEY, 0x80000002);
// const HKEY_USERS = @intToPtr(HKEY, 0x80000003);
// const HKEY_CLASSES_ROOT: HKEY= @ptrFromInt(0x80000000);
const HKEY_CURRENT_USER: HKEY = @ptrFromInt(0x80000001);
const HKEY_LOCAL_MACHINE: HKEY = @ptrFromInt(0x80000002);
// const HKEY_USERS: HKEY= @ptrFromInt(0x80000003);

// const RRF_RT_ANY: DWORD = 0xFFFF;
// const RRF_RT_REG_BINARY: DWORD = 0x08;
Expand Down Expand Up @@ -142,7 +144,7 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig

// get the data
const buffer = allocator.alloc(u8, len) catch unreachable;
len = @intCast(DWORD, buffer.len);
len = @as(DWORD, @intCast(buffer.len));
res = RegGetValueA(key, null, value, RRF_RT_REG_SZ, null, buffer.ptr, &len);
if (res == ERROR_SUCCESS) {
for (buffer[0..len], 0..) |c, i| {
Expand Down
Empty file added strings.xml
Empty file.