build_linux.dart 3.69 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 '../base/analyze_size.dart';
6
import '../base/common.dart';
7
import '../base/os.dart';
8 9
import '../build_info.dart';
import '../cache.dart';
10
import '../features.dart';
11
import '../globals.dart' as globals;
12
import '../linux/build_linux.dart';
13 14 15 16 17 18
import '../project.dart';
import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart';

/// A command to build a linux desktop target through a build shell script.
class BuildLinuxCommand extends BuildSubCommand {
19
  BuildLinuxCommand({
20
    required OperatingSystemUtils operatingSystemUtils,
21
    bool verboseHelp = false,
22 23
  }) : _operatingSystemUtils = operatingSystemUtils,
       super(verboseHelp: verboseHelp) {
24
    addCommonDesktopBuildOptions(verboseHelp: verboseHelp);
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    final String defaultTargetPlatform =
        (_operatingSystemUtils.hostPlatform == HostPlatform.linux_arm64) ?
            'linux-arm64' : 'linux-x64';
    argParser.addOption('target-platform',
      defaultsTo: defaultTargetPlatform,
      allowed: <String>['linux-arm64', 'linux-x64'],
      help: 'The target platform for which the app is compiled.',
    );
    argParser.addOption('target-sysroot',
      defaultsTo: '/',
      help: 'The root filesystem path of target platform for which '
            'the app is compiled. This option is valid only '
            'if the current host and target architectures are different.',
    );
39 40
  }

41 42
  final OperatingSystemUtils _operatingSystemUtils;

43 44 45 46
  @override
  final String name = 'linux';

  @override
47
  bool get hidden => !featureFlags.isLinuxEnabled || !globals.platform.isLinux;
48 49 50 51 52 53 54

  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.linux,
  };

  @override
55
  String get description => 'Build a Linux desktop application.';
56 57 58

  @override
  Future<FlutterCommandResult> runCommand() async {
59
    final BuildInfo buildInfo = await getBuildInfo();
60
    final FlutterProject flutterProject = FlutterProject.current();
61
    final TargetPlatform targetPlatform =
62
        getTargetPlatformForName(stringArg('target-platform')!);
63 64 65 66
    final bool needCrossBuild =
        getNameForHostPlatformArch(_operatingSystemUtils.hostPlatform)
            != getNameForTargetPlatformArch(targetPlatform);

67
    if (!featureFlags.isLinuxEnabled) {
68
      throwToolExit('"build linux" is not currently supported. To enable, run "flutter config --enable-linux-desktop".');
69
    }
70
    if (!globals.platform.isLinux) {
71 72
      throwToolExit('"build linux" only supported on Linux hosts.');
    }
73 74 75 76 77 78 79 80 81 82 83
    // Cross-building for x64 targets on arm64 hosts is not supported.
    if (_operatingSystemUtils.hostPlatform != HostPlatform.linux_x64 &&
        targetPlatform != TargetPlatform.linux_arm64) {
      throwToolExit('"cross-building" only supported on Linux x64 hosts.');
    }
    // TODO(fujino): https://github.com/flutter/flutter/issues/74929
    if (_operatingSystemUtils.hostPlatform == HostPlatform.linux_x64 &&
        targetPlatform == TargetPlatform.linux_arm64) {
      throwToolExit(
          'Cross-build from Linux x64 host to Linux arm64 target is not currently supported.');
    }
84
    displayNullSafetyMode(buildInfo);
85 86 87 88 89 90 91
    await buildLinux(
      flutterProject.linux,
      buildInfo,
      target: targetFile,
      sizeAnalyzer: SizeAnalyzer(
        fileSystem: globals.fs,
        logger: globals.logger,
92
        flutterUsage: globals.flutterUsage,
93
      ),
94 95
      needCrossBuild: needCrossBuild,
      targetPlatform: targetPlatform,
96
      targetSysroot: stringArg('target-sysroot')!,
97
    );
98
    return FlutterCommandResult.success();
99 100
  }
}