Simplify Spack cc script

This commit is contained in:
Erik Schnetter 2015-12-21 10:21:33 -05:00
parent 5d61888657
commit ba22fc8b78

289
lib/spack/env/cc vendored
View File

@ -65,7 +65,7 @@ function die {
} }
for param in $parameters; do for param in $parameters; do
if [ -z "${!param}" ]; then if [[ -z ${!param} ]]; then
die "Spack compiler must be run from spack! Input $param was missing!" die "Spack compiler must be run from spack! Input $param was missing!"
fi fi
done done
@ -114,114 +114,118 @@ case "$command" in
esac esac
# Finish setting up the mode. # Finish setting up the mode.
if [ -z "$mode" ]; then if [[ -z $mode ]]; then
mode=ccld mode=ccld
for arg in "$@"; do for arg in "$@"; do
if [ "$arg" = -v -o "$arg" = -V -o "$arg" = --version -o "$arg" = -dumpversion ]; then case "$arg" in
mode=vcheck -v|-V|--version|-dumpversion)
break mode=vcheck
elif [ "$arg" = -E ]; then break
mode=cpp ;;
break -E)
elif [ "$arg" = -c ]; then mode=cpp
mode=cc break
break ;;
elif [ "$arg" = -S ]; then -c)
mode=as mode=cc
echo "spac cc mode as" "$@" break
exit 1 ;;
break -S)
fi mode=as
break
;;
esac
done done
fi fi
# Dump the version and exist if we're in testing mode. # Dump the version and exist if we're in testing mode.
if [ "$SPACK_TEST_COMMAND" = "dump-mode" ]; then if [[ $SPACK_TEST_COMMAND = dump-mode ]]; then
echo "$mode" echo "$mode"
exit exit
fi fi
# Check that at least one of the real commands was actually selected, # Check that at least one of the real commands was actually selected,
# otherwise we don't know what to execute. # otherwise we don't know what to execute.
if [ -z "$command" ]; then if [[ -z $command ]]; then
die "ERROR: Compiler '$SPACK_COMPILER_SPEC' does not support compiling $language programs." die "ERROR: Compiler '$SPACK_COMPILER_SPEC' does not support compiling $language programs."
fi fi
# Save original command for debug logging # Save original command for debug logging
input_command="$@" input_command="$@"
args=("$@")
#
# Now do real parsing of the command line args, trying hard to keep
# non-rpath linker arguments in the proper order w.r.t. other command
# line arguments. This is important for things like groups.
#
includes=()
libraries=()
libs=()
rpaths=()
other_args=()
all_args=("$@")
while [ -n "$1" ]; do
case "$1" in
-I*)
arg="${1#-I}"
if [ -z "$arg" ]; then shift; arg="$1"; fi
includes+=("$arg")
;;
-L*)
arg="${1#-L}"
if [ -z "$arg" ]; then shift; arg="$1"; fi
libraries+=("$arg")
;;
-l*)
arg="${1#-l}"
if [ -z "$arg" ]; then shift; arg="$1"; fi
libs+=("$arg")
;;
-Wl,*)
arg="${1#-Wl,}"
if [ -z "$arg" ]; then shift; arg="$1"; fi
if [[ "$arg" = -rpath=* ]]; then
rpaths+=("${arg#-rpath=}")
elif [[ "$arg" = -rpath,* ]]; then
rpaths+=("${arg#-rpath,}")
elif [[ "$arg" = -rpath ]]; then
shift; arg="$1"
if [[ "$arg" != -Wl,* ]]; then
die "-Wl,-rpath was not followed by -Wl,*"
fi
rpaths+=("${arg#-Wl,}")
else
other_args+=("-Wl,$arg")
fi
;;
-Xlinker,*)
arg="${1#-Xlinker,}"
if [ -z "$arg" ]; then shift; arg="$1"; fi
if [[ "$arg" = -rpath=* ]]; then
rpaths+=("${arg#-rpath=}")
elif [[ "$arg" = -rpath,* ]]; then
rpaths+=("${arg#-rpath,}")
elif [[ "$arg" = -rpath ]]; then
shift; arg="$1"
if [[ "$arg" != -Xlinker,* ]]; then
die "-Xlinker,-rpath was not followed by -Xlinker,*"
fi
rpaths+=("${arg#-Xlinker,}")
else
other_args+=("-Xlinker,$arg")
fi
;;
*)
other_args+=("$1")
;;
esac
shift
done
# Dump parsed values for unit testing if asked for # Dump parsed values for unit testing if asked for
if [ -n "$SPACK_TEST_COMMAND" ]; then if [[ -n $SPACK_TEST_COMMAND ]]; then
#
# Now do real parsing of the command line args, trying hard to keep
# non-rpath linker arguments in the proper order w.r.t. other command line
# arguments. This is important for things like groups.
#
includes=()
libraries=()
libs=()
rpaths=()
other_args=()
while [[ -n $1 ]]; do
case "$1" in
-I*)
arg="${1#-I}"
if [[ -z $arg ]]; then shift; arg="$1"; fi
includes+=("$arg")
;;
-L*)
arg="${1#-L}"
if [[ -z $arg ]]; then shift; arg="$1"; fi
libraries+=("$arg")
;;
-l*)
arg="${1#-l}"
if [[ -z $arg ]]; then shift; arg="$1"; fi
libs+=("$arg")
;;
-Wl,*)
arg="${1#-Wl,}"
if [[ -z $arg ]]; then shift; arg="$1"; fi
if [[ $arg = -rpath=* ]]; then
rpaths+=("${arg#-rpath=}")
elif [[ $arg = -rpath,* ]]; then
rpaths+=("${arg#-rpath,}")
elif [[ $arg = -rpath ]]; then
shift; arg="$1"
if [[ $arg != -Wl,* ]]; then
die "-Wl,-rpath was not followed by -Wl,*"
fi
rpaths+=("${arg#-Wl,}")
else
other_args+=("-Wl,$arg")
fi
;;
-Xlinker,*)
arg="${1#-Xlinker,}"
if [[ -z $arg ]]; then shift; arg="$1"; fi
if [[ $arg = -rpath=* ]]; then
rpaths+=("${arg#-rpath=}")
elif [[ $arg = -rpath,* ]]; then
rpaths+=("${arg#-rpath,}")
elif [[ $arg = -rpath ]]; then
shift; arg="$1"
if [[ $arg != -Xlinker,* ]]; then
die "-Xlinker,-rpath was not followed by -Xlinker,*"
fi
rpaths+=("${arg#-Xlinker,}")
else
other_args+=("-Xlinker,$arg")
fi
;;
*)
other_args+=("$1")
;;
esac
shift
done
IFS=$'\n' IFS=$'\n'
case "$SPACK_TEST_COMMAND" in case "$SPACK_TEST_COMMAND" in
dump-includes) echo "${includes[*]}";; dump-includes) echo "${includes[*]}";;
@ -246,8 +250,8 @@ if [ -n "$SPACK_TEST_COMMAND" ]; then
echo "${other_args[*]}" echo "${other_args[*]}"
;; ;;
*) *)
echo "ERROR: Unknown test command" die "ERROR: Unknown test command"
exit 1 ;; ;;
esac esac
exit exit
fi fi
@ -255,66 +259,44 @@ fi
# Read spack dependencies from the path environment variable # Read spack dependencies from the path environment variable
IFS=':' read -ra deps <<< "$SPACK_DEPENDENCIES" IFS=':' read -ra deps <<< "$SPACK_DEPENDENCIES"
for dep in "${deps[@]}"; do for dep in "${deps[@]}"; do
if [ -d "$dep/include" ]; then if [[ -d $dep/include ]]; then
includes+=("$dep/include") args=("-I$dep/include" "${args[@]}")
all_args=("-I$dep/include" ${all_args[@]})
fi fi
if [ -d "$dep/lib" ]; then if [[ -d $dep/lib ]]; then
libraries+=("$dep/lib") # libraries+=("$dep/lib")
rpaths+=("$dep/lib") if [[ $mode = ccld ]]; then
if [ "$mode" = ccld ]; then args=("-L$dep/lib" "-Wl,-rpath,$dep/lib" "${args[@]}")
all_args=("-L$dep/lib" "-Wl,-rpath,$dep/lib" ${all_args[@]}) elif [[ $mode = ld ]]; then
elif [ "$mode" = ld ]; then args=("-L$dep/lib" "-rpath" "$dep/lib" "${args[@]}")
all_args=("-L$dep/lib" "-rpath" "$dep/lib" ${all_args[@]}) fi
fi
fi fi
if [ -d "$dep/lib64" ]; then if [[ -d $dep/lib64 ]]; then
libraries+=("$dep/lib64") # libraries+=("$dep/lib64")
rpaths+=("$dep/lib64") if [[ $mode = ccld ]]; then
if [ "$mode" = ccld ]; then args=("-L$dep/lib" "-Wl,-rpath,$dep/lib" "${args[@]}")
all_args=("-L$dep/lib" "-Wl,-rpath,$dep/lib" ${all_args[@]}) elif [[ $mode = ld ]]; then
elif [ "$mode" = ld ]; then args=("-L$dep/lib" "-rpath" "$dep/lib" "${args[@]}")
all_args=("-L$dep/lib" "-rpath" "$dep/lib" ${all_args[@]}) fi
fi
fi fi
done done
# Include all -L's and prefix/whatever dirs in rpath # Include all -L's and prefix/whatever dirs in rpath
for dir in "${libraries[@]}"; do if [[ $mode = ccld ]]; then
[[ dir = $SPACK_INSTALL* ]] && rpaths+=("$dir") # for dir in "${libraries[@]}"; do
if [ "$mode" = ccld ]; then # if [[ dir = $SPACK_INSTALL* ]]; then
[[ dir = $SPACK_INSTALL* ]] && all_args=("-Wl,-rpath,$dir" ${all_args[@]}) # args=("-Wl,-rpath,$dir" "${args[@]}")
elif [ "$mode" = ld ]; then # fi
[[ dir = $SPACK_INSTALL* ]] && all_args=("-rpath" "$dir" ${all_args[@]}) # done
fi args=("-Wl,-rpath,$SPACK_PREFIX/lib" "-Wl,-rpath,$SPACK_PREFIX/lib64" ${args[@]})
done elif [[ $mode = ld ]]; then
rpaths+=("$SPACK_PREFIX/lib") # for dir in "${libraries[@]}"; do
rpaths+=("$SPACK_PREFIX/lib64") # if [[ dir = $SPACK_INSTALL* ]]; then
if [ "$mode" = ccld ]; then # args=("-rpath" "$dir" "${args[@]}")
all_args=("-Wl,-rpath,$SPACK_PREFIX/lib" "-Wl,-rpath,$SPACK_PREFIX/lib64" ${all_args[@]}) # fi
elif [ "$mode" = ld ]; then # done
all_args=("-rpath" "$SPACK_PREFIX/lib" "-rpath" "$SPACK_PREFIX/lib64" ${all_args[@]}) args=("-rpath" "$SPACK_PREFIX/lib" "-rpath" "$SPACK_PREFIX/lib64" ${args[@]})
fi
# Put the arguments together
args=()
for dir in "${includes[@]}"; do args+=("-I$dir"); done
args+=("${other_args[@]}")
for dir in "${libraries[@]}"; do args+=("-L$dir"); done
for lib in "${libs[@]}"; do args+=("-l$lib"); done
if [ "$mode" = ccld ]; then
for dir in "${rpaths[@]}"; do
args+=("-Wl,-rpath")
args+=("-Wl,$dir");
done
elif [ "$mode" = ld ]; then
for dir in "${rpaths[@]}"; do
args+=("-rpath")
args+=("$dir");
done
fi fi
# #
@ -330,34 +312,29 @@ unset DYLD_LIBRARY_PATH
# #
IFS=':' read -ra env_path <<< "$PATH" IFS=':' read -ra env_path <<< "$PATH"
IFS=':' read -ra spack_env_dirs <<< "$SPACK_ENV_PATH" IFS=':' read -ra spack_env_dirs <<< "$SPACK_ENV_PATH"
spack_env_dirs+=(".") spack_env_dirs+=("" ".")
PATH="" PATH=""
for dir in "${env_path[@]}"; do for dir in "${env_path[@]}"; do
remove="" remove=""
for rm_dir in "${spack_env_dirs[@]}"; do for rm_dir in "${spack_env_dirs[@]}"; do
if [ "$dir" = "$rm_dir" ]; then remove=True; fi if [[ $dir = $rm_dir ]]; then remove=True; fi
done done
if [ -z "$remove" ]; then if [[ -z $remove ]]; then
if [ -z "$PATH" ]; then PATH="${PATH:+$PATH:}$dir"
PATH="$dir"
else
PATH="$PATH:$dir"
fi
fi fi
done done
export PATH export PATH
full_command=("$command") full_command=("$command")
# full_command+=("${args[@]}") full_command+=("${args[@]}")
full_command+=("${all_args[@]}")
# #
# Write the input and output commands to debug logs if it's asked for. # Write the input and output commands to debug logs if it's asked for.
# #
if [ "$SPACK_DEBUG" = "TRUE" ]; then if [[ $SPACK_DEBUG = TRUE ]]; then
input_log="$SPACK_DEBUG_LOG_DIR/spack-cc-$SPACK_SHORT_SPEC.in.log" input_log="$SPACK_DEBUG_LOG_DIR/spack-cc-$SPACK_SHORT_SPEC.in.log"
output_log="$SPACK_DEBUG_LOG_DIR/spack-cc-$SPACK_SHORT_SPEC.out.log" output_log="$SPACK_DEBUG_LOG_DIR/spack-cc-$SPACK_SHORT_SPEC.out.log"
echo "$input_command" >> $input_log echo "$input_command" >> $input_log
echo "$mode ${full_command[@]}" >> $output_log echo "$mode ${full_command[@]}" >> $output_log
fi fi