|
| 1 | +#!/bin/bash |
| 2 | +##===----------------------------------------------------------------------===## |
| 3 | +## |
| 4 | +## This source file is part of the Swift.org open source project |
| 5 | +## |
| 6 | +## Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 7 | +## Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +## |
| 9 | +## See https://swift.org/LICENSE.txt for license information |
| 10 | +## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +## |
| 12 | +##===----------------------------------------------------------------------===## |
| 13 | +##===----------------------------------------------------------------------===## |
| 14 | +## |
| 15 | +## This source file is part of the SwiftNIO open source project |
| 16 | +## |
| 17 | +## Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors |
| 18 | +## Licensed under Apache License v2.0 |
| 19 | +## |
| 20 | +## See LICENSE.txt for license information |
| 21 | +## See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 22 | +## |
| 23 | +## SPDX-License-Identifier: Apache-2.0 |
| 24 | +## |
| 25 | +##===----------------------------------------------------------------------===## |
| 26 | + |
| 27 | +set -eu |
| 28 | + |
| 29 | +log() { printf -- "** %s\n" "$*" >&2; } |
| 30 | +error() { printf -- "** ERROR: %s\n" "$*" >&2; } |
| 31 | +fatal() { error "$@"; exit 1; } |
| 32 | + |
| 33 | +config="${CONFIG_JSON:=""}" |
| 34 | +fail_on_changes="${FAIL_ON_CHANGES:="false"}" |
| 35 | + |
| 36 | +if [ -z "$config" ]; then |
| 37 | + fatal "Configuration must be provided." |
| 38 | +fi |
| 39 | + |
| 40 | +here=$(pwd) |
| 41 | + |
| 42 | +case "$(uname -s)" in |
| 43 | + Darwin) |
| 44 | + find=gfind # brew install findutils |
| 45 | + ;; |
| 46 | + *) |
| 47 | + find='find' |
| 48 | + ;; |
| 49 | +esac |
| 50 | + |
| 51 | +function update_cmakelists_source() { |
| 52 | + src_root="$here/Sources/$1" |
| 53 | + |
| 54 | + src_exts=("*.c" "*.swift" "*.cc") |
| 55 | + num_exts=${#src_exts[@]} |
| 56 | + log "Finding source files (" "${src_exts[@]}" ") and platform independent assembly files under $src_root" |
| 57 | + |
| 58 | + # Build file extensions argument for `find` |
| 59 | + declare -a exts_arg |
| 60 | + exts_arg+=(-name "${src_exts[0]}") |
| 61 | + for (( i=1; i<num_exts; i++ )); |
| 62 | + do |
| 63 | + exts_arg+=(-o -name "${src_exts[$i]}") |
| 64 | + done |
| 65 | + |
| 66 | + # Build an array with the rest of the arguments |
| 67 | + shift |
| 68 | + exceptions=("$@") |
| 69 | + # Add path exceptions for `find` |
| 70 | + if (( ${#exceptions[@]} )); then |
| 71 | + log "Excluding source paths (" "${exceptions[@]}" ") under $src_root" |
| 72 | + num_exceptions=${#exceptions[@]} |
| 73 | + for (( i=0; i<num_exceptions; i++ )); |
| 74 | + do |
| 75 | + exts_arg+=(! -path "${exceptions[$i]}") |
| 76 | + done |
| 77 | + fi |
| 78 | + |
| 79 | + # Wrap quotes around each filename since it might contain spaces |
| 80 | + srcs=$($find -L "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort) |
| 81 | + asm_srcs=$($find -L "${src_root}" -type f \( \( -name "*.S" -a ! -name "*x86_64*" -a ! -name "*arm*" -a ! -name "*apple*" -a ! -name "*linux*" \) \) -printf ' "$<$<NOT:$<PLATFORM_ID:Windows>>:%P>"\n' | LC_ALL=POSIX sort) |
| 82 | + |
| 83 | + srcs="$srcs"$'\n'"$asm_srcs" |
| 84 | + log "$srcs" |
| 85 | + |
| 86 | + # Update list of source files in CMakeLists.txt |
| 87 | + # The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line; |
| 88 | + # the second is so that we can pass in a variable to replace with. |
| 89 | + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/add_library\(([^\n]+)\n([^\)]+)/add_library\($1\n$replace/' "$srcs" "$src_root/CMakeLists.txt" |
| 90 | + log "Updated $src_root/CMakeLists.txt" |
| 91 | +} |
| 92 | + |
| 93 | +function update_cmakelists_assembly() { |
| 94 | + src_root="$here/Sources/$1" |
| 95 | + log "Finding assembly files (.S) under $src_root" |
| 96 | + |
| 97 | + mac_x86_64_asms=$($find "${src_root}" -type f \( -name "*x86_64*" -or -name "*avx2*" \) -name "*apple*" -name "*.S" -printf ' %P\n' | LC_ALL=POSIX sort) |
| 98 | + linux_x86_64_asms=$($find "${src_root}" -type f \( -name "*x86_64*" -or -name "*avx2*" \) -name "*linux*" -name "*.S" -printf ' %P\n' | LC_ALL=POSIX sort) |
| 99 | + mac_aarch64_asms=$($find "${src_root}" -type f -name "*armv8*" -name "*apple*" -name "*.S" -printf ' %P\n' | LC_ALL=POSIX sort) |
| 100 | + linux_aarch64_asms=$($find "${src_root}" -type f -name "*armv8*" -name "*linux*" -name "*.S" -printf ' %P\n' | LC_ALL=POSIX sort) |
| 101 | + log "$mac_x86_64_asms" |
| 102 | + log "$linux_x86_64_asms" |
| 103 | + log "$mac_aarch64_asms" |
| 104 | + log "$linux_aarch64_asms" |
| 105 | + |
| 106 | + # Update list of assembly files in CMakeLists.txt |
| 107 | + # The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line; |
| 108 | + # the second is so that we can pass in a variable to replace with. |
| 109 | + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Darwin([^\)]+)x86_64"\)\n target_sources\(([^\n]+)\n([^\)]+)/Darwin$1x86_64"\)\n target_sources\($2\n$replace/' "$mac_x86_64_asms" "$src_root/CMakeLists.txt" |
| 110 | + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Linux([^\)]+)x86_64"\)\n target_sources\(([^\n]+)\n([^\)]+)/Linux$1x86_64"\)\n target_sources\($2\n$replace/' "$linux_x86_64_asms" "$src_root/CMakeLists.txt" |
| 111 | + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Darwin([^\)]+)aarch64"\)\n target_sources\(([^\n]+)\n([^\)]+)/Darwin$1aarch64"\)\n target_sources\($2\n$replace/' "$mac_aarch64_asms" "$src_root/CMakeLists.txt" |
| 112 | + perl -pi -e 'BEGIN { undef $/; $replace = shift } s/Linux([^\)]+)aarch64"\)\n target_sources\(([^\n]+)\n([^\)]+)/Linux$1aarch64"\)\n target_sources\($2\n$replace/' "$linux_aarch64_asms" "$src_root/CMakeLists.txt" |
| 113 | + log "Updated $src_root/CMakeLists.txt" |
| 114 | +} |
| 115 | + |
| 116 | +echo "$config" | jq -c '.targets[]' | while read -r target; do |
| 117 | + name="$(echo "$target" | jq -r .name)" |
| 118 | + type="$(echo "$target" | jq -r .type)" |
| 119 | + exceptions=("$(echo "$target" | jq -r .exceptions | jq -r @sh)") |
| 120 | + log "Updating cmake list for ${name}" |
| 121 | + |
| 122 | + case "$type" in |
| 123 | + source) |
| 124 | + update_cmakelists_source "$name" "${exceptions[@]}" |
| 125 | + ;; |
| 126 | + assembly) |
| 127 | + update_cmakelists_assembly "$name" |
| 128 | + ;; |
| 129 | + *) |
| 130 | + fatal "Unknown target type: $type" |
| 131 | + ;; |
| 132 | + esac |
| 133 | +done |
| 134 | + |
| 135 | +if [[ "${fail_on_changes}" == true ]]; then |
| 136 | + if [ -n "$(git status --untracked-files=no --porcelain)" ]; then |
| 137 | + fatal "Changes in the cmake files detected. Please update." |
| 138 | + else |
| 139 | + log "✅ CMake files are up-to-date." |
| 140 | + fi |
| 141 | +fi |
0 commit comments