xcode_backend.sh 8.55 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() {
xster's avatar
xster committed
7 8 9
  if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
    echo "♦ $*"
  fi
10
  "$@"
11 12 13
  return $?
}

14 15 16 17 18 19 20 21
# When provided with a pipe by the host Flutter build process, output to the
# pipe goes to stdout of the Flutter build process directly.
StreamOutput() {
  if [[ -n "$SCRIPT_OUTPUT_STREAM_FILE" ]]; then
    echo "$1" > $SCRIPT_OUTPUT_STREAM_FILE
  fi
}

22 23 24 25 26
EchoError() {
  echo "$@" 1>&2
}

AssertExists() {
27 28 29 30 31 32
  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
33 34 35 36 37 38
    exit -1
  fi
  return 0
}

BuildApp() {
39 40
  local project_path="${SOURCE_ROOT}/.."
  if [[ -n "$FLUTTER_APPLICATION_PATH" ]]; then
41
    project_path="${FLUTTER_APPLICATION_PATH}"
42 43 44 45
  fi

  local target_path="lib/main.dart"
  if [[ -n "$FLUTTER_TARGET" ]]; then
46
    target_path="${FLUTTER_TARGET}"
47 48
  fi

49 50 51 52 53 54 55 56 57 58 59 60 61
  # 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

62 63
  local build_mode="release"
  if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
64
    build_mode="${FLUTTER_BUILD_MODE}"
65 66 67
  fi

  local artifact_variant="unknown"
68
  case "$build_mode" in
69 70 71
    release) artifact_variant="ios-release";;
    profile) artifact_variant="ios-profile";;
    debug) artifact_variant="ios";;
72
    *) echo "Unknown FLUTTER_BUILD_MODE: $FLUTTER_BUILD_MODE";;
73 74 75
  esac

  local framework_path="${FLUTTER_ROOT}/bin/cache/artifacts/engine/${artifact_variant}"
76 77 78 79
  if [[ -n "$FLUTTER_FRAMEWORK_DIR" ]]; then
    framework_path="${FLUTTER_FRAMEWORK_DIR}"
  fi

80
  AssertExists "${project_path}"
81

82 83 84
  local derived_dir="${SOURCE_ROOT}/Flutter"
  RunCommand mkdir -p -- "$derived_dir"
  AssertExists "$derived_dir"
85

86
  RunCommand rm -rf -- "${derived_dir}/Flutter.framework"
87
  RunCommand rm -rf -- "${derived_dir}/App.framework"
88
  RunCommand cp -r -- "${framework_path}/Flutter.framework" "${derived_dir}"
89
  RunCommand find "${derived_dir}/Flutter.framework" -type f -exec chmod a-w "{}" \;
90
  RunCommand pushd "${project_path}" > /dev/null
91

92
  AssertExists "${target_path}"
93

94 95 96 97 98
  local verbose_flag=""
  if [[ -n "$VERBOSE_SCRIPT_LOGGING" ]]; then
    verbose_flag="--verbose"
  fi

99
  local build_dir="${FLUTTER_BUILD_DIR:-build}"
100 101 102 103 104
  local local_engine_flag=""
  if [[ -n "$LOCAL_ENGINE" ]]; then
    local_engine_flag="--local-engine=$LOCAL_ENGINE"
  fi

105 106 107
  local preview_dart_2_flag=""
  if [[ -n "$PREVIEW_DART_2" ]]; then
    preview_dart_2_flag="--preview-dart-2"
108 109
  else
    preview_dart_2_flag="--no-preview-dart-2"
110 111
  fi

112
  if [[ "${build_mode}" != "debug" ]]; then
113
    StreamOutput " ├─Building Dart code..."
114 115
    # Transform ARCHS to comma-separated list of target architectures.
    local archs="${ARCHS// /,}"
116 117 118
    RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics           \
      ${verbose_flag}                                                       \
      build aot                                                             \
119 120 121
      --output-dir="${build_dir}/aot"                                       \
      --target-platform=ios                                                 \
      --target="${target_path}"                                             \
122
      --${build_mode}                                                       \
123
      --ios-arch="${archs}"                                                 \
124
      ${local_engine_flag}                                                  \
125
      ${preview_dart_2_flag}
126 127 128 129 130

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

133
    RunCommand cp -r -- "${build_dir}/aot/App.framework" "${derived_dir}"
134
  else
135
    RunCommand mkdir -p -- "${derived_dir}/App.framework"
136 137 138 139 140 141 142 143

    # Build stub for all requested architectures.
    local arch_flags=""
    read -r -a archs <<< "$ARCHS"
    for arch in "${archs[@]}"; do
      arch_flags="${arch_flags}-arch $arch "
    done

144
    RunCommand eval "$(echo "static const int Moo = 88;" | xcrun clang -x c \
145
        ${arch_flags} \
146 147 148 149 150
        -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" -)"
151
  fi
152
  RunCommand cp -- "${derived_dir}/AppFrameworkInfo.plist" "${derived_dir}/App.framework/Info.plist"
153 154

  local precompilation_flag=""
155
  if [[ "$CURRENT_ARCH" != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
156 157 158
    precompilation_flag="--precompiled"
  fi

159
  StreamOutput " ├─Assembling Flutter resources..."
160 161 162 163 164 165 166 167 168
  RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics             \
    ${verbose_flag}                                                         \
    build bundle                                                            \
    --target="${target_path}"                                               \
    --snapshot="${build_dir}/snapshot_blob.bin"                             \
    --depfile="${build_dir}/snapshot_blob.bin.d"                            \
    --asset-dir="${derived_dir}/flutter_assets"                             \
    ${precompilation_flag}                                                  \
    ${local_engine_flag}                                                    \
169
    ${preview_dart_2_flag}
170 171 172 173 174

  if [[ $? -ne 0 ]]; then
    EchoError "Failed to package ${project_path}."
    exit -1
  fi
175 176
  StreamOutput "done"
  StreamOutput " └─Compiling, linking and signing..."
177

178
  RunCommand popd > /dev/null
179 180 181 182 183

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

184 185 186 187 188 189 190 191 192 193 194 195 196 197
# 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
198 199
  # Split $@ into an array.
  read -r -a archs <<< "$@"
200 201 202

  # Extract architecture-specific framework executables.
  local all_executables=()
203
  for arch in "${archs[@]}"; do
204
    local output="${executable}_${arch}"
205
    local lipo_info="$(lipo -info "${executable}")"
206 207 208 209 210 211
    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
212
    else
213 214 215 216 217 218 219 220
      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
221 222 223
    fi
  done

224 225 226 227 228 229 230 231 232
  # 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
233 234 235 236 237 238 239 240 241 242
}

# Destructively thins the specified framework to include only the specified
# architectures.
ThinFramework() {
  local framework_dir="$1"
  shift

  local plist_path="${framework_dir}/Info.plist"
  local executable="$(GetFrameworkExecutablePath "${framework_dir}")"
243
  LipoExecutable "${executable}" "$@"
244 245 246 247 248 249 250
}

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

  [[ -d "$frameworks_dir" ]] || return 0
251
  find "${app_path}" -type d -name "*.framework" | while read framework_dir; do
252 253 254 255 256 257
    ThinFramework "$framework_dir" "$ARCHS"
  done
}

# Main entry point.

258 259
# TODO(cbracken) improve error handling, then enable set -e

260
if [[ $# == 0 ]]; then
Josh Soref's avatar
Josh Soref committed
261
  # Backwards-compatibility: if no args are provided, build.
262 263 264 265 266 267 268 269 270
  BuildApp
else
  case $1 in
    "build")
      BuildApp ;;
    "thin")
      ThinAppFrameworks ;;
  esac
fi