aboutsummaryrefslogtreecommitdiff
path: root/src/Compilation.zig
diff options
context:
space:
mode:
authorStephen Gutekanst <stephen@hexops.com>2021-11-22 00:44:49 -0700
committerGitHub <noreply@github.com>2021-11-22 08:44:49 +0100
commit9836f1b2f975cc484499847225eeb0ad54116942 (patch)
treebb8bf2340fc4045f571d323d56e13c48d0524ef5 /src/Compilation.zig
parent722c6b95671fcda0da1561205e487d169c046473 (diff)
downloadzig-9836f1b2f975cc484499847225eeb0ad54116942.tar.gz
zig-9836f1b2f975cc484499847225eeb0ad54116942.zip
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>
Diffstat (limited to 'src/Compilation.zig')
-rw-r--r--src/Compilation.zig16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index 177746ae90..e23b6a12e4 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -3307,7 +3307,7 @@ pub fn addCCArgs(
try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });
switch (ext) {
- .c, .cpp, .m, .h => {
+ .c, .cpp, .m, .mm, .h => {
try argv.appendSlice(&[_][]const u8{
"-nostdinc",
"-fno-spell-checking",
@@ -3316,6 +3316,10 @@ pub fn addCCArgs(
try argv.append("-flto");
}
+ if (ext == .mm) {
+ try argv.append("-ObjC++");
+ }
+
// According to Rich Felker libc headers are supposed to go before C language headers.
// However as noted by @dimenus, appending libc headers before c_headers breaks intrinsics
// and other compiler specific items.
@@ -3599,6 +3603,7 @@ pub const FileExt = enum {
cpp,
h,
m,
+ mm,
ll,
bc,
assembly,
@@ -3610,7 +3615,7 @@ pub const FileExt = enum {
pub fn clangSupportsDepFile(ext: FileExt) bool {
return switch (ext) {
- .c, .cpp, .h, .m => true,
+ .c, .cpp, .h, .m, .mm => true,
.ll,
.bc,
@@ -3648,6 +3653,10 @@ pub fn hasObjCExt(filename: []const u8) bool {
return mem.endsWith(u8, filename, ".m");
}
+pub fn hasObjCppExt(filename: []const u8) bool {
+ return mem.endsWith(u8, filename, ".mm");
+}
+
pub fn hasAsmExt(filename: []const u8) bool {
return mem.endsWith(u8, filename, ".s") or mem.endsWith(u8, filename, ".S");
}
@@ -3686,6 +3695,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {
return .cpp;
} else if (hasObjCExt(filename)) {
return .m;
+ } else if (hasObjCppExt(filename)) {
+ return .mm;
} else if (mem.endsWith(u8, filename, ".ll")) {
return .ll;
} else if (mem.endsWith(u8, filename, ".bc")) {
@@ -3710,6 +3721,7 @@ pub fn classifyFileExt(filename: []const u8) FileExt {
test "classifyFileExt" {
try std.testing.expectEqual(FileExt.cpp, classifyFileExt("foo.cc"));
try std.testing.expectEqual(FileExt.m, classifyFileExt("foo.m"));
+ try std.testing.expectEqual(FileExt.mm, classifyFileExt("foo.mm"));
try std.testing.expectEqual(FileExt.unknown, classifyFileExt("foo.nim"));
try std.testing.expectEqual(FileExt.shared_library, classifyFileExt("foo.so"));
try std.testing.expectEqual(FileExt.shared_library, classifyFileExt("foo.so.1"));