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