macos_assemble.sh 5.01 KB
Newer Older
1
#!/usr/bin/env bash
Ian Hickson's avatar
Ian Hickson committed
2
# Copyright 2014 The Flutter Authors. All rights reserved.
3 4 5
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

6
# TODO(jonahwilliams): refactor this and xcode_backend.sh into one script
7
# once iOS is using 'assemble'.
8 9 10 11 12 13 14 15 16 17 18 19
RunCommand() {
  if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
    echo "♦ $*"
  fi
  "$@"
  return $?
}

EchoError() {
  echo "$@" 1>&2
}

20 21 22 23
BuildApp() {
  # Set the working directory to the project root
  local project_path="${SOURCE_ROOT}/.."
  RunCommand pushd "${project_path}" > /dev/null
24

25 26 27 28 29
  # Set the target file.
  local target_path="lib/main.dart"
  if [[ -n "$FLUTTER_TARGET" ]]; then
      target_path="${FLUTTER_TARGET}"
  fi
30

31 32
  # Set the build mode
  local build_mode="$(echo "${FLUTTER_BUILD_MODE:-${CONFIGURATION}}" | tr "[:upper:]" "[:lower:]")"
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47
  if [[ -n "$LOCAL_ENGINE" ]]; then
    if [[ $(echo "$LOCAL_ENGINE" | tr "[:upper:]" "[:lower:]") != *"$build_mode"* ]]; then
      EchoError "========================================================================"
      EchoError "ERROR: Requested build with Flutter local engine at '${LOCAL_ENGINE}'"
      EchoError "This engine is not compatible with FLUTTER_BUILD_MODE: '${build_mode}'."
      EchoError "You can fix this by updating the LOCAL_ENGINE environment variable, or"
      EchoError "by running:"
      EchoError "  flutter build macos --local-engine=host_${build_mode}"
      EchoError "or"
      EchoError "  flutter build macos --local-engine=host_${build_mode}_unopt"
      EchoError "========================================================================"
      exit -1
    fi
  fi
48

49 50 51 52 53
  # The path where the input/output xcfilelists are stored. These are used by xcode
  # to conditionally skip this script phase if neither have changed.
  local ephemeral_dir="${SOURCE_ROOT}/Flutter/ephemeral"
  local build_inputs_path="${ephemeral_dir}/FlutterInputs.xcfilelist"
  local build_outputs_path="${ephemeral_dir}/FlutterOutputs.xcfilelist"
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  # Construct the "flutter assemble" argument array. Arguments should be added
  # as quoted string elements of the flutter_args array, otherwise an argument
  # (like a path) with spaces in it might be interpreted as two separate
  # arguments.
  local flutter_args=("${FLUTTER_ROOT}/bin/flutter")
  if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
    flutter_args+=('--verbose')
  fi
  if [[ -n "$FLUTTER_ENGINE" ]]; then
    flutter_args+=("--local-engine-src-path=${FLUTTER_ENGINE}")
  fi
  if [[ -n "$LOCAL_ENGINE" ]]; then
    flutter_args+=("--local-engine=${LOCAL_ENGINE}")
  fi
  flutter_args+=(
    "assemble"
    "--no-version-check"
    "-dTargetPlatform=darwin"
    "-dDarwinArchs=x86_64"
    "-dTargetFile=${target_path}"
    "-dBuildMode=${build_mode}"
    "-dTreeShakeIcons=${TREE_SHAKE_ICONS}"
    "-dDartObfuscation=${DART_OBFUSCATION}"
    "-dSplitDebugInfo=${SPLIT_DEBUG_INFO}"
    "-dTrackWidgetCreation=${TRACK_WIDGET_CREATION}"
    "--DartDefines=${DART_DEFINES}"
    "--ExtraGenSnapshotOptions=${EXTRA_GEN_SNAPSHOT_OPTIONS}"
    "--ExtraFrontEndOptions=${EXTRA_FRONT_END_OPTIONS}"
    "--build-inputs=${build_inputs_path}"
    "--build-outputs=${build_outputs_path}"
    "--output=${BUILT_PRODUCTS_DIR}"
  )
87
  if [[ -n "$PERFORMANCE_MEASUREMENT_FILE" ]]; then
88
    flutter_args+=("--performance-measurement-file=${PERFORMANCE_MEASUREMENT_FILE}")
89 90
  fi
  if [[ -n "$BUNDLE_SKSL_PATH" ]]; then
91
    flutter_args+=("-dBundleSkSLPath=${BUNDLE_SKSL_PATH}")
92 93
  fi
  if [[ -n "$CODE_SIZE_DIRECTORY" ]]; then
94
    flutter_args+=("-dCodeSizeDirectory=${CODE_SIZE_DIRECTORY}")
95
  fi
96
  flutter_args+=("${build_mode}_macos_bundle_flutter_assets")
97

98
  RunCommand "${flutter_args[@]}"
99 100 101 102 103 104 105 106 107
}

# Adds the App.framework as an embedded binary and the flutter_assets as
# resources.
EmbedFrameworks() {
  # Embed App.framework from Flutter into the app (after creating the Frameworks directory
  # if it doesn't already exist).
  local xcode_frameworks_dir="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
  RunCommand mkdir -p -- "${xcode_frameworks_dir}"
108
  RunCommand rsync -av --delete --filter "- .DS_Store" "${BUILT_PRODUCTS_DIR}/App.framework" "${xcode_frameworks_dir}"
109

110 111 112 113
  # Embed the actual FlutterMacOS.framework that the Flutter app expects to run against,
  # which could be a local build or an arch/type specific build.

  # Copy Xcode behavior and don't copy over headers or modules.
114
  RunCommand rsync -av --delete --filter "- .DS_Store" --filter "- Headers" --filter "- Modules" "${BUILT_PRODUCTS_DIR}/FlutterMacOS.framework" "${xcode_frameworks_dir}/"
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  # Sign the binaries we moved.
  if [[ -n "${EXPANDED_CODE_SIGN_IDENTITY:-}" ]]; then
    RunCommand codesign --force --verbose --sign "${EXPANDED_CODE_SIGN_IDENTITY}" -- "${xcode_frameworks_dir}/App.framework/App"
    RunCommand codesign --force --verbose --sign "${EXPANDED_CODE_SIGN_IDENTITY}" -- "${xcode_frameworks_dir}/FlutterMacOS.framework/FlutterMacOS"
  fi
}

# Main entry point.
if [[ $# == 0 ]]; then
  # Unnamed entry point defaults to build.
  BuildApp
else
  case $1 in
    "build")
      BuildApp ;;
    "embed")
      EmbedFrameworks ;;
  esac
fi