Skip to content

Commit

Permalink
Switch from /bin/bash to /bin/sh since it's supported on more platforms.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 716344463
  • Loading branch information
Samuel Marks authored and copybara-github committed Jan 16, 2025
1 parent f3bec91 commit c972572
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 99 deletions.
46 changes: 25 additions & 21 deletions grain/oss/build_whl.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#!/bin/bash
#!/bin/sh
# build wheel for python version specified in $PYTHON_VERSION

set -e -x

function main() {
OUTPUT_DIR="${OUTPUT_DIR:-/tmp/grain}"

main() {
bazel clean
bazel build ... --action_env PYTHON_BIN_PATH="${PYTHON_BIN}" --action_env MACOSX_DEPLOYMENT_TARGET=11.0
bazel build ... --action_env PYTHON_BIN_PATH="${PYTHON_BIN}" --action_env MACOSX_DEPLOYMENT_TARGET='11.0'
bazel test --verbose_failures --test_output=errors ... --action_env PYTHON_BIN_PATH="${PYTHON_BIN}"

DEST="/tmp/grain/all_dist"
DEST="${OUTPUT_DIR}"'/all_dist'
mkdir -p "${DEST}"

echo "=== Destination directory: ${DEST}"
printf '=== Destination directory: %s\n' "${DEST}"

TMPDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
TMPDIR="$(mktemp -d -t tmp.XXXXXXXXXX)"

echo $(date) : "=== Using tmpdir: ${TMPDIR}"
printf '%s : "=== Using tmpdir: %s\n' "$(date)" "${TMPDIR}"

echo "=== Copy grain files"
printf "=== Copy grain files\n"

cp README.md "${TMPDIR}"
cp setup.py "${TMPDIR}"
Expand All @@ -28,27 +30,29 @@ function main() {
--exclude="*.runfiles" --exclude="*_obj" --include="*/" --exclude="*" \
bazel-bin/grain "${TMPDIR}"

pushd ${TMPDIR}
echo $(date) : "=== Building wheel"
plat_name=""
if [[ "$(uname)" == "Darwin" ]]; then
previous_wd="$(pwd)"
cd "${TMPDIR}"
printf '%s : "=== Building wheel\n' "$(date)"
if [ "$(uname)" = "Darwin" ]; then
plat_name="--plat-name macosx_11_0_$(uname -m)"
"$PYTHON_BIN" setup.py bdist_wheel --python-tag py3"${PYTHON_MINOR_VERSION}" "$plat_name"
else
"$PYTHON_BIN" setup.py bdist_wheel --python-tag py3"${PYTHON_MINOR_VERSION}"
fi

$PYTHON_BIN setup.py bdist_wheel --python-tag py3${PYTHON_MINOR_VERSION} $plat_name
cp dist/*.whl "${DEST}"
cp 'dist/'*.whl "${DEST}"

if [ -n "${AUDITWHEEL_PLATFORM}" ]; then
echo $(date) : "=== Auditing wheel"
auditwheel repair --plat ${AUDITWHEEL_PLATFORM} -w dist dist/*.whl
printf '%s : "=== Auditing wheel\n' "$(date)"
auditwheel repair --plat "${AUDITWHEEL_PLATFORM}" -w dist dist/*.whl
fi

echo $(date) : "=== Listing wheel"
ls -lrt dist/*.whl
cp dist/*.whl "${DEST}"
popd
printf '%s : "=== Listing wheel\n' "$(date)"
ls -lrt 'dist/'*.whl
cp 'dist/'*.whl "${DEST}"
cd "${previous_wd}"

echo $(date) : "=== Output wheel file is in: ${DEST}"
printf '%s : "=== Output wheel file is in: %s\n' "$(date)" "${DEST}"
}

main "$@"
50 changes: 26 additions & 24 deletions grain/oss/runner.sh
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
#!/bin/bash
#!/bin/sh

set -x -e

# Expects the source code to be under $PWD/third_party/py/grain. The wheels are
# going to be under $OUTPUT_DIR/all_dist.
SOURCE_DIR=$PWD
OUTPUT_DIR="/tmp/grain"
mkdir -p ${OUTPUT_DIR}
SOURCE_DIR="${PWD}"
OUTPUT_DIR="${OUTPUT_DIR:-/tmp/grain}"
ORIGINAL_OUTPUT_DIR="${OUTPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"

# Copybara export to OUTPUT_DIR.
copybara ${SOURCE_DIR}/third_party/py/grain/oss/copy.bara.sky local .. \
--init-history --folder-dir=$OUTPUT_DIR --ignore-noop
copybara "${SOURCE_DIR}"'/third_party/py/grain/oss/copy.bara.sky' local .. \
--init-history --folder-dir="$OUTPUT_DIR" --ignore-noop

# If BUILD_ARRAY_RECORD is set, download array_record source from github and
# build it's wheels. In such case, the wheels will also be installed as Grain's
# dependency.
if [ -n "${BUILD_ARRAY_RECORD}" ]; then
export ARRAY_RECORD_OUTPUT_DIR="/tmp/array_record"
git clone https://github.com/google/array_record ${ARRAY_RECORD_OUTPUT_DIR}
pushd ${ARRAY_RECORD_OUTPUT_DIR}
git clone https://github.com/google/array_record "${ARRAY_RECORD_OUTPUT_DIR}"
previous_wd="$(pwd)"
cd "${ARRAY_RECORD_OUTPUT_DIR}"
# Use ArrayRecord commit when build was still working.
git reset --hard ef9be1b9de19e9e9ca5c272490a2fca4afb3c4ec
git apply ${OUTPUT_DIR}/grain/oss/array_record/WORKSPACE.patch
git apply ${OUTPUT_DIR}/grain/oss/array_record/build_whl.patch
git apply ${OUTPUT_DIR}/grain/oss/array_record/runner_common.patch
git apply ${OUTPUT_DIR}/grain/oss/array_record/Dockerfile.patch
git apply ${OUTPUT_DIR}/grain/oss/array_record/setup.patch
git apply ${OUTPUT_DIR}/grain/oss/array_record/array_record_reader.patch

source ${ARRAY_RECORD_OUTPUT_DIR}/oss/runner_common.sh
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/WORKSPACE.patch'
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/build_whl.patch'
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/runner_common.patch'
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/Dockerfile.patch'
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/setup.patch'
git apply "${OUTPUT_DIR}"'/grain/oss/array_record/array_record_reader.patch'

. "${ARRAY_RECORD_OUTPUT_DIR}"'/oss/runner_common.sh'
PLATFORM="$(uname)"
if [[ "$PLATFORM" == "Darwin" ]]; then
if [ "$PLATFORM" = "Darwin" ]; then
build_and_test_array_record_macos "${ARRAY_RECORD_OUTPUT_DIR}"
else
build_and_test_array_record_linux "${ARRAY_RECORD_OUTPUT_DIR}"
fi

# array-record scripts override these variables, so we need to reset them.
popd
SOURCE_DIR=$PWD
OUTPUT_DIR="/tmp/grain"
mkdir -p ${OUTPUT_DIR}/grain/oss/array_record
cp -r ${ARRAY_RECORD_OUTPUT_DIR}/all_dist/* ${OUTPUT_DIR}/grain/oss/array_record
cd "${previous_wd}"
SOURCE_DIR="${PWD}"
OUTPUT_DIR="${ORIGINAL_OUTPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"'/grain/oss/array_record'
cp -r "${ARRAY_RECORD_OUTPUT_DIR}"'/all_dist/'* "${OUTPUT_DIR}"'/grain/oss/array_record'
fi


source ${SOURCE_DIR}/third_party/py/grain/oss/runner_common.sh
. "${SOURCE_DIR}"'/third_party/py/grain/oss/runner_common.sh'
PLATFORM="$(uname)"
if [[ "$PLATFORM" == "Darwin" ]]; then
if [ "$PLATFORM" = "Darwin" ]; then
build_and_test_grain_macos "${OUTPUT_DIR}"
else
build_and_test_grain_linux "${OUTPUT_DIR}"
Expand Down
117 changes: 63 additions & 54 deletions grain/oss/runner_common.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#!/bin/bash
#!/bin/sh

set -e -x

OUTPUT_DIR="${OUTPUT_DIR:-/tmp/grain}"
PYENV_ROOT="${PYENV_ROOT:-$HOME/.pyenv}"

# Builds Grain from source code located in SOURCE_DIR producing wheels under
# $SOURCE_DIR/all_dist.
function build_and_test_grain_linux() {
SOURCE_DIR=$1
build_and_test_grain_linux() {
SOURCE_DIR="$1"

# Automatically decide which platform to build for by checking on which
# platform this runs.
AUDITWHEEL_PLATFORM="manylinux2014_$(uname -m)"
AUDITWHEEL_PLATFORM='manylinux2014_'"$(uname -m)"

# Using a previous version of Blaze to avoid:
# https://github.com/bazelbuild/bazel/issues/8622
export BAZEL_VERSION="5.4.0"
export BAZEL_VERSION='5.4.0'

# Build wheels for multiple Python minor versions.
PYTHON_MAJOR_VERSION=3
Expand All @@ -21,73 +26,77 @@ function build_and_test_grain_linux() {
docker rmi -f grain:${PYTHON_VERSION}
docker rm -f grain
DOCKER_BUILDKIT=1 docker build --progress=plain --no-cache \
--build-arg AUDITWHEEL_PLATFORM=${AUDITWHEEL_PLATFORM} \
--build-arg PYTHON_VERSION=${PYTHON_VERSION} \
--build-arg BAZEL_VERSION=${BAZEL_VERSION} \
-t grain:${PYTHON_VERSION} ${SOURCE_DIR}/grain/oss
--build-arg AUDITWHEEL_PLATFORM="${AUDITWHEEL_PLATFORM}" \
--build-arg PYTHON_VERSION="${PYTHON_VERSION}" \
--build-arg BAZEL_VERSION="${BAZEL_VERSION}" \
-t grain:"${PYTHON_VERSION}" "${SOURCE_DIR}"'/grain/oss'

docker run --rm -a stdin -a stdout -a stderr \
--env PYTHON_VERSION=$PYTHON_MAJOR_VERSION.$PYTHON_MINOR_VERSION \
--env PYTHON_MAJOR_VERSION=${PYTHON_MAJOR_VERSION} \
--env PYTHON_MINOR_VERSION=${PYTHON_MINOR_VERSION} \
--env BAZEL_VERSION=${BAZEL_VERSION} \
--env AUDITWHEEL_PLATFORM=${AUDITWHEEL_PLATFORM} \
-v $SOURCE_DIR:/tmp/grain \
--name grain grain:${PYTHON_VERSION} \
bash grain/oss/build_whl.sh
--env PYTHON_VERSION="${PYTHON_MAJOR_VERSION}"'.'"${PYTHON_MINOR_VERSION}" \
--env PYTHON_MAJOR_VERSION="${PYTHON_MAJOR_VERSION}" \
--env PYTHON_MINOR_VERSION="${PYTHON_MINOR_VERSION}" \
--env BAZEL_VERSION="${BAZEL_VERSION}" \
--env AUDITWHEEL_PLATFORM="${AUDITWHEEL_PLATFORM}" \
-v "${SOURCE_DIR}":"${OUTPUT_DIR}" \
--name grain grain:"${PYTHON_VERSION}" \
sh grain/oss/build_whl.sh
done

ls ${SOURCE_DIR}/all_dist/*.whl
ls "${SOURCE_DIR}"'/all_dist/'*.whl
}

function install_and_init_pyenv {
pyenv_root=${1:-$HOME/.pyenv}
export PYENV_ROOT=$pyenv_root
if [[ ! -d $PYENV_ROOT ]]; then
echo "Installing pyenv.."
install_and_init_pyenv() {
pyenv_root="${1:-$HOME/.pyenv}"
export PYENV_ROOT="$pyenv_root"
if [ ! -d "$PYENV_ROOT" ]; then
printf 'Installing pyenv..\n'
git clone https://github.com/pyenv/pyenv.git "$PYENV_ROOT"
pushd "$PYENV_ROOT"
previous_wd="$(pwd)"
cd "${PYENV_ROOT}"
git checkout "v2.4.21"
popd
export PATH="/home/kbuilder/.local/bin:$PYENV_ROOT/bin:$PATH"
cd "${previous_wd}"
export PATH='/home/kbuilder/.local/bin:'"$PYENV_ROOT"'/bin:'"$PATH"
eval "$(pyenv init --path)"
fi

echo "Python setup..."
printf "Python setup...\n"
pyenv install -s "$PYENV_PYTHON_VERSION"
pyenv global "$PYENV_PYTHON_VERSION"
export PYTHON_BIN=$(pyenv which python)
PYTHON_BIN="$(pyenv which python)"
export PYTHON_BIN
}

function setup_env_vars_py {
setup_env_vars_py() {
# This controls the python binary to use.
PYTHON_MAJOR_VERSION=$1
PYTHON_MINOR_VERSION=$2
PYENV_PYTHON_VERSION=${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}
PYTHON="python$PYENV_PYTHON_VERSION"
PYTHON_MAJOR_VERSION="$1"
PYTHON_MINOR_VERSION="$2"
PYENV_PYTHON_VERSION="${PYTHON_MAJOR_VERSION}"'.'"${PYTHON_MINOR_VERSION}"
PYTHON='python'"${PYENV_PYTHON_VERSION}"
export PYTHON
}

function update_bazel_macos {
BAZEL_VERSION=$1
update_bazel_macos() {
BAZEL_VERSION="$1"
ARCH="$(uname -m)"
curl -L https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-darwin-${ARCH}.sh -O
curl -L 'https://github.com/bazelbuild/bazel/releases/download/'"${BAZEL_VERSION}"'/bazel-'"${BAZEL_VERSION}"'-installer-darwin-'"${ARCH}"'.sh' -O
ls
chmod +x bazel-*.sh
./bazel-${BAZEL_VERSION}-installer-darwin-${ARCH}.sh --user
rm -f ./bazel-${BAZEL_VERSION}-installer-darwin-${ARCH}.sh
chmod +x 'bazel-'*.sh
./'bazel-'"${BAZEL_VERSION}"'-installer-darwin-'"${ARCH}"'.sh' --user
rm -f ./'bazel-'"${BAZEL_VERSION}"'-installer-darwin-'"${ARCH}"'.sh'
# Add new bazel installation to path
export PATH="$HOME/bin:$PATH"
PATH="$HOME"'/bin:'"$PATH"
export PATH
}

function install_grain_deps {
AR_DIR="$SOURCE_DIR/grain/oss/array_record"
$PYTHON_BIN -m pip install -U --find-links=$AR_DIR array_record --no-cache-dir;
$PYTHON_BIN -m pip install -U \
install_grain_deps() {
AR_DIR="$SOURCE_DIR"'/grain/oss/array_record'
"$PYTHON_BIN" -m pip install -U --find-links="$AR_DIR" array_record --no-cache-dir;
"$PYTHON_BIN" -m pip install -U \
absl-py \
build \
cloudpickle \
dm-tree \
etils[epath,epy] \
'etils[epath,epy]' \
jaxtyping \
more-itertools>=9.1.0 \
numpy \
Expand All @@ -100,27 +109,27 @@ function install_grain_deps {
tensorflow-datasets;
}

function build_and_test_grain_macos() {
SOURCE_DIR=$1
build_and_test_grain_macos() {
SOURCE_DIR="$1"
# Set up Bazel only if not set up for Array Record build.
if [ ! -n "${BUILD_ARRAY_RECORD}" ]; then
# Using a previous version of Bazel to avoid:
# https://github.com/bazelbuild/bazel/issues/8622
export BAZEL_VERSION="5.4.0"
update_bazel_macos ${BAZEL_VERSION}
export BAZEL_VERSION='5.4.0'
update_bazel_macos "${BAZEL_VERSION}"
bazel --version
fi

PYTHON_MAJOR_VERSION=3
for PYTHON_MINOR_VERSION in 10 11 12
do
# Set up Pyenv.
PYTHON_VERSION=${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}
echo "Creating Grain wheel for Python Version $PYTHON_VERSION"
setup_env_vars_py $PYTHON_MAJOR_VERSION $PYTHON_MINOR_VERSION
install_and_init_pyenv
PYTHON_VERSION="${PYTHON_MAJOR_VERSION}"'.'"${PYTHON_MINOR_VERSION}"
printf 'Creating Grain wheel for Python Version %s\n' "$PYTHON_VERSION"
setup_env_vars_py "$PYTHON_MAJOR_VERSION" "$PYTHON_MINOR_VERSION"
install_and_init_pyenv "${PYENV_ROOT}"
install_grain_deps

bash "${SOURCE_DIR}/grain/oss/build_whl.sh"
sh "${SOURCE_DIR}/grain/oss/build_whl.sh"
done
}

0 comments on commit c972572

Please sign in to comment.