]> git.lyx.org Git - lyx.git/blob - development/tools/header_check.sh
9b4125415845e86196cdb80b63bf951682c434e8
[lyx.git] / development / tools / header_check.sh
1 #!/usr/bin/env bash
2
3 # Typical usage: cd src; ../development/tools/header_check.sh
4
5 # file header_check.sh
6 # This file is part of LyX, the document processor.
7 # Licence details can be found in the file COPYING.
8
9 # author Scott Kostyshak
10
11 # Full author contact details are available in file CREDITS
12
13 # Description:
14
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.
21
22 # The results are stored in header_check.sh.log
23
24 set -u
25
26 LOG_FILE="$(basename $0).log"
27
28 # For only standard headers:
29   PATTERN='^#include <'
30 # For all headers:
31 # PATTERN='^#include'
32
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
38 # various ifdefs...
39 EXCLUDE='\(debug.h\|cstdio\|config\)'
40
41 NCORES=$(grep "CPU" /proc/cpuinfo | wc -l)
42
43 function BUILD_FN ()
44 {
45         PREFIX=''
46
47         # This is not a clean make.
48         IFS='' ERROR_OUTPUT=$(make -j${NCORES} 2>&1)
49         ERROR_CODE=$?
50
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: ")
54
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
60                         PREFIX='suspicious: '
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}"
67                 fi
68         fi
69         return "${ERROR_CODE}"
70 }
71
72 echo Making the tree first...
73 make -j${NCORES} 2>&1 >/dev/null || exit
74
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; }
77
78 find -regex ".*\(cpp\|h\)$" | \
79 while read FILE_
80 do
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_}" | \
86         while read INCLUDE
87         do
88                 echo -n ${INCLUDE},
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_}"
93
94                         BUILD_FN
95                         BUILD_FN_RET=$?
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}"
100                         fi
101                 fi
102         done
103         echo 
104         cp "${FILE_COPY}" "${FILE_}"
105 done