[cmake-developers] Adding Swift support to CMake

Brad King brad.king at kitware.com
Fri Jun 19 10:51:24 EDT 2015


On 06/17/2015 05:52 AM, Eric Wing wrote:
> To meet these objectives, I see two things we need to do:
> - For the first and simplest case, we need to enhance the CMake Xcode
> generator so it can handle Swift. Xcode already knows how to handle
> Swift files automatically, so we should leverage it by making CMake
> generate a correct project for it.

Let's start with Xcode and make enabling the language an error for
other generators at first.

> GetSourcecodeValueFromFileExtension(), I added swift right after the
> Obj-C entries.
> 
>   else if (ext == "swift")
>     {
>     sourcecode += ".swift";
>     }

Good.

> set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m;swift)
> 
> This works pretty well for my initial test case which is a mixture of
> C/Obj-C/Swift. But I was told we should be looking at building a
> separate language generator instead of this latter hack.

Right.  There have been efforts here in the past to make separate
OBJC and OBJCXX languages too.  Using the C and CXX languages for
those has always been a partial solution.  For Swift we should do it
right the first time.

> 	CMakeDetermineSwiftCompiler.cmake

Let's start by getting that to work for the Xcode generator.  Currently
Modules/CompilerId/Xcode-3.pbxproj.in gets configured by the
CMAKE_DETERMINE_COMPILER_ID_BUILD function as a small test project
to compile the C or C++ source for compiler identification.  From
the output of that build we are able to extract the path to the
compiler tool.  One would have to do something similar here.  Can
you provide a minimal pbxproj file and swift source file that
work together to build an executable?

> - However, since Swift is designed to operate with C and Obj-C, there
> does seem to be a mechanism to create files that can be linked with
> C/Obj-C (needed by the Makefile generator; should be automatic by
> Xcode)

What is the proper sequence of commands to build some C and Swift
sources and link a single executable?  library?

Thanks,
-Brad



More information about the cmake-developers mailing list