]> git.lyx.org Git - features.git/blob - development/tools/header_check.sh
* header_check.sh: Apply new changes from Scott.
[features.git] / development / tools / header_check.sh
1 #!/usr/bin/env bash
2
3 # file header_check.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # author Scott Kostyshak
8
9 # Full author contact details are available in file CREDITS
10
11 # Description:
12
13 # All .cpp and .h files in the current directory and subdirectories
14 # are checked to see which include statements could be omitted without
15 # causing a build error. Many of these omissions would not be desired.
16 # For example, currently if you don't include Undo.h in Undo.cpp, there
17 # is no error because Undo.h is included in Cursor.h which is included
18 # in Undo.cpp. But clearly we do want to include Undo.h in Undo.cpp.
19
20 # The results are stored in header_check.sh.log
21
22 set -u
23
24 LOG_FILE="$(basename $0).log"
25
26 # For only standard headers:
27   PATTERN='^#include <'
28 # For all headers:
29 # PATTERN='^#include'
30
31 # Exclude common headers with regex
32 # (e.g. 'debug.h' will exclude 'support/debug.h')
33 # LyX was compiled on exotic environments and these sometimes
34 # require headers not needed on win/linux. So check the logs before
35 # deleting "redundant" standard libraries or includes around various
36 # ifdefs...
37 EXCLUDE='\(debug.h\|cstdio\)'
38
39 NCORES=$(grep "CPU" /proc/cpuinfo | wc -l)
40
41 function BUILD_FN ()
42 {
43         PREFIX=''
44
45         # This is not a clean make.
46         IFS='' ERROR_OUTPUT=$(make -j${NCORES} 2>&1 >/dev/null)
47         ERROR_CODE=$?
48
49         # Without the grep, ERROR_OUTPUT might contain messages such as:
50         # 2885 translated messages, 2169 fuzzy translations, 1356 untranslated messages.
51         ERROR_OUTPUT=$(echo "${ERROR_OUTPUT}" | grep -i "error")
52
53         # The sed regex is more strict than it needs to be.
54         if (( ERROR_CODE != 0 )); then
55                 cppORh=$(echo "${ERROR_OUTPUT}" | head -n 1 | \
56                         sed 's/.*\.\(cpp\|h\):[0-9]\+:[0-9]\+: error: .*/\1/')
57                 if [ "${cppORh}" = "cpp" ]; then
58                         PREFIX='suspicious: '
59                 elif [ "${cppORh}" != "h" ]; then
60                         echo -e "Warning: the error was not parsed correctly."\
61                                 "\nThe following string was expected to be"\
62                                 "'.cpp' or '.h': \n ${cppORh}" >&2
63                 fi
64         fi
65         return "${ERROR_CODE}"
66 }
67
68 echo "BUILD_FN exited without error after removing
69 the following include statements invididually:" > "${LOG_FILE}" \
70 || { echo "ERROR: could not create log file, ${LOG_FILE}"; exit 1; }
71
72 find -regex ".*\(cpp\|h\)$" | \
73 while read FILE_
74 do
75         FILE_COPY=$( tempfile )
76         cp "${FILE_}" "${FILE_COPY}" \
77                 || { echo "ERROR: bu copy failed" >&2; exit 1; }
78         echo "processing ${FILE_}..."
79         grep "${PATTERN}" "${FILE_}" | \
80         while read INCLUDE
81         do
82                 if echo "${INCLUDE}" | grep -q -v "${EXCLUDE}"; then
83                         cp "${FILE_COPY}" "${FILE_}" \
84                                 || { echo "ERROR: restore copy failed" >&2; exit 1; }
85                         sed -i "s@${INCLUDE}@@" "${FILE_}"
86
87                         BUILD_FN
88                         BUILD_FN_RET=$?
89                         if [ "${BUILD_FN_RET}" = 0 ]; then
90                                 echo "${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
91                         elif [ -n "${PREFIX}" ]; then
92                                 echo "${PREFIX}${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
93                         fi
94                 fi
95         done
96         cp "${FILE_COPY}" "${FILE_}"
97 done