3 # Typical usage: cd src; ../development/tools/header_check.sh
6 # This file is part of LyX, the document processor.
7 # Licence details can be found in the file COPYING.
9 # author Scott Kostyshak
11 # Full author contact details are available in file CREDITS
15 # All .cpp and .h files in the current directory and subdirectories
16 # are checked to see which include statements could be omitted without
17 # causing a build error. Many of these omissions would not be desired.
18 # For example, currently if you don't include Undo.h in Undo.cpp, there
19 # is no error because Undo.h is included in Cursor.h which is included
20 # in Undo.cpp. But clearly we do want to include Undo.h in Undo.cpp.
22 # The results are stored in header_check.sh.log
26 LOG_FILE="$(basename $0).log"
28 # For only standard headers:
29 # PATTERN='^#include <'
33 # Exclude common headers with regex
34 # (e.g. 'debug.h' will exclude 'support/debug.h')
35 # LyX was compiled on exotic environments and these sometimes
36 # require headers not needed on win/linux. So check the logs before
37 # deleting "redundant" standard libraries, Qt headers or includes around
39 EXCLUDE='\(debug.h\|cstdio\|config.h\)'
41 NCORES=$(grep "CPU" /proc/cpuinfo | wc -l)
47 # This is not a clean make.
48 make -j${NCORES} 2>/dev/null 1>/dev/null
52 # The sed regex is more strict than it needs to be.
53 if (( ERROR_CODE != 0 )); then
54 # Use just one core, so we don't mix outputs
55 IFS='' ERROR_OUTPUT=$(make 2>&1)
56 # Without the grep, ERROR_OUTPUT might contain messages such as:
57 # 2885 translated messages, 2169 fuzzy translations, 1356 untranslated messages.
58 ERROR_OUTPUT=$(echo "${ERROR_OUTPUT}" | grep -i "error: ")
60 cppORh=$(echo "${ERROR_OUTPUT}" | head -n 1 | \
61 sed 's/.*\.\(cpp\|h\):[0-9]\+:[0-9]\+: error: .*/\1/')
62 if [ "${cppORh}" = "cpp" ]; then
64 elif [ "${cppORh}" != "h" ]; then
65 echo -e "Warning: the error was not parsed correctly."\
66 "\nThe following string was expected to be"\
67 "'.cpp' or '.h': \n ${cppORh}" >&2
68 echo ERROR_OUTPUT: "${ERROR_OUTPUT}"
69 echo cppORh: "${cppORh}"
72 return "${ERROR_CODE}"
75 echo Making the tree first...
76 make -j${NCORES} 2>&1 >/dev/null || exit
78 echo "BUILD_FN exited without error after removing the following include statements invididually:" > "${LOG_FILE}" \
79 || { echo "ERROR: could not create log file, ${LOG_FILE}"; exit 1; }
81 find -regex ".*\(cpp\|h\)$" | grep -vE "frontends/qt/ui_|frontends/qt/moc_" | sort |
84 FILE_COPY=$( tempfile )
85 cp "${FILE_}" "${FILE_COPY}" \
86 || { echo "ERROR: bu copy failed" >&2; exit 1; }
87 echo -n "processing ${FILE_}..."
88 grep "${PATTERN}" "${FILE_}" | \
92 if echo "${INCLUDE}" | grep -q -v "${EXCLUDE}"; then
93 cp "${FILE_COPY}" "${FILE_}" \
94 || { echo "ERROR: restore copy failed" >&2; exit 1; }
95 sed -i "s@${INCLUDE}@@" "${FILE_}"
99 if [ "${BUILD_FN_RET}" = 0 ]; then
100 echo "${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
101 elif [ -n "${PREFIX}" ]; then
102 if [ ${FILE_:(-2):2} == .h ]; then
103 echo "${PREFIX}${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
109 cp "${FILE_COPY}" "${FILE_}"