Utils/build-script: how to specify C and C++ compiler flags?

How do I specify the CFLAGS and CXXFLAGS? I tried using --extra-cmake-options="-DCMAKE_C_FLAGS= and --extra-cmake-options="-DCMAKE_CXX_FLAGS=, but they didn't seem to have any effect (I'm trying to add more paths to the include path).

It depends what you're trying to build, but since most of the Swift toolchain build is configured by CMake, it should automatically pick up those flags from the environment, ie you won't need to pass them in that way. You can usually check this by examining the CMakeCache.txt file in your build directory.

The one exception might be building the compiler and stdlib, as those still mostly use their own specialized CMake config that predates CMake itself supporting Swift. What is the precise error you're seeing?

build-script doesn't seem to update CMakeCache.txt whenever you modify --extra-cmake-options. You'll need to either modify the CMakeCache.txt manually or just rm it and rerun build-script.

I am trying to build the compiler with additional include paths that aren't in the environment variables.

It has the flag --reconfigure to force running CMake again. Of course, CMake is a little weird in what it allows you to reset that way, so sometimes you're right that the only way is those manual methods.

Your flags should work for that when run with --reconfigure on an existing build, but those will apply to LLVM and all other repos too. You probably want the --swift-cmake-options flag instead, which will only apply to building the Swift compiler frontend and stdlib. However, as I said, the compiler/stdlib often does not take just regular CMake flags and rolls its own CMake build config.

1 Like

--reconfigure works to get it to see the LDFLAGS I tried to add, but --swift-cmake-options forcibly splits the LDFLAGS (-DCMAKE_EXE_LINKER_FLAGS etc) on spaces. Also, it seems to override my attempts to add -DCMAKE_C_FLAGS and similar, as it provides its own -DCMAKE_C_FLAGS to CMake and mine don't show up.

Am I supposed to use -DSWIFT_COMPILER_SOURCES_SDK_FLAGS instead?

I'm currently trying this: utils/build-script --release-debuginfo --export-compile-commands --reconfigure --swift-cmake-options="-DSWIFT_COMPILER_SOURCES_SDK_FLAGS='$CFLAGS $LDFLAGS'"

(where I set CFLAGS and LDFLAGS earlier)

Yeah, that may not work, depending on exactly which part of the compiler or stdlib you need it in.

That flag is used only to provide outside flags to the host Swift compiler when building the portions of a fresh Swift compiler that are themselves written in Swift. As such, passing it in C/linker flags probably does nothing.

The fundamental issue is that the Swift compiler and stdlib have their own bespoke CMake config dating back more than a decade now, that obviously predates CMake itself supporting Swift. Here is a sense of how large it is, 20 klocs:

> tokei -t=CMake -f -s code
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Language                                    Files        Lines         Code     Comments       Blanks
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 CMake                                         318        27021        20146         3562         3313
───────────────────────────────────────────────────────────────────────────────────────────────────────
 ./stdlib/cmake/modules/AddSwiftStdlib.cmake               3470         2426          690          354
 ./CMakeLists.txt                                          1635         1105          245          285
 ./stdlib/cmake/modules/SwiftSource.cmake                  1299          982          147          170
 ./cmake/modules/AddSwift.cmake                            1058          695          230          133
 |hmark/cmake/modules/AddSwiftBenchmarkSuite.cmake          798          636           71           91
 ./test/CMakeLists.txt                                      648          524           36           88
 ./stdlib/public/Platform/CMakeLists.txt                    572          482           15           75
 |/SourceKit/cmake/modules/AddSwiftSourceKit.cmake          595          422           86           87
 ./cmake/modules/SwiftConfigureSDK.cmake                    578          414           96           68
 ./stdlib/public/core/CMakeLists.txt                        474          399           40           35
 ./Runtimes/Core/core/CMakeLists.txt                        346          306           14           26

The good news is that there is work underway to move the stdlib to use CMake's new built-in support for Swift more, by @etcwilde and others.

In the meantime, unless you give us more info on exactly what you're trying to accomplish, such as the precise build error I asked for last week and why you need those extra paths, it is difficult for us to guess at what's going wrong for you.