authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-02 08:01:15+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-08-02 08:01:15+02:00
logeba153f88f0bdb36a654bd2cb794d2ead9d4e398
tree9c7bb9eb49062b505b83d3519dd7ea62d12b4bad
parentdae4c18aa7999a866b6b145ed3c88cb6c06852be
parent0ce54a141628b27ceab074dac4c19827a2188165
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Add standalone Objective-C enabled on macOS only

6 files changed, 57 insertions(+), 0 deletions(-)

src/link/MachO.zig+1
......@@ -1471,6 +1471,7 @@ fn sortSections(self: *MachO) !void {
14711471 &self.cstring_section_index,
14721472 &self.ustring_section_index,
14731473 &self.text_const_section_index,
1474 &self.objc_methlist_section_index,
14741475 &self.objc_methname_section_index,
14751476 &self.objc_methtype_section_index,
14761477 &self.objc_classname_section_index,
test/standalone.zig+4
......@@ -37,6 +37,10 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
3737 if (std.Target.current.os.tag == .linux) {
3838 cases.addBuildFile("test/standalone/pie/build.zig", .{});
3939 }
40 // Try to build and run an Objective-C executable.
41 if (std.Target.current.os.tag == .macos) {
42 cases.addBuildFile("test/standalone/objc/build.zig", .{ .build_modes = true });
43 }
4044
4145 // Ensure the development tools are buildable.
4246 cases.add("tools/gen_spirv_spec.zig");
test/standalone/objc/Foo.h created+7
......@@ -0,0 +1,7 @@
1#import <Foundation/Foundation.h>
2
3@interface Foo : NSObject
4
5- (NSString *)name;
6
7@end
test/standalone/objc/Foo.m created+11
......@@ -0,0 +1,11 @@
1#import "Foo.h"
2
3@implementation Foo
4
5- (NSString *)name
6{
7 NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
8 return str;
9}
10
11@end
test/standalone/objc/build.zig created+22
......@@ -0,0 +1,22 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6 const target = b.standardTargetOptions(.{});
7
8 const test_step = b.step("test", "Test the program");
9
10 const exe = b.addExecutable("test", null);
11 b.default_step.dependOn(&exe.step);
12 exe.addIncludeDir(".");
13 exe.addCSourceFile("Foo.m", &[0][]const u8{});
14 exe.addCSourceFile("test.m", &[0][]const u8{});
15 exe.setBuildMode(mode);
16 exe.setTarget(target);
17 exe.linkLibC();
18 exe.linkFramework("Foundation");
19
20 const run_cmd = exe.run();
21 test_step.dependOn(&run_cmd.step);
22}
test/standalone/objc/test.m created+12
......@@ -0,0 +1,12 @@
1#import "Foo.h"
2#import <assert.h>
3
4int main(int argc, char *argv[])
5{
6 @autoreleasepool {
7 Foo *foo = [[Foo alloc] init];
8 NSString *result = [foo name];
9 assert([result isEqualToString:@"Zig"]);
10 return 0;
11 }
12}