1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2019-2020 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set -eu
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
build_opts=( -c release )
function die() {
echo >&2 "ERROR: $*"
exit 1
}
function make_git_commit_all() {
git init > /dev/null
git checkout -b main > /dev/null
if [[ "$(git config user.email)" == "" ]]; then
git config --local user.email does@really-not.matter
git config --local user.name 'Does Not Matter'
fi
git add . > /dev/null
git commit -m 'everything' > /dev/null
}
# <extra_dependencies_file> <swiftpm_pkg_name> <targets...>
function hooked_package_swift_start() {
local extra_dependencies_file=$1
local swiftpm_pkg_name=$2
shift 2
cat <<"EOF"
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "allocation-counter-tests",
products: [
EOF
for f in "$@"; do
local module
module=$(module_name_from_path "$f")
echo ".executable(name: \"$module\", targets: [\"bootstrap_$module\"]),"
done
cat <<EOF
],
dependencies: [
.package(url: "HookedFunctions/", .branch("main")),
.package(url: "$swiftpm_pkg_name/", .branch("main")),
EOF
if [[ -n "$extra_dependencies_file" ]]; then
cat "$extra_dependencies_file"
fi
cat <<EOF
],
targets: [
EOF
}
# <target_name> <deps...>
function hooked_package_swift_target() {
local target_name="$1"
shift
local deps=""
for dep in "$@"; do
deps="$deps \"$dep\","
done
cat <<EOF
.target(name: "Test_$target_name", dependencies: [$deps]),
.target(name: "bootstrap_$target_name",
dependencies: ["Test_$target_name", "HookedFunctions"]),
EOF
}
function hooked_package_swift_end() {
cat <<"EOF"
]
)
EOF
}
function abs_path() {
if [[ "${1:0:1}" == "/" ]]; then
echo "$1"
else
echo "$PWD/$1"
fi
}
function dir_basename() {
test -d "$1"
basename "$(cd "$1" && pwd)"
}
function fake_package_swift() {
cat > Package.swift <<EOF
// swift-tools-version:5.0
import PackageDescription
let package = Package(name: "$1")
EOF
}
# <target> <template> <swiftpm_pkg_root> <swiftpm_pkg_name> <hooked_function_module> <bootstrap_module>
# <extra_dependencies_file> <shared files...> -- <modules...> -- <test_files...>
function build_package() {
local target=$1
local template=$2
local swiftpm_pkg_root=$3
local swiftpm_pkg_name=$4
local hooked_function_module=$5
local bootstrap_module=$6
local extra_dependencies_file=$7
shift 7
local shared_files=()
while [[ "$1" != "--" ]]; do
shared_files+=( "$1" )
shift
done
shift
local modules=()
while [[ "$1" != "--" ]]; do
modules+=( "$1" )
shift
done
shift
test -d "$target" || die "target dir '$target' not a directory"
test -d "$template" || die "template dir '$template' not a directory"
test -d "$swiftpm_pkg_root" || die "root dir '$swiftpm_pkg_root' not a directory"
test -n "$hooked_function_module" || die "hooked function module empty"
test -n "$bootstrap_module" || die "bootstrap module empty"
cp -R "$template"/* "$target/"
mv "$target/$hooked_function_module" "$target/HookedFunctions"
mv "$target/Sources/$bootstrap_module" "$target/Sources/bootstrap"
(
set -eu
cd "$target"
cd HookedFunctions
make_git_commit_all
cd ..
cd AtomicCounter
make_git_commit_all
cd ..
mkdir "$swiftpm_pkg_name"
cd "$swiftpm_pkg_name"
fake_package_swift "$swiftpm_pkg_name"
make_git_commit_all
cd ..
hooked_package_swift_start "$extra_dependencies_file" "$swiftpm_pkg_name" "$@" > Package.swift
for f in "$@"; do
local module
module=$(module_name_from_path "$f")
hooked_package_swift_target "$module" "${modules[@]}" >> Package.swift
mkdir "Sources/bootstrap_$module"
ln -s "../bootstrap/main.c" "Sources/bootstrap_$module"
mkdir "Sources/Test_$module"
cat > "Sources/Test_$module/trampoline.swift" <<EOF
@_cdecl("swift_main")
func swift_main() {
run(identifier: "$module")
}
EOF
ln -s "$f" "Sources/Test_$module/file.swift"
ln -s "../../scaffolding.swift" "Sources/Test_$module/"
for shared_file in "${shared_files[@]+"${shared_files[@]}"}"; do
name=$(basename "$shared_file")
ln -s "$shared_file" "Sources/Test_$module/$name"
done
done
hooked_package_swift_end >> Package.swift
swift package edit --path "$swiftpm_pkg_root" "$swiftpm_pkg_name"
)
}
function module_name_from_path() {
basename "${1%.*}" | tr ". " "__"
}
# <pkg_root>
function find_swiftpm_package_name() {
(
set -eu
cd "$1"
swift package dump-package | grep '^ "name"' | cut -d'"' -f4
)
}
do_hooking=true
pkg_root="$here/.."
shared_files=()
modules=()
extra_dependencies_file=""
tmp_dir="/tmp"
while getopts "ns:p:m:d:t:" opt; do
case "$opt" in
n)
do_hooking=false
;;
s)
shared_files+=( $(abs_path "$OPTARG") )
;;
p)
pkg_root=$(abs_path "$OPTARG")
;;
m)
modules+=( "$OPTARG" )
;;
d)
extra_dependencies_file="$OPTARG"
;;
t)
tmp_dir="$OPTARG"
;;
\?)
die "unknown option $opt"
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $# -lt 1 ]]; then
die "not enough files provided"
fi
if [[ "${#modules}" == 0 ]]; then
die "no modules specified, use '-m <MODULE>' for every module you plan to use"
fi
if [[ ! -f "$pkg_root/Package.swift" ]]; then
die "package root '$pkg_root' doesn't contain a Package.swift file, use -p"
fi
files=()
for f in "$@"; do
files+=( "$(abs_path "$f")" )
done
test -d "$pkg_root" || die "package root '$pkg_root' not a directory"
for f in "${files[@]}"; do
test -f "$f" || die "file '$f' not a file"
done
working_dir=$(mktemp -d "$tmp_dir/.nio_alloc_counter_tests_XXXXXX")
selected_hooked_functions="HookedFunctionsDoHook"
selected_bootstrap="bootstrapDoHook"
if ! $do_hooking; then
selected_hooked_functions=HookedFunctionsDoNotHook
selected_bootstrap=bootstrapDoNotHook
fi
build_package \
"$working_dir" \
"$here/template" \
"$pkg_root" \
"$(find_swiftpm_package_name "$pkg_root")" \
"$selected_hooked_functions" \
"$selected_bootstrap" \
"$extra_dependencies_file" \
"${shared_files[@]+"${shared_files[@]}"}" \
-- \
"${modules[@]}" \
-- \
"${files[@]}"
(
set -eu
cd "$working_dir"
swift build "${build_opts[@]}"
for f in "${files[@]}"; do
echo "- $f"
swift run "${build_opts[@]}" "$(module_name_from_path "$f")"
done
)
|