build_test.dart 17.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/artifacts.dart';
7
import 'package:flutter_tools/src/base/build.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/build_info.dart';
10
import 'package:flutter_tools/src/macos/xcode.dart';
11

12
import '../../src/common.dart';
13
import '../../src/fake_process_manager.dart';
14

15 16 17 18 19 20 21
const FakeCommand kWhichSysctlCommand = FakeCommand(
  command: <String>[
    'which',
    'sysctl',
  ],
);

22 23 24 25 26 27 28 29
const FakeCommand kARMCheckCommand = FakeCommand(
  command: <String>[
    'sysctl',
    'hw.optional.arm64',
  ],
  exitCode: 1,
);

30
const List<String> kDefaultClang = <String>[
31
  '-miphoneos-version-min=11.0',
32
  '-isysroot',
33
  'path/to/sdk',
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  '-dynamiclib',
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@executable_path/Frameworks',
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@loader_path/Frameworks',
  '-install_name',
  '@rpath/App.framework/App',
  '-o',
  'build/foo/App.framework/App',
  'build/foo/snapshot_assembly.o',
];
49

50
void main() {
51 52
  group('SnapshotType', () {
    test('does not throw, if target platform is null', () {
53
      expect(() => SnapshotType(null, BuildMode.release), returnsNormally);
54 55
    });
  });
56

57
  group('GenSnapshot', () {
58 59 60 61
    late GenSnapshot genSnapshot;
    late Artifacts artifacts;
    late FakeProcessManager processManager;
    late BufferLogger logger;
62 63

    setUp(() async {
64
      artifacts = Artifacts.test();
65 66 67
      logger = BufferLogger.test();
      processManager = FakeProcessManager.list(<  FakeCommand>[]);
      genSnapshot = GenSnapshot(
68
        artifacts: artifacts,
69 70 71
        logger: logger,
        processManager: processManager,
      );
72 73
    });

74
    testWithoutContext('android_x64', () async {
75 76 77 78
      processManager.addCommand(
        FakeCommand(
          command: <String>[
            artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_x64, mode: BuildMode.release),
79
            '--additional_arg',
80 81 82
          ],
        ),
      );
83 84 85 86 87 88 89 90 91

      final int result = await genSnapshot.run(
        snapshotType: SnapshotType(TargetPlatform.android_x64, BuildMode.release),
        additionalArgs: <String>['--additional_arg'],
      );
      expect(result, 0);
    });

    testWithoutContext('iOS arm64', () async {
92 93 94 95 96
      final String genSnapshotPath = artifacts.getArtifactPath(
        Artifact.genSnapshot,
        platform: TargetPlatform.ios,
        mode: BuildMode.release,
      );
97 98 99
      processManager.addCommand(
        FakeCommand(
          command: <String>[
100
            '${genSnapshotPath}_arm64',
101 102 103 104
           '--additional_arg',
          ],
        ),
      );
105 106 107 108 109 110 111 112 113 114 115

      final int result = await genSnapshot.run(
        snapshotType: SnapshotType(TargetPlatform.ios, BuildMode.release),
        darwinArch: DarwinArch.arm64,
        additionalArgs: <String>['--additional_arg'],
      );
      expect(result, 0);
    });

    testWithoutContext('--strip filters error output from gen_snapshot', () async {
        processManager.addCommand(FakeCommand(
116 117 118 119
        command: <String>[
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_x64, mode: BuildMode.release),
          '--strip',
        ],
120 121 122 123 124 125 126
        stderr: 'ABC\n${GenSnapshot.kIgnoredWarnings.join('\n')}\nXYZ\n'
      ));

      final int result = await genSnapshot.run(
        snapshotType: SnapshotType(TargetPlatform.android_x64, BuildMode.release),
        additionalArgs: <String>['--strip'],
      );
127

128 129 130 131 132 133 134 135
      expect(result, 0);
      expect(logger.errorText, contains('ABC'));
      for (final String ignoredWarning in GenSnapshot.kIgnoredWarnings)  {
        expect(logger.errorText, isNot(contains(ignoredWarning)));
      }
      expect(logger.errorText, contains('XYZ'));
    });
  });
136

137
  group('AOTSnapshotter', () {
138 139 140 141
    late MemoryFileSystem fileSystem;
    late AOTSnapshotter snapshotter;
    late Artifacts artifacts;
    late FakeProcessManager processManager;
142 143

    setUp(() async {
144
      fileSystem = MemoryFileSystem.test();
145
      artifacts = Artifacts.test();
146
      processManager = FakeProcessManager.empty();
147 148
      snapshotter = AOTSnapshotter(
        fileSystem: fileSystem,
149 150
        logger: BufferLogger.test(),
        xcode: Xcode.test(
151 152
          processManager: processManager,
        ),
153
        artifacts: artifacts,
154 155
        processManager: processManager,
      );
156 157
    });

158 159 160
    testWithoutContext('does not build iOS with debug build mode', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');

161
      expect(await snapshotter.build(
162
        platform: TargetPlatform.ios,
163
        darwinArch: DarwinArch.arm64,
164
        sdkRoot: 'path/to/sdk',
165
        buildMode: BuildMode.debug,
166
        mainPath: 'main.dill',
167
        outputPath: outputPath,
168
        dartObfuscation: false,
169
      ), isNot(equals(0)));
170 171 172 173
    });

    testWithoutContext('does not build android-arm with debug build mode', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
174

175 176 177 178 179
      expect(await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        outputPath: outputPath,
180
        dartObfuscation: false,
181
      ), isNot(0));
182 183 184 185
    });

    testWithoutContext('does not build android-arm64 with debug build mode', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
186

187 188 189 190 191
      expect(await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        outputPath: outputPath,
192
        dartObfuscation: false,
193
      ), isNot(0));
194
    });
195

196
    testWithoutContext('builds iOS snapshot with dwarfStackTraces', () async {
197 198
      final String outputPath = fileSystem.path.join('build', 'foo');
      final String assembly = fileSystem.path.join(outputPath, 'snapshot_assembly.S');
199
      final String debugPath = fileSystem.path.join('foo', 'app.ios-arm64.symbols');
200 201 202 203 204
      final String genSnapshotPath = artifacts.getArtifactPath(
        Artifact.genSnapshot,
        platform: TargetPlatform.ios,
        mode: BuildMode.profile,
      );
205 206
      processManager.addCommands(<FakeCommand>[
        FakeCommand(command: <String>[
207
          '${genSnapshotPath}_arm64',
208 209 210 211
          '--deterministic',
          '--snapshot_kind=app-aot-assembly',
          '--assembly=$assembly',
          '--dwarf-stack-traces',
212
          '--resolve-dwarf-paths',
213 214
          '--save-debugging-info=$debugPath',
          'main.dill',
215 216 217 218
        ]),
        kWhichSysctlCommand,
        kARMCheckCommand,
        const FakeCommand(command: <String>[
219 220 221
          'xcrun',
          'cc',
          '-arch',
222
          'arm64',
223
          '-miphoneos-version-min=11.0',
224
          '-isysroot',
225
          'path/to/sdk',
226 227 228 229
          '-c',
          'build/foo/snapshot_assembly.S',
          '-o',
          'build/foo/snapshot_assembly.o',
230 231
        ]),
        const FakeCommand(command: <String>[
232 233 234
          'xcrun',
          'clang',
          '-arch',
235
          'arm64',
236
          ...kDefaultClang,
237
        ]),
238 239 240 241 242 243 244 245 246 247
        const FakeCommand(command: <String>[
          'xcrun',
          'dsymutil',
          '-o',
          'build/foo/App.framework.dSYM',
          'build/foo/App.framework/App',
        ]),
        const FakeCommand(command: <String>[
          'xcrun',
          'strip',
248
          '-x',
249 250 251 252
          'build/foo/App.framework/App',
          '-o',
          'build/foo/App.framework/App',
        ]),
253
      ]);
254

255 256 257 258 259
      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        outputPath: outputPath,
260
        darwinArch: DarwinArch.arm64,
261
        sdkRoot: 'path/to/sdk',
262 263 264 265 266
        splitDebugInfo: 'foo',
        dartObfuscation: false,
      );

      expect(genSnapshotExitCode, 0);
267
      expect(processManager, hasNoRemainingExpectations);
268 269
    });

270
    testWithoutContext('builds iOS snapshot with obfuscate', () async {
271 272
      final String outputPath = fileSystem.path.join('build', 'foo');
      final String assembly = fileSystem.path.join(outputPath, 'snapshot_assembly.S');
273 274 275 276 277
      final String genSnapshotPath = artifacts.getArtifactPath(
        Artifact.genSnapshot,
        platform: TargetPlatform.ios,
        mode: BuildMode.profile,
      );
278 279
      processManager.addCommands(<FakeCommand>[
        FakeCommand(command: <String>[
280
          '${genSnapshotPath}_arm64',
281 282 283 284 285
          '--deterministic',
          '--snapshot_kind=app-aot-assembly',
          '--assembly=$assembly',
          '--obfuscate',
          'main.dill',
286 287 288 289
        ]),
        kWhichSysctlCommand,
        kARMCheckCommand,
        const FakeCommand(command: <String>[
290 291 292
          'xcrun',
          'cc',
          '-arch',
293
          'arm64',
294
          '-miphoneos-version-min=11.0',
295
          '-isysroot',
296
          'path/to/sdk',
297 298 299 300
          '-c',
          'build/foo/snapshot_assembly.S',
          '-o',
          'build/foo/snapshot_assembly.o',
301 302
        ]),
        const FakeCommand(command: <String>[
303 304 305
          'xcrun',
          'clang',
          '-arch',
306
          'arm64',
307
          ...kDefaultClang,
308
        ]),
309 310 311 312 313 314 315 316 317 318
        const FakeCommand(command: <String>[
          'xcrun',
          'dsymutil',
          '-o',
          'build/foo/App.framework.dSYM',
          'build/foo/App.framework/App',
        ]),
        const FakeCommand(command: <String>[
          'xcrun',
          'strip',
319
          '-x',
320 321 322 323
          'build/foo/App.framework/App',
          '-o',
          'build/foo/App.framework/App',
        ]),
324
      ]);
325 326 327 328 329 330

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        outputPath: outputPath,
331
        darwinArch: DarwinArch.arm64,
332
        sdkRoot: 'path/to/sdk',
333 334 335 336
        dartObfuscation: true,
      );

      expect(genSnapshotExitCode, 0);
337
      expect(processManager, hasNoRemainingExpectations);
338
    });
339

340
    testWithoutContext('builds iOS snapshot', () async {
341
      final String outputPath = fileSystem.path.join('build', 'foo');
342 343 344 345 346
      final String genSnapshotPath = artifacts.getArtifactPath(
        Artifact.genSnapshot,
        platform: TargetPlatform.ios,
        mode: BuildMode.release,
      );
347 348
      processManager.addCommands(<FakeCommand>[
        FakeCommand(command: <String>[
349
          '${genSnapshotPath}_arm64',
350 351 352 353
          '--deterministic',
          '--snapshot_kind=app-aot-assembly',
          '--assembly=${fileSystem.path.join(outputPath, 'snapshot_assembly.S')}',
          'main.dill',
354 355 356 357
        ]),
        kWhichSysctlCommand,
        kARMCheckCommand,
        const FakeCommand(command: <String>[
358 359 360 361
          'xcrun',
          'cc',
          '-arch',
          'arm64',
362
          '-miphoneos-version-min=11.0',
363
          '-isysroot',
364
          'path/to/sdk',
365 366 367 368
          '-c',
          'build/foo/snapshot_assembly.S',
          '-o',
          'build/foo/snapshot_assembly.o',
369 370
        ]),
        const FakeCommand(command: <String>[
371 372 373 374 375
          'xcrun',
          'clang',
          '-arch',
          'arm64',
          ...kDefaultClang,
376
        ]),
377 378 379 380 381 382 383 384 385 386
        const FakeCommand(command: <String>[
          'xcrun',
          'dsymutil',
          '-o',
          'build/foo/App.framework.dSYM',
          'build/foo/App.framework/App',
        ]),
        const FakeCommand(command: <String>[
          'xcrun',
          'strip',
387
          '-x',
388 389 390 391
          'build/foo/App.framework/App',
          '-o',
          'build/foo/App.framework/App',
        ]),
392
      ]);
393

394
      final int genSnapshotExitCode = await snapshotter.build(
395 396
        platform: TargetPlatform.ios,
        buildMode: BuildMode.release,
397
        mainPath: 'main.dill',
398
        outputPath: outputPath,
399
        darwinArch: DarwinArch.arm64,
400
        sdkRoot: 'path/to/sdk',
401
        dartObfuscation: false,
402 403 404
      );

      expect(genSnapshotExitCode, 0);
405
      expect(processManager, hasNoRemainingExpectations);
406 407 408 409
    });

    testWithoutContext('builds shared library for android-arm (32bit)', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
410
      processManager.addCommand(FakeCommand(
411
        command: <String>[
412
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm, mode: BuildMode.release),
413 414 415 416 417 418 419 420 421
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          '--no-sim-use-hardfp',
          '--no-use-integer-division',
          'main.dill',
        ]
      ));
422 423

      final int genSnapshotExitCode = await snapshotter.build(
424
        platform: TargetPlatform.android_arm,
425 426 427
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
428
        dartObfuscation: false,
429 430 431
      );

      expect(genSnapshotExitCode, 0);
432
      expect(processManager, hasNoRemainingExpectations);
433 434 435 436 437 438 439
    });

    testWithoutContext('builds shared library for android-arm with dwarf stack traces', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
      final String debugPath = fileSystem.path.join('foo', 'app.android-arm.symbols');
      processManager.addCommand(FakeCommand(
        command: <String>[
440
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm, mode: BuildMode.release),
441 442 443 444 445 446 447
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          '--no-sim-use-hardfp',
          '--no-use-integer-division',
          '--dwarf-stack-traces',
448
          '--resolve-dwarf-paths',
449 450 451 452
          '--save-debugging-info=$debugPath',
          'main.dill',
        ]
      ));
453 454 455 456 457 458 459

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
        splitDebugInfo: 'foo',
460
        dartObfuscation: false,
461 462
      );

463
      expect(genSnapshotExitCode, 0);
464
      expect(processManager, hasNoRemainingExpectations);
465 466 467 468
    });

    testWithoutContext('builds shared library for android-arm with obfuscate', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
469
      processManager.addCommand(FakeCommand(
470
        command: <String>[
471
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm, mode: BuildMode.release),
472 473 474 475 476 477 478 479 480 481
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          '--no-sim-use-hardfp',
          '--no-use-integer-division',
          '--obfuscate',
          'main.dill',
        ]
      ));
482 483 484 485 486 487 488 489 490 491

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
        dartObfuscation: true,
      );

      expect(genSnapshotExitCode, 0);
492
      expect(processManager, hasNoRemainingExpectations);
493 494 495 496
    });

    testWithoutContext('builds shared library for android-arm without dwarf stack traces due to empty string', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
497
      processManager.addCommand(FakeCommand(
498
        command: <String>[
499
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm, mode: BuildMode.release),
500 501 502 503 504 505 506 507 508
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          '--no-sim-use-hardfp',
          '--no-use-integer-division',
          'main.dill',
        ]
      ));
509 510 511 512 513 514 515

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
        splitDebugInfo: '',
516
        dartObfuscation: false,
517 518 519
      );

      expect(genSnapshotExitCode, 0);
520
       expect(processManager, hasNoRemainingExpectations);
521 522 523 524
    });

    testWithoutContext('builds shared library for android-arm64', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
525
      processManager.addCommand(FakeCommand(
526
        command: <String>[
527
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm64, mode: BuildMode.release),
528 529 530 531 532 533 534
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          'main.dill',
        ]
      ));
535 536 537 538 539 540

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
541
        dartObfuscation: false,
542 543 544
      );

      expect(genSnapshotExitCode, 0);
545
      expect(processManager, hasNoRemainingExpectations);
546
    });
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

    testWithoutContext('--no-strip in extraGenSnapshotOptions suppresses --strip', () async {
      final String outputPath = fileSystem.path.join('build', 'foo');
      processManager.addCommand(FakeCommand(
        command: <String>[
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm64, mode: BuildMode.release),
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          'main.dill',
        ]
      ));

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        outputPath: outputPath,
        dartObfuscation: false,
        extraGenSnapshotOptions: const <String>['--no-strip'],
      );

      expect(genSnapshotExitCode, 0);
570
      expect(processManager, hasNoRemainingExpectations);
571
    });
572
  });
573
}