Merge pull request #9501 from ziglang/macho-objc-cleanup

Add standalone Objective-C enabled on macOS only
This commit is contained in:
Jakub Konka 2021-08-02 08:01:15 +02:00 committed by GitHub
commit eba153f88f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 0 deletions

View File

@ -1471,6 +1471,7 @@ fn sortSections(self: *MachO) !void {
&self.cstring_section_index,
&self.ustring_section_index,
&self.text_const_section_index,
&self.objc_methlist_section_index,
&self.objc_methname_section_index,
&self.objc_methtype_section_index,
&self.objc_classname_section_index,

View File

@ -37,6 +37,10 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
if (std.Target.current.os.tag == .linux) {
cases.addBuildFile("test/standalone/pie/build.zig", .{});
}
// Try to build and run an Objective-C executable.
if (std.Target.current.os.tag == .macos) {
cases.addBuildFile("test/standalone/objc/build.zig", .{ .build_modes = true });
}
// Ensure the development tools are buildable.
cases.add("tools/gen_spirv_spec.zig");

View File

@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>
@interface Foo : NSObject
- (NSString *)name;
@end

View File

@ -0,0 +1,11 @@
#import "Foo.h"
@implementation Foo
- (NSString *)name
{
NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
return str;
}
@end

View File

@ -0,0 +1,22 @@
const std = @import("std");
const Builder = std.build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Test the program");
const exe = b.addExecutable("test", null);
b.default_step.dependOn(&exe.step);
exe.addIncludeDir(".");
exe.addCSourceFile("Foo.m", &[0][]const u8{});
exe.addCSourceFile("test.m", &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibC();
exe.linkFramework("Foundation");
const run_cmd = exe.run();
test_step.dependOn(&run_cmd.step);
}

View File

@ -0,0 +1,12 @@
#import "Foo.h"
#import <assert.h>
int main(int argc, char *argv[])
{
@autoreleasepool {
Foo *foo = [[Foo alloc] init];
NSString *result = [foo name];
assert([result isEqualToString:@"Zig"]);
return 0;
}
}