build_linux.dart 7.09 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 '../artifacts.dart';
6
import '../base/analyze_size.dart';
7
import '../base/common.dart';
8
import '../base/file_system.dart';
9
import '../base/logger.dart';
10
import '../base/project_migrator.dart';
11
import '../base/utils.dart';
12 13
import '../build_info.dart';
import '../cache.dart';
14
import '../cmake.dart';
15
import '../cmake_project.dart';
16
import '../convert.dart';
17
import '../flutter_plugins.dart';
18
import '../globals.dart' as globals;
19
import '../migrations/cmake_custom_command_migration.dart';
20
import '../migrations/cmake_native_assets_migration.dart';
21

22
// Matches the following error and warning patterns:
23
// - <file path>:<line>:<column>: (fatal) error: <error...>
24 25
// - <file path>:<line>:<column>: warning: <warning...>
// - clang: error: <link error...>
26 27
// - Error: <tool error...>
final RegExp errorMatcher = RegExp(r'(?:(?:.*:\d+:\d+|clang):\s)?(fatal\s)?(?:error|warning):\s.*', caseSensitive: false);
28

29
/// Builds the Linux project through the Makefile.
30 31 32
Future<void> buildLinux(
  LinuxProject linuxProject,
  BuildInfo buildInfo, {
33 34 35 36 37 38 39
  String? target,
  SizeAnalyzer? sizeAnalyzer,
  bool needCrossBuild = false,
  required TargetPlatform targetPlatform,
  String targetSysroot = '/',
  required Logger logger,
}) async {
40
  target ??= 'lib/main.dart';
41
  if (!linuxProject.cmakeFile.existsSync()) {
42
    throwToolExit('No Linux desktop project configured. See '
43
      'https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
44 45 46
      'to learn about adding Linux support to a project.');
  }

47
  final List<ProjectMigrator> migrators = <ProjectMigrator>[
48
    CmakeCustomCommandMigration(linuxProject, logger),
49
    CmakeNativeAssetsMigration(linuxProject, 'linux', logger),
50 51 52
  ];

  final ProjectMigration migration = ProjectMigration(migrators);
53
  migration.run();
54

55 56
  // Build the environment that needs to be set for the re-entrant flutter build
  // step.
57
  final Map<String, String> environmentConfig = buildInfo.toEnvironmentConfig();
58
  environmentConfig['FLUTTER_TARGET'] = target;
59 60
  final LocalEngineInfo? localEngineInfo = globals.artifacts?.localEngineInfo;
  if (localEngineInfo != null) {
61 62 63 64 65
    final String targetOutPath = localEngineInfo.targetOutPath;
    // $ENGINE/src/out/foo_bar_baz -> $ENGINE/src
    environmentConfig['FLUTTER_ENGINE'] = globals.fs.path.dirname(globals.fs.path.dirname(targetOutPath));
    environmentConfig['LOCAL_ENGINE'] = localEngineInfo.localTargetName;
    environmentConfig['LOCAL_ENGINE_HOST'] = localEngineInfo.localHostName;
66
  }
67
  writeGeneratedCmakeConfig(Cache.flutterRoot!, linuxProject, buildInfo, environmentConfig, logger);
68

69
  createPluginSymlinks(linuxProject.parent);
70

71
  final Status status = logger.startProgress(
72 73
    'Building Linux application...',
  );
74
  try {
75
    final String buildModeName = buildInfo.mode.cliName;
76 77 78 79
    final Directory buildDirectory =
        globals.fs.directory(getLinuxBuildDirectory(targetPlatform)).childDirectory(buildModeName);
    await _runCmake(buildModeName, linuxProject.cmakeFile.parent, buildDirectory,
                    needCrossBuild, targetPlatform, targetSysroot);
80 81 82 83
    await _runBuild(buildDirectory);
  } finally {
    status.cancel();
  }
84
  if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
85
    final String arch = getNameForTargetPlatform(targetPlatform);
86 87 88 89
    final File codeSizeFile = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('snapshot.$arch.json');
    final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('trace.$arch.json');
90
    final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
91 92 93
      aotSnapshot: codeSizeFile,
      // This analysis is only supported for release builds.
      outputDirectory: globals.fs.directory(
94
        globals.fs.path.join(getLinuxBuildDirectory(targetPlatform), 'release', 'bundle'),
95 96 97 98 99
      ),
      precompilerTrace: precompilerTrace,
      type: 'linux',
    );
    final File outputFile = globals.fsUtils.getUniqueFile(
100 101 102
      globals.fs
        .directory(globals.fsUtils.homeDirPath)
        .childDirectory('.flutter-devtools'), 'linux-code-size-analysis', 'json',
103 104
    )..writeAsStringSync(jsonEncode(output));
    // This message is used as a sentinel in analyze_apk_size_test.dart
105
    logger.printStatus(
106 107
      'A summary of your Linux bundle analysis can be found at: ${outputFile.path}',
    );
108 109 110

    // DevTools expects a file path relative to the .flutter-devtools/ dir.
    final String relativeAppSizePath = outputFile.path.split('.flutter-devtools/').last.trim();
111
    logger.printStatus(
112
      '\nTo analyze your app size in Dart DevTools, run the following command:\n'
113
      'dart devtools --appSizeBase=$relativeAppSizePath'
114
    );
115
  }
116 117
}

118 119
Future<void> _runCmake(String buildModeName, Directory sourceDir, Directory buildDir,
    bool needCrossBuild, TargetPlatform targetPlatform, String targetSysroot) async {
120 121
  final Stopwatch sw = Stopwatch()..start();

122 123
  await buildDir.create(recursive: true);

124
  final String buildFlag = sentenceCase(buildModeName);
125 126
  final bool needCrossBuildOptionsForArm64 = needCrossBuild
      && targetPlatform == TargetPlatform.linux_arm64;
127
  int result;
128 129
  if (!globals.processManager.canRun('cmake')) {
    throwToolExit(globals.userMessages.cmakeMissing);
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
  result = await globals.processUtils.stream(
    <String>[
      'cmake',
      '-G',
      'Ninja',
      '-DCMAKE_BUILD_TYPE=$buildFlag',
      '-DFLUTTER_TARGET_PLATFORM=${getNameForTargetPlatform(targetPlatform)}',
      // Support cross-building for arm64 targets on x64 hosts.
      // (Cross-building for x64 on arm64 hosts isn't supported now.)
      if (needCrossBuild)
        '-DFLUTTER_TARGET_PLATFORM_SYSROOT=$targetSysroot',
      if (needCrossBuildOptionsForArm64)
        '-DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu',
      if (needCrossBuildOptionsForArm64)
        '-DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu',
      sourceDir.path,
    ],
    workingDirectory: buildDir.path,
    environment: <String, String>{
      'CC': 'clang',
      'CXX': 'clang++',
    },
    trace: true,
  );
155 156 157 158 159 160 161 162 163
  if (result != 0) {
    throwToolExit('Unable to generate build files');
  }
  globals.flutterUsage.sendTiming('build', 'cmake-linux', Duration(milliseconds: sw.elapsedMilliseconds));
}

Future<void> _runBuild(Directory buildDir) async {
  final Stopwatch sw = Stopwatch()..start();

164 165
  int result;
  try {
166
    result = await globals.processUtils.stream(
167
      <String>[
168
        'ninja',
169
        '-C',
170 171
        buildDir.path,
        'install',
172 173 174
      ],
      environment: <String, String>{
        if (globals.logger.isVerbose)
175 176 177
          'VERBOSE_SCRIPT_LOGGING': 'true',
        if (!globals.logger.isVerbose)
          'PREFIXED_ERROR_LOGGING': 'true',
178 179
      },
      trace: true,
180
      stdoutErrorMatcher: errorMatcher,
181
    );
182
  } on ArgumentError {
183
    throwToolExit("ninja not found. Run 'flutter doctor' for more information.");
184 185 186 187
  }
  if (result != 0) {
    throwToolExit('Build process failed');
  }
188
  globals.flutterUsage.sendTiming('build', 'linux-ninja', Duration(milliseconds: sw.elapsedMilliseconds));
189
}