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:
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\)'
41 NCORES=$(grep "CPU" /proc/cpuinfo | wc -l)
47 # This is not a clean make.
48 IFS='' ERROR_OUTPUT=$(make -j${NCORES} 2>&1)
51 # Without the grep, ERROR_OUTPUT might contain messages such as:
52 # 2885 translated messages, 2169 fuzzy translations, 1356 untranslated messages.
53 ERROR_OUTPUT=$(echo "${ERROR_OUTPUT}" | grep -i "error: ")
55 # The sed regex is more strict than it needs to be.
56 if (( ERROR_CODE != 0 )); then
57 cppORh=$(echo "${ERROR_OUTPUT}" | head -n 1 | \
58 sed 's/.*\.\(cpp\|h\):[0-9]\+:[0-9]\+: error: .*/\1/')
59 if [ "${cppORh}" = "cpp" ]; then
61 elif [ "${cppORh}" != "h" ]; then
62 echo -e "Warning: the error was not parsed correctly."\
63 "\nThe following string was expected to be"\
64 "'.cpp' or '.h': \n ${cppORh}" >&2
65 echo ERROR_OUTPUT: "${ERROR_OUTPUT}"
66 echo cppORh: "${cppORh}"
69 return "${ERROR_CODE}"
72 echo Making the tree first...
73 make -j${NCORES} 2>&1 >/dev/null || exit
75 echo "BUILD_FN exited without error after removing the following include statements invididually:" > "${LOG_FILE}" \
76 || { echo "ERROR: could not create log file, ${LOG_FILE}"; exit 1; }
78 find -regex ".*\(cpp\|h\)$" | \
81 FILE_COPY=$( tempfile )
82 cp "${FILE_}" "${FILE_COPY}" \
83 || { echo "ERROR: bu copy failed" >&2; exit 1; }
84 echo -n "processing ${FILE_}..."
85 grep "${PATTERN}" "${FILE_}" | \
89 if echo "${INCLUDE}" | grep -q -v "${EXCLUDE}"; then
90 cp "${FILE_COPY}" "${FILE_}" \
91 || { echo "ERROR: restore copy failed" >&2; exit 1; }
92 sed -i "s@${INCLUDE}@@" "${FILE_}"
96 if [ "${BUILD_FN_RET}" = 0 ]; then
97 echo "${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
98 elif [ -n "${PREFIX}" ]; then
99 echo "${PREFIX}${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
104 cp "${FILE_COPY}" "${FILE_}"