Skip to content

Commit f3a3eef

Browse files
authored
Merge pull request #65824 from apple/maxd/5.9-toolchain-dispatch
[5.9] CMake: fix missing `SWIFT_CONCURRENCY_GLOBAL_EXECUTOR` Explanation: Resolves issues with static linking on Linux Risk: Medium, affects Linux builds and top-level CMake declarations. Original PR: #65795. Reviewed by: @al45tair @drexin @etcwilde Resolves: some of the issues reported in #65097 Tests: Added in swiftlang/swift-integration-tests#115 `SWIFT_CONCURRENCY_GLOBAL_EXECUTOR` is defined in `stdlib/cmake/modules/StdlibOptions.cmake`, which is not included during the first pass of evaluation of the root `CMakeLists.txt`. It is available on subsequent evaluations after the value is stored in CMake cache. This led to subtle bugs, where `usr/lib/swift_static/linux/static-stdlib-args.lnk` didn't contain certain flags on clean toolchain builds, but did contain them in incremental builds. Not having these autolinking flags in toolchain builds leads to errors when statically linking executables on Linux. Additionally, since are trivial tests previously didn't link Dispatch statically, the didn't expose a bug where `%import-static-libdispatch` substitution had a missing value. To fix that I had to update `lit.cfg` and clean up some of the related path computations to infer a correct substitution value. Resolves some of the errors reported in #65097.
2 parents 0763971 + be35d46 commit f3a3eef

File tree

6 files changed

+26
-23
lines changed

6 files changed

+26
-23
lines changed

CMakeLists.txt

+2-9
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,6 @@ if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
696696
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
697697
endif()
698698

699-
# Use dispatch as the system scheduler by default.
700-
# For convenience, we set this to false when concurrency is disabled.
701-
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
702-
if(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY AND "${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR}" STREQUAL "dispatch")
703-
set(SWIFT_CONCURRENCY_USES_DISPATCH TRUE)
704-
endif()
705-
706699
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
707700
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
708701
# Only build libdispatch for the host if the host tools are being built and
@@ -711,9 +704,9 @@ if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
711704
set(SWIFT_BUILD_HOST_DISPATCH TRUE)
712705
endif()
713706

714-
if(SWIFT_BUILD_HOST_DISPATCH OR SWIFT_CONCURRENCY_USES_DISPATCH)
707+
if(SWIFT_BUILD_HOST_DISPATCH)
715708
if(NOT EXISTS "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
716-
message(SEND_ERROR "SourceKit and concurrency require libdispatch on non-Darwin hosts. Please specify SWIFT_PATH_TO_LIBDISPATCH_SOURCE")
709+
message(SEND_ERROR "SourceKit requires libdispatch on non-Darwin hosts. Please specify SWIFT_PATH_TO_LIBDISPATCH_SOURCE")
717710
endif()
718711
endif()
719712
endif()

stdlib/cmake/modules/StdlibOptions.cmake

+13
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,16 @@ set(SWIFT_RUNTIME_FIXED_BACKTRACER_PATH "" CACHE STRING
239239
"If set, provides a fixed path to the swift-backtrace binary. This
240240
will disable dynamic determination of the path and will also disable
241241
the setting in SWIFT_BACKTRACE.")
242+
243+
# Use dispatch as the system scheduler by default.
244+
# For convenience, we set this to false when concurrency is disabled.
245+
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
246+
if(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY AND "${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR}" STREQUAL "dispatch")
247+
set(SWIFT_CONCURRENCY_USES_DISPATCH TRUE)
248+
endif()
249+
250+
if(SWIFT_CONCURRENCY_USES_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
251+
if(NOT EXISTS "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
252+
message(SEND_ERROR "Concurrency require libdispatch on non-Darwin hosts. Please specify SWIFT_PATH_TO_LIBDISPATCH_SOURCE")
253+
endif()
254+
endif()

test/Driver/static-stdlib-autolink-linux.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// RUN: echo 'public func asyncFunc() async { print("Hello") }' > %t/asyncModule.swift
88

99
// RUN: %target-swiftc_driver -emit-library -emit-module -module-name asyncModule -module-link-name asyncModule %t/asyncModule.swift -static -static-stdlib -o %t/libasyncModule.a
10-
// TODO: "-ldispatch -lBlocksRuntime" should be told by asyncModule.swiftmodule transitively
11-
// RUN: %target-swiftc_driver -parse-as-library -static -static-stdlib -module-name main %s %import-static-libdispatch -I%t -L%t -ldispatch -lBlocksRuntime -o %t/main
10+
// RUN: %target-swiftc_driver -parse-as-library -static -static-stdlib -module-name main %s %import-static-libdispatch -I%t -L%t -o %t/main
1211

1312
// RUN: %t/main | %FileCheck %s
1413
// CHECK: Hello

test/Driver/static-stdlib-linux.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// REQUIRES: static_stdlib
44
print("hello world!")
55
// RUN: %empty-directory(%t)
6-
// RUN: %target-swiftc_driver -static-stdlib -o %t/static-stdlib %s
6+
// RUN: %target-swiftc_driver %import-static-libdispatch -static-stdlib -o %t/static-stdlib %s
77
// RUN: %t/static-stdlib | %FileCheck %s
88
// RUN: ldd %t/static-stdlib | %FileCheck %s --check-prefix=LDD
99
// CHECK: hello world!

test/lit.cfg

+8-10
Original file line numberDiff line numberDiff line change
@@ -1615,19 +1615,13 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-
16151615
config.import_libdispatch = ('-I %s -I %s -L %s'
16161616
% (libdispatch_source_dir, libdispatch_swift_module_dir, libdispatch_artifact_dir))
16171617

1618-
libdispatch_static_artifact_dir = config.libdispatch_static_build_path
1619-
libdispatch_swift_static_module_dir = make_path(libdispatch_static_artifact_dir, 'src', 'swift', 'swift')
1618+
libdispatch_static_artifact_dir = os.path.join(config.libdispatch_static_build_path, 'lib')
16201619
libdispatch_static_artifacts = [
1621-
make_path(libdispatch_static_artifact_dir, 'src', 'libdispatch.a'),
1622-
make_path(libdispatch_static_artifact_dir, 'src', 'swift', 'libswiftDispatch.a'),
1623-
make_path(libdispatch_swift_static_module_dir, 'Dispatch.swiftmodule')]
1620+
make_path(libdispatch_static_artifact_dir, 'libdispatch.a'),
1621+
make_path(libdispatch_static_artifact_dir, 'libBlocksRuntime.a')]
16241622
if (all(os.path.exists(p) for p in libdispatch_static_artifacts)):
16251623
config.available_features.add('libdispatch_static')
1626-
config.import_libdispatch_static = ('-I %s -I %s -L %s -L %s -L %s'
1627-
% (libdispatch_source_dir, libdispatch_swift_static_module_dir,
1628-
make_path(libdispatch_static_artifact_dir, 'src'),
1629-
make_path(libdispatch_static_artifact_dir, 'src', 'BlocksRuntime'),
1630-
make_path(libdispatch_static_artifact_dir, 'src', 'swift')))
1624+
config.import_libdispatch_static = '-L %s' % libdispatch_static_artifact_dir
16311625

16321626
config.target_build_swift = (
16331627
'%s -target %s -toolchain-stdlib-rpath %s %s %s %s %s'
@@ -2668,6 +2662,10 @@ run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitiz
26682662
config.substitutions.append(('%FileCheck', run_filecheck))
26692663
config.substitutions.append(('%raw-FileCheck', shell_quote(config.filecheck)))
26702664
config.substitutions.append(('%import-libdispatch', getattr(config, 'import_libdispatch', '')))
2665+
# WARNING: the order of components in a substitution name has to be different from the previous one, as lit does
2666+
# a pure string substitution without understanding that these components are grouped together. That is, the following
2667+
# subsitution name can't be `%import-libdispatch-static`, otherwise the first two components will be substituted with
2668+
# the value of `%import-libdispatch` substitution with `-static` string appended to it.
26712669
config.substitutions.append(('%import-static-libdispatch', getattr(config, 'import_libdispatch_static', '')))
26722670

26732671
# Disable COW sanity checks in the swift runtime by default.

utils/build-script-impl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ for host in "${ALL_HOSTS[@]}"; do
19011901
-DSWIFT_PATH_TO_CMARK_BUILD:PATH="$(build_directory ${host} cmark)"
19021902
-DSWIFT_PATH_TO_LIBDISPATCH_SOURCE:PATH="${LIBDISPATCH_SOURCE_DIR}"
19031903
-DSWIFT_PATH_TO_LIBDISPATCH_BUILD:PATH="$(build_directory ${host} libdispatch)"
1904-
-DSWIFT_PATH_TO_LIBDISPATCH_STATIC_BUILD:PATH="$(build_directory ${host} libdispatch_static)"
1904+
-DSWIFT_PATH_TO_LIBDISPATCH_STATIC_BUILD:PATH="$(build_directory ${host} swift)/$(basename $(build_directory ${host} libdispatch))-static-prefix"
19051905
)
19061906

19071907
if [[ "${SWIFT_SDKS}" ]] ; then

0 commit comments

Comments
 (0)