"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter_tools/lib/src/commands/build_ios.dart" (13 Nov 2020, 6185 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:async';
    6 
    7 import 'package:file/file.dart';
    8 import 'package:meta/meta.dart';
    9 
   10 import '../application_package.dart';
   11 import '../base/analyze_size.dart';
   12 import '../base/common.dart';
   13 import '../base/utils.dart';
   14 import '../build_info.dart';
   15 import '../convert.dart';
   16 import '../globals.dart' as globals;
   17 import '../ios/mac.dart';
   18 import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult;
   19 import 'build.dart';
   20 
   21 /// Builds an .app for an iOS app to be used for local testing on an iOS device
   22 /// or simulator. Can only be run on a macOS host. For producing deployment
   23 /// .ipas, see https://flutter.dev/docs/deployment/ios.
   24 class BuildIOSCommand extends BuildSubCommand {
   25   BuildIOSCommand({ @required bool verboseHelp }) {
   26     addTreeShakeIconsFlag();
   27     addSplitDebugInfoOption();
   28     addBuildModeFlags(defaultToRelease: true);
   29     usesTargetOption();
   30     usesFlavorOption();
   31     usesPubOption();
   32     usesBuildNumberOption();
   33     usesBuildNameOption();
   34     addDartObfuscationOption();
   35     usesDartDefineOption();
   36     usesExtraFrontendOptions();
   37     addEnableExperimentation(hide: !verboseHelp);
   38     addBuildPerformanceFile(hide: !verboseHelp);
   39     addBundleSkSLPathOption(hide: !verboseHelp);
   40     addNullSafetyModeOptions(hide: !verboseHelp);
   41     usesAnalyzeSizeFlag();
   42     argParser
   43       ..addFlag('config-only',
   44         help: 'Update the project configuration without performing a build. '
   45           'This can be used in CI/CD process that create an archive to avoid '
   46           'performing duplicate work.'
   47       )
   48       ..addFlag('simulator',
   49         help: 'Build for the iOS simulator instead of the device. This changes '
   50           'the default build mode to debug if otherwise unspecified.',
   51       )
   52       ..addFlag('codesign',
   53         defaultsTo: true,
   54         help: 'Codesign the application bundle (only available on device builds).',
   55       );
   56   }
   57 
   58   @override
   59   final String name = 'ios';
   60 
   61   @override
   62   final String description = 'Build an iOS application bundle (Mac OS X host only).';
   63 
   64   @override
   65   Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
   66     DevelopmentArtifact.iOS,
   67   };
   68 
   69   @override
   70   Future<FlutterCommandResult> runCommand() async {
   71     final bool forSimulator = boolArg('simulator');
   72     final bool configOnly = boolArg('config-only');
   73     final bool shouldCodesign = boolArg('codesign');
   74     defaultBuildMode = forSimulator ? BuildMode.debug : BuildMode.release;
   75     final BuildInfo buildInfo = getBuildInfo();
   76 
   77     if (!globals.platform.isMacOS) {
   78       throwToolExit('Building for iOS is only supported on macOS.');
   79     }
   80     if (forSimulator && !buildInfo.supportsSimulator) {
   81       throwToolExit('${toTitleCase(buildInfo.friendlyModeName)} mode is not supported for simulators.');
   82     }
   83     if (configOnly && buildInfo.codeSizeDirectory != null) {
   84       throwToolExit('Cannot analyze code size without performing a full build.');
   85     }
   86     if (!forSimulator && !shouldCodesign) {
   87       globals.printStatus(
   88         'Warning: Building for device with codesigning disabled. You will '
   89         'have to manually codesign before deploying to device.',
   90       );
   91     }
   92 
   93     final BuildableIOSApp app = await applicationPackages.getPackageForPlatform(
   94       TargetPlatform.ios,
   95       buildInfo,
   96     ) as BuildableIOSApp;
   97 
   98     if (app == null) {
   99       throwToolExit('Application not configured for iOS');
  100     }
  101 
  102     final String logTarget = forSimulator ? 'simulator' : 'device';
  103     final String typeName = globals.artifacts.getEngineType(TargetPlatform.ios, buildInfo.mode);
  104     globals.printStatus('Building $app for $logTarget ($typeName)...');
  105     final XcodeBuildResult result = await buildXcodeProject(
  106       app: app,
  107       buildInfo: buildInfo,
  108       targetOverride: targetFile,
  109       buildForDevice: !forSimulator,
  110       codesign: shouldCodesign,
  111       configOnly: configOnly,
  112     );
  113 
  114     if (!result.success) {
  115       await diagnoseXcodeBuildFailure(result, globals.flutterUsage, globals.logger);
  116       throwToolExit('Encountered error while building for $logTarget.');
  117     }
  118 
  119     if (buildInfo.codeSizeDirectory != null) {
  120       final SizeAnalyzer sizeAnalyzer = SizeAnalyzer(
  121         fileSystem: globals.fs,
  122         logger: globals.logger,
  123         flutterUsage: globals.flutterUsage,
  124         appFilenamePattern: 'App'
  125       );
  126       // Only support 64bit iOS code size analysis.
  127       final String arch = getNameForDarwinArch(DarwinArch.arm64);
  128       final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
  129         .childFile('snapshot.$arch.json');
  130       final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
  131         .childFile('trace.$arch.json');
  132 
  133       // This analysis is only supported for release builds, which also excludes the simulator.
  134       // Attempt to guess the correct .app by picking the first one.
  135       final Directory candidateDirectory = globals.fs.directory(
  136         globals.fs.path.join(getIosBuildDirectory(), 'Release-iphoneos'),
  137       );
  138       final Directory appDirectory = candidateDirectory.listSync()
  139         .whereType<Directory>()
  140         .firstWhere((Directory directory) {
  141         return globals.fs.path.extension(directory.path) == '.app';
  142       });
  143       final Map<String, Object> output = await sizeAnalyzer.analyzeAotSnapshot(
  144         aotSnapshot: aotSnapshot,
  145         precompilerTrace: precompilerTrace,
  146         outputDirectory: appDirectory,
  147         type: 'ios',
  148       );
  149       final File outputFile = globals.fsUtils.getUniqueFile(
  150         globals.fs.directory(getBuildDirectory()),'ios-code-size-analysis', 'json',
  151       )..writeAsStringSync(jsonEncode(output));
  152       // This message is used as a sentinel in analyze_apk_size_test.dart
  153       globals.printStatus(
  154         'A summary of your iOS bundle analysis can be found at: ${outputFile.path}',
  155       );
  156     }
  157 
  158     if (result.output != null) {
  159       globals.printStatus('Built ${result.output}.');
  160     }
  161 
  162     return FlutterCommandResult.success();
  163   }
  164 }