xcode_backend.sh 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/bin/sh
# 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() {
  echo "♦ " $@
  $@
  return $?
}

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

AssertExists() {
  RunCommand ls $1
  if [ $? -ne 0 ]; then
    EchoError "The path $1 does not exist"
    exit -1
  fi
  return 0
}

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

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

36 37 38
  local build_mode="release"
  if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
    build_mode=${FLUTTER_BUILD_MODE}
39 40 41
  fi

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

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

54 55 56 57 58 59 60
  AssertExists ${project_path}

  local derived_dir=${SOURCE_ROOT}/Flutter
  RunCommand mkdir -p $derived_dir
  AssertExists $derived_dir

  RunCommand rm -f ${derived_dir}/Flutter.framework
61
  RunCommand rm -f ${derived_dir}/app.dylib
62
  RunCommand rm -f ${derived_dir}/app.flx
63
  RunCommand cp -r ${framework_path}/Flutter.framework ${derived_dir}
64 65
  RunCommand pushd ${project_path}

66 67
  AssertExists ${target_path}

68
  local build_dir=${FLUTTER_BUILD_DIR:-build}
69 70 71 72 73 74
  local local_engine_flag=""
  if [[ -n "$LOCAL_ENGINE" ]]; then
    local_engine_flag="--local-engine=$LOCAL_ENGINE"
  fi

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

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

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

94
    RunCommand cp ${build_dir}/aot/app.dylib ${derived_dir}/app.dylib
95
  else
96
    RunCommand eval "$(echo \"static const int Moo = 88;\" | xcrun clang -x c --shared -o ${derived_dir}/app.dylib -)"
97 98 99
  fi

  local precompilation_flag=""
100
  if [[ $CURRENT_ARCH != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
101 102 103 104
    precompilation_flag="--precompiled"
  fi

  RunCommand ${FLUTTER_ROOT}/bin/flutter --suppress-analytics build flx \
105
    --target=${target_path}                                             \
106
    --output-file=${derived_dir}/app.flx                                \
107 108 109
    --snapshot=${build_dir}/snapshot_blob.bin                           \
    --depfile=${build_dir}/snapshot_blob.bin.d                          \
    --working-dir=${build_dir}/flx                                      \
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    ${precompilation_flag}                                              \
    ${local_engine_flag}                                                \

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

  RunCommand popd

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

BuildApp