build_linux.dart 3.79 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

6
import '../base/analyze_size.dart';
7
import '../base/common.dart';
8
import '../base/logger.dart';
9
import '../base/os.dart';
10 11
import '../build_info.dart';
import '../cache.dart';
12
import '../features.dart';
13
import '../globals.dart' as globals;
14
import '../linux/build_linux.dart';
15 16 17 18 19 20
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 {
21
  BuildLinuxCommand({
22
    required super.logger,
23
    required OperatingSystemUtils operatingSystemUtils,
24
    bool verboseHelp = false,
25 26
  }) : _operatingSystemUtils = operatingSystemUtils,
       super(verboseHelp: verboseHelp) {
27
    addCommonDesktopBuildOptions(verboseHelp: verboseHelp);
28 29 30 31 32 33 34 35 36 37 38 39 40 41
    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.',
    );
42 43
  }

44 45
  final OperatingSystemUtils _operatingSystemUtils;

46 47 48 49
  @override
  final String name = 'linux';

  @override
50
  bool get hidden => !featureFlags.isLinuxEnabled || !globals.platform.isLinux;
51 52 53 54 55 56 57

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

  @override
58
  String get description => 'Build a Linux desktop application.';
59 60 61

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

70
    if (!featureFlags.isLinuxEnabled) {
71
      throwToolExit('"build linux" is not currently supported. To enable, run "flutter config --enable-linux-desktop".');
72
    }
73
    if (!globals.platform.isLinux) {
74 75
      throwToolExit('"build linux" only supported on Linux hosts.');
    }
76 77 78 79 80 81 82 83 84 85 86
    // 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.');
    }
87
    displayNullSafetyMode(buildInfo);
88
    final Logger logger = globals.logger;
89 90 91 92 93 94
    await buildLinux(
      flutterProject.linux,
      buildInfo,
      target: targetFile,
      sizeAnalyzer: SizeAnalyzer(
        fileSystem: globals.fs,
95
        logger: logger,
96
        flutterUsage: globals.flutterUsage,
97
        analytics: analytics,
98
      ),
99 100
      needCrossBuild: needCrossBuild,
      targetPlatform: targetPlatform,
101
      targetSysroot: stringArg('target-sysroot')!,
102
      logger: logger,
103
    );
104
    return FlutterCommandResult.success();
105 106
  }
}