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