Swift plugin for CLion

Hi,

Upd. Swift plugin for CLion 2016.3 version adds Swift 3 support. Read more.

Upd. Swift plugin for CLion 2016.2 version adds Swift 2.2 support, Introduce Variable refactoring and parameter placeholders for Swift.

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:

  1. Install the plugin from our repository.
  2. Download and setup the Swift toolchain (make sure you get the development snapshot, not the release one).
  3. 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):
    settings_norm
  4. 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.)
  5. 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:
    live_template
    SWIFT_SDK variable expands to the value specified in the settings.
  6. A sample CMake configuration file may look like this:
    sample_cmake
  7. 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:
    completion
  • Swift code formatter with a separate set of Swift code style settings:
    code_style
  • Navigation actions and File structure view.
  • Find usages.
  • Rename refactoring:
    refactoring
  • Override/Implement for functions and protocols.
  • And even a debugger, including proper rendering of variables and types:
    debug

    and expression evaluation:
    evaluate_code_fragment1

    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.

Sincerely yours,
The CLion Team

This entry was posted in Tips'n'Tricks and tagged , , , , . Bookmark the permalink.

74 Responses to Swift plugin for CLion

  1. Alex Polozov says:

    Why just CLion and not a general IDEA Platform plugin?

    • Anastasia Kazakova says:

      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).

  2. Edu Garcia says:

    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

    • Anastasia Kazakova says:

      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.

  3. Hisham says:

    So is the plan to only have Swift support for Linux in CLion? What about AppCode?

    • Anastasia Kazakova says:

      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.

    • Alexander says:

      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).

  4. Steve Miller says:

    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?

    • Anastasia Kazakova says:

      Yes, so for debugging C++ apps, better use either usual bundled LLDB (if you are on OS X) or bundled/selected GDB.

  5. Dev says:

    This is awesome. I was using gedit for writing Swift on Linux, so this should be a big upgrade :-)

  6. Даниил Водопьян says:

    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?

    • Anastasia Kazakova says:

      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.

      • Даниил Водопьян says:

        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.

  7. User says:

    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.

    • Anastasia Kazakova says:

      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

      • User says:

        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.

        • User says:

          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.

        • Anastasia Kazakova says:

          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?

          • User says:

            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.

          • Anastasia Kazakova says:

            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.

          • Anastasia Kazakova says:

            One more question: could you please show the output for ls command for /home/ubuntu/Applications/clion-144.2151.8/ path?

          • Anastasia Kazakova says:

            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!

          • Anastasia Kazakova says:

            Done. Please, check the note in number 4 in the instruction in this blog post.

    • Anastasia Kazakova says:

      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)

  8. Chris Hatton says:

    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?

  9. Tim says:

    Could you pls. explain how to set up Executable for swift project?
    https://i.imgur.com/CRqznMd.jpg

    • Anastasia Kazakova says:

      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.

    • Anastasia Kazakova says:

      let me know if it helps

      • Tim says:

        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.

        • Anastasia Kazakova says:

          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.

          • Tim says:

            Sorry Anastasia you’re right it was my fault I forgot to add custom target name.

          • Anastasia Kazakova says:

            Do ou have .build directory created after project building now?

          • Tim says:

            “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.

          • Anastasia Kazakova says:

            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)

          • Tim says:

            “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.

          • Anastasia Kazakova says:

            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.

          • Tim says:

            I sent project with logs.

          • Anastasia Kazakova says:

            Got it, thanks. I’ll have a look shortly and reply via email.

  10. Tim says:

    By the way after building there’s no .build directory.

  11. Swift says:

    Hello

    Can you please paste the code of the CMakeList instead of a screenshot

    thanks

    • Anastasia Kazakova says:

      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.

  12. wu says:

    error: no Package.swift file found

  13. wu says:

    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

    • wu says:

      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)

    • wu says:

      /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

  14. James Dang says:

    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.

  15. Mathijs says:

    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?

    • Anastasia Kazakova says:

      Yes, you are right, on Linux completion doesn’t work with imported libraries for now, only with a standard one (https://youtrack.jetbrains.com/issue/CPP-5325).

    • Darian says:

      I’m unable to build the project…
      This is what I get:
      /home/darian/Programi/clion-2016.2.3/bin/cmake/bin/cmake –build /home/darian/Desktop/Swift/Pokusaj/cmake-build-debug –target Pokusaj — -j 1
      error: rootManifestFileNotFound
      CMakeFiles/Pokusaj.dir/build.make:57: recipe for target ‘CMakeFiles/Pokusaj’ failed
      make[3]: *** [CMakeFiles/Pokusaj] Error 1
      CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/Pokusaj.dir/all’ failed
      make[2]: *** [CMakeFiles/Pokusaj.dir/all] Error 2
      CMakeFiles/Makefile2:74: recipe for target ‘CMakeFiles/Pokusaj.dir/rule’ failed
      make[1]: *** [CMakeFiles/Pokusaj.dir/rule] Error 2
      Makefile:118: recipe for target ‘Pokusaj’ failed
      make: *** [Pokusaj] Error 2

  16. Pingback: Programming in Swift on Linux | Tomek Wyszomirski - blog

  17. Lukas says:

    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.

  18. Lukas says:

    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

  19. mhmd salem says:

    Plugin ‘Swift’ is incompatible with this installation

  20. David Moles says:

    Is it possible to create a cross-platform project that can be built with either CLion or AppCode?

    • Anastasia Kazakova says:

      CMake is a cross-platform system, so if you have a cross-platform CMake project, you can build it with CLion.

  21. Tom Bruno says:

    I’m now getting a strange error with CLion 2016.1 and swift:

    /home/tbruno/.local/share/umake/ide/clion/bin/cmake/bin/cmake –build /home/tbruno/.CLion2016.1/system/cmake/generated/untitled-6a568cf7/6a568cf7/Debug –target COMMAND — -j 8
    error: unable to invoke subcommand: /home/tbruno/.local/share/umake/swift/swift-lang/usr/bin/swift-build (No such file or directory)

    I don’t know why it thinks the subcommand is swift-build the CMakelists.txt is:
    add_custom_target(
    COMMAND /home/tbruno/.local/share/umake/swift/swift-lang/usr/bin/swift build
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    SOURCES Package.swift Sources/main.swift)

  22. Christopher Hatton says:

    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 :-)

  23. chnmrc says:

    For when is planned support for module map, i need the autocompletition feature on module dependencies.
    Thank’s for the great IDE under Linux.

  24. sydd says:

    Hello

    Does the Swift plugin work with Swift 3 on Linux? I see that the plugin is updated regularly, but I cant find changelogs.

Leave a Reply

Your email address will not be published. Required fields are marked *