build_test.dart 17.8 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
  '-dynamiclib',
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@executable_path/Frameworks',
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@loader_path/Frameworks',
43
  '-fapplication-extension',
44 45 46 47 48 49
  '-install_name',
  '@rpath/App.framework/App',
  '-o',
  'build/foo/App.framework/App',
  'build/foo/snapshot_assembly.o',
];
50

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

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

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

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

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

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

      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(
117 118 119 120
        command: <String>[
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_x64, mode: BuildMode.release),
          '--strip',
        ],
121 122 123 124 125 126 127
        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'],
      );
128

129 130 131 132 133 134 135 136
      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'));
    });
  });
137

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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>[
441
          artifacts.getArtifactPath(Artifact.genSnapshot, platform: TargetPlatform.android_arm, mode: BuildMode.release),
442 443 444 445 446 447 448
          '--deterministic',
          '--snapshot_kind=app-aot-elf',
          '--elf=build/foo/app.so',
          '--strip',
          '--no-sim-use-hardfp',
          '--no-use-integer-division',
          '--dwarf-stack-traces',
449
          '--resolve-dwarf-paths',
450 451 452 453
          '--save-debugging-info=$debugPath',
          'main.dill',
        ]
      ));
454 455 456 457 458 459 460

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

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

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

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

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

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

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

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

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

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

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

    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);
571
      expect(processManager, hasNoRemainingExpectations);
572
    });
573
  });
574
}