Skip to content

Commit

Permalink
initial Zig 12 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomy10 committed Sep 12, 2023
1 parent a790783 commit a876bf8
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 48 deletions.
Empty file added AndroidManifest.xml
Empty file.
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);
}

/// 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 }, "");
}

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 => {
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
Empty file added android-21-arm64.conf
Empty file.
Empty file added android-21-armeabi.conf
Empty file.
Empty file added android-21-x86.conf
Empty file.
6 changes: 6 additions & 0 deletions android-21-x86_64.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include
sys_include_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/x86_64-linux-android
crt_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/x86_64-linux-android/21/
msvc_lib_dir=
kernel32_lib_dir=
gcc_dir=
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const std = @import("std");
const Sdk = @import("Sdk.zig");

pub fn build(b: *std.build.Builder) !void {
// Default-initialize SDK
// // Default-initialize SDK
const sdk = Sdk.init(b, null, .{});
const mode = b.standardOptimizeOption(.{});
const android_version = b.option(Sdk.AndroidVersion, "android", "Select the android version, default is 'android5'") orelse .android5;
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| {
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;
// }
} 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.

0 comments on commit a876bf8

Please sign in to comment.