check-installed-headers.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #! /bin/sh
  2. # Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. #
  5. # The GNU C Library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # The GNU C Library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with the GNU C Library; if not, see
  17. # <http://www.gnu.org/licenses/>.
  18. # Check installed headers for cleanliness. For each header, confirm
  19. # that it's possible to compile a file that includes that header and
  20. # does nothing else, in several different compilation modes. Also,
  21. # scan the header for a set of obsolete typedefs that should no longer
  22. # appear.
  23. # These compilation switches assume GCC or compatible, which is probably
  24. # fine since we also assume that when _building_ glibc.
  25. c_modes="-std=c89 -std=gnu89 -std=c11 -std=gnu11"
  26. cxx_modes="-std=c++98 -std=gnu++98 -std=c++11 -std=gnu++11"
  27. # An exhaustive test of feature selection macros would take far too long.
  28. # These are probably the most commonly used three.
  29. lib_modes="-D_DEFAULT_SOURCE=1 -D_GNU_SOURCE=1 -D_XOPEN_SOURCE=700"
  30. # sys/types.h+bits/types.h have to define the obsolete types.
  31. # rpc(svc)/* have the obsolete types too deeply embedded in their API
  32. # to remove.
  33. skip_obsolete_type_check='*/sys/types.h|*/bits/types.h|*/rpc/*|*/rpcsvc/*'
  34. obsolete_type_re=\
  35. '\<((__)?(quad_t|u(short|int|long|_(char|short|int([0-9]+_t)?|long|quad_t))))\>'
  36. if [ $# -lt 3 ]; then
  37. echo "usage: $0 c|c++ \"compile command\" header header header..." >&2
  38. exit 2
  39. fi
  40. case "$1" in
  41. (c)
  42. lang_modes="$c_modes"
  43. cih_test_c=$(mktemp ${TMPDIR-/tmp}/cih_test_XXXXXX.c)
  44. already="$skip_obsolete_type_check"
  45. ;;
  46. (c++)
  47. lang_modes="$cxx_modes"
  48. cih_test_c=$(mktemp ${TMPDIR-/tmp}/cih_test_XXXXXX.cc)
  49. # The obsolete-type check can be skipped for C++; it is
  50. # sufficient to do it for C.
  51. already="*"
  52. ;;
  53. (*)
  54. echo "usage: $0 c|c++ \"compile command\" header header header..." >&2
  55. exit 2;;
  56. esac
  57. shift
  58. cc_cmd="$1"
  59. shift
  60. trap "rm -f '$cih_test_c'" 0
  61. failed=0
  62. is_x86_64=unknown
  63. is_x32=unknown
  64. for header in "$@"; do
  65. # Skip various headers for which this test gets a false failure.
  66. case "$header" in
  67. # bits/* are not meant to be included directly and usually #error
  68. # out if you try it.
  69. # regexp.h is a stub containing only an #error.
  70. # Sun RPC's .x files are traditionally installed in
  71. # $prefix/include/rpcsvc, but they are not C header files.
  72. (bits/* | regexp.h | rpcsvc/*.x)
  73. continue;;
  74. # All extant versions of sys/elf.h contain nothing more than an
  75. # exhortation (either a #warning or an #error) to use sys/procfs.h
  76. # instead, plus an inclusion of that header.
  77. (sys/elf.h)
  78. continue;;
  79. # sys/sysctl.h is unsupported for x32.
  80. (sys/sysctl.h)
  81. case "$is_x32" in
  82. (yes) continue;;
  83. (no) ;;
  84. (unknown)
  85. cat >"$cih_test_c" <<EOF
  86. #if defined __x86_64__ && defined __ILP32__
  87. # error "is x32"
  88. #endif
  89. EOF
  90. if $cc_cmd -fsyntax-only "$cih_test_c" > /dev/null 2>&1
  91. then
  92. is_x32=no
  93. else
  94. is_x32=yes
  95. continue
  96. fi
  97. ;;
  98. esac
  99. ;;
  100. # sys/vm86.h is "unsupported on x86-64" and errors out on that target.
  101. (sys/vm86.h)
  102. case "$is_x86_64" in
  103. (yes) continue;;
  104. (no) ;;
  105. (unknown)
  106. cat >"$cih_test_c" <<EOF
  107. #if defined __x86_64__ && __x86_64__
  108. #error "is x86-64"
  109. #endif
  110. EOF
  111. if $cc_cmd -fsyntax-only "$cih_test_c" > /dev/null 2>&1
  112. then
  113. is_x86_64=no
  114. else
  115. is_x86_64=yes
  116. continue
  117. fi
  118. ;;
  119. esac
  120. ;;
  121. esac
  122. echo :: "$header"
  123. for lang_mode in "" $lang_modes; do
  124. for lib_mode in "" $lib_modes; do
  125. echo :::: $lang_mode $lib_mode
  126. if [ -z "$lib_mode" ]; then
  127. expanded_lib_mode='/* default library mode */'
  128. else
  129. expanded_lib_mode=$(echo : $lib_mode | \
  130. sed 's/^: -D/#define /; s/=/ /')
  131. fi
  132. cat >"$cih_test_c" <<EOF
  133. /* These macros may have been defined on the command line. They are
  134. inappropriate for this test. */
  135. #undef _LIBC
  136. #undef _GNU_SOURCE
  137. /* The library mode is selected here rather than on the command line to
  138. ensure that this selection wins. */
  139. $expanded_lib_mode
  140. #include <$header>
  141. int avoid_empty_translation_unit;
  142. EOF
  143. if $cc_cmd -fsyntax-only $lang_mode "$cih_test_c" 2>&1
  144. then
  145. includes=$($cc_cmd -fsyntax-only -H $lang_mode \
  146. "$cih_test_c" 2>&1 | sed -ne 's/^[.][.]* //p')
  147. for h in $includes; do
  148. # Don't repeat work.
  149. eval 'case "$h" in ('"$already"') continue;; esac'
  150. if grep -qE "$obsolete_type_re" "$h"; then
  151. echo "*** Obsolete types detected:"
  152. grep -HE "$obsolete_type_re" "$h"
  153. failed=1
  154. fi
  155. already="$already|$h"
  156. done
  157. else
  158. failed=1
  159. fi
  160. done
  161. done
  162. done
  163. exit $failed