xcode_backend.sh 7.5 KB
Newer Older
1
#!/bin/bash
2 3 4 5 6
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

RunCommand() {
7 8
  echo "♦ $*"
  "$@"
9 10 11 12 13 14 15 16
  return $?
}

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

AssertExists() {
17 18 19 20 21 22
  if [[ ! -e "$1" ]]; then
    if [[ -h "$1" ]]; then
      EchoError "The path $1 is a symlink to a path that does not exist"
    else
      EchoError "The path $1 does not exist"
    fi
23 24 25 26 27 28
    exit -1
  fi
  return 0
}

BuildApp() {
29 30
  local project_path="${SOURCE_ROOT}/.."
  if [[ -n "$FLUTTER_APPLICATION_PATH" ]]; then
31
    project_path="${FLUTTER_APPLICATION_PATH}"
32 33 34 35
  fi

  local target_path="lib/main.dart"
  if [[ -n "$FLUTTER_TARGET" ]]; then
36
    target_path="${FLUTTER_TARGET}"
37 38
  fi

39 40 41 42 43 44 45 46 47 48 49 50 51
  # Archive builds (ACTION=install) should always run in release mode.
  if [[ "$ACTION" == "install" && "$FLUTTER_BUILD_MODE" != "release" ]]; then
    EchoError "========================================================================"
    EchoError "ERROR: Flutter archive builds must be run in Release mode."
    EchoError ""
    EchoError "To correct, run:"
    EchoError "flutter build ios --release"
    EchoError ""
    EchoError "then re-run Archive from Xcode."
    EchoError "========================================================================"
    exit -1
  fi

52 53
  local build_mode="release"
  if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
54
    build_mode="${FLUTTER_BUILD_MODE}"
55 56 57
  fi

  local artifact_variant="unknown"
58
  case "$build_mode" in
59 60 61
    release) artifact_variant="ios-release";;
    profile) artifact_variant="ios-profile";;
    debug) artifact_variant="ios";;
62
    *) echo "Unknown FLUTTER_BUILD_MODE: $FLUTTER_BUILD_MODE";;
63 64 65
  esac

  local framework_path="${FLUTTER_ROOT}/bin/cache/artifacts/engine/${artifact_variant}"
66 67 68 69
  if [[ -n "$FLUTTER_FRAMEWORK_DIR" ]]; then
    framework_path="${FLUTTER_FRAMEWORK_DIR}"
  fi

70
  AssertExists "${project_path}"
71

72 73 74
  local derived_dir="${SOURCE_ROOT}/Flutter"
  RunCommand mkdir -p -- "$derived_dir"
  AssertExists "$derived_dir"
75

76
  RunCommand rm -rf -- "${derived_dir}/Flutter.framework"
77
  RunCommand rm -rf -- "${derived_dir}/App.framework"
78 79
  RunCommand rm -f -- "${derived_dir}/app.flx"
  RunCommand cp -r -- "${framework_path}/Flutter.framework" "${derived_dir}"
80
  RunCommand find "${derived_dir}/Flutter.framework" -type f -exec chmod a-w "{}" \;
81
  RunCommand pushd "${project_path}" > /dev/null
82

83
  AssertExists "${target_path}"
84

85
  local build_dir="${FLUTTER_BUILD_DIR:-build}"
86 87 88 89 90
  local local_engine_flag=""
  if [[ -n "$LOCAL_ENGINE" ]]; then
    local_engine_flag="--local-engine=$LOCAL_ENGINE"
  fi

91 92 93 94 95
  local preview_dart_2_flag=""
  if [[ -n "$PREVIEW_DART_2" ]]; then
    preview_dart_2_flag="--preview-dart-2"
  fi

96
  if [[ "$CURRENT_ARCH" != "x86_64" ]]; then
97
    local aot_flags=""
98
    if [[ "$build_mode" == "debug" ]]; then
99 100
      aot_flags="--interpreter --debug"
    else
101
      aot_flags="--${build_mode}"
102 103
    fi

104 105 106 107 108
    RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics build aot \
      --output-dir="${build_dir}/aot"                                       \
      --target-platform=ios                                                 \
      --target="${target_path}"                                             \
      ${aot_flags}                                                          \
109 110
      ${local_engine_flag}                                                  \
      ${preview_dart_2_flag}
111 112 113 114 115 116

    if [[ $? -ne 0 ]]; then
      EchoError "Failed to build ${project_path}."
      exit -1
    fi

117
    RunCommand cp -r -- "${build_dir}/aot/App.framework" "${derived_dir}"
118
  else
119 120 121 122 123 124 125
    RunCommand mkdir -p -- "${derived_dir}/App.framework"
    RunCommand eval "$(echo "static const int Moo = 88;" | xcrun clang -x c \
        -dynamiclib \
        -Xlinker -rpath -Xlinker '@executable_path/Frameworks' \
        -Xlinker -rpath -Xlinker '@loader_path/Frameworks' \
        -install_name '@rpath/App.framework/App' \
        -o "${derived_dir}/App.framework/App" -)"
126
  fi
127
  RunCommand cp -- "${derived_dir}/AppFrameworkInfo.plist" "${derived_dir}/App.framework/Info.plist"
128 129

  local precompilation_flag=""
130
  if [[ "$CURRENT_ARCH" != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
131 132 133
    precompilation_flag="--precompiled"
  fi

134 135 136 137 138 139 140 141
  RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics build flx \
    --target="${target_path}"                                             \
    --output-file="${derived_dir}/app.flx"                                \
    --snapshot="${build_dir}/snapshot_blob.bin"                           \
    --depfile="${build_dir}/snapshot_blob.bin.d"                          \
    --working-dir="${build_dir}/flx"                                      \
    ${precompilation_flag}                                                \
    ${local_engine_flag}                                                  \
142
    ${preview_dart_2_flag}                                                \
143 144 145 146 147 148

  if [[ $? -ne 0 ]]; then
    EchoError "Failed to package ${project_path}."
    exit -1
  fi

149
  RunCommand popd > /dev/null
150 151 152 153 154

  echo "Project ${project_path} built and packaged successfully."
  return 0
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168
# Returns the CFBundleExecutable for the specified framework directory.
GetFrameworkExecutablePath() {
  local framework_dir="$1"

  local plist_path="${framework_dir}/Info.plist"
  local executable="$(defaults read "${plist_path}" CFBundleExecutable)"
  echo "${framework_dir}/${executable}"
}

# Destructively thins the specified executable file to include only the
# specified architectures.
LipoExecutable() {
  local executable="$1"
  shift
169
  local archs=("$@")
170 171 172

  # Extract architecture-specific framework executables.
  local all_executables=()
173
  for arch in "${archs[@]}"; do
174
    local output="${executable}_${arch}"
175
    local lipo_info="$(lipo -info "${executable}")"
176 177 178 179 180 181
    if [[ "${lipo_info}" == "Non-fat file:"* ]]; then
      if [[ "${lipo_info}" != *"${arch}" ]]; then
        echo "Non-fat binary ${executable} is not ${arch}. Running lipo -info:"
        echo "${lipo_info}"
        exit 1
      fi
182
    else
183 184 185 186 187 188 189 190
      lipo -output "${output}" -extract "${arch}" "${executable}"
      if [[ $? == 0 ]]; then
        all_executables+=("${output}")
      else
        echo "Failed to extract ${arch} for ${executable}. Running lipo -info:"
        lipo -info "${executable}"
        exit 1
      fi
191 192 193
    fi
  done

194 195 196 197 198 199 200 201 202
  # Generate a merged binary from the architecture-specific executables.
  # Skip this step for non-fat executables.
  if [[ ${#all_executables[@]} > 0 ]]; then
    local merged="${executable}_merged"
    lipo -output "${merged}" -create "${all_executables[@]}"

    cp -f -- "${merged}" "${executable}" > /dev/null
    rm -f -- "${merged}" "${all_executables[@]}"
  fi
203 204 205 206 207 208 209
}

# Destructively thins the specified framework to include only the specified
# architectures.
ThinFramework() {
  local framework_dir="$1"
  shift
210
  local archs=("$@")
211 212 213

  local plist_path="${framework_dir}/Info.plist"
  local executable="$(GetFrameworkExecutablePath "${framework_dir}")"
214
  LipoExecutable "${executable}" "${archs[@]}"
215 216 217 218 219 220 221
}

ThinAppFrameworks() {
  local app_path="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
  local frameworks_dir="${app_path}/Frameworks"

  [[ -d "$frameworks_dir" ]] || return 0
222
  find "${app_path}" -type d -name "*.framework" | while read framework_dir; do
223 224 225 226 227 228
    ThinFramework "$framework_dir" "$ARCHS"
  done
}

# Main entry point.

229 230
# TODO(cbracken) improve error handling, then enable set -e

231 232 233 234 235 236 237 238 239 240 241
if [[ $# == 0 ]]; then
  # Backwards-comptibility: if no args are provided, build.
  BuildApp
else
  case $1 in
    "build")
      BuildApp ;;
    "thin")
      ThinAppFrameworks ;;
  esac
fi