Skip to content

Commit

Permalink
Fixed unbound variable in commands and recursive symlinks (#514)
Browse files Browse the repository at this point in the history
* Fixed unbound variables

* Fixed MacOS recursive symlinks
  • Loading branch information
UebelAndre committed Feb 22, 2021
1 parent 5a09829 commit 87df6b2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions test/expected/inner_fun_text.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IFS=$'
'
local children=($(find -H "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
symlink_to_dir "$child" "$target"
done
fi
Expand All @@ -32,7 +32,7 @@ local children=($(find -H "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
if [[ "$dirname" != *.ext_build_deps ]]; then
symlink_to_dir "$child" "$target/$dirname"
fi
Expand Down
6 changes: 4 additions & 2 deletions test/expected/inner_fun_text_osx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IFS=$'
'
local children=($(find "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
symlink_to_dir "$child" "$target"
done
fi
Expand All @@ -32,8 +32,10 @@ local children=($(find "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
if [[ "$dirname" != *.ext_build_deps ]]; then
symlink_to_dir "$child" "$target/$dirname"
fi
done
else
echo "Can not copy $1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def mkdirs(path):
return "mkdir -p " + path

def if_else(condition, if_text, else_text):
return """
return """\
if [ {condition} ]; then
{if_text}
else
Expand All @@ -53,7 +53,8 @@ def define_function(name, text):

def replace_in_files(dir, from_, to_):
return FunctionAndCall(
text = """if [ -d "$1" ]; then
text = """\
if [ -d "$1" ]; then
find -L $1 -type f \\( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.cmake" \\) -exec sed -i 's@'"$2"'@'"$3"'@g' {} ';'
fi
""",
Expand All @@ -63,7 +64,8 @@ def copy_dir_contents_to_dir(source, target):
return """cp -L -r --no-target-directory "{}" "{}" """.format(source, target)

def symlink_contents_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
##symlink_to_dir## "$1" "$target"
Expand All @@ -75,15 +77,16 @@ elif [[ -d "$1" ]]; then
IFS=$'\n'
local children=($(find -H "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
##symlink_to_dir## "$child" "$target"
done
fi
"""
return FunctionAndCall(text = text)

def symlink_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
ln -s -f -t "$target" "$1"
Expand All @@ -96,7 +99,7 @@ elif [[ -d "$1" ]]; then
local children=($(find -H "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
local dirname=$(basename "$1")
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
if [[ "$dirname" != *.ext_build_deps ]]; then
##symlink_to_dir## "$child" "$target/$dirname"
fi
Expand All @@ -111,7 +114,8 @@ def script_prelude():
return "set -euo pipefail"

def increment_pkg_config_path(source):
text = """local children=$(find $1 -mindepth 1 -name '*.pc')
text = """\
local children=$(find $1 -mindepth 1 -name '*.pc')
# assume there is only one directory with pkg config
for child in $children; do
export PKG_CONFIG_PATH="$${PKG_CONFIG_PATH:-}$$:$(dirname $child)"
Expand Down Expand Up @@ -141,7 +145,8 @@ def cleanup_function(on_success, on_failure):
return FunctionAndCall(text = text, call = "trap \"cleanup_function\" EXIT")

def children_to_path(dir_):
text = """if [ -d {dir_} ]; then
text = """\
if [ -d {dir_} ]; then
local tools=$(find $EXT_BUILD_DEPS/bin -maxdepth 1 -mindepth 1)
for tool in $tools;
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def define_function(name, text):

def replace_in_files(dir, from_, to_):
return FunctionAndCall(
text = """if [ -d "$1" ]; then
text = """\
if [ -d "$1" ]; then
find -L $1 -type f \\( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.cmake" \\) -exec sed -i 's@'"$2"'@'"$3"'@g' {} ';'
fi
""",
Expand All @@ -63,7 +64,8 @@ def copy_dir_contents_to_dir(source, target):
return """cp -L -r --no-target-directory "{}" "{}" """.format(source, target)

def symlink_contents_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
##symlink_to_dir## "$1" "$target"
Expand All @@ -75,15 +77,16 @@ elif [[ -d "$1" ]]; then
IFS=$'\n'
local children=($(find -H "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
##symlink_to_dir## "$child" "$target"
done
fi
"""
return FunctionAndCall(text = text)

def symlink_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
ln -s -f -t "$target" "$1"
Expand All @@ -96,7 +99,7 @@ elif [[ -d "$1" ]]; then
IFS=$SAVEIFS
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
if [[ "$dirname" != *.ext_build_deps ]]; then
##symlink_to_dir## "$child" "$target/$dirname"
fi
Expand All @@ -111,7 +114,8 @@ def script_prelude():
return "set -euo pipefail"

def increment_pkg_config_path(source):
text = """local children=$(find $1 -mindepth 1 -name '*.pc')
text = """\
local children=$(find $1 -mindepth 1 -name '*.pc')
# assume there is only one directory with pkg config
for child in $children; do
export PKG_CONFIG_PATH="$${PKG_CONFIG_PATH:-}$$:$(dirname $child)"
Expand Down Expand Up @@ -141,7 +145,8 @@ def cleanup_function(on_success, on_failure):
return FunctionAndCall(text = text, call = "trap \"cleanup_function\" EXIT")

def children_to_path(dir_):
text = """if [ -d {dir_} ]; then
text = """\
if [ -d {dir_} ]; then
local tools=$(find $EXT_BUILD_DEPS/bin -maxdepth 1 -mindepth 1)
for tool in $tools;
do
Expand Down
26 changes: 16 additions & 10 deletions tools/build_defs/shell_toolchain/toolchains/impl/osx_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def mkdirs(path):
return "mkdir -p " + path

def if_else(condition, if_text, else_text):
return """
return """\
if [ {condition} ]; then
{if_text}
else
Expand All @@ -53,14 +53,15 @@ def define_function(name, text):

def replace_in_files(dir, from_, to_):
return FunctionAndCall(
text = """if [ -d "$1" ]; then
text = """\
if [ -d "$1" ]; then
find -L -f $1 \\( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.cmake" \\) -exec sed -i -e 's@'"$2"'@'"$3"'@g' {} ';'
fi
""",
)

def copy_dir_contents_to_dir(source, target):
text = """
text = """\
SAVEIFS=$IFS
IFS=$'\n'
local children=($(find "$1" -maxdepth 1 -mindepth 1))
Expand Down Expand Up @@ -89,7 +90,8 @@ done
return FunctionAndCall(text = text)

def symlink_contents_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
##symlink_to_dir## "$1" "$target"
Expand All @@ -101,15 +103,16 @@ elif [[ -d "$1" ]]; then
IFS=$'\n'
local children=($(find "$1" -maxdepth 1 -mindepth 1))
IFS=$SAVEIFS
for child in "${children[@]}"; do
for child in "${children[@]:-}"; do
##symlink_to_dir## "$child" "$target"
done
fi
"""
return FunctionAndCall(text = text)

def symlink_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
ln -s -f "$1" "$target"
Expand All @@ -122,8 +125,10 @@ elif [[ -d "$1" ]]; then
IFS=$SAVEIFS
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in "${children[@]}"; do
##symlink_to_dir## "$child" "$target/$dirname"
for child in "${children[@]:-}"; do
if [[ "$dirname" != *.ext_build_deps ]]; then
##symlink_to_dir## "$child" "$target/$dirname"
fi
done
else
echo "Can not copy $1"
Expand All @@ -135,7 +140,7 @@ def script_prelude():
return "set -euo pipefail"

def increment_pkg_config_path(source):
text = """
text = """\
local children=$(find $1 -mindepth 1 -name '*.pc')
# assume there is only one directory with pkg config
for child in $children; do
Expand Down Expand Up @@ -166,7 +171,8 @@ def cleanup_function(on_success, on_failure):
return FunctionAndCall(text = text, call = "trap \"cleanup_function\" EXIT")

def children_to_path(dir_):
text = """if [ -d {dir_} ]; then
text = """\
if [ -d {dir_} ]; then
local tools=$(find $EXT_BUILD_DEPS/bin -maxdepth 1 -mindepth 1)
for tool in $tools;
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def mkdirs(path):
return "mkdir -p " + path

def if_else(condition, if_text, else_text):
return """
return """\
if [ {condition} ]; then
{if_text}
else
Expand All @@ -53,7 +53,8 @@ def define_function(name, text):

def replace_in_files(dir, from_, to_):
return FunctionAndCall(
text = """if [ -d "$1" ]; then
text = """\
if [ -d "$1" ]; then
$REAL_FIND -L $1 -type f \\( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.cmake" \\) -exec sed -i 's@'"$2"'@'"$3"'@g' {} ';'
fi
""",
Expand All @@ -63,7 +64,8 @@ def copy_dir_contents_to_dir(source, target):
return """cp -L -r --no-target-directory "{}" "{}" """.format(source, target)

def symlink_contents_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
##symlink_to_dir## "$1" "$target"
Expand All @@ -83,7 +85,8 @@ fi
return FunctionAndCall(text = text)

def symlink_to_dir(source, target):
text = """local target="$2"
text = """\
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
ln -s -f -t "$target" "$1"
Expand All @@ -108,7 +111,8 @@ fi
return FunctionAndCall(text = text)

def script_prelude():
return """set -euo pipefail
return """\
set -euo pipefail
if [ -f /usr/bin/find ]; then
REAL_FIND="/usr/bin/find"
else
Expand All @@ -120,7 +124,8 @@ export SYSTEMDRIVE="C:"
"""

def increment_pkg_config_path(source):
text = """local children=$($REAL_FIND $1 -mindepth 1 -name '*.pc')
text = """\
local children=$($REAL_FIND $1 -mindepth 1 -name '*.pc')
# assume there is only one directory with pkg config
for child in $children; do
export PKG_CONFIG_PATH="$${PKG_CONFIG_PATH:-}$$:$(dirname $child)"
Expand Down Expand Up @@ -150,7 +155,8 @@ def cleanup_function(on_success, on_failure):
return FunctionAndCall(text = text, call = "trap \"cleanup_function\" EXIT")

def children_to_path(dir_):
text = """if [ -d {dir_} ]; then
text = """\
if [ -d {dir_} ]; then
local tools=$($REAL_FIND $EXT_BUILD_DEPS/bin -maxdepth 1 -mindepth 1)
for tool in $tools;
do
Expand Down

0 comments on commit 87df6b2

Please sign in to comment.