-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Swift support #423
Comments
We don't currently have any plans around this. We'd happily help someone get it in, but we don't have a need for it internally yet. |
I did some investigation into the ins and outs of using the swift and clang command-line to compile binaries which use Swift and Objective-C together. Here's what I found: https://www.facebook.com/beng/posts/10153662256352720 Example: |
I hacked together very very basic Swift support for Xcode project generation: https://github.com/facebook/buck/tree/swift I updated the demo app to use it: https://github.com/fbsamples/bucksamples/tree/swift This doesn't support Swift frameworks yet, but the basics work. |
I'd love to see this too. Is anyone actively working on it? Can I be of help? |
I don't believe anybody is actively working on it, so if you wanted to try and pick up @bhamiltoncx's work and drive it forward, I'm pretty sure that would be okay. |
If you are interested in continuing that work, @Coneko has a good write up of how Buck uses graphs, which is a good primer for one of our core pieces of architecture: https://www.facebook.com/notes/uri-baghin/architecture-of-buck-1-graphs/1660525917559134 |
Sorry for the long silence here. Expect to see some movement soon! |
Summary: * Add Swift tool * Swift sdk and target params * Introduce the package com.facebook.buck.swift * Add Swift tool * swift_library() rule to compile swift sources into `.swiftmodule`/`.o`; example: ``` swift_library( name='greetings', srcs=['greetings.swift', 'input.swift'] ) ``` * Linking swift modules into Mach-O binaries * Add swiftName (optional) to platform, as it sometimes differ from the Xcode SDK names. * Copying swift standard libraries for app bundles Part of the long journey for #423 Test Plan: CI Reviewed By: bhamiltoncx fb-gh-sync-id: 2a5b7f6
Summary: * Swift looks for `swift_static` and will default to static linking (currently only MacOS has this defined) * Add integration test for OS X binaries. * Automatically add runtime library path when linking. Part of the long journey for #423 Test Plan: CI Reviewed By: bhamiltoncx fb-gh-sync-id: 7cf7683
Summary: This allows Buck-built swift apps to be installable on device. Part of the long journey for #423 Test Plan: Build the `iphoneos-arm64` flavor of a Swift app (for example: `test/com/facebook/buck/swift/testdata/simple_swift_application_bundle`with Buck, make sure it installs and runs on device. Reviewed By: bhamiltoncx fb-gh-sync-id: 9d0ac39
So the very basics have landed. You can build
|
How can I specify a particular swift version? I've tried to use |
You're right, right now we can only override the toolchain globally. @nguyentruongtho is working on adding swift-specific toolchain override as a config option. |
In the mean time, you could use this settings:
|
@nguyentruongtho thank you! It worked! |
I continued playing with Buck and Swift and faced some strange behaviour while running Swift tests only. Objective-C tests are working fine! And even a mix of Objective-C and Swift is working fine. I've created a small example that illustrates the issues here: https://github.com/fkorotkov/buck-sandbox. Even added Travis so you can easily see output! 😜 https://travis-ci.org/fkorotkov/buck-sandbox/builds/162268899 |
@nguyentruongtho I would love to but |
@nguyentruongtho I built Buck locally with the fix and only swift tests are working now! |
Very nice, I didn't think of this when working on #895. Good to know 🚀! |
BTW I'm working on a fix for #909. |
I doesn't look like an issue to me, you should not include a header from parent folder like in |
Agreed on a bad example structure. Fixed the example :-) see #909 (comment) |
I have a library that has Swift and Objective-C code. Some Swift classes I want to export to Objective-C, I see that buck correctly generates
|
Currently, |
Created issue here: #943 |
If you are working on it, do you have a branch somewhere for me to have a look? I want my team to migrate to Buck, but we may have to implement few things, that seem to be missing right now, for example support for clang module maps, and better swift support. |
Wondering what the status is on this, sorry if I missed something |
I am now trying to understand if buck is ready for mixed obj-c and swift project |
@adc-amatosov my guess is that a better person to ask would be @milend as he is pushing a lot of Swift-related changes since August. |
Thanks @FredLoh |
I think the current state is the you can mix Swift and Objective-C in an apple_library rule. Can you verify that everything works for you? |
I will try to migrate one small mixed language project in next couple days and will keep you posted. If it works well I will try to migrate something bigger. |
Mixing Swift + Obj-C is supported but there are multiple caveats:
|
Thanks for very detailed update @milend
This looks like pretty severe to me. Are there any workaround for this? |
There are no workarounds. Breakpoints still work, though, you just lose the ability to inspect variables (either through Xcode's debugger view or via |
@milend Is that because Swift librairies are compiled as static libs? Any hope to have the debugger working for static libraries at some point or is that just never going to work until the dynamic framework workflow is implemented? |
Yes and no. The usage of static libs exposes an underlying bug. The details are documented in SR-2660.
It's just a question of getting SR-2660 addressed, so that it starts working. Given that Xcode 9 added official support for Swift static libs, I believe this would be addressed in the future. But that's just my guess, I'm not aware of anyone addressing the bug at present. |
One aspect I forgot to mention is that you can still debug a single Swift module at a time (not ideal but at least it's something). This can be done by adding something like the following to the linker flags for the final binary (macOS example):
|
@KieranLafferty I encountered the same issue when the |
Ok! So the app now launches and runs the main.m but I'm unable to make reference to my AppDelegate file that's contained in the apple_library. I've attempted the following with no success
This gets very close and code completion works for this file but I get a compilation error because the #import <CompanyLibrary/CompanyLibrary-swift.h> contains a reference to I also tried simply going into <CompanyLibrary/CompanyLibrary-swift.h> and finding the class definition for my AppDelegate class which I found to have the following definition:
I thought that simply copying the string Definitely getting closer but would be great to hear how you got it to load your AppDelegate that was contained in dependency Note: I've also removed
|
Hi @KieranLafferty, the real name of your swift class from Obj-C is |
Thanks for the response @adc-amatosov, I tried what you suggested (with and without @objc before the class) and didn't have any luck. Without @objc in AppDelegate
With @objc in AppDelegate
In both situations I get the following error
|
That is weird. Are you sure your swift library links successfully to the app binary? |
@KieranLafferty The names of Obj-C classes defined in Swift are mangled. You will usually something like:
Instead, you should do the following:
|
Tried that too but it can’t compile the Lib-swift.h (see my previous comment above) because the header tried to import another swift library of mine using @import which isn’t supported yet by buck |
There's a trick to workaround this. Instead of importing
Then whenever you want to import |
@milend Did you mean to say
? I.e. the idea is to import private |
It has been almost a year since the last discussion on this. At that time it seemed possible to build mixed-language projects but the process wasn't very straightforward. What is the current state of Swift support and mixed Swift-ObjC projects in Buck? Would using Buck be straightforward if we split mixed projects into language-specific projects? |
Currently there is no clean documentation about Swift support in Buck. It supports it up to some degree and I personally don't know what is that degree (e.g. does it support Swift 4/4.1/4.2?), but you can check out the unit tests as they have some clues how would you combine ObjC and Swift in a single module. The rest of the information is available in this thread. I wish it could be structured somehow on buckbuild.com. This is what we have now. |
Since all the xcode problems are not limited to Objective-C 😆 My question is if there are any plans to support Swift.
The text was updated successfully, but these errors were encountered: