]> git.lyx.org Git - lyx.git/blob - development/tools/header_check.sh
prepare Qt 5.6 builds
[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.h\)'
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         make -j${NCORES} 2>/dev/null 1>/dev/null
49         ERROR_CODE=$?
50
51
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: ")
59         
60                 cppORh=$(echo "${ERROR_OUTPUT}" | head -n 1 | \
61                         sed 's/.*\.\(cpp\|h\):[0-9]\+:[0-9]\+: error: .*/\1/')
62                 if [ "${cppORh}" = "cpp" ]; then
63                         PREFIX='suspicious: '
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}"
70                 fi
71         fi
72         return "${ERROR_CODE}"
73 }
74
75 echo Making the tree first...
76 make -j${NCORES} 2>&1 >/dev/null || exit
77
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; }
80
81 find -regex ".*\(cpp\|h\)$" |  grep -vE "frontends/qt4/ui_|frontends/qt4/moc_" | sort |
82 while read FILE_
83 do
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_}" | \
89         while read INCLUDE
90         do
91                 echo -n ${INCLUDE},
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_}"
96
97                         BUILD_FN
98                         BUILD_FN_RET=$?
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}"
104                                 fi
105                         fi
106                 fi
107         done
108         echo 
109         cp "${FILE_COPY}" "${FILE_}"
110 done