bazelbuild / rules_foreign_cc

Build rules for interfacing with "foreign" (non-Bazel) build systems (CMake, configure-make, GNU Make, boost, ninja, Meson)
https://bazelbuild.github.io/rules_foreign_cc
Apache License 2.0
649 stars 232 forks source link

EXT_BUILD_ROOT in generated build_script.sh references wrong directory #1147

Closed vymao closed 6 months ago

vymao commented 6 months ago

I am debugging an issue with a generated bash script when trying to build a cmake project. More specifically, the file has the path of the following: bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext_foreign_cc/build_script.sh.

If I try running this, I get the following error:

Can not copy /Users/victor/Desktop/Morris/cpp/external/cmake-3.26.4-macos-universal/bin/cmake
+ /Users/victor/Desktop/Morris/cpp/external/cmake-3.26.4-macos-universal/bin/cmake '-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64' -DCMAKE_OSX_DEPLOYMENT_TARGET=13.5 -DOCOS_ONNXRUNTIME_VERSION=1.16.2 -DCMAKE_TOOLCHAIN_FILE=/Users/victor/Desktop/Morris/cpp/bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext.build_tmpdir/crosstool_bazel.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/Users/victor/Desktop/Morris/cpp/bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext -DCMAKE_PREFIX_PATH=/Users/victor/Desktop/Morris/cpp/bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext.ext_build_deps -DCMAKE_RANLIB= -G 'Unix Makefiles' /Users/victor/Desktop/Morris/cpp/external/onnx_runtime_extensions
bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext_foreign_cc/build_script.sh: line 114: /Users/victor/Desktop/Morris/cpp/external/cmake-3.26.4-macos-universal/bin/cmake: No such file or directory

If I look at the first 114 lines, I get the following:

#!/usr/bin/env bash
function symlink_to_dir() {
if [[ -z "$1" ]]; then
echo "arg 1 to symlink_to_dir is unexpectedly empty"
exit 1
fi
if [[ -z "$2" ]]; then
echo "arg 2 to symlink_to_dir is unexpectedly empty"
exit 1
fi
local target="$2"
mkdir -p "$target"
local replace_in_files="${3:-}"
if [[ -f "$1" ]]; then
# In order to be able to use `replace_in_files`, we ensure that we create copies of specfieid
# files so updating them is possible.
if [[ "$1" == *.pc || "$1" == *.la || "$1" == *-config || "$1" == *.mk || "$1" == *.cmake ]]; then
dest="$target/$(basename "$1")"
cp "$1" "$dest" && chmod +w "$dest" && touch -r "$1" "$dest"
else
ln -s -f "$1" "$target"
fi
elif [[ -L "$1" && ! -d "$1" ]]; then
cp -pR "$1" "$2"
elif [[ -d "$1" ]]; then

# If not replacing in files, simply create a symbolic link rather than traversing tree of files, which can result in very slow builds
if [[ "$replace_in_files" = False ]]; then
ln -s -f "$1" "$target"
return
fi

SAVEIFS=$IFS
IFS=$'
'
local children=($(find "$1/" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in "${children[@]:-}"; do
if [[ -n "$child" && "$dirname" != *.ext_build_deps ]]; then
symlink_to_dir "$child" "$target/$dirname" "$replace_in_files"
fi
done
else
echo "Can not copy $1"
fi
}
function replace_in_files() {
if [ -d "$1" ]; then
SAVEIFS=$IFS
IFS=$'
'
# Find all real files. Symlinks are assumed to be relative to something within the directory we're seaching and thus ignored
local files=($(find -P -f "$1" \( -type f -and \( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.mk" -or -name "*.cmake" \) \)))
IFS=$SAVEIFS
for file in ${files[@]+"${files[@]}"}; do
local backup=$(mktemp)
touch -r "${file}" "${backup}"
/usr/bin/sed -i '' -e 's@'"$2"'@'"$3"'@g' "${file}"
if [[ "$?" -ne "0" ]]; then
exit 1
fi
touch -r "${backup}" "${file}"
rm "${backup}"
done
fi
}
echo """"
echo ""Bazel external C/C++ Rules. Building library 'onnx_runtime_ext'""
echo """"
set -euo pipefail
export EXT_BUILD_ROOT=$(pwd)
export INSTALLDIR=$EXT_BUILD_ROOT/bazel-out/darwin-fastbuild/bin/external/onnx_runtime_extensions/onnx_runtime_ext
export BUILD_TMPDIR=$INSTALLDIR.build_tmpdir
export EXT_BUILD_DEPS=$INSTALLDIR.ext_build_deps
export PATH="$EXT_BUILD_ROOT:$PATH"
rm -rf $BUILD_TMPDIR
rm -rf $EXT_BUILD_DEPS
mkdir -p $INSTALLDIR
mkdir -p $BUILD_TMPDIR
mkdir -p $EXT_BUILD_DEPS
echo ""Environment:______________""
env
echo ""__________________________""
mkdir -p $EXT_BUILD_DEPS/bin
symlink_to_dir $EXT_BUILD_ROOT/external/cmake-3.26.4-macos-universal/bin/cmake $EXT_BUILD_DEPS/bin/ False
symlink_to_dir $EXT_BUILD_ROOT/bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make $EXT_BUILD_DEPS/bin/ False
export PATH="$EXT_BUILD_DEPS/bin:$PATH"
cd $BUILD_TMPDIR
__var_CMAKE_AR="/usr/bin/libtool"
__var_CMAKE_ASM_FLAGS_INIT="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=\\\\\\\"redacted\\\\\\\" -D__TIMESTAMP__=\\\\\\\"redacted\\\\\\\" -D__TIME__=\\\\\\\"redacted\\\\\\\""
__var_CMAKE_CXX_ARCHIVE_CREATE="<CMAKE_AR> -static -s -o <TARGET> <OBJECTS>"
__var_CMAKE_CXX_COMPILER="${EXT_BUILD_ROOT//\\//}/external/local_config_cc/cc_wrapper.sh"
__var_CMAKE_CXX_FLAGS_INIT="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer -std=c++14 -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=\\\\\\\"redacted\\\\\\\" -D__TIMESTAMP__=\\\\\\\"redacted\\\\\\\" -D__TIME__=\\\\\\\"redacted\\\\\\\" --std=c++17"
__var_CMAKE_C_ARCHIVE_CREATE="<CMAKE_AR> -static -s -o <TARGET> <OBJECTS>"
__var_CMAKE_C_COMPILER="${EXT_BUILD_ROOT//\\//}/external/local_config_cc/cc_wrapper.sh"
__var_CMAKE_C_FLAGS_INIT="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=\\\\\\\"redacted\\\\\\\" -D__TIMESTAMP__=\\\\\\\"redacted\\\\\\\" -D__TIME__=\\\\\\\"redacted\\\\\\\" -fPIC"
__var_CMAKE_EXE_LINKER_FLAGS_INIT="-Wl,-S -undefined dynamic_lookup -headerpad_max_install_names -lc++ -lm"
__var_CMAKE_SHARED_LINKER_FLAGS_INIT="-shared -Wl,-S -undefined dynamic_lookup -headerpad_max_install_names -lc++ -lm"
cat > crosstool_bazel.cmake << EOF
set(CMAKE_AR "$__var_CMAKE_AR" CACHE FILEPATH "Archiver")
set(CMAKE_ASM_FLAGS_INIT "$__var_CMAKE_ASM_FLAGS_INIT")
set(CMAKE_CXX_ARCHIVE_CREATE "$__var_CMAKE_CXX_ARCHIVE_CREATE")
set(CMAKE_CXX_COMPILER "$__var_CMAKE_CXX_COMPILER")
set(CMAKE_CXX_FLAGS_INIT "$__var_CMAKE_CXX_FLAGS_INIT")
set(CMAKE_C_ARCHIVE_CREATE "$__var_CMAKE_C_ARCHIVE_CREATE")
set(CMAKE_C_COMPILER "$__var_CMAKE_C_COMPILER")
set(CMAKE_C_FLAGS_INIT "$__var_CMAKE_C_FLAGS_INIT")
set(CMAKE_EXE_LINKER_FLAGS_INIT "$__var_CMAKE_EXE_LINKER_FLAGS_INIT")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "$__var_CMAKE_SHARED_LINKER_FLAGS_INIT")
EOF
set -x
$EXT_BUILD_ROOT/external/cmake-3.26.4-macos-universal/bin/cmake -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DCMAKE_OSX_DEPLOYMENT_TARGET="13.5" -DOCOS_ONNXRUNTIME_VERSION="1.16.2" -DCMAKE_TOOLCHAIN_FILE="$BUILD_TMPDIR/crosstool_bazel.cmake" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_INSTALL_PREFIX="$INSTALLDIR" -DCMAKE_PREFIX_PATH="$EXT_BUILD_DEPS" -DCMAKE_RANLIB=""  -G 'Unix Makefiles' $EXT_BUILD_ROOT/external/onnx_runtime_extensions

It seems to be complaining about $EXT_BUILD_ROOT, which seems to come from this line: export EXT_BUILD_ROOT=$(pwd). This seems to set EXT_BUILD_ROOT to the directory from which I run bazel build, which doesn't seem right? I expect it to be perhaps EXT_BUILD_ROOT=$(pwd)/bazel-cpp instead or something.

Is this a bug?

jsharpe commented 6 months ago

The script is designed to be run inside the execroot / sandbox which is what happens when you execute bazel. If you want to reproduce this by hand you need to run with --sandbox_debug which will leave the sandbox directory in place and you can then copy the command that is run in order to execute the script locally.

vymao commented 6 months ago

Got it, thanks