Using LLVM to preprocess and merge all files into one

Hi everyone.


In old C development there was a practice to use the C pre-processor to pre-process each file on a large project and merge into a single file that was, then, compiled by the C compiler. By doing that, the compiling time of a large system decreased considerably - I was capable to down the compiling time from 3 hours to 5 minutes using this technique.


So, is there a way to pre-process swift files in a similar way in order to make the build process less painful?

Accepted Reply

There's no preprocessor step in Swift like there is in C-family languages, so this isn't possible. There's no macro expansion involved in compiling Swift source, and compiling a single .swift source file doesn't require the re-compilation of all the declarations of included headers as C does (above and beyond preprocessing). In effect, the output of the Swift compiler is the precompiled data for other compilations.


If your builds are slow, that presumably reflects the fact that the Swift compiler isn't aggressivly optimized yet (at least in some areas), and the fact that it's pretty slow in compiling code where it has to infer types in generic/overloaded constructs. (For example, there have been recent threads here about long compilation times for an expression like "a + b + c + d + e", but much shorter times for "a - b - c - d - e". That's presumably because the "+" operator is much more heavily overloaded than the "-" operator.)


I guess if you have a build that seems unreasonably slow, you could submit a bug report with a sample project. If you want to discuss the issue with people who have more technical knowledge than the lurkers here, I'd suggest you go ask on the mailing lists over at swift.org. That's where the cool kids hang out. 😉

Replies

There's no preprocessor step in Swift like there is in C-family languages, so this isn't possible. There's no macro expansion involved in compiling Swift source, and compiling a single .swift source file doesn't require the re-compilation of all the declarations of included headers as C does (above and beyond preprocessing). In effect, the output of the Swift compiler is the precompiled data for other compilations.


If your builds are slow, that presumably reflects the fact that the Swift compiler isn't aggressivly optimized yet (at least in some areas), and the fact that it's pretty slow in compiling code where it has to infer types in generic/overloaded constructs. (For example, there have been recent threads here about long compilation times for an expression like "a + b + c + d + e", but much shorter times for "a - b - c - d - e". That's presumably because the "+" operator is much more heavily overloaded than the "-" operator.)


I guess if you have a build that seems unreasonably slow, you could submit a bug report with a sample project. If you want to discuss the issue with people who have more technical knowledge than the lurkers here, I'd suggest you go ask on the mailing lists over at swift.org. That's where the cool kids hang out. 😉

Thanks for your reply, Quincey. I already knew that there is no pre-processing step in swift compilation. I was wondering if there was a way to force LLVM to output an intermediate form in order to speed up compiling but even so it should not be the case. As you have stated, complexities on the language like type inference and operator overloads are the ones that slow down compiling. This is a discussion that has shown up in CocoaHeads group. There is a guy that is taking up to 50 minutes to compile an iOS app. One solution found is to avoid recompiling dependencies. People is downloading the code, compiling the frameworks and keeping the binaries to avoid recompiling the whole thing over and over again. Mainly with cocoapods, people have noted that Xcode is recompiling everything, even if nothing was touched. It seems to be a bug to be reported to apple. Anyways, thanks again for you time. .