]> git.lyx.org Git - features.git/blob - development/tools/header_check.sh
Comment.
[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 function BUILD_FN ()
40 {
41         # This is not a clean make.
42         make -j$(grep "CPU" /proc/cpuinfo | wc -l)
43 }
44
45 echo "BUILD_FN exited without error after removing
46 the following include statements invididually:" > "${LOG_FILE}" \
47 || { echo "ERROR: could not create log file, ${LOG_FILE}"; exit 1; }
48
49 find -regex ".*\(cpp\|h\)$" | \
50 while read FILE_
51 do
52         FILE_COPY=$( tempfile )
53         cp "${FILE_}" "${FILE_COPY}" \
54                 || { echo "ERROR: bu copy failed" >&2; exit 1; }
55         echo "processing ${FILE_}..."
56         grep "${PATTERN}" "${FILE_}" | \
57         while read INCLUDE
58         do
59                 if echo "${INCLUDE}" | grep -q -v "${EXCLUDE}"; then
60                         cp "${FILE_COPY}" "${FILE_}" \
61                                 || { echo "ERROR: restore copy failed" >&2; exit 1; }
62                         sed -i "s@${INCLUDE}@@" "${FILE_}"
63                         ( BUILD_FN ) &>/dev/null && echo "${FILE_}::${INCLUDE}" >> "${LOG_FILE}"
64                 fi
65         done
66         cp "${FILE_COPY}" "${FILE_}"
67 done