Hi,
Recently we’ve opened the Early Access Program for CLion 1.5. In this post we highlight another new feature: Swift support in CLion!
Background
This summer Apple promised to open source Swift language before the end of 2015, and just recently they did it! Now developers can contribute to Swift compiler and standard library, SourceKit, core libraries like Foundation, libdispatch and XCTest, Swift package manager, LLDB for Swift, and more.
This work involved not only OS X, with many Linux developers joining in as well. This has created a growing need for a Swift IDE on several platforms, especially on Linux. We at JetBrains do support iOS/OS X development, including Objective-C and Swift languages, with a standalone IDE called AppCode. It’s compatible with Xcode and its main focus is iOS/OS X development (meaning toolchains, environment, etc.). Unfortunately, if you’d like to try Swift on Linux, AppCode is not a proper choice.
CLion, on the other hand, is cross-platform. And with Swift compiler and libraries open-sourced, we’ve managed to take the platform-independent part of Swift support and brought it to CLion via a separate plugin. That means you can use CLion as a Swift IDE on Linux!
Initial setup
Getting started with Swift in CLion takes a few simple steps:
- Download and install the plugin from disk.
- Download and setup the Swift toolchain.
- In CLion settings
Build, Execution, Deployment | Swift
specify a Swift SDK (on OS X the default is/Library/Developer/Toolchains/swift-latest.xctoolchain, and on Linux it’s empty):
- If you are planning to use Swift debugger, enable Use Swift debugger.
(Note: Since the debugger comes from the Swift SDK and is customized, avoid using it for debugging non-Swift programs.) - CLion relies on CMake project model, so you need to start a CMake project for Swift. To simplify the configuration process, we’ve added a special CMake live template that adds a Swift build target:
SWIFT_SDK variable expands to the value specified in the settings. - A sample CMake configuration file may look like this:
- After you build the project, .build directory will be created in your project folder. Now all you need to run the project is to add an executable that is located in .build directory to the configuration corresponding to your target name (in our case it’s HelloSwift).
And now you are ready for Swift!
Features
The set of features includes the most essential ones for you to try:
- Parse/resolve classes in project and standard library (note that non-standard libraries are not resolved yet).
- Code highlighting and completion:
- Swift code formatter with a separate set of Swift code style settings:
- Navigation actions and File structure view.
- Find usages.
- Rename refactoring:
- Override/Implement for functions and protocols.
- And even a debugger, including proper rendering of variables and types:
and expression evaluation:
Please note that on OS X the Swift debugger itself seems to have problems and fails to render classes correctly.
To see the above features in action, watch this short demo:
width="640" height="360" src="https://www.youtube.com/embed/PQ1dL47dPr0?feature=oembed" frameborder="0" allowfullscreen="" style="border-width: 0px; margin: 0px; padding: 0px; vertical-align: baseline; background: transparent;">
Share your feedback in the comments section below. We’ll be glad to hear what you think about the Swift plugin for CLion! And we’ll rely on your feedback in our decision where to move forward with the plugin.
Sincerely yours,
The CLion Team
Why just CLion and not a general IDEA Platform plugin?
Actually it was much easier, since CLion and AppCode have a lot in common, and CMake support worked as a charmed (and it’s in CLion now).
This is fantastic news!
But I’ll ask a question that I’m sure I’m not alone wondering: Will this come to IDEA as well? I understand C/C++ not coming, because it’s the “core” of CLion (although you can say the same about HTML/JS and WebStorm, but I digress), but as Swift is not confined to a single IDE now, is there any reason why it won’t make it’s way to IDEA?
Thank you in advance for your response
Thanks!
Swift plugin for CLion was now more essential since AppCode and CLion have a lot of things in common, and CMake in CLion worked well for Swift projects on Linux. However, none of these (AppCode, CLion) are currently included into IntelliJ IDEA. We could consider some of these later, but currently more tasks in the queue with much higher priority.
So is the plan to only have Swift support for Linux in CLion? What about AppCode?
AppCode is an IDE for iOS/OS X dev, so it has Swift, but for development for Apple devices only. That’s by design. And we were not planning to change the thing. At least no such plans for now.
Great job!! I had been hoping for this, but assumed I’d have to wait for years… This is actually quite exciting.
A quick question on the comment, “Swift SDK comes with its own LLDB library; avoid using it when debugging non-Swift apps”. Does that mean if I check that box that Swift’s LLDB will be used when debugging a C++ app?
Yes, so for debugging C++ apps, better use either usual bundled LLDB (if you are on OS X) or bundled/selected GDB.
This is awesome. I was using gedit for writing Swift on Linux, so this should be a big upgrade
Great news!
A tech question, if i may:
Does the plugin use clang internally for code parsing/resolving? If it doesn’t, what is it then and now is it better?
Plugin uses the same parser AppCode does (that means our own), our IDE for iOS/OS X development – https://www.jetbrains.com/objc/. We have it for more than a year already and it works quite well. We are adding new Swift features there all the time.
But why can’t you use clang API? Can I read about the reasoning somewhere?
Sorry for asking, I believe you have a good reason for anything you do. The thing is that I got an argument with a friend, would be great if you somehow help me out.
Compiler parser actually is not a thing you can use easily inside the IDE. The main problem is that you need to quickly build a full project symbol index and store it, otherwise you won’t be able to make refactorings, for example, across the whole project.
If you go to this post (http://blog.jetbrains.com/blog/2015/08/06/jetbrains-way-to-cpp-the-inside-story-of-our-journey/) to the C++ parser section, you could find very similar reasoning why we are not able currently to use libclang for C++ parsing for example.
The debugger isn’t working for me:
Error running App: /home/ubuntu/Applications/clion-144.2151.8/CIDR/cidr-debugger/bin/LLDB/linux/LLDBFrontend not found
I’m not sure if this is what you mean by ‘non-standard libraries’, but Clion couldn’t resolve a module I’m using. It’d be nice to have built-in support for module maps.
The “swift build” command wouldn’t work for me, so I had to use “swift Sources/*.swift -I/all/the/dependencies”. Possibly because of that(?) Clion couldn’t find the executable, so I had to add the “-o” flag to specify the output and then manually configure Clion to use it.
It’d be nice if at setup time the user chooses to include Swift support, for Clion to go download Swift and configure itself to use it.
This is a great first step and I can’t wait to see where it goes.
Have you set SDK path correctly? Have you switched on the usage of Swift debugger in settings? Does this /home/ubuntu/Applications/clion-144.2151.8/CIDR/cidr-debugger/bin/LLDB/linux/LLDBFrontend really exist?
Do you know the reasons why “swift build” command is not working in our case? This could be the source of the problem…
Yes, only standard library is resolved for now.
Since you can build your own version of Swift compiler, not sure automatic download makes sense. But feel free to add all your requests/problems into our tracker: https://youtrack.jetbrains.com/issues/CPP
No, the directory doesn’t exist, starting with the “CIDR” directory. It wasn’t in the Clion file I downloaded, or the Swift plugin, so not sure where I’m supposed to get that, or if it’s just a bug that means it’s accidentally missing in the build.
My CMake file is something like this:
add_custom_target(HelloSwift
COMMAND /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift Sources/*.swift -I/all/the/dependencies
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
Yes, possibly I just need to figure out how to get “swift build” to work with my project. In any case, manually configuring the target executable worked fine.
OK, thanks. I can’t imagine a large percentage of users would be building their own compiler, but fair point.
And yes, I had selected to use the Swift debugger in the settings, and I believe I had set the Swift SDK correctly as I could compile and run the app.
And could you please show which path is set for Swift SDK in settings?
My swift SDK is at: /home/ubuntu/swift, and this is what is configured in the clion settings.
Is it just a sample or do you really have “/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift” in your CMake? Looks like you should have there: /home/ubuntu/swift/usr/bin/swift or similar. Is this the case?
Yes, just an example (I don’t have the project in front of me). That path is configured correctly for sure as the code compiles and runs.
Ok, thanks. We’ll try to find out if non-working build command could be the source of the problem or not, and will let you know.
One more question: could you please show the output for ls command for /home/ubuntu/Applications/clion-144.2151.8/ path?
Looks like we’ve found the source of the problem. Ubuntu’s installer is missing the LLDBFrontend file accidentally. Sorry for that. We’ll put it shortly to the confluence page and will update the post here with the instruction on how to set it up. Next EAP builds will include the files by themselves. Sorry once again. And thanks for helping us investigate this!
Thanks
Done. Please, check the note in number 4 in the instruction in this blog post.
By the way, have you added the custom target in your CMakeLists.txt. Something like:
add_custom_target(HelloSwift
COMMAND /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift build
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
SOURCES Package.swift main.swift MyClass.swift)
Great news! JetBrain’s support for Linux in Swift will be a huge boost to the language and ecosystem. This really strengthens the cross-platform roadmap for our product.
I’m having some trouble with the instructions above, however, which seem to be incomplete; I’ve added the live template, set-up the SDK and my test code builds – but does not run. If I point ‘Executable’ parameter of the Run Configuration to a previously built executable inside ‘.debug’, an LLDB error is thrown.
How should the run configuration be set up?
Could you please share the content of the CMake file and which LLDB error you get?
Could you pls. explain how to set up Executable for swift project?
https://i.imgur.com/CRqznMd.jpg
If you’d added a custom target, smth like:
add_custom_target(HelloSwift
COMMAND /usr/bin/swift build
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
SOURCES Package.swift main.swift MyClass.swift)
Then go to HelloSwift configuration for this target and after building select an executable from the .build directory.
let me know if it helps
I added custom target then build it but still can’t run project see screenshot:
http://i.imgur.com/bHKNASN.jpg
What I should select as Executable? It seems Clion can’t build swift project properly or I’m doing something wrong.
First, you are missing the custom target name in your CMake command. In my sample it’s HelloSwift.
Then, let’s check what directories do you have in your project after you successfully build the project. You should have .build in there.
Then, you need to Edit configuration HelloSwift (Build All is cmake special configuration for building all the project), and set an executable there. Press “Select other” in the drop down menu and if needed select “Show hidden files and directories” while searching for the .build dir in the list.
Sorry Anastasia you’re right it was my fault I forgot to add custom target name.
Do ou have .build directory created after project building now?
“Do ou have .build directory created after project building now?”
No I don’t have .build directory.https://i.imgur.com/YLJQgBr.jpg
I don’t know what I’m doing wrong. But at least in terminal using swift build command it works.
What I see on the screenshot is a Build all configuration, since it’s All Targets, not the swift_hello target.
Then, have you built this configuration from CLion? Run | Build (or Ctrl+F9)
“Then, have you built this configuration from CLion? Run | Build (or Ctrl+F9)”
Yes I used Clean then Build (Alt+Shift+D/Ctrl+F9) then tried Shift+F10 (Run Build All) and got Error: Executable is not specified.
Ok, could you please build the project, then compress the project directory (the whole one, including .idea and others) and send it to clion-support at jetbrains.com? We’ll what’s going on there.
I sent project with logs.
Got it, thanks. I’ll have a look shortly and reply via email.
By the way after building there’s no .build directory.
Hello
Can you please paste the code of the CMakeList instead of a screenshot
thanks
You can easily generate this CMake with add_swift live template. It will take proper swift SDK path from the settings automatically, you’ll just need to write target name.
error: no Package.swift file found
Do you actually have this file in our project? Could you please share the project you are testing on?
15:37:20 Build finished in 183ms
15:37:20 Error running s04: File not found: /home/wz/.CLion15/system/cmake/generated/s04-de9964d6/de9964d6/Debug/s04
Hello
That’s the project logs on Ubuntu.
CMake file like this :
add_custom_target(s04
COMMAND /home/wz/swift/swift-2.2-SNAPSHOT-2016-01-11-a-ubuntu15.10/usr/bin/swift build
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
SOURCES Package.swift main.swift)
/home/wz/.local/share/Trash/files/clion-144.2608.6/bin/cmake/bin/cmake –build /home/wz/.CLion15/system/cmake/generated/s04-de9964d6/de9964d6/Debug –target s04 — -j 2
error: Your source structure is not supported due to invalid sources layout: /home/wz/ClionProjects/s04. There should be no source files under: /home/wz/ClionProjects/s04.
That’s the bulid message:
CMakeFiles/s04.dir/build.make:57: recipe for target ‘CMakeFiles/s04’ failed
make[3]: *** [CMakeFiles/s04] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/s04.dir/all’ failed
make[2]: *** [CMakeFiles/s04.dir/all] Error 2
CMakeFiles/Makefile2:74: recipe for target ‘CMakeFiles/s04.dir/rule’ failed
make[1]: *** [CMakeFiles/s04.dir/rule] Error 2
Makefile:118: recipe for target ‘s04’ failed
make: *** [s04] Error 2