build_linux.dart 4.87 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/utils.dart';
11 12
import '../build_info.dart';
import '../cache.dart';
13
import '../cmake.dart';
14
import '../convert.dart';
15
import '../globals.dart' as globals;
16
import '../plugins.dart';
17 18
import '../project.dart';

19
/// Builds the Linux project through the Makefile.
20 21 22 23
Future<void> buildLinux(
  LinuxProject linuxProject,
  BuildInfo buildInfo, {
    String target = 'lib/main.dart',
24
    SizeAnalyzer sizeAnalyzer,
25
  }) async {
26
  if (!linuxProject.cmakeFile.existsSync()) {
27
    throwToolExit('No Linux desktop project configured. See '
28
      'https://flutter.dev/desktop#add-desktop-support-to-an-existing-app '
29 30 31
      'to learn about adding Linux support to a project.');
  }

32 33
  // Build the environment that needs to be set for the re-entrant flutter build
  // step.
34
  final Map<String, String> environmentConfig = buildInfo.toEnvironmentConfig();
35
  environmentConfig['FLUTTER_TARGET'] = target;
36 37
  if (globals.artifacts is LocalEngineArtifacts) {
    final LocalEngineArtifacts localEngineArtifacts = globals.artifacts as LocalEngineArtifacts;
38
    final String engineOutPath = localEngineArtifacts.engineOutPath;
39 40
    environmentConfig['FLUTTER_ENGINE'] = globals.fs.path.dirname(globals.fs.path.dirname(engineOutPath));
    environmentConfig['LOCAL_ENGINE'] = globals.fs.path.basename(engineOutPath);
41
  }
42
  writeGeneratedCmakeConfig(Cache.flutterRoot, linuxProject, environmentConfig);
43

44
  createPluginSymlinks(linuxProject.parent);
45

46
  final Status status = globals.logger.startProgress(
47 48
    'Building Linux application...',
  );
49 50 51 52 53 54 55 56
  try {
    final String buildModeName = getNameForBuildMode(buildInfo.mode ?? BuildMode.release);
    final Directory buildDirectory = globals.fs.directory(getLinuxBuildDirectory()).childDirectory(buildModeName);
    await _runCmake(buildModeName, linuxProject.cmakeFile.parent, buildDirectory);
    await _runBuild(buildDirectory);
  } finally {
    status.cancel();
  }
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
    final String arch = getNameForTargetPlatform(TargetPlatform.linux_x64);
    final File codeSizeFile = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('snapshot.$arch.json');
    final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('trace.$arch.json');
    final Map<String, Object> output = await sizeAnalyzer.analyzeAotSnapshot(
      aotSnapshot: codeSizeFile,
      // This analysis is only supported for release builds.
      outputDirectory: globals.fs.directory(
        globals.fs.path.join(getLinuxBuildDirectory(), 'release', 'bundle'),
      ),
      precompilerTrace: precompilerTrace,
      type: 'linux',
    );
    final File outputFile = globals.fsUtils.getUniqueFile(
      globals.fs.directory(getBuildDirectory()),'linux-code-size-analysis', 'json',
    )..writeAsStringSync(jsonEncode(output));
    // This message is used as a sentinel in analyze_apk_size_test.dart
    globals.printStatus(
      'A summary of your Linux bundle analysis can be found at: ${outputFile.path}',
    );
  }
80 81 82 83 84
}

Future<void> _runCmake(String buildModeName, Directory sourceDir, Directory buildDir) async {
  final Stopwatch sw = Stopwatch()..start();

85 86
  await buildDir.create(recursive: true);

87 88 89
  final String buildFlag = toTitleCase(buildModeName);
  int result;
  try {
90
    result = await globals.processUtils.stream(
91 92 93 94 95
      <String>[
        'cmake',
        '-G',
        'Ninja',
        '-DCMAKE_BUILD_TYPE=$buildFlag',
96
        sourceDir.path,
97
      ],
98
      workingDirectory: buildDir.path,
99 100 101 102 103 104
      environment: <String, String>{
        'CC': 'clang',
        'CXX': 'clang++'
      },
      trace: true,
    );
105
  } on ArgumentError {
106 107 108 109 110 111 112 113 114 115 116
    throwToolExit("cmake not found. Run 'flutter doctor' for more information.");
  }
  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();

117 118
  int result;
  try {
119
    result = await globals.processUtils.stream(
120
      <String>[
121
        'ninja',
122
        '-C',
123 124
        buildDir.path,
        'install',
125 126 127 128
      ],
      environment: <String, String>{
        if (globals.logger.isVerbose)
          'VERBOSE_SCRIPT_LOGGING': 'true'
129 130
      },
      trace: true,
131
    );
132
  } on ArgumentError {
133
    throwToolExit("ninja not found. Run 'flutter doctor' for more information.");
134 135 136 137
  }
  if (result != 0) {
    throwToolExit('Build process failed');
  }
138
  globals.flutterUsage.sendTiming('build', 'linux-ninja', Duration(milliseconds: sw.elapsedMilliseconds));
139
}