Commit 3449545e authored by Chris Bracken's avatar Chris Bracken Committed by GitHub

Quote all paths in Xcode build backend (#8039)

Required to handle Flutter SDK (and other) paths that include spaces.

Also includes general cleanup:
* Declare explicitly that we use /bin/bash, since we rely on its
  features.
* Add -- where possible, to avoid interpreting files starting in - as
  options.
* Suppress output of pushd/popd.
* Avoid stringifying arrays.
parent 9a0a69ee
#!/bin/sh #!/bin/bash
# Copyright 2016 The Chromium Authors. All rights reserved. # Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
RunCommand() { RunCommand() {
echo "♦ " $@ echo "♦ $*"
$@ "$@"
return $? return $?
} }
...@@ -28,17 +28,17 @@ AssertExists() { ...@@ -28,17 +28,17 @@ AssertExists() {
BuildApp() { BuildApp() {
local project_path="${SOURCE_ROOT}/.." local project_path="${SOURCE_ROOT}/.."
if [[ -n "$FLUTTER_APPLICATION_PATH" ]]; then if [[ -n "$FLUTTER_APPLICATION_PATH" ]]; then
project_path=${FLUTTER_APPLICATION_PATH} project_path="${FLUTTER_APPLICATION_PATH}"
fi fi
local target_path="lib/main.dart" local target_path="lib/main.dart"
if [[ -n "$FLUTTER_TARGET" ]]; then if [[ -n "$FLUTTER_TARGET" ]]; then
target_path=${FLUTTER_TARGET} target_path="${FLUTTER_TARGET}"
fi fi
local build_mode="release" local build_mode="release"
if [[ -n "$FLUTTER_BUILD_MODE" ]]; then if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
build_mode=${FLUTTER_BUILD_MODE} build_mode="${FLUTTER_BUILD_MODE}"
fi fi
local artifact_variant="unknown" local artifact_variant="unknown"
...@@ -54,27 +54,27 @@ BuildApp() { ...@@ -54,27 +54,27 @@ BuildApp() {
framework_path="${FLUTTER_FRAMEWORK_DIR}" framework_path="${FLUTTER_FRAMEWORK_DIR}"
fi fi
AssertExists ${project_path} AssertExists "${project_path}"
local derived_dir=${SOURCE_ROOT}/Flutter local derived_dir="${SOURCE_ROOT}/Flutter"
RunCommand mkdir -p $derived_dir RunCommand mkdir -p -- "$derived_dir"
AssertExists $derived_dir AssertExists "$derived_dir"
RunCommand rm -f ${derived_dir}/Flutter.framework RunCommand rm -rf -- "${derived_dir}/Flutter.framework"
RunCommand rm -f ${derived_dir}/app.dylib RunCommand rm -f -- "${derived_dir}/app.dylib"
RunCommand rm -f ${derived_dir}/app.flx RunCommand rm -f -- "${derived_dir}/app.flx"
RunCommand cp -r ${framework_path}/Flutter.framework ${derived_dir} RunCommand cp -r -- "${framework_path}/Flutter.framework" "${derived_dir}"
RunCommand pushd ${project_path} RunCommand pushd "${project_path}" > /dev/null
AssertExists ${target_path} AssertExists "${target_path}"
local build_dir=${FLUTTER_BUILD_DIR:-build} local build_dir="${FLUTTER_BUILD_DIR:-build}"
local local_engine_flag="" local local_engine_flag=""
if [[ -n "$LOCAL_ENGINE" ]]; then if [[ -n "$LOCAL_ENGINE" ]]; then
local_engine_flag="--local-engine=$LOCAL_ENGINE" local_engine_flag="--local-engine=$LOCAL_ENGINE"
fi fi
if [[ $CURRENT_ARCH != "x86_64" ]]; then if [[ "$CURRENT_ARCH" != "x86_64" ]]; then
local aot_flags="" local aot_flags=""
if [[ "$build_mode" == "debug" ]]; then if [[ "$build_mode" == "debug" ]]; then
aot_flags="--interpreter --debug" aot_flags="--interpreter --debug"
...@@ -82,11 +82,11 @@ BuildApp() { ...@@ -82,11 +82,11 @@ BuildApp() {
aot_flags="--${build_mode}" aot_flags="--${build_mode}"
fi fi
RunCommand ${FLUTTER_ROOT}/bin/flutter --suppress-analytics build aot \ RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics build aot \
--output-dir=${build_dir}/aot \ --output-dir="${build_dir}/aot" \
--target-platform=ios \ --target-platform=ios \
--target=${target_path} \ --target="${target_path}" \
${aot_flags} \ ${aot_flags} \
${local_engine_flag} ${local_engine_flag}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
...@@ -94,31 +94,31 @@ BuildApp() { ...@@ -94,31 +94,31 @@ BuildApp() {
exit -1 exit -1
fi fi
RunCommand cp ${build_dir}/aot/app.dylib ${derived_dir}/app.dylib RunCommand cp -f -- "${build_dir}/aot/app.dylib" "${derived_dir}/app.dylib"
else else
RunCommand eval "$(echo \"static const int Moo = 88;\" | xcrun clang -x c --shared -o ${derived_dir}/app.dylib -)" RunCommand eval "$(echo "static const int Moo = 88;" | xcrun clang -x c --shared -o "${derived_dir}/app.dylib" -)"
fi fi
local precompilation_flag="" local precompilation_flag=""
if [[ $CURRENT_ARCH != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then if [[ "$CURRENT_ARCH" != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
precompilation_flag="--precompiled" precompilation_flag="--precompiled"
fi fi
RunCommand ${FLUTTER_ROOT}/bin/flutter --suppress-analytics build flx \ RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics build flx \
--target=${target_path} \ --target="${target_path}" \
--output-file=${derived_dir}/app.flx \ --output-file="${derived_dir}/app.flx" \
--snapshot=${build_dir}/snapshot_blob.bin \ --snapshot="${build_dir}/snapshot_blob.bin" \
--depfile=${build_dir}/snapshot_blob.bin.d \ --depfile="${build_dir}/snapshot_blob.bin.d" \
--working-dir=${build_dir}/flx \ --working-dir="${build_dir}/flx" \
${precompilation_flag} \ ${precompilation_flag} \
${local_engine_flag} \ ${local_engine_flag} \
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
EchoError "Failed to package ${project_path}." EchoError "Failed to package ${project_path}."
exit -1 exit -1
fi fi
RunCommand popd RunCommand popd > /dev/null
echo "Project ${project_path} built and packaged successfully." echo "Project ${project_path} built and packaged successfully."
return 0 return 0
...@@ -138,13 +138,13 @@ GetFrameworkExecutablePath() { ...@@ -138,13 +138,13 @@ GetFrameworkExecutablePath() {
LipoExecutable() { LipoExecutable() {
local executable="$1" local executable="$1"
shift shift
local archs="$@" local archs=("$@")
# Extract architecture-specific framework executables. # Extract architecture-specific framework executables.
local all_executables=() local all_executables=()
for arch in $archs; do for arch in "${archs[@]}"; do
local output="${executable}_${arch}" local output="${executable}_${arch}"
local lipo_info=$(lipo -info "${executable}") local lipo_info="$(lipo -info "${executable}")"
if [[ "${lipo_info}" == "Non-fat file:"* ]]; then if [[ "${lipo_info}" == "Non-fat file:"* ]]; then
if [[ "${lipo_info}" != *"${arch}" ]]; then if [[ "${lipo_info}" != *"${arch}" ]]; then
echo "Non-fat binary ${executable} is not ${arch}. Running lipo -info:" echo "Non-fat binary ${executable} is not ${arch}. Running lipo -info:"
...@@ -168,8 +168,8 @@ LipoExecutable() { ...@@ -168,8 +168,8 @@ LipoExecutable() {
lipo -output "${merged}" -create "${all_executables[@]}" lipo -output "${merged}" -create "${all_executables[@]}"
# Replace the original executable with the thinned one and clean up. # Replace the original executable with the thinned one and clean up.
cp -f "${merged}" "${executable}" > /dev/null cp -f -- "${merged}" "${executable}" > /dev/null
rm -f "${merged}" "${all_executables[@]}" rm -f -- "${merged}" "${all_executables[@]}"
} }
# Destructively thins the specified framework to include only the specified # Destructively thins the specified framework to include only the specified
...@@ -177,11 +177,11 @@ LipoExecutable() { ...@@ -177,11 +177,11 @@ LipoExecutable() {
ThinFramework() { ThinFramework() {
local framework_dir="$1" local framework_dir="$1"
shift shift
local archs="$@" local archs=("$@")
local plist_path="${framework_dir}/Info.plist" local plist_path="${framework_dir}/Info.plist"
local executable="$(GetFrameworkExecutablePath "${framework_dir}")" local executable="$(GetFrameworkExecutablePath "${framework_dir}")"
LipoExecutable "${executable}" "$archs" LipoExecutable "${executable}" "${archs[@]}"
} }
ThinAppFrameworks() { ThinAppFrameworks() {
...@@ -189,13 +189,15 @@ ThinAppFrameworks() { ...@@ -189,13 +189,15 @@ ThinAppFrameworks() {
local frameworks_dir="${app_path}/Frameworks" local frameworks_dir="${app_path}/Frameworks"
[[ -d "$frameworks_dir" ]] || return 0 [[ -d "$frameworks_dir" ]] || return 0
for framework_dir in "$(find "${app_path}" -type d -name "*.framework")"; do for framework_dir in $(find "${app_path}" -type d -name "*.framework"); do
ThinFramework "$framework_dir" "$ARCHS" ThinFramework "$framework_dir" "$ARCHS"
done done
} }
# Main entry point. # Main entry point.
# TODO(cbracken) improve error handling, then enable set -e
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
# Backwards-comptibility: if no args are provided, build. # Backwards-comptibility: if no args are provided, build.
BuildApp BuildApp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment