Skip to content

Commit

Permalink
Merge removed extended globs in bash scripts
Browse files Browse the repository at this point in the history
In an effort to support a wider range of shells (especially the bash
shell on macOS), all requested extended globs (requested via
`shopt -s`) like `globstar` and `extglob` were removed and any usage
of these extended globs replaced.

Additionally, all scripts which will be automatically executed when a
form of `make` is called were extended to include more debug
information if an error occurs.



Related PR: #280
  • Loading branch information
Thomas Grützmacher authored Mar 27, 2019
2 parents baec61a + 1a439b9 commit d05cd1a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 48 deletions.
93 changes: 70 additions & 23 deletions dev_tools/scripts/add_license.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
#!/usr/bin/env bash
shopt -s globstar
shopt -s extglob

THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )
GINKGO_ROOT_DIR="${THIS_DIR}/../.."
# ${THIS_DIR} is expected to be: ${GINKGO_ROOT_DIR}/dev_tools/scripts

# Use local paths, so there is less chance of a newline being in a path of a found file
cd "${THIS_DIR}/../.."
GINKGO_ROOT_DIR="."


LICENSE_FILE="${GINKGO_ROOT_DIR}/LICENSE"
PATTERNS="cpp|hpp|cuh|cu|hpp.in"
EXCLUDED_DIRECTORIES="build|third_party"
GINKGO_LICENSE_BEACON="******************************<GINKGO LICENSE>******************************"

echo -e "/*${GINKGO_LICENSE_BEACON}\n$(cat ${LICENSE_FILE})\n${GINKGO_LICENSE_BEACON}*/\n" > commented_license.tmp

for i in ${GINKGO_ROOT_DIR}/!(${EXCLUDED_DIRECTORIES})/**/*.@(${PATTERNS})
do
if ! grep -q "${GINKGO_LICENSE_BEACON}" $i
then
cat commented_license.tmp $i >$i.new && mv $i.new $i
else
beginning=$(grep -n "/\*${GINKGO_LICENSE_BEACON}" $i | cut -d":" -f1)
end=$(grep -n "${GINKGO_LICENSE_BEACON}.*/" $i | cut -d":" -f1)
end=$((end+1))
diff -u <(sed -n "${beginning},${end}p" $i) commented_license.tmp > diff.patch
if [ "$(cat diff.patch)" != "" ]
# These two files are temporary files which will be created (and deleted).
# Therefore, the files should not already exist.
COMMENTED_LICENSE_FILE="${THIS_DIR}/commented_license.tmp"
DIFF_FILE="${THIS_DIR}/diff.patch.tmp"

# Test if required commands are present on the system:
command -v find &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `find` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v diff &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `diff` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v patch &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `patch` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v grep &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `grep` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v sed &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `sed` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v cut &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `cut` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi


echo -e "/*${GINKGO_LICENSE_BEACON}\n$(cat ${LICENSE_FILE})\n${GINKGO_LICENSE_BEACON}*/\n" > "${COMMENTED_LICENSE_FILE}"

# Does not work if a found file (including the path) contains a newline
find "${GINKGO_ROOT_DIR}" \
! \( -name "build" -prune -o -name "third_party" -prune \) \
\( -name '*.cuh' -o -name '*.hpp' -o -name '*.hpp.in' -o -name '*.cpp' -o -name '*.cu' \) \
-type f -print \
| \
while IFS='' read -r i; do
# `grep -F` is important here because the characters in the beacon should be matched against
# and not interpreted as an expression.
if ! grep -F -q -e "${GINKGO_LICENSE_BEACON}" "${i}"
then
patch $i diff.patch
cat "${COMMENTED_LICENSE_FILE}" "${i}" >"${i}.new" && mv "${i}.new" "${i}"
else
beginning=$(grep -F -n -e "/*${GINKGO_LICENSE_BEACON}" "${i}" | cut -d":" -f1)
end=$(grep -F -n -e "${GINKGO_LICENSE_BEACON}*/" "${i}" | cut -d":" -f1)
end=$((end+1))
diff -u <(sed -n "${beginning},${end}p" "${i}") "${COMMENTED_LICENSE_FILE}" > "${DIFF_FILE}"
if [ "$(cat "${DIFF_FILE}")" != "" ]
then
patch "${i}" "${DIFF_FILE}"
fi
rm "${DIFF_FILE}"
fi
rm diff.patch
fi
done
done

rm commented_license.tmp
rm "${COMMENTED_LICENSE_FILE}"
82 changes: 57 additions & 25 deletions dev_tools/scripts/update_ginkgo_header.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
#!/usr/bin/env bash
shopt -s globstar
shopt -s extglob

PLACE_HOLDER="#PUBLIC_HEADER_PLACE_HOLDER"

THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )
INCLUDE_DIR="${THIS_DIR}/../../include"

ROOT_DIR="${THIS_DIR}/../../"
INCLUDE_DIR="${ROOT_DIR}/include"

cd ${INCLUDE_DIR}
# Use local paths, so there is less chance of a newline being in a path of a found file
cd "${INCLUDE_DIR}"
TOP_HEADER_FOLDER="."

GINKGO_HEADER_FILE="ginkgo/ginkgo.hpp"
GINKGO_HEADER_TEMPLATE_FILE="${GINKGO_HEADER_FILE}.in"

HEADER_LIST="global_includes.hpp.tmp"

# Add every header file inside the ginkgo folder to the file ${HEADER_LIST}
for file in ginkgo/**/*.hpp; do
if [ "${file}" == "${GINKGO_HEADER_FILE}" ]; then
continue
fi
echo "${file}" >> ${HEADER_LIST}
done
# Test if required commands are present on the system:
command -v find &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `find` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v sort &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `sort` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi
command -v cmp &> /dev/null
if [ ${?} -ne 0 ]; then
echo 'The command `cmp` is required for this script to work, but not supported by your system.' 1>&2
exit 1
fi

# Put all header files as a list (separated by newlines) in the file ${HEADER_LIST}
# Requires detected files (including the path) to not contain newlines
find "${TOP_HEADER_FOLDER}" -name '*.hpp' -type f -print > "${HEADER_LIST}"

if [ ${?} -ne 0 ]; then
echo 'Exiting due to an error being returned by `find`!' 1>&2
rm "${HEADER_LIST}"
exit 1
fi

# It must be a POSIX locale in order to sort according to ASCII
export LC_ALL=C
# Sorting is necessary to group them according to the folders the header are in
sort -o ${HEADER_LIST} ${HEADER_LIST}
sort -o "${HEADER_LIST}" "${HEADER_LIST}"

if [ ${?} -ne 0 ]; then
echo 'Exiting due to an error being returned by `sort`!' 1>&2
rm "${HEADER_LIST}"
exit 1
fi

# Generate a new, temporary ginkgo header file.
# It will get compared at the end to the existing file in order to prevent
Expand All @@ -37,26 +60,35 @@ GINKGO_HEADER_TMP="${GINKGO_HEADER_FILE}.tmp"

PREVIOUS_FOLDER=""
# "IFS=''" sets the word delimiters for read.
# An empty $IFS means the given name (after `read`) will be set to the whole line.
# An empty ${IFS} means the given name (after `read`) will be set to the whole line,
# and in this case it means it will not ignore leading and trailing whitespaces.
while IFS='' read -r line; do
if [ "${line}" != ${PLACE_HOLDER} ]; then
echo "${line}" >> ${GINKGO_HEADER_TMP}
if [ "${line}" != "${PLACE_HOLDER}" ]; then
echo "${line}" >> "${GINKGO_HEADER_TMP}"
else
READING_FIRST_LINE=true
while IFS='' read -r file; do
CURRENT_FOLDER=$(dirname ${file})
while IFS='' read -r prefixed_file; do
# Remove the include directory from the file name
file="${prefixed_file#${TOP_HEADER_FOLDER}/}"

# Do not include yourself
if [ "${file}" == "${GINKGO_HEADER_FILE}" ]; then
continue
fi

CURRENT_FOLDER="$(dirname ${file})"
# add newline between different include folder
if [ "${READING_FIRST_LINE}" != true ] && \
[ "${CURRENT_FOLDER}" != "${PREVIOUS_FOLDER}" ]
then
echo "" >> ${GINKGO_HEADER_TMP}
echo "" >> "${GINKGO_HEADER_TMP}"
fi
PREVIOUS_FOLDER=${CURRENT_FOLDER}
echo "#include <${file}>" >> ${GINKGO_HEADER_TMP}
PREVIOUS_FOLDER="${CURRENT_FOLDER}"
echo "#include <${file}>" >> "${GINKGO_HEADER_TMP}"
READING_FIRST_LINE=false
done < "${HEADER_LIST}"
fi
done < ${GINKGO_HEADER_TEMPLATE_FILE}
done < "${GINKGO_HEADER_TEMPLATE_FILE}"

# Use the generated file ONLY when the public header does not exist yet
# or the generated one is different to the existing one
Expand All @@ -65,7 +97,7 @@ if [ ! -f "${GINKGO_HEADER_FILE}" ] || \
then
mv "${GINKGO_HEADER_TMP}" "${GINKGO_HEADER_FILE}"
else
rm ${GINKGO_HEADER_TMP}
rm "${GINKGO_HEADER_TMP}"
fi

rm ${HEADER_LIST}
rm "${HEADER_LIST}"

0 comments on commit d05cd1a

Please sign in to comment.