2013-05-02 06:40:48 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2013-05-15 07:02:16 +00:00
|
|
|
# Typical usage: cd src; ../development/tools/header_check.sh
|
|
|
|
|
2013-05-02 06:40:48 +00:00
|
|
|
# file header_check.sh
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Scott Kostyshak
|
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
|
|
|
# Description:
|
|
|
|
|
|
|
|
# All .cpp and .h files in the current directory and subdirectories
|
|
|
|
# are checked to see which include statements could be omitted without
|
|
|
|
# causing a build error. Many of these omissions would not be desired.
|
|
|
|
# For example, currently if you don't include Undo.h in Undo.cpp, there
|
|
|
|
# is no error because Undo.h is included in Cursor.h which is included
|
|
|
|
# in Undo.cpp. But clearly we do want to include Undo.h in Undo.cpp.
|
|
|
|
|
|
|
|
# The results are stored in header_check.sh.log
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
LOG_FILE="$(basename $0).log"
|
|
|
|
|
|
|
|
# For only standard headers:
|
2013-05-15 10:46:04 +00:00
|
|
|
# PATTERN='^#include <'
|
2013-05-02 06:40:48 +00:00
|
|
|
# For all headers:
|
2013-05-15 10:46:04 +00:00
|
|
|
PATTERN='^#include'
|
2013-05-02 06:40:48 +00:00
|
|
|
|
2013-05-03 05:41:43 +00:00
|
|
|
# Exclude common headers with regex
|
|
|
|
# (e.g. 'debug.h' will exclude 'support/debug.h')
|
2013-05-04 04:45:59 +00:00
|
|
|
# LyX was compiled on exotic environments and these sometimes
|
|
|
|
# require headers not needed on win/linux. So check the logs before
|
2013-05-15 07:02:16 +00:00
|
|
|
# deleting "redundant" standard libraries, Qt headers or includes around
|
|
|
|
# various ifdefs...
|
2013-05-15 10:42:13 +00:00
|
|
|
EXCLUDE='\(debug.h\|cstdio\|config.h\)'
|
2013-05-03 05:41:43 +00:00
|
|
|
|
2013-05-15 06:59:42 +00:00
|
|
|
NCORES=$(grep "CPU" /proc/cpuinfo | wc -l)
|
|
|
|
|
2013-05-02 06:40:48 +00:00
|
|
|
function BUILD_FN ()
|
|
|
|
{
|
2013-05-15 06:59:42 +00:00
|
|
|
PREFIX=''
|
|
|
|
|
2013-05-02 06:40:48 +00:00
|
|
|
# This is not a clean make.
|
2013-05-15 10:46:04 +00:00
|
|
|
make -j${NCORES} 2>/dev/null 1>/dev/null
|
2013-05-15 06:59:42 +00:00
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
|
|
|
|
# The sed regex is more strict than it needs to be.
|
|
|
|
if (( ERROR_CODE != 0 )); then
|
2013-05-15 10:42:13 +00:00
|
|
|
# Use just one core, so we don't mix outputs
|
|
|
|
IFS='' ERROR_OUTPUT=$(make 2>&1)
|
|
|
|
# Without the grep, ERROR_OUTPUT might contain messages such as:
|
|
|
|
# 2885 translated messages, 2169 fuzzy translations, 1356 untranslated messages.
|
|
|
|
ERROR_OUTPUT=$(echo "${ERROR_OUTPUT}" | grep -i "error: ")
|
|
|
|
|
2013-05-15 06:59:42 +00:00
|
|
|
cppORh=$(echo "${ERROR_OUTPUT}" | head -n 1 | \
|
|
|
|
sed 's/.*\.\(cpp\|h\):[0-9]\+:[0-9]\+: error: .*/\1/')
|
|
|
|
if [ "${cppORh}" = "cpp" ]; then
|
|
|
|
PREFIX='suspicious: '
|
|
|
|
elif [ "${cppORh}" != "h" ]; then
|
|
|
|
echo -e "Warning: the error was not parsed correctly."\
|
|
|
|
"\nThe following string was expected to be"\
|
|
|
|
"'.cpp' or '.h': \n ${cppORh}" >&2
|
2013-05-15 07:02:16 +00:00
|
|
|
echo ERROR_OUTPUT: "${ERROR_OUTPUT}"
|
|
|
|
echo cppORh: "${cppORh}"
|
2013-05-15 06:59:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
return "${ERROR_CODE}"
|
2013-05-02 06:40:48 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 07:02:16 +00:00
|
|
|
echo Making the tree first...
|
|
|
|
make -j${NCORES} 2>&1 >/dev/null || exit
|
|
|
|
|
|
|
|
echo "BUILD_FN exited without error after removing the following include statements invididually:" > "${LOG_FILE}" \
|
2013-05-02 06:40:48 +00:00
|
|
|
|| { echo "ERROR: could not create log file, ${LOG_FILE}"; exit 1; }
|
|
|
|
|
2019-07-20 21:06:05 +00:00
|
|
|
find -regex ".*\(cpp\|h\)$" | grep -vE "frontends/qt/ui_|frontends/qt/moc_" | sort |
|
2013-05-02 06:40:48 +00:00
|
|
|
while read FILE_
|
|
|
|
do
|
|
|
|
FILE_COPY=$( tempfile )
|
|
|
|
cp "${FILE_}" "${FILE_COPY}" \
|
|
|
|
|| { echo "ERROR: bu copy failed" >&2; exit 1; }
|
2013-05-15 07:02:16 +00:00
|
|
|
echo -n "processing ${FILE_}..."
|
2013-05-02 06:40:48 +00:00
|
|
|
grep "${PATTERN}" "${FILE_}" | \
|
|
|
|
while read INCLUDE
|
|
|
|
do
|
2013-05-15 07:02:16 +00:00
|
|
|
echo -n ${INCLUDE},
|
2013-05-03 05:41:43 +00:00
|
|
|
if echo "${INCLUDE}" | grep -q -v "${EXCLUDE}"; then
|
|
|
|
cp "${FILE_COPY}" "${FILE_}" \
|
|
|
|
|| { echo "ERROR: restore copy failed" >&2; exit 1; }
|
|
|
|
sed -i "s@${INCLUDE}@@" "${FILE_}"
|
2013-05-15 06:59:42 +00:00
|
|
|
|
|
|
|
BUILD_FN
|
|
|
|
BUILD_FN_RET=$?
|
|
|
|
if [ "${BUILD_FN_RET}" = 0 ]; then
|
|
|
|
echo "${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
|
|
|
|
elif [ -n "${PREFIX}" ]; then
|
2013-05-15 10:46:04 +00:00
|
|
|
if [ ${FILE_:(-2):2} == .h ]; then
|
|
|
|
echo "${PREFIX}${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
|
|
|
|
fi
|
2013-05-15 06:59:42 +00:00
|
|
|
fi
|
2013-05-03 05:41:43 +00:00
|
|
|
fi
|
2013-05-02 06:40:48 +00:00
|
|
|
done
|
2013-05-15 07:02:16 +00:00
|
|
|
echo
|
2013-05-02 06:40:48 +00:00
|
|
|
cp "${FILE_COPY}" "${FILE_}"
|
|
|
|
done
|