Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for compiling Objective-C++ code #10096

Merged
merged 2 commits into from
Nov 22, 2021
Merged

add support for compiling Objective-C++ code #10096

merged 2 commits into from
Nov 22, 2021

Conversation

slimsag
Copy link
Sponsor Contributor

@slimsag slimsag commented Nov 4, 2021

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 implementation using only a build.zig file.

Helps hexops/mach#21

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

@kubkon
Copy link
Member

kubkon commented Nov 4, 2021

Wow, cool! Out of curiosity, does ObjC++ link fine with zld?

@slimsag
Copy link
Sponsor Contributor Author

slimsag commented Nov 5, 2021

@kubkon Yep! So far so good, Dawn has ~5.5k lines of ObjC++ across ~20 files and it links and runs that just fine without complaint. I did run into #10103 but it's not related to ObjC++

slimsag added a commit to hexops/mach that referenced this pull request Nov 5, 2021
* MacOS support functional
* C shims for dawn_native functionality, e.g. required utilities for binding to a GLFW window.
* Fully building with Zig, but depends on ziglang/zig#10096 for Obj-C++ support.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
slimsag added a commit to hexops/mach that referenced this pull request Nov 5, 2021
* MacOS support functional
* C shims for dawn_native functionality, e.g. required utilities for binding to a GLFW window.
* Fully building with Zig, but depends on ziglang/zig#10096 for Obj-C++ support.

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

@kubkon kubkon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look great. I just have one tiny request: would you be able to add some basic standalone tests that are enabled on macOS with SDK present only (we have a flag for that -Denable-macos-sdk)? The test would then link against some frameworks or what not, unless you think this is unnecessary.

@slimsag
Copy link
Sponsor Contributor Author

slimsag commented Nov 5, 2021

I'd be very happy to do that! I dug around a bit and it seems like I would basically need to add a directory like this one ("standalone test") with something like a hello-world ObjC++ file called from Zig, plus maybe linking a framework in? Does that sound about right?

@kubkon
Copy link
Member

kubkon commented Nov 5, 2021

I'd be very happy to do that! I dug around a bit and it seems like I would basically need to add a directory like this one ("standalone test") with something like a hello-world ObjC++ file called from Zig, plus maybe linking a framework in? Does that sound about right?

Yep, sounds right. And then in standalone.zig you can enable it only in the presence of macOS SDK (I don't think the test would make much sense on a different platform).

slimsag added a commit to hexops/mach that referenced this pull request Nov 16, 2021
* MacOS support functional
* C shims for dawn_native functionality, e.g. required utilities for binding to a GLFW window.
* Fully building with Zig, but depends on ziglang/zig#10096 for Obj-C++ support.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
@slimsag
Copy link
Sponsor Contributor Author

slimsag commented Nov 21, 2021

@kubkon PTAL - I rebased against master and added a standalone test (I based it on the existing objc standalone test, just adapted it for ObjC++)

@kubkon
Copy link
Member

kubkon commented Nov 21, 2021

@kubkon PTAL - I rebased against master and added a standalone test (I based it on the existing objc standalone test, just adapted it for ObjC++)

Thanks @slimsag! Just an FYI that in terms of the failing macOS CI, that's my doing and I'll fix ASAP :-)

slimsag added a commit to hexops/mach that referenced this pull request Nov 21, 2021
* MacOS support functional
* C shims for dawn_native functionality, e.g. required utilities for binding to a GLFW window.
* Fully building with Zig, but depends on ziglang/zig#10096 for Obj-C++ support.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
@kubkon
Copy link
Member

kubkon commented Nov 21, 2021

@slimsag if you rebase against master, macOS CI should be green again :-)

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>
Based on the existing objc example, just tweaked for ObjC++.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
@slimsag
Copy link
Sponsor Contributor Author

slimsag commented Nov 22, 2021

Done! 🤞

@kubkon kubkon merged commit 9836f1b into ziglang:master Nov 22, 2021
@slimsag slimsag deleted the sg/objective-cpp branch November 22, 2021 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants