Commit Graph

63 Commits

Author SHA1 Message Date
Brad King
67639002ad Ninja,Makefile: Drop now-unused placeholders from link rule variables
Previously we used separate placeholders for builtin linker flags:

* CMAKE_<LANG>_LINK_FLAGS for EXECUTABLEs
* CMAKE_SHARED_LIBRARY_CREATE_<LANG>_FLAGS for SHARED libraries
* CMAKE_SHARED_MODULE_CREATE_<LANG>_FLAGS for MODULE libraries

These are now always replaced by the empty string, so drop them
from our rule variables.
2025-06-10 14:43:15 -04:00
Brad King
58a70cfc38 Swift: Drop unused placeholder in shared library link rule
The `CMAKE_SHARED_LIBRARY_<LANG>_FLAGS` placeholder is meant for flags
used to compile sources meant for a shared library.  It is used for
C/CXX to add PIC flags.  It is not documented for public use, and we do
not populate it for Swift anyway.
2025-06-09 16:04:10 -04:00
Kitware Robot
1772622772 LICENSE: Replace references to Copyright.txt with LICENSE.rst
```
git grep -lz 'Copyright.txt or https://cmake.org/licensing ' |
  while IFS= read -r -d $'\0' f ; do
    sed -i '/Copyright.txt or https:\/\/cmake.org\/licensing / {
              s/Copyright.txt/LICENSE.rst/
            }' "$f" ; done
```
2025-03-03 10:43:35 -05:00
Marc Chevrier
0619c064ff Linker configuration: enhance usability
The linker configuration file is now optional: It is loaded only if
variable CMAKE_<LANG>_USE_LINKER_CONFIGURATION is set to TRUE.

The file CMakeAddNewLanguage.txt is updated to take into account the
linker configuration.

Fixes: #26393
2024-10-24 16:37:30 +02:00
Brad King
618c60367f Merge topic 'swift-install-names'
8669176576 Swift: Fix INSTALL_NAME_DIR under CMP0157 NEW behavior

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !9692
2024-08-02 09:47:51 -04:00
Evan Wilde
8669176576 Swift: Fix INSTALL_NAME_DIR under CMP0157 NEW behavior
Setting `CMAKE_INSTALL_NAME_DIR` or the `INSTALL_NAME_DIR` on Swift
targets had no effect when CMP0157 was set to `NEW`.  This was a result
of missing the `<TARGET_INSTALLNAME_DIR>` before the `<TARGET_SONAME>`.
Fix that and add a test to verify that the install name directory is
included in the install name.

Fixes: #26175
2024-08-01 10:49:36 -04:00
Marc Chevrier
c1c4cf9545 Linker configuration: introduce a new architecture
A new set of files are dedicated to linker configuration.
This set of files enable a fine-tuned configuration based of the linker
type as identified during compiler detection.

Fixes: #25360
2024-07-20 19:05:54 +02:00
Evan Wilde
56e5cea600 Swift: Support module libraries with command-line build systems
Wire up the flags needed to support module libraries built and used with
Swift. We need to pass `-bundle` to the linker when linking module
libraries on Darwin, and we need to pass `-export-dynamic` to the linker
when emitting an executable that exports symbols on Linux. This patch
wires up `CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS` and
`CMAKE_SHARED_MODULE_LOADER_Swift_FLAG` on Darwin, and hooks up
`CMAKE_EXE_EXPORTS_Swift_FLAG` on Linux in order to support passing
things correctly.

We can't expose `CMAKE_EXE_LINKER_FLAGS` to Swift, as it contains flags
that the Swift compiler doesn't recognize, but the other
language-specific variables are safe to expose.
2024-04-15 08:23:39 -07:00
Evan Wilde
ff7a09d719 Swift: Honor CMAKE_COLOR_DIAGNOSTICS for command-line build systems
Add the missing flags to enable and disable color diagnostics for Swift.

Fixes: #25815
2024-03-25 15:06:13 -04:00
Saleem Abdulrasool
a2aad7eb8d Swift: fix Windows DLL import library support for CMP0157 NEW
This was accidentally removed when the command templates were
reorganized to introduce the new policy. Restore the flag in the shared
library creation to ensure that we emit the import libraries to the
correct location.
2024-01-10 08:02:36 -08:00
Evan Wilde
a6a5c43300 Swift/Ninja: Add support for response files
Adding support for `CMAKE_NINJA_FORCE_RESPONSE_FILE` with Swift.

Issue: #25490
Fixes: #25563
2024-01-08 09:11:27 -08:00
Evan Wilde
9bed4f4d81 Swift/Ninja: Split compilation model
Splitting the Swift build into an object build and a separate link step,
instead of building and linking in one step. The immediate benefit is
LSP support because we are able to emit compile-commands for Swift files
now. Additionally, it is possible to specify flags to the compile step,
enabling folks to emit C and C++ headers from their Swift builds for
C/C++ interop, without needing custom commands. Eventually, this gives
us a path toward working object libraries.

Object Libraries:
 - Object libraries don't work today because CMake doesn't emit targets
   for object libraries into the Ninja build file.
 - tl;dr: Object libraries work if they aren't WMO. Still need work to
   make WMO'd object libraries work.

   Object libraries still don't completely work with this patch because,
   while we emit the targets, the `TARGET_OBJECTS` generator expression
   expansion has a separate mechanism for determining what the names of
   the objects are based on the input source files, so targets that
   depend on an object library built with a whole-module optimization
   will depend on objects based on the name of the source file instead
   of the actual emitted object file.

These features require being able to accurately model wholemodule builds
though, because we actually need to track object files and WMO affects
what objects are emitted. For that, we require CMP0157 use the NEW
policy. When it's OLD, we have to fall back on the old behavior and
cannot provide object libraries or the compile-commands for LSP.

Issue: #25308
2023-12-15 05:51:13 -08:00
Evan Wilde
c1d787e473 Swift: Add abstraction for compilation mode
Add a `CMAKE_Swift_COMPILATION_MODE` variable and corresponding
`Swift_COMPILATION_MODE` target property to control the compilation
mode.  Select among `wholemodule`, `singlefile`, and `incremental`.

Add policy CMP0157 to remove the default `-wmo` flags in favor of the
abstract setting.

Issue: #25366
2023-11-17 08:43:21 -05:00
Gregor Jasny
25977e177b apple: add preliminary visionOS support 2023-07-11 20:20:25 +02:00
Brad King
d79237a580 Merge topic 'swift-old-driver'
006e1995eb Swift: Restore compatibility with old C++ driver

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !8365
2023-03-29 09:13:33 -04:00
YR Chen
006e1995eb Swift: Restore compatibility with old C++ driver
The `-wmo` flag added by commit 6063428de7 (Swift: Update default build
flags, 2022-10-03, v3.26.0-rc1~585^2~1) behaves differently with the old
driver.  Detect when the old driver is being used, and avoid adding that
flag.

Fixes: #24641
2023-03-28 10:02:05 -04:00
Saleem Abdulrasool
9693191582 Swift: Emit debug information on Windows
When building on Windows, pass `-debug` to the linker to emit the PDB
(or `-debug:dwarf` when building with lld and using DWARF).  We would
previously not do this which meant that we never emitted the debug
information making debugging more difficult.

Fixes: #24423
2023-02-16 16:10:05 -05:00
Brad King
1d52007564 Merge topic 'ninja-swift-exported-executables'
4165eb3d0b Ninja: Emit swiftmodule from executable with exports

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !8048
2023-01-20 09:36:00 -05:00
Evan Wilde
4165eb3d0b Ninja: Emit swiftmodule from executable with exports
This patch adds support for tracking the swiftmodules for executables
exporting symbols.

This fixes a bug in the earlier implementation around emitting the
swiftmodule. Previously, the code would use
`CMAKE_EXE_EXPORTS_Swift_FLAG` to inject the `-emit-module`, and module
path information into the `CMAKE_Swift_LINK_EXECUTABLE` rule. Because
Swift skips the build step and only runs during the link phase, these
flags were injected in `cmNinjaNormalTargetGenerator::ComputeLinkCmd`
instead of `cmLocalGenerator::GetTargetFlags` where it is done normally.

Unfortunately, injecting in `ComputeLinkCmd` didn't do anything because
we have a `linkCmd` so `ComputeLinkCmd` exits early, before the
EXE_EXPORT flags get added to the link command.

Instead of playing with that flag, CMake checks
`CMAKE_Swift_LINK_EXECUTABLE_WITH_EXPORTS` and uses that as the link
rule if it exists and falls back on `CMAKE_Swift_LINK_EXECUTABLE`. I've
defined that variable in terms of `CMAKE_Swift_LINK_EXECUTABLE` with the
necessary additional flags for emitting the swift module instead. This
has the same end effect as the desired behavior, but simplifies things a
bit.

Since we're generating the swiftmodule for executables with exports,
I've also updated the dependency graph to include the swiftmodule as an
output in the build dependency graph if the executable has exports.

Tests updated:
 - RunCMake/NoWorkToDo:
   Ensure that the build graph does not result in unnecessary rebuilds
   with exporting executables.

 - SwiftOnly:
   Ensure that we can consume functions defined in the executable by a
   library getting linked into said executable.
2023-01-19 11:49:24 -08:00
Brad King
44abf24848 Swift: Add comment about where platform-specific flags should go 2023-01-17 09:21:15 -05:00
Craig Hutchinson
9a013c9e9d Swift: Use macOS-style linker flags on iOS, tvOS, and watchOS too
Without this, building a shared Swift library with `CMAKE_SYSTEM_NAME=iOS`
produces the error `ld: unknown option: -soname`.  iOS is an Apple platform,
and so should use the `-install_name` option.
2023-01-17 09:20:39 -05:00
Evan Wilde
e88509d0e8 Swift: Omit output-file-map when used as a linker
Swift is used as the linker for non-swift files because it needs to pull
files like swiftrt.o in when swift symbols are present to ensure that
the swift runtime is linked.

The swift driver uses clang as the underlying linker, which pulls in
crtbegin.o and friends when appropriate, so using Swift as a linker for
C/C++ libraries is fine.

The output-file-map was getting passed to all Swift invocations,
regardless of whether or not we generated one. This patch changes it so
that we only include the output-file-map in the Swift compiler
invocation if we have actually generated the file.
2022-10-28 16:44:26 -07:00
Evan Wilde
6063428de7 Swift: Update default build flags
Fully-optimized builds should be using whole-module optimizations(WMO)
to get all the optimizations the compiler can do for a given module.
As such, it makes sense for the release builds to pass
`-whole-module-optimization` or `-wmo` to the compiler by default.
`-whole-module-optimization` and `-wmo` are aliased and have the same
impact on the build.

Removing `-incrementa' from the `CMAKE_Swift_CREATE_*` variable:
WMO is incompatible with incremental builds, so it is removed to avoid
warnings from the Swift compiler.

Pass `-num-threads` to the driver in `CMAKE_Swift_CREATE_*`:
WMO doesn't use the `-j` flag, but instead uses `-num-threads` to get
parallelism. The two flags are applied in mutually exclusive contexts,
so `-j N` is a no-op in WMO, while `-num-threads` is a no-op in other
modes. Passing both at the same time will catch both cases without
negatively impacting the other case.
2022-10-11 11:55:45 -07:00
David Geldreich
20fe9cf7e9 Swift: Disable optimization when compiling in Debug
By default no `-O...` flag is specified for Debug configuration for
Swift so Xcode adds a (wrong) `-O` flag that prevents one from accessing
variables while debugging.  Add `-Onone` to the Swift Debug
configuration to avoid this.

Fixes: #23818
2022-08-04 09:45:31 -04:00
Marc Chevrier
14e57e9637 LINK_WHAT_YOU_USE feature: externalize configuration
Currently, this feature is only supported on ELF platforms. So, the property
LINK_WHAT_YOU_USE will be ignored for other plateforms.
Moreover, flags and commands are now controled by CMake variables.

Fixes: #20174
2021-07-09 14:50:50 +02:00
Saleem Abdulrasool
f481b3a947 Swift: repair RPATH handling for macOS
The configuration previously handled Linux properly but did not function
on macOS as `ld64` does not support `:` delimited paths.  Account for
that by setting it to the empty string which will use multiple
invocations of the `-Xlinker -rpath -Xlinker ...` pattern to compute the
correct RPATH.
2020-03-11 08:45:47 -04:00
Saleem Abdulrasool
321df5783d Swift: support -rpath for executables
This adjusts the flags to enable setting the RPATH for executables.
2020-02-17 11:07:40 -05:00
Saleem Abdulrasool
1e26d57188 Ninja: properly handle exports from Swift exes
This adds logic to properly handle Swift executables.  Only executables
marked as exporting symbols will now generate module interfaces for the
executable.
2020-02-01 17:25:57 -08:00
Saleem Abdulrasool
576c2e7a2d Swift: move windows handling into platform file (NFC)
This moves the definition of the import library flags for Windows into a
platform specific file (much like the Darwin `-sdk` flag).  This is in
preparation for re-use of the flag to make the flags handling for
executables more precise.
2019-12-21 18:25:54 -08:00
Brad King
9c9bbedd31 Merge topic 'swift-rpath-darwin'
ff6c336127 Swift: support `-rpath` on Darwin

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !4059
2019-11-18 13:43:11 -05:00
Brad King
eead6a2504 Merge topic 'swift-darwin-install-name'
f935de6746 Swift: support `INSTALL_NAME_DIR` on Darwin

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !4060
2019-11-18 13:42:09 -05:00
Saleem Abdulrasool
ff6c336127 Swift: support -rpath on Darwin
Darwin also has the concept of RPATH.  Additionally, the flag is
identical to that on other Unixish platforms.  Simply avoid the `-rpath`
handling on Windows.

This enables the use of `BUILD_WITH_INSTALL_RPATH` and `INSTALL_RPATH`
with Swift targets on Darwin.
2019-11-18 10:04:09 -05:00
Saleem Abdulrasool
f935de6746 Swift: support INSTALL_NAME_DIR on Darwin
Adjust the shared object rule to ensure that we honour the
`INSTALL_NAME_DIR` property on Swift targets.  This enables the use of
`INSTALL_NAME_DIR` and `BUILD_WITH_INSTALL_NAME_DIR` on Darwin with
Ninja.
2019-11-18 10:02:50 -05:00
Saleem Abdulrasool
a27a1c7e88 Swift: Allow build and installed RPATHs to differ
Define the RPATH separator to `:` so that CMake can reserve room to edit
the ELF RPATH at install time.  This enables the use of `BUILD_RPATH` and
`INSTALL_RPATH` with differing values.
2019-11-12 10:02:06 -05:00
Brad King
a53921b016 Merge branch 'swift-rpath' into release-3.16
Merge-request: !3965
2019-10-31 09:08:34 -04:00
Saleem Abdulrasool
1e05f89f4b Swift: support BUILD_RPATH properties
Enable passing a RPATH to Swift shared libraries.  This enables testing
libraries before they have been installed.
2019-10-30 14:18:15 -04:00
Saleem Abdulrasool
d9dd7cca66 Swift: correct flags for parallel jobs
`-num-threads` is for the threading, not the job control.  Use `-j` to
launch the parallel jobs.  This enables parallel builds for Swift again
after driver updates preventing the parallelization through
`-num-threads`.
2019-10-28 11:36:57 -04:00
Brad King
e3746821df Merge topic 'swift-mode-flags'
4d83e47c05 Swift: define `CMAKE_Swift_FLAGS` correctly

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Ben Boeckel <ben.boeckel@kitware.com>
Merge-request: !3684
2019-08-19 15:08:22 -04:00
Kyle Edwards
c41c79285b Merge topic 'swift-import-library-location'
5480f65ab1 Swift: honour `IMPLIB_LOCATION` property

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Ben Boeckel <ben.boeckel@kitware.com>
Merge-request: !3686
2019-08-19 13:25:16 -04:00
Saleem Abdulrasool
5480f65ab1 Swift: honour IMPLIB_LOCATION property
The rules did not account for the import library location (through
`<TARGET_IMPLB>`) and instead would always write the import library to
the default location (next to the shared library/executable).  This
prevented the use of `CMAKE_RUNTIME_OUTPUT_LOCATION` and
`CMAKE_ARCHIVE_OUTPUT_LOCATION`.
2019-08-11 22:29:49 -07:00
Saleem Abdulrasool
4d83e47c05 Swift: define CMAKE_Swift_FLAGS correctly
Invoke `cmake_initialize_per_config_variable` to ensure that build type
flags are properly initialised.
2019-08-11 17:59:52 -07:00
Saleem Abdulrasool
689be6235e Generator: support per-language link library flag
This enables the use of MSVC and Swift on Windows in a single project.
MSVC uses no flag to indicate linked libraries while Swift uses `-l`.
Add support for a language specific link library flag which takes
precedence over the global `CMAKE_LINK_LIBRARY_FLAG` which preserves
compatibility with earlier releases.
2019-08-09 08:16:29 -07:00
Saleem Abdulrasool
2171f6ec0e Swift: correct SONAME flag for Darwin targets
Adjust the build rules for Swift to fix the SONAME handling for Darwin.
2019-08-05 17:29:41 -07:00
Saleem Abdulrasool
151ef7cef4 Swift: refactor rule construction to reduce duplication
Use the placeholder expansion to avoid constructing the `-target` option
by hand if specified.  This reduces duplication and simplifies the
rules.
2019-07-17 10:26:46 -07:00
Saleem Abdulrasool
6af97178ef Swift: support SONAME on ELFish targets
We should enable the soname to be setup for ELF shared libraries.
Failure to do so generates binaries with incorrect names which prevents
loading.
2019-07-11 11:12:26 -07:00
Saleem Abdulrasool
08cd7f6a02 Swift: support multithreaded compilation
Query the number of logical CPUs available to enable parallel
compilation for Swift.
2019-07-02 10:45:07 -07:00
Saleem Abdulrasool
8aa0b63bc6 Swift: add rules for static linking
The Swift driver recently learnt how to generate static libraries using
the `-static` flag.  This enables us to generate proper static libraries
with dependency tracking with Swift as well.
2019-06-27 21:28:48 -07:00
Saleem Abdulrasool
a982916304 Support per-language library link flags
This changes the behaviour of the generators to use a per-language
library search path flag.  This is needed for multi-language projects
with different compilers (e.g. cl + gfortran).  Since the adjusted
variable has been part of the user settings, we control this based on a
policy.

Fixes: #19307
2019-06-06 15:28:43 -07:00
Brad King
a1c291e0ac Merge topic 'swift-wrapper'
6baa80d1e3 Modules: correct linker wrapper flag for Swift

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3390
2019-05-29 09:17:44 -04:00
Brad King
f093ac36c0 Merge topic 'linker-preference'
abea06efdf Modules: setup `CMAKE_Swift_LINKER_PREFERENCE`

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3373
2019-05-29 09:16:17 -04:00