Skip to content

Instantly share code, notes, and snippets.

@bjacob
Created December 15, 2020 21:30
Show Gist options
  • Save bjacob/c40c52c34cc6e7d44e42f0ffee1aedaa to your computer and use it in GitHub Desktop.
Save bjacob/c40c52c34cc6e7d44e42f0ffee1aedaa to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# A CMake wrapper for concise command lines and fewer stateful surprises.
set -e
# This syntax allows the user to override this by defining environment variables.
: ${CC:=clang}
: ${CXX:=clang++}
if [[ $# < 1 || $1 == "-h" || $1 == "--help" ]]
then
echo "Usage: $(basename $0) [source_dir] [list of configuration options] [build [list of targets]]"
echo
echo "Runs CMake to perform configuration and/or build."
echo
echo "Examples:"
echo
echo " Configure and build with ASan, CCache, and Tracy. Note how 'build' comes last!"
echo " $(basename $0) ../iree asan ccache tracy build"
echo
echo " Configure and build and Android NDK build, passing the NDK path. Also with CCache and Tracy."
echo " $(basename $0) ../iree ndk ~/android-ndk-r21d ccache tracy build"
echo
echo " Configure a Debug build, build only iree_tools_iree-translate:"
echo " $(basename $0) ../iree Debug build iree_tools_iree-translate"
echo
echo " Configure, but do not build, a Release build with Python bindings and TensorFlow compiler:"
echo " $(basename $0) ../iree Release py tf"
echo
echo " Do not configure, just build iree_tools_iree-translate"
echo " $(basename $0) build iree_tools_iree-translate"
echo
echo " Configure and build a build with ASan and CCache, only build iree_tools_iree-translate:"
exit 1
fi
if [[ -f "CMakeLists.txt" ]]
then
echo "Error: We seem to be in a source directory. Please cd into a build directory."
exit 1
fi
# The first argument should be either the source directory (for a configure
# command) or just 'build'.
if [[ "$1" != build ]]
then
source_dir="$1"
shift
fi
if [[ ! -z "${source_dir}" && ! -f "${source_dir}/CMakeLists.txt" ]]
then
echo "Error: ${source_dir} does not look like a source directory: it should contain CMakeLists.txt"
exit 1
fi
# Sane default build type
# TODO: upstream this sane default to IREE's CMakeLists
arg_build_type=RelWithDebInfo
# Parse command-line args
args=("$@")
for (( i=0; i < $#; i++ ))
do
case "${args[i]}" in
ccache) arg_ccache=1;;
asan) arg_asan=1;;
msan) arg_msan=1;;
tsan) arg_tsan=1;;
py) arg_py=1;;
tf) arg_tf=1;;
tflite) arg_tflite=1;;
xla) arg_xla=1;;
tracy) arg_tracy=1;;
docs) arg_docs=1;;
Debug|Release|RelWithDebInfo|MinSizeRel) arg_build_type="${args[i]}";;
ndk) arg_ndk="${args[$((i+1))]}"; i=$((i+1));;
build)
arg_build=1
if [[ $i < $(($# - 1)) ]]
then
arg_targets="${args[@]:$((i+1))}"
fi
break;;
*) echo "Error: unkown argument ${args[i]}"; exit 1;;
esac
done
# Detect Linux builds (not including Android)
if [[ "$OSTYPE" == "linux-gnu"* && -z "${arg_ndk}" ]]
then
is_linux_build=1
fi
# Build a list of CMake variables to set.
cmake_var_names=()
cmake_var_values=()
function add_cmake_var() {
if [[ ! -z "$1" ]]
then
cmake_var_names+=("$2")
cmake_var_values+=("$3")
fi
}
add_cmake_var "${arg_build_type}" CMAKE_BUILD_TYPE "${arg_build_type}"
add_cmake_var "${arg_ndk}" CMAKE_TOOLCHAIN_FILE "${arg_ndk}/build/cmake/android.toolchain.cmake"
add_cmake_var "${arg_ndk}" ANDROID_ABI "arm64-v8a"
add_cmake_var "${arg_ndk}" ANDROID_PLATFORM android-29
add_cmake_var "${arg_ndk}" IREE_HOST_C_COMPILER "$(which "$CC")"
add_cmake_var "${arg_ndk}" IREE_HOST_CXX_COMPILER "$(which "$CXX")"
add_cmake_var "${arg_ccache}" IREE_ENABLE_CCACHE ON
add_cmake_var "${arg_ccache}" LLVM_CCACHE_BUILD ON
add_cmake_var "${arg_asan}" IREE_ENABLE_ASAN ON
add_cmake_var "${arg_msan}" IREE_ENABLE_MSAN ON
add_cmake_var "${arg_tsan}" IREE_ENABLE_TSAN ON
add_cmake_var "${arg_tf}" IREE_BUILD_TENSORFLOW_COMPILER ON
add_cmake_var "${arg_tflite}" IREE_BUILD_TFLITE_COMPILER ON
add_cmake_var "${arg_xla}" IREE_BUILD_XLA_COMPILER ON
add_cmake_var "${arg_py}" IREE_BUILD_PYTHON_BINDINGS ON
add_cmake_var "${arg_tracy}" IREE_ENABLE_RUNTIME_TRACING ON
# On Linux, the default choice of compiler might be GCC, so we need to override
# that.
add_cmake_var "${is_linux_build}" CMAKE_C_COMPILER "$(which "$CC")"
add_cmake_var "${is_linux_build}" CMAKE_CXX_COMPILER "$(which "$CXX")"
# Build the CMake configure command line.
num_cmake_vars=${#cmake_var_names[@]}
if [[ ! -z "${source_dir}" && "$num_cmake_vars" > 0 ]]
then
configure_cmdline="cmake ${source_dir} -G Ninja"
for (( i = 0; i < $num_cmake_vars; i++ ))
do
configure_cmdline+=$' \\\n'" -D${cmake_var_names[$i]}=${cmake_var_values[$i]}"
done
echo "CMake configuration command line:"
echo
echo "$configure_cmdline"
echo
fi
# Build the CMake build command line.
if [[ ! -z "${arg_build}" ]]
then
build_cmdline="cmake --build ."
if [[ ! -z "${arg_targets}" ]]
then
build_cmdline+=$' \\\n'" --target ${arg_targets}"
fi
# The CMake build may invoke Bazel to build some directory such as
# Tensorflow. On Linux, this may require overriding the default choice of
# compiler, GCC.
if [[ "${is_linux_build}" == 1 ]]
then
build_cmdline="CC=${CC} CXX=${CXX} ${build_cmdline}"
fi
echo "CMake build command line:"
echo
echo "$build_cmdline"
echo
fi
# Run configure.
if [[ ! -z "configure_cmdline" ]]
then
eval "$configure_cmdline"
# Check if CMakeCache.txt has what we requested.
for (( i = 0; i < $num_cmake_vars; i++ ))
do
if ! grep -xq "${cmake_var_names[$i]}\b.*=${cmake_var_values[$i]}" "CMakeCache.txt"
then
echo "Error: After running CMake, CMakeCache.txt does not have ${cmake_var_names[$i]} set to ${cmake_var_values[$i]} as requested."
echo "Suggestion: rm CMakeCache.txt and rerun this script."
exit 1
fi
done
fi
# Run build.
if [[ ! -z "build_cmdline" ]]
then
eval "$build_cmdline"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment