build_linux.dart 4.42 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/common.dart';
7
import '../base/file_system.dart';
8
import '../base/logger.dart';
9
import '../base/process.dart';
10 11
import '../build_info.dart';
import '../cache.dart';
12
import '../globals.dart' as globals;
13
import '../plugins.dart';
14 15
import '../project.dart';

16
/// Builds the Linux project through the Makefile.
17
Future<void> buildLinux(LinuxProject linuxProject, BuildInfo buildInfo, {String target = 'lib/main.dart'}) async {
18 19 20 21 22 23
  if (!linuxProject.makeFile.existsSync()) {
    throwToolExit('No Linux desktop project configured. See '
      'https://github.com/flutter/flutter/wiki/Desktop-shells#create '
      'to learn about adding Linux support to a project.');
  }

24 25 26 27 28 29 30 31 32 33 34 35 36 37
  // Check for incompatibility between the Flutter tool version and the project
  // template version, since the tempalte isn't stable yet.
  final int templateCompareResult = _compareTemplateVersions(linuxProject);
  if (templateCompareResult < 0) {
    throwToolExit('The Linux runner was created with an earlier version of the '
      'template, which is not yet stable.\n\n'
      'Delete the linux/ directory and re-run \'flutter create .\', '
      're-applying any previous changes.');
  } else if (templateCompareResult > 0) {
    throwToolExit('The Linux runner was created with a newer version of the '
      'template, which is not yet stable.\n\n'
      'Upgrade Flutter and try again.');
  }

38 39 40 41 42 43 44
  final StringBuffer buffer = StringBuffer('''
# Generated code do not commit.
export FLUTTER_ROOT=${Cache.flutterRoot}
export TRACK_WIDGET_CREATION=${buildInfo?.trackWidgetCreation == true}
export FLUTTER_TARGET=$target
export PROJECT_DIR=${linuxProject.project.directory.path}
''');
45 46
  if (globals.artifacts is LocalEngineArtifacts) {
    final LocalEngineArtifacts localEngineArtifacts = globals.artifacts as LocalEngineArtifacts;
47
    final String engineOutPath = localEngineArtifacts.engineOutPath;
48 49
    buffer.writeln('export FLUTTER_ENGINE=${globals.fs.path.dirname(globals.fs.path.dirname(engineOutPath))}');
    buffer.writeln('export LOCAL_ENGINE=${globals.fs.path.basename(engineOutPath)}');
50 51 52
  }

  /// Cache flutter configuration files in the linux directory.
53
  linuxProject.generatedMakeConfigFile
54
    ..createSync(recursive: true)
55
    ..writeAsStringSync(buffer.toString());
56
  createPluginSymlinks(linuxProject.project);
57

58 59
  if (!buildInfo.isDebug) {
    const String warning = '🚧 ';
60 61 62 63 64
    globals.printStatus(warning * 20);
    globals.printStatus('Warning: Only debug is currently implemented for Linux. This is effectively a debug build.');
    globals.printStatus('See https://github.com/flutter/flutter/issues/38478 for details and updates.');
    globals.printStatus(warning * 20);
    globals.printStatus('');
65 66
  }

67
  // Invoke make.
68
  final String buildFlag = getNameForBuildMode(buildInfo.mode ?? BuildMode.release);
69
  final Stopwatch sw = Stopwatch()..start();
70
  final Status status = globals.logger.startProgress(
71 72 73 74 75
    'Building Linux application...',
    timeout: null,
  );
  int result;
  try {
76
    result = await processUtils.stream(<String>[
77 78 79 80
      'make',
      '-C',
      linuxProject.makeFile.parent.path,
      'BUILD=$buildFlag',
81
    ], trace: true);
82
  } on ArgumentError {
83
    throwToolExit("make not found. Run 'flutter doctor' for more information.");
84 85 86 87 88 89
  } finally {
    status.cancel();
  }
  if (result != 0) {
    throwToolExit('Build process failed');
  }
90
  globals.flutterUsage.sendTiming('build', 'make-linux', Duration(milliseconds: sw.elapsedMilliseconds));
91
}
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

// Checks the template version of [project] against the current template
// version. Returns < 0 if the project is older than the current template, > 0
// if it's newer, and 0 if they match.
int _compareTemplateVersions(LinuxProject project) {
  const String projectVersionBasename = '.template_version';
  final int expectedVersion = int.parse(globals.fs.file(globals.fs.path.join(
    globals.fs.path.absolute(Cache.flutterRoot),
    'packages',
    'flutter_tools',
    'templates',
    'app',
    'linux.tmpl',
    'flutter',
    projectVersionBasename,
  )).readAsStringSync());
  final File projectVersionFile = project.managedDirectory.childFile(projectVersionBasename);
  final int version = projectVersionFile.existsSync()
      ? int.tryParse(projectVersionFile.readAsStringSync())
      : 0;
  return version.compareTo(expectedVersion);
}