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!

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:

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.

Here’s a use case:
I have a project which is a Swift web server, but I’m doing my views in Haml, a Ruby-based language. So currently I have to open a CLion project with the server-side code, and then I have a separate RubyMine project with my clent-side code (Haml, CSS, JS).
If it were available in IDEA, then I’d be able to use one project for both. I don’t particularly mind it the way it is right now, having two separate projects, and I don’t think it should be a high priority, but I figured I’d let you know that there are situations where this is applicable.

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?

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.

To develop in full, you’ll need interface builder (AppCode has replacement as plugin, but it is not actively developed and may not support modern features of xibs and storyboards), iOS/tvOS/watchOS simulators (unless you have almost all such devices), application packager, application uploader. All them are available only on MacOS (if I don’t miss something).

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?

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.

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.

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.

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…

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.

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?

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!

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.

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.

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.

This is great news! With good IDE support now, we just need Swift on Linux to flesh out its core libraries and to support building shared libraries. Then it can really start to pervade the Linux ecosystem.

Great that you guys are working on a swift plugin. I am using the phpstorm IDE for quite a while now and loving it!

I can’t get one of the main features to work: autocomplete. It does work with the swift types (Int, etc), vars and objects. But not properties, methods or objects from imported libraries. Any suggestion on this?

How to configure CMakeLists.txt file that executable will see resources like *.html *.js files from main project folder? I’m trying to run Blackfish-example (https://github.com/elliottminns/blackfish-example) in Clion. Everything works ok, project starts, but executable not see resources files (Public and Resources folder). When I run executable builded manually from terminal then resources for executable are visible.

I found that
file(COPY Resources DESTINATION /Users/plum/Downloads/blackfish-example/.build/debug/)
file(COPY Public DESTINATION /Users/plum/Downloads/blackfish-example/.build/debug/)
works for me but better will be I could share resources without copy them to .build/debug

I have a renewed interest in developing something with Swift 3 on Linux now. Notes after returning to this, using Apples official Swift 3 release:
– Need to create symlink for expected liblldb using: ln -s ./liblldb.so.3.9.0 liblldb.so.3
– The need to select executable from .build feels like a big gap in the process.
– So does CMake setup for Swift, is a ‘New Swift project’ feature too much to ask?
– Autocompletion is not working at all in IDE.
I am very much looking to further development and a Swift 3 compatible version of the plugin. Thanks

As for the autocompletion, could you please share the example? What exactly is not completed? Please, note that on Linux completion doesn’t work with imported libraries for now, only with a standard one (https://youtrack.jetbrains.com/issue/CPP-5325).

Feel free also to add convenient workflow and other feature requests to our tracker.

Swift plugin supports only a specific build of Swift toolchain only on Linux, there is no restriction on macOS. The reason is that Linux builds of Swift toolchain do not contain SourceKit, so CLion plugin has to bundle one. SourceKit is tightly coupled with the rest of the toolchain, and usually works correctly only if everything was built from the same sources.

You can download 3.1 toolchain by replacing 3.1.1 with 3.1 in the download link.

Please install the required library (libblocksruntime0 package if you are on Ubuntu). You might need to install other packages listed in the requirements section for Swift project: https://github.com/apple/swift

Hello. I’m not sure that this is right place to ask but don’t know where should I do it. I’m absolutely new in Swift ad CMake and trying to configure Mac/Linux environment to build simple app with https://github.com/PerfectlySoft/Perfect-HTTPServer . I’ve created swift project with swift template in Clion, added dependency for the library. Everything compile and run on both platforms. Only thing that annoying me it’s the fact that CLion doesn’t see classes from the library, I can’t navigate to them or use code completion. Should I add them somehow as a library to CMakeLists.txt or something lese? Is it supported now or not?