Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 1 | #!/bin/bash -eu |
| 2 | |
| 3 | # This test exercises the bootstrapping process of the build system |
| 4 | # in a source tree that only contains enough files for Bazel and Soong to work. |
| 5 | |
| 6 | HARDWIRED_MOCK_TOP= |
Lukacs T. Berki | 720b396 | 2021-03-17 13:34:30 +0100 | [diff] [blame] | 7 | # Uncomment this to be able to view the source tree after a test is run |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 8 | # HARDWIRED_MOCK_TOP=/tmp/td |
| 9 | |
| 10 | REAL_TOP="$(readlink -f "$(dirname "$0")"/../..)" |
| 11 | |
| 12 | function fail { |
| 13 | echo ERROR: $1 |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | function copy_directory() { |
| 18 | local dir="$1" |
| 19 | local parent="$(dirname "$dir")" |
| 20 | |
| 21 | mkdir -p "$MOCK_TOP/$parent" |
| 22 | cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent" |
| 23 | } |
| 24 | |
| 25 | function symlink_file() { |
| 26 | local file="$1" |
| 27 | |
| 28 | mkdir -p "$MOCK_TOP/$(dirname "$file")" |
| 29 | ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file" |
| 30 | } |
| 31 | |
| 32 | function symlink_directory() { |
| 33 | local dir="$1" |
| 34 | |
| 35 | mkdir -p "$MOCK_TOP/$dir" |
| 36 | # We need to symlink the contents of the directory individually instead of |
| 37 | # using one symlink for the whole directory because finder.go doesn't follow |
| 38 | # symlinks when looking for Android.bp files |
| 39 | for i in $(ls "$REAL_TOP/$dir"); do |
| 40 | local target="$MOCK_TOP/$dir/$i" |
| 41 | local source="$REAL_TOP/$dir/$i" |
| 42 | |
| 43 | if [[ -e "$target" ]]; then |
| 44 | if [[ ! -d "$source" || ! -d "$target" ]]; then |
| 45 | fail "Trying to symlink $dir twice" |
| 46 | fi |
| 47 | else |
| 48 | ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i"; |
| 49 | fi |
| 50 | done |
| 51 | } |
| 52 | |
| 53 | function setup_bazel() { |
| 54 | copy_directory build/bazel |
| 55 | |
| 56 | symlink_directory prebuilts/bazel |
| 57 | symlink_directory prebuilts/jdk |
| 58 | |
| 59 | symlink_file WORKSPACE |
| 60 | symlink_file tools/bazel |
| 61 | } |
| 62 | |
| 63 | function setup() { |
| 64 | if [[ ! -z "$HARDWIRED_MOCK_TOP" ]]; then |
| 65 | MOCK_TOP="$HARDWIRED_MOCK_TOP" |
| 66 | rm -fr "$MOCK_TOP" |
| 67 | mkdir -p "$MOCK_TOP" |
| 68 | else |
| 69 | MOCK_TOP=$(mktemp -t -d st.XXXXX) |
| 70 | trap 'echo cd / && echo rm -fr "$MOCK_TOP"' EXIT |
| 71 | fi |
| 72 | |
| 73 | echo "Test case: ${FUNCNAME[1]}, mock top path: $MOCK_TOP" |
| 74 | cd "$MOCK_TOP" |
| 75 | |
| 76 | copy_directory build/blueprint |
| 77 | copy_directory build/soong |
| 78 | |
| 79 | symlink_directory prebuilts/go |
| 80 | symlink_directory prebuilts/build-tools |
| 81 | symlink_directory external/golang-protobuf |
| 82 | |
| 83 | touch "$MOCK_TOP/Android.bp" |
| 84 | |
| 85 | export ALLOW_MISSING_DEPENDENCIES=true |
| 86 | |
| 87 | mkdir -p out/soong |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | function run_soong() { |
| 91 | build/soong/soong_ui.bash --make-mode --skip-ninja --skip-make --skip-soong-tests |
| 92 | } |
| 93 | |
| 94 | function test_smoke { |
| 95 | setup |
| 96 | run_soong |
| 97 | } |
| 98 | |
| 99 | function test_bazel_smoke { |
| 100 | setup |
| 101 | setup_bazel |
| 102 | |
| 103 | tools/bazel info |
| 104 | |
| 105 | } |
| 106 | function test_null_build() { |
| 107 | setup |
| 108 | run_soong |
| 109 | local bootstrap_mtime1=$(stat -c "%y" out/soong/.bootstrap/build.ninja) |
| 110 | local output_mtime1=$(stat -c "%y" out/soong/build.ninja) |
| 111 | run_soong |
| 112 | local bootstrap_mtime2=$(stat -c "%y" out/soong/.bootstrap/build.ninja) |
| 113 | local output_mtime2=$(stat -c "%y" out/soong/build.ninja) |
| 114 | |
| 115 | if [[ "$bootstrap_mtime1" == "$bootstrap_mtime2" ]]; then |
| 116 | # Bootstrapping is always done. It doesn't take a measurable amount of time. |
| 117 | fail "Bootstrap Ninja file did not change on null build" |
| 118 | fi |
| 119 | |
| 120 | if [[ "$output_mtime1" != "$output_mtime2" ]]; then |
| 121 | fail "Output Ninja file changed on null build" |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | function test_soong_build_rebuilt_if_blueprint_changes() { |
| 126 | setup |
| 127 | run_soong |
| 128 | local mtime1=$(stat -c "%y" out/soong/.bootstrap/build.ninja) |
| 129 | |
| 130 | sed -i 's/pluginGenSrcCmd/pluginGenSrcCmd2/g' build/blueprint/bootstrap/bootstrap.go |
| 131 | |
| 132 | run_soong |
| 133 | local mtime2=$(stat -c "%y" out/soong/.bootstrap/build.ninja) |
| 134 | |
| 135 | if [[ "$mtime1" == "$mtime2" ]]; then |
| 136 | fail "Bootstrap Ninja file did not change" |
| 137 | fi |
| 138 | } |
| 139 | |
| 140 | function test_change_android_bp() { |
| 141 | setup |
| 142 | mkdir -p a |
| 143 | cat > a/Android.bp <<'EOF' |
| 144 | python_binary_host { |
| 145 | name: "my_little_binary_host", |
| 146 | srcs: ["my_little_binary_host.py"] |
| 147 | } |
| 148 | EOF |
| 149 | touch a/my_little_binary_host.py |
| 150 | run_soong |
| 151 | |
| 152 | grep -q "^# Module:.*my_little_binary_host" out/soong/build.ninja || fail "module not found" |
| 153 | |
| 154 | cat > a/Android.bp <<'EOF' |
| 155 | python_binary_host { |
| 156 | name: "my_great_binary_host", |
| 157 | srcs: ["my_great_binary_host.py"] |
| 158 | } |
| 159 | EOF |
| 160 | touch a/my_great_binary_host.py |
| 161 | run_soong |
| 162 | |
| 163 | grep -q "^# Module:.*my_little_binary_host" out/soong/build.ninja && fail "old module found" |
| 164 | grep -q "^# Module:.*my_great_binary_host" out/soong/build.ninja || fail "new module not found" |
| 165 | } |
| 166 | |
| 167 | |
| 168 | function test_add_android_bp() { |
| 169 | setup |
| 170 | run_soong |
| 171 | local mtime1=$(stat -c "%y" out/soong/build.ninja) |
| 172 | |
| 173 | mkdir -p a |
| 174 | cat > a/Android.bp <<'EOF' |
| 175 | python_binary_host { |
| 176 | name: "my_little_binary_host", |
| 177 | srcs: ["my_little_binary_host.py"] |
| 178 | } |
| 179 | EOF |
| 180 | touch a/my_little_binary_host.py |
| 181 | run_soong |
| 182 | |
| 183 | local mtime2=$(stat -c "%y" out/soong/build.ninja) |
| 184 | if [[ "$mtime1" == "$mtime2" ]]; then |
| 185 | fail "Output Ninja file did not change" |
| 186 | fi |
| 187 | |
| 188 | grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja || fail "New module not in output" |
| 189 | |
| 190 | run_soong |
| 191 | } |
| 192 | |
| 193 | function test_delete_android_bp() { |
| 194 | setup |
| 195 | mkdir -p a |
| 196 | cat > a/Android.bp <<'EOF' |
| 197 | python_binary_host { |
| 198 | name: "my_little_binary_host", |
| 199 | srcs: ["my_little_binary_host.py"] |
| 200 | } |
| 201 | EOF |
| 202 | touch a/my_little_binary_host.py |
| 203 | run_soong |
| 204 | |
| 205 | grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja || fail "Module not in output" |
| 206 | |
| 207 | rm a/Android.bp |
| 208 | run_soong |
| 209 | |
| 210 | grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja && fail "Old module in output" |
| 211 | } |
| 212 | |
| 213 | function test_add_file_to_glob() { |
| 214 | setup |
| 215 | |
| 216 | mkdir -p a |
| 217 | cat > a/Android.bp <<'EOF' |
| 218 | python_binary_host { |
| 219 | name: "my_little_binary_host", |
| 220 | srcs: ["*.py"], |
| 221 | } |
| 222 | EOF |
| 223 | touch a/my_little_binary_host.py |
| 224 | run_soong |
| 225 | local mtime1=$(stat -c "%y" out/soong/build.ninja) |
| 226 | |
| 227 | touch a/my_little_library.py |
| 228 | run_soong |
| 229 | |
| 230 | local mtime2=$(stat -c "%y" out/soong/build.ninja) |
| 231 | if [[ "$mtime1" == "$mtime2" ]]; then |
| 232 | fail "Output Ninja file did not change" |
| 233 | fi |
| 234 | |
| 235 | grep -q my_little_library.py out/soong/build.ninja || fail "new file is not in output" |
| 236 | } |
| 237 | |
| 238 | function test_add_file_to_soong_build() { |
| 239 | setup |
| 240 | run_soong |
| 241 | local mtime1=$(stat -c "%y" out/soong/build.ninja) |
| 242 | |
| 243 | mkdir -p a |
| 244 | cat > a/Android.bp <<'EOF' |
| 245 | bootstrap_go_package { |
| 246 | name: "picard-soong-rules", |
| 247 | pkgPath: "android/soong/picard", |
| 248 | deps: [ |
| 249 | "blueprint", |
| 250 | "soong", |
| 251 | "soong-android", |
| 252 | ], |
| 253 | srcs: [ |
| 254 | "picard.go", |
| 255 | ], |
| 256 | pluginFor: ["soong_build"], |
| 257 | } |
| 258 | EOF |
| 259 | |
| 260 | cat > a/picard.go <<'EOF' |
| 261 | package picard |
| 262 | |
| 263 | import ( |
| 264 | "android/soong/android" |
| 265 | "github.com/google/blueprint" |
| 266 | ) |
| 267 | |
| 268 | var ( |
| 269 | pctx = android.NewPackageContext("picard") |
| 270 | ) |
| 271 | |
| 272 | func init() { |
| 273 | android.RegisterSingletonType("picard", PicardSingleton) |
| 274 | } |
| 275 | |
| 276 | func PicardSingleton() android.Singleton { |
| 277 | return &picardSingleton{} |
| 278 | } |
| 279 | |
| 280 | type picardSingleton struct{} |
| 281 | |
| 282 | func (p *picardSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 283 | picardRule := ctx.Rule(pctx, "picard", |
| 284 | blueprint.RuleParams{ |
| 285 | Command: "echo Make it so. > ${out}", |
| 286 | CommandDeps: []string{}, |
| 287 | Description: "Something quotable", |
| 288 | }) |
| 289 | |
| 290 | outputFile := android.PathForOutput(ctx, "picard", "picard.txt") |
| 291 | var deps android.Paths |
| 292 | |
| 293 | ctx.Build(pctx, android.BuildParams{ |
| 294 | Rule: picardRule, |
| 295 | Output: outputFile, |
| 296 | Inputs: deps, |
| 297 | }) |
| 298 | } |
| 299 | |
| 300 | EOF |
| 301 | |
| 302 | run_soong |
| 303 | local mtime2=$(stat -c "%y" out/soong/build.ninja) |
| 304 | if [[ "$mtime1" == "$mtime2" ]]; then |
| 305 | fail "Output Ninja file did not change" |
| 306 | fi |
| 307 | |
| 308 | grep -q "Make it so" out/soong/build.ninja || fail "New action not present" |
| 309 | } |
| 310 | |
| 311 | test_bazel_smoke |
| 312 | test_smoke |
| 313 | test_null_build |
| 314 | test_soong_build_rebuilt_if_blueprint_changes |
| 315 | test_add_file_to_glob |
| 316 | test_add_android_bp |
| 317 | test_change_android_bp |
| 318 | test_delete_android_bp |
| 319 | test_add_file_to_soong_build |