|
#!/bin/zsh |
|
|
|
# Test if the Swift compiler knows about a particular language feature. |
|
# |
|
# Usage: |
|
# |
|
# swift-has-feature [--swift SWIFT_PATH] [--language-version LANGUAGE_VERSION] FEATURE |
|
# |
|
# The feature should be an upcoming or experimental language feature, |
|
# such as `"StrictConcurrency"` or `"ExistentialAny"`. |
|
# |
|
# Use swift-list-features.sh to list all known features of a given compiler version. |
|
|
|
read -r -d '' usage << EOF |
|
$(tput bold)USAGE:$(tput sgr0) swift-has-feature [--swift <path_to_swift>] [--language-version <language_version>] <feature> |
|
|
|
Test if the Swift compiler knows about a particular language feature. |
|
|
|
$(tput bold)OPTIONS$(tput sgr0) |
|
feature A named Swift language feature, such as "StrictConcurrency". |
|
Use swift-list-features.sh to list all known features. |
|
--swift <path_to_swift> Path to the Swift compiler you want to test. |
|
Default: "swift". |
|
--language-version <version> Set the language version you want to test. |
|
We pass this to the compiler with the -swift-version flag. |
|
Default: whatever the Swift compiler uses by default. |
|
|
|
$(tput bold)EXAMPLES$(tput sgr0) |
|
swift-has-feature ExistentialAny |
|
swift-has-feature --language-version 6 StrictConcurrency |
|
swift-has-feature --swift /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift --language-version 6 StrictConcurrency |
|
EOF |
|
|
|
zmodload zsh/zutil |
|
zparseopts -D -F -- -swift:=arg_swift_path -language-version:=arg_language_version || exit 1 |
|
|
|
if test -z "$1"; then |
|
echo "$usage" |
|
exit |
|
fi |
|
|
|
if test -z "${arg_swift_path[*]}"; then |
|
swift_path=("swift") |
|
else |
|
# $arg_swift_path is an array of the form ("--swift" "/path/to/swift -arguments") |
|
# Take the last argument. |
|
swift_path=${arg_swift_path[-1]} |
|
fi |
|
|
|
swift_args=() |
|
if test -n "${arg_language_version[*]}"; then |
|
# $arg_language_version is an array of the form ("--language_version" "6"). |
|
# Take the last argument. |
|
language_version=${arg_language_version[-1]} |
|
swift_args+=("-swift-version" "$language_version") |
|
fi |
|
|
|
# Print compiler and language version. |
|
echo -n "$(tput bold)Swift compiler version:$(tput sgr0) " |
|
"$swift_path" --version |
|
if test -z "${arg_swift_path[*]}"; then |
|
echo "(Pass --swift <path_to_swift> to test a different compiler executable.)" |
|
fi |
|
echo "" |
|
|
|
echo "$(tput bold)Language version:$(tput sgr0) ${language_version:-"(default)"}" |
|
if test -z "$language_version"; then |
|
echo "(Pass --language-version <version> to set the language version.)" |
|
fi |
|
echo "" |
|
|
|
# Swift code to test if a feature is known to the compiler. |
|
read -r -d '' swift_script << EOF |
|
import Foundation |
|
|
|
#if hasFeature($1) |
|
exit(EXIT_SUCCESS) |
|
#else |
|
exit(EXIT_FAILURE) |
|
#endif |
|
EOF |
|
|
|
echo "$(tput setaf 3)Running Swift compiler…$(tput sgr0)" |
|
# Compile and run the Swift code multiple times with different compiler flags. |
|
echo "$swift_script" | "$swift_path" "${swift_args[@]}" - |
|
is_builtin=$? # 0 == The feature is always on, no feature flags needed. |
|
echo "$swift_script" | "$swift_path" "${swift_args[@]}" -enable-upcoming-feature "$1" - |
|
is_upcoming=$? # 0 == It's an upcoming feature flag. |
|
echo "$swift_script" | "$swift_path" "${swift_args[@]}" -enable-experimental-feature "$1" - |
|
is_experimental=$? # 0 == It's an experimental feature flag. |
|
echo "$(tput setaf 3)Done.$(tput sgr0)" |
|
echo "" |
|
|
|
# Print the result. |
|
read -r -d '' output_upcoming << EOF |
|
$(tput bold)Result:$(tput sgr0) '$1' is an $(tput bold)upcoming$(tput sgr0) feature flag. |
|
Usage: |
|
• swiftc $(tput bold)-enable-upcoming-feature $1$(tput sgr0) |
|
• SwiftPM $(tput bold).enableUpcomingFeature($1)$(tput sgr0) |
|
EOF |
|
|
|
read -r -d '' output_experimental << EOF |
|
$(tput bold)Result:$(tput sgr0) '$1' is an $(tput bold)experimental$(tput sgr0) feature flag. |
|
Usage: |
|
• swiftc $(tput bold)-enable-experimental-feature $1$(tput sgr0) |
|
• SwiftPM $(tput bold).enableExperimentalFeature($1)$(tput sgr0) |
|
EOF |
|
|
|
if test $is_builtin -eq 0; then |
|
echo "$(tput bold)Result:$(tput sgr0) The feature '$1' is $(tput bold)enabled unconditionally$(tput sgr0) in this language version. You can use it without passing any feature flags." |
|
elif test $is_upcoming -eq 0; then |
|
echo "$output_upcoming" |
|
elif test $is_experimental -eq 0; then |
|
echo "$output_experimental" |
|
else |
|
echo "$(tput bold)Result:$(tput sgr0) Unknown feature flag '$1'. The Swift compiler doesn’t know this feature." |
|
exit 1 |
|
fi |