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

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

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

45
  createPluginSymlinks(linuxProject.parent);
46

47
  final Status status = globals.logger.startProgress(
48 49
    'Building Linux application...',
  );
50 51 52 53 54 55 56 57
  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();
  }
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  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}',
    );
  }
81 82 83 84 85
}

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

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

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

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