• Tess Strickland's avatar
    Mark defaultTargetPlatform as constant for non-debug non-web builds. (#141105) · 7ca4b7b8
    Tess Strickland authored
    This PR adds the Dart VM `vm:platform-const-if` pragma introduced in
    https://github.com/dart-lang/sdk/commit/57a1168875 to the
    `defaultTargetPlatform` property, allowing it to be computed as if it
    was a constant field in non-debug AOT builds. In particular, this means
    that platform-specific code executed conditionally based on this
    property can be tree-shaken in release builds. Note that this PR changes
    `defaultTargetPlatform` to only allow overriding via
    `debugDefaultTargetPlatformOverride` in debug builds, and makes it so
    that compilation throws an error if code assigns
    to`debugDefaultTargetPlatformOverride` in other build modes.
    
    Related issue: #14233
    
    ## Pre-launch Checklist
    
    - [X] I read the [Contributor Guide] and followed the process outlined
    there for submitting PRs.
    - [X] I read the [Tree Hygiene] wiki page, which explains my
    responsibilities.
    - [X] I read and followed the [Flutter Style Guide], including [Features
    we expect every widget to implement].
    - [X] I signed the [CLA].
    - [X] I listed at least one issue that this PR fixes in the description
    above.
    - [X] I updated/added relevant documentation (doc comments with `///`).
    - [X] I added new tests to check the change I am making, or this PR is
    [test-exempt].
    - [X] All existing and new tests are passing.
    Unverified
    7ca4b7b8
_platform_io.dart 1.5 KB
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';
import 'assertions.dart';
import 'constants.dart';
import 'platform.dart' as platform;

export 'platform.dart' show TargetPlatform;

/// The dart:io implementation of [platform.defaultTargetPlatform].
@pragma('vm:platform-const-if', !kDebugMode)
platform.TargetPlatform get defaultTargetPlatform {
  platform.TargetPlatform? result;
  if (Platform.isAndroid) {
    result = platform.TargetPlatform.android;
  } else if (Platform.isIOS) {
    result = platform.TargetPlatform.iOS;
  } else if (Platform.isFuchsia) {
    result = platform.TargetPlatform.fuchsia;
  } else if (Platform.isLinux) {
    result = platform.TargetPlatform.linux;
  } else if (Platform.isMacOS) {
    result = platform.TargetPlatform.macOS;
  } else if (Platform.isWindows) {
    result = platform.TargetPlatform.windows;
  }
  assert(() {
    if (Platform.environment.containsKey('FLUTTER_TEST')) {
      result = platform.TargetPlatform.android;
    }
    return true;
  }());
  if (kDebugMode && platform.debugDefaultTargetPlatformOverride != null) {
    result = platform.debugDefaultTargetPlatformOverride;
  }
  if (result == null) {
    throw FlutterError(
      'Unknown platform.\n'
      '${Platform.operatingSystem} was not recognized as a target platform. '
      'Consider updating the list of TargetPlatforms to include this platform.',
    );
  }
  return result!;
}