Stephen Gutekanst 9836f1b2f9
add support for compiling Objective-C++ code (#10096)
* add support for compiling Objective-C++ code

Prior to this change, calling `step.addCSourceFiles` with Obj-C++ file extensions
(`.mm`) would result in an error due to Zig not being aware of that extension.
Clang supports an `-ObjC++` compilation mode flag, but it was only possible to use
if you violated standards and renamed your `.mm` Obj-C++ files to `.m` (Obj-C) to
workaround Zig being unaware of the extension.

This change makes Zig aware of `.mm` files so they can be compiled, enabling compilation
of projects such as [Google's Dawn WebGPU](https://dawn.googlesource.com/dawn/) using
a `build.zig` file only.

Helps hexops/mach#21

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>

* test/standalone: add ObjC++ compilation/linking test

Based on the existing objc example, just tweaked for ObjC++.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2021-11-22 08:44:49 +01:00

37 lines
1.1 KiB
Zig

const std = @import("std");
const Builder = std.build.Builder;
const CrossTarget = std.zig.CrossTarget;
fn isRunnableTarget(t: CrossTarget) bool {
// TODO I think we might be able to run this on Linux via Darling.
// Add a check for that here, and return true if Darling is available.
if (t.isNative() and t.getOsTag() == .macos)
return true
else
return false;
}
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.mm", &[0][]const u8{});
exe.addCSourceFile("test.mm", &[0][]const u8{});
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibCpp();
// TODO when we figure out how to ship framework stubs for cross-compilation,
// populate paths to the sysroot here.
exe.linkFramework("Foundation");
if (isRunnableTarget(target)) {
const run_cmd = exe.run();
test_step.dependOn(&run_cmd.step);
}
}