xcode_backend.sh 6.67 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
  local build_mode="release"
  if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
41
    build_mode="${FLUTTER_BUILD_MODE}"
42 43 44
  fi

  local artifact_variant="unknown"
45
  case "$build_mode" in
46 47 48
    release) artifact_variant="ios-release";;
    profile) artifact_variant="ios-profile";;
    debug) artifact_variant="ios";;
49
    *) echo "Unknown FLUTTER_BUILD_MODE: $FLUTTER_BUILD_MODE";;
50 51 52
  esac

  local framework_path="${FLUTTER_ROOT}/bin/cache/artifacts/engine/${artifact_variant}"
53 54 55 56
  if [[ -n "$FLUTTER_FRAMEWORK_DIR" ]]; then
    framework_path="${FLUTTER_FRAMEWORK_DIR}"
  fi

57
  AssertExists "${project_path}"
58

59 60 61
  local derived_dir="${SOURCE_ROOT}/Flutter"
  RunCommand mkdir -p -- "$derived_dir"
  AssertExists "$derived_dir"
62

63
  RunCommand rm -rf -- "${derived_dir}/Flutter.framework"
64
  RunCommand rm -rf -- "${derived_dir}/App.framework"
65 66
  RunCommand rm -f -- "${derived_dir}/app.flx"
  RunCommand cp -r -- "${framework_path}/Flutter.framework" "${derived_dir}"
67
  RunCommand find "${derived_dir}/Flutter.framework" -type f -exec chmod a-w "{}" \;
68
  RunCommand pushd "${project_path}" > /dev/null
69

70
  AssertExists "${target_path}"
71

72
  local build_dir="${FLUTTER_BUILD_DIR:-build}"
73 74 75 76 77
  local local_engine_flag=""
  if [[ -n "$LOCAL_ENGINE" ]]; then
    local_engine_flag="--local-engine=$LOCAL_ENGINE"
  fi

78
  if [[ "$CURRENT_ARCH" != "x86_64" ]]; then
79
    local aot_flags=""
80
    if [[ "$build_mode" == "debug" ]]; then
81 82
      aot_flags="--interpreter --debug"
    else
83
      aot_flags="--${build_mode}"
84 85
    fi

86 87 88 89 90
    RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics build aot \
      --output-dir="${build_dir}/aot"                                       \
      --target-platform=ios                                                 \
      --target="${target_path}"                                             \
      ${aot_flags}                                                          \
91 92 93 94 95 96 97
      ${local_engine_flag}

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

98
    RunCommand cp -r -- "${build_dir}/aot/App.framework" "${derived_dir}"
99
  else
100 101 102 103 104 105 106
    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" -)"
107
  fi
108
  RunCommand cp -- "${derived_dir}/AppFrameworkInfo.plist" "${derived_dir}/App.framework/Info.plist"
109 110

  local precompilation_flag=""
111
  if [[ "$CURRENT_ARCH" != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
112 113 114
    precompilation_flag="--precompiled"
  fi

115 116 117 118 119 120 121 122
  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}                                                  \
123 124 125 126 127 128

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

129
  RunCommand popd > /dev/null
130 131 132 133 134

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148
# 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
149
  local archs=("$@")
150 151 152

  # Extract architecture-specific framework executables.
  local all_executables=()
153
  for arch in "${archs[@]}"; do
154
    local output="${executable}_${arch}"
155
    local lipo_info="$(lipo -info "${executable}")"
156 157 158 159 160 161
    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
162
    else
163 164 165 166 167 168 169 170
      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
171 172 173
    fi
  done

174 175 176 177 178 179 180 181 182
  # 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
183 184 185 186 187 188 189
}

# Destructively thins the specified framework to include only the specified
# architectures.
ThinFramework() {
  local framework_dir="$1"
  shift
190
  local archs=("$@")
191 192 193

  local plist_path="${framework_dir}/Info.plist"
  local executable="$(GetFrameworkExecutablePath "${framework_dir}")"
194
  LipoExecutable "${executable}" "${archs[@]}"
195 196 197 198 199 200 201
}

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

  [[ -d "$frameworks_dir" ]] || return 0
202
  find "${app_path}" -type d -name "*.framework" | while read framework_dir; do
203 204 205 206 207 208
    ThinFramework "$framework_dir" "$ARCHS"
  done
}

# Main entry point.

209 210
# TODO(cbracken) improve error handling, then enable set -e

211 212 213 214 215 216 217 218 219 220 221
if [[ $# == 0 ]]; then
  # Backwards-comptibility: if no args are provided, build.
  BuildApp
else
  case $1 in
    "build")
      BuildApp ;;
    "thin")
      ThinAppFrameworks ;;
  esac
fi