check-installed-headers.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #! /bin/sh
  2. # Copyright (C) 2016-2026 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. # <https://www.gnu.org/licenses/>.
  18. # For each installed header, confirm that it's possible to compile a
  19. # file that includes that header and does nothing else, in several
  20. # different compilation modes.
  21. # These compilation switches assume GCC or compatible, which is probably
  22. # fine since we also assume that when _building_ glibc.
  23. c_modes="-std=c89 -std=gnu89 -std=c11 -std=gnu11"
  24. cxx_modes="-std=c++98 -std=gnu++98 -std=c++11 -std=gnu++11"
  25. # An exhaustive test of feature selection macros would take far too long.
  26. # These are probably the most commonly used three.
  27. lib_modes="-D_DEFAULT_SOURCE=1 -D_GNU_SOURCE=1 -D_XOPEN_SOURCE=700"
  28. # Also check for fortify modes, since it might be enabled as default. The
  29. # maximum value to be checked is define by maximum_fortify argument.
  30. fortify_modes=""
  31. if [ $# -lt 3 ]; then
  32. echo "usage: $0 c|c++ maximum_fortify \"compile command\" header header header..." >&2
  33. exit 2
  34. fi
  35. case "$1" in
  36. (c)
  37. lang_modes="$c_modes"
  38. cih_test_c=$(mktemp ${TMPDIR-/tmp}/cih_test_XXXXXX.c)
  39. ;;
  40. (c++)
  41. lang_modes="$cxx_modes"
  42. cih_test_c=$(mktemp ${TMPDIR-/tmp}/cih_test_XXXXXX.cc)
  43. ;;
  44. (*)
  45. echo "usage: $0 c|c++ \"compile command\" header header header..." >&2
  46. exit 2;;
  47. esac
  48. shift
  49. fortify_modes=$(seq -s' ' 1 $1)
  50. shift
  51. cc_cmd="$1"
  52. shift
  53. trap "rm -f '$cih_test_c'" 0
  54. failed=0
  55. is_x86_64=unknown
  56. for header in "$@"; do
  57. # Skip various headers for which this test gets a false failure.
  58. case "$header" in
  59. # bits/* are not meant to be included directly and usually #error
  60. # out if you try it.
  61. # regexp.h is a stub containing only an #error.
  62. # Sun RPC's .x files are traditionally installed in
  63. # $prefix/include/rpcsvc, but they are not C header files.
  64. (bits/* | regexp.h | rpcsvc/*.x)
  65. continue;;
  66. # All extant versions of sys/elf.h contain nothing more than an
  67. # exhortation (either a #warning or an #error) to use sys/procfs.h
  68. # instead, plus an inclusion of that header.
  69. (sys/elf.h)
  70. continue;;
  71. # Skip Fortran headers.
  72. (finclude/*)
  73. continue;;
  74. # sys/vm86.h is "unsupported on x86-64" and errors out on that target.
  75. (sys/vm86.h)
  76. case "$is_x86_64" in
  77. (yes) continue;;
  78. (no) ;;
  79. (unknown)
  80. cat >"$cih_test_c" <<EOF
  81. #if defined __x86_64__ && __x86_64__
  82. #error "is x86-64"
  83. #endif
  84. EOF
  85. if $cc_cmd -fsyntax-only "$cih_test_c" > /dev/null 2>&1
  86. then
  87. is_x86_64=no
  88. else
  89. is_x86_64=yes
  90. continue
  91. fi
  92. ;;
  93. esac
  94. ;;
  95. esac
  96. echo :: "$header"
  97. for lang_mode in "" $lang_modes; do
  98. for lib_mode in "" $lib_modes; do
  99. for fortify_mode in "" $fortify_modes; do
  100. if [ -z "$lib_mode" ]; then
  101. expanded_lib_mode='/* default library mode */'
  102. else
  103. expanded_lib_mode=$(echo : $lib_mode | \
  104. sed 's/^: -D/#define /; s/=/ /')
  105. fi
  106. if [ ! -z $fortify_mode ]; then
  107. fortify_mode="#define _FORTIFY_SOURCE $fortify_mode"
  108. fi
  109. echo :::: $lang_mode $lib_mode $fortify_mode
  110. cat >"$cih_test_c" <<EOF
  111. /* These macros may have been defined on the command line. They are
  112. inappropriate for this test. */
  113. #undef _LIBC
  114. #undef _GNU_SOURCE
  115. #undef _FORTIFY_SOURCE
  116. $fortify_mode
  117. /* The library mode is selected here rather than on the command line to
  118. ensure that this selection wins. */
  119. $expanded_lib_mode
  120. #include <$header>
  121. int avoid_empty_translation_unit;
  122. EOF
  123. if $cc_cmd -fsyntax-only $lang_mode "$cih_test_c" 2>&1
  124. then :
  125. else failed=1
  126. fi
  127. done
  128. done
  129. done
  130. done
  131. exit $failed