fixinc.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #!/bin/sh
  2. #
  3. # Install modified versions of certain ANSI-incompatible system header
  4. # files which are fixed to work correctly with ANSI C and placed in a
  5. # directory that GCC will search.
  6. #
  7. # See README-fixinc for more information.
  8. #
  9. # fixincludes copyright (c) 1998, 1999, 2000, 2002, 2009
  10. # The Free Software Foundation, Inc.
  11. #
  12. # fixincludes is free software.
  13. #
  14. # You may redistribute it and/or modify it under the terms of the
  15. # GNU General Public License, as published by the Free Software
  16. # Foundation; either version 3, or (at your option) any later version.
  17. #
  18. # fixincludes is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. # See the GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with fixincludes; see the file COPYING3. If not see
  25. # <http://www.gnu.org/licenses/>.
  26. #
  27. # # # # # # # # # # # # # # # # # # # # #
  28. # Usage: fixinc.sh output-dir input-dir
  29. #
  30. # Directory in which to store the results.
  31. # Fail if no arg to specify a directory for the output.
  32. if [ "x$1" = "x" ]
  33. then
  34. echo fixincludes: no output directory specified
  35. exit 1
  36. fi
  37. LIB=${1}
  38. shift
  39. # Make sure it exists.
  40. if [ ! -d $LIB ]; then
  41. mkdir -p $LIB || {
  42. echo fixincludes: output dir '`'$LIB"' cannot be created"
  43. exit 1
  44. }
  45. else
  46. ( cd $LIB && touch DONE && rm DONE ) || {
  47. echo fixincludes: output dir '`'$LIB"' is an invalid directory"
  48. exit 1
  49. }
  50. fi
  51. if test -z "$VERBOSE"
  52. then
  53. VERBOSE=2
  54. export VERBOSE
  55. else
  56. case "$VERBOSE" in
  57. [0-9] ) : ;;
  58. * ) VERBOSE=3 ;;
  59. esac
  60. fi
  61. # Define what target system we're fixing.
  62. #
  63. if test -r ./Makefile; then
  64. target_canonical="`sed -n -e 's,^target[ ]*=[ ]*\(.*\)$,\1,p' < Makefile`"
  65. fi
  66. # If not from the Makefile, then try config.guess
  67. #
  68. if test -z "${target_canonical}" ; then
  69. if test -x ./config.guess ; then
  70. target_canonical="`config.guess`" ; fi
  71. test -z "${target_canonical}" && target_canonical=unknown
  72. fi
  73. export target_canonical
  74. # # # # # # # # # # # # # # # # # # # # #
  75. #
  76. # Define PWDCMD as a command to use to get the working dir
  77. # in the form that we want.
  78. PWDCMD=${PWDCMD-pwd}
  79. case "`$PWDCMD`" in
  80. //*)
  81. # On an Apollo, discard everything before `/usr'.
  82. PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'"
  83. ;;
  84. esac
  85. # Original directory.
  86. ORIGDIR=`${PWDCMD}`
  87. export ORIGDIR
  88. FIXINCL=`${PWDCMD}`/fixincl
  89. if [ ! -x $FIXINCL ] ; then
  90. echo "Cannot find fixincl" >&2
  91. exit 1
  92. fi
  93. export FIXINCL
  94. # Make LIB absolute only if needed to avoid problems with the amd.
  95. case $LIB in
  96. /*)
  97. ;;
  98. *)
  99. cd $LIB; LIB=`${PWDCMD}`
  100. ;;
  101. esac
  102. if test $VERBOSE -gt 0
  103. then echo Fixing headers into ${LIB} for ${target_canonical} target ; fi
  104. # Determine whether this system has symbolic links.
  105. if test -n "$DJDIR"; then
  106. LINKS=false
  107. elif ln -s X $LIB/ShouldNotExist 2>/dev/null; then
  108. rm -f $LIB/ShouldNotExist
  109. LINKS=true
  110. elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
  111. rm -f /tmp/ShouldNotExist
  112. LINKS=true
  113. else
  114. LINKS=false
  115. fi
  116. # # # # # # # # # # # # # # # # # # # # #
  117. #
  118. # Check to see if the machine_name fix needs to be disabled.
  119. #
  120. # On some platforms, machine_name doesn't work properly and
  121. # breaks some of the header files. Since everything works
  122. # properly without it, just wipe the macro list to
  123. # disable the fix.
  124. case "${target_canonical}" in
  125. *-*-vxworks* | powerpc*-*-linux*)
  126. test -f ${MACRO_LIST} && echo > ${MACRO_LIST}
  127. ;;
  128. esac
  129. # # # # # # # # # # # # # # # # # # # # #
  130. #
  131. # In the file macro_list are listed all the predefined
  132. # macros that are not in the C89 reserved namespace (the reserved
  133. # namespace is all identifiers beginnning with two underscores or one
  134. # underscore followed by a capital letter). A regular expression to find
  135. # any of those macros in a header file is written to MN_NAME_PAT.
  136. #
  137. # Note dependency on ASCII. \012 = newline.
  138. # tr ' ' '\n' is, alas, not portable.
  139. if test -s ${MACRO_LIST}
  140. then
  141. if test $VERBOSE -gt 0; then
  142. echo "Forbidden identifiers: `tr '\012' ' ' < ${MACRO_LIST}`"
  143. fi
  144. MN_NAME_PAT="`sed 's/^/\\\\</; s/$/\\\\>/; $!s/$/|/' \
  145. < ${MACRO_LIST} | tr -d '\012'`"
  146. export MN_NAME_PAT
  147. else
  148. if test $VERBOSE -gt 0
  149. then echo "No forbidden identifiers defined by this target" ; fi
  150. fi
  151. # # # # # # # # # # # # # # # # # # # # #
  152. #
  153. # Search each input directory for broken header files.
  154. # This loop ends near the end of the file.
  155. #
  156. if test $# -eq 0
  157. then
  158. INPUTLIST="/usr/include"
  159. else
  160. INPUTLIST="$@"
  161. fi
  162. for INPUT in ${INPUTLIST} ; do
  163. cd ${ORIGDIR}
  164. # Make sure a directory exists before changing into it,
  165. # otherwise Solaris2 will fail-exit the script.
  166. #
  167. if [ ! -d ${INPUT} ]; then
  168. continue
  169. fi
  170. cd ${INPUT}
  171. INPUT=`${PWDCMD}`
  172. export INPUT
  173. #
  174. # # # # # # # # # # # # # # # # # # # # #
  175. #
  176. if test $VERBOSE -gt 1
  177. then echo Finding directories and links to directories ; fi
  178. # Find all directories and all symlinks that point to directories.
  179. # Put the list in $all_dirs.
  180. # Each time we find a symlink, add it to newdirs
  181. # so that we do another find within the dir the link points to.
  182. # Note that $all_dirs may have duplicates in it;
  183. # later parts of this file are supposed to ignore them.
  184. dirs="."
  185. levels=2
  186. all_dirs=""
  187. search_dirs=""
  188. while [ -n "$dirs" ] && [ $levels -gt 0 ]
  189. do
  190. levels=`expr $levels - 1`
  191. newdirs=
  192. for d in $dirs
  193. do
  194. if test $VERBOSE -gt 1
  195. then echo " Searching $INPUT/$d" ; fi
  196. # Find all directories under $d, relative to $d, excluding $d itself.
  197. # (The /. is needed after $d in case $d is a symlink.)
  198. all_dirs="$all_dirs `find $d/. -type d -print | \
  199. sed -e '/\/\.$/d' -e 's@/./@/@g'`"
  200. # Find all links to directories.
  201. # Using `-exec test -d' in find fails on some systems,
  202. # and trying to run test via sh fails on others,
  203. # so this is the simplest alternative left.
  204. # First find all the links, then test each one.
  205. theselinks=
  206. $LINKS && \
  207. theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'`
  208. for d1 in $theselinks --dummy--
  209. do
  210. # If the link points to a directory,
  211. # add that dir to $newdirs
  212. if [ -d $d1 ]
  213. then
  214. all_dirs="$all_dirs $d1"
  215. if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
  216. then
  217. newdirs="$newdirs $d1"
  218. search_dirs="$search_dirs $d1"
  219. fi
  220. fi
  221. done
  222. done
  223. dirs="$newdirs"
  224. done
  225. # # # # # # # # # # # # # # # # # # # # #
  226. #
  227. dirs=
  228. if test $VERBOSE -gt 2
  229. then echo "All directories (including links to directories):"
  230. echo $all_dirs
  231. fi
  232. OLDDIR=`${PWDCMD}`
  233. cd $LIB
  234. echo "$all_dirs" | xargs mkdir -p
  235. cd ${OLDDIR}
  236. mkdir $LIB/root
  237. # # # # # # # # # # # # # # # # # # # # #
  238. #
  239. # treetops gets an alternating list
  240. # of old directories to copy
  241. # and the new directories to copy to.
  242. treetops=". ${LIB}"
  243. if $LINKS; then
  244. if test $VERBOSE -gt 1
  245. then echo 'Making symbolic directory links' ; fi
  246. cwd=`${PWDCMD}`
  247. for sym_link in $search_dirs; do
  248. cd ${INPUT}
  249. dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`
  250. # In case $dest is relative, get to ${sym_link}'s dir first.
  251. #
  252. cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`
  253. # Check that the target directory exists.
  254. # Redirections changed to avoid bug in sh on Ultrix.
  255. #
  256. (cd $dest) > /dev/null 2>&1
  257. if [ $? = 0 ]; then
  258. cd $dest
  259. # full_dest_dir gets the dir that the link actually leads to.
  260. #
  261. full_dest_dir=`${PWDCMD}`
  262. # Canonicalize ${INPUT} now to minimize the time an
  263. # automounter has to change the result of ${PWDCMD}.
  264. #
  265. cinput=`cd ${INPUT}; ${PWDCMD}`
  266. # If a link points to ., make a similar link to .
  267. #
  268. if [ ${full_dest_dir} = ${cinput} ]; then
  269. if test $VERBOSE -gt 2
  270. then echo ${sym_link} '->' . ': Making self link' ; fi
  271. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  272. ln -s . ${LIB}/${sym_link} > /dev/null 2>&1
  273. # If link leads back into ${INPUT},
  274. # make a similar link here.
  275. #
  276. elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
  277. # Y gets the actual target dir name, relative to ${INPUT}.
  278. y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
  279. # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
  280. dots=`echo "${sym_link}" |
  281. sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
  282. if test $VERBOSE -gt 2
  283. then echo ${sym_link} '->' $dots$y ': Making local link' ; fi
  284. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  285. ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1
  286. else
  287. # If the link is to a dir $target outside ${INPUT},
  288. # repoint the link at ${INPUT}/root$target
  289. # and process $target into ${INPUT}/root$target
  290. # treat this directory as if it actually contained the files.
  291. #
  292. if test $VERBOSE -gt 2
  293. then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
  294. fi
  295. if [ -d $LIB/root${full_dest_dir} ]
  296. then true
  297. else
  298. dirname=root${full_dest_dir}/
  299. dirmade=.
  300. cd $LIB
  301. while [ x$dirname != x ]; do
  302. component=`echo $dirname | sed -e 's|/.*$||'`
  303. mkdir $component >/dev/null 2>&1
  304. cd $component
  305. dirmade=$dirmade/$component
  306. dirname=`echo $dirname | sed -e 's|[^/]*//*||'`
  307. done
  308. fi
  309. # Duplicate directory structure created in ${LIB}/${sym_link} in new
  310. # root area.
  311. #
  312. for file2 in $all_dirs; do
  313. case $file2 in
  314. ${sym_link}/*)
  315. dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
  316. sed -n "s|^${sym_link}/||p"`
  317. if test $VERBOSE -gt 2
  318. then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi
  319. if [ -d ${dupdir} ]
  320. then true
  321. else
  322. mkdir ${dupdir}
  323. fi
  324. ;;
  325. *)
  326. ;;
  327. esac
  328. done
  329. # Get the path from ${LIB} to ${sym_link}, accounting for symlinks.
  330. #
  331. parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
  332. libabs=`cd ${LIB}; ${PWDCMD}`
  333. file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
  334. # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
  335. #
  336. dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
  337. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  338. ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
  339. treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
  340. fi
  341. fi
  342. done
  343. fi
  344. # # # # # # # # # # # # # # # # # # # # #
  345. #
  346. required=
  347. set x $treetops
  348. shift
  349. while [ $# != 0 ]; do
  350. # $1 is an old directory to copy, and $2 is the new directory to copy to.
  351. #
  352. SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
  353. export SRCDIR
  354. FIND_BASE=$1
  355. export FIND_BASE
  356. shift
  357. DESTDIR=`cd $1;${PWDCMD}`
  358. export DESTDIR
  359. shift
  360. # The same dir can appear more than once in treetops.
  361. # There's no need to scan it more than once.
  362. #
  363. if [ -f ${DESTDIR}/DONE ]
  364. then continue ; fi
  365. touch ${DESTDIR}/DONE
  366. if test $VERBOSE -gt 1
  367. then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi
  368. # Check files which are symlinks as well as those which are files.
  369. #
  370. cd ${INPUT}
  371. required="$required `if $LINKS; then
  372. find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
  373. else
  374. find ${FIND_BASE}/. -name '*.h' -type f -print
  375. fi | \
  376. sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
  377. ${FIXINCL}`"
  378. done
  379. ## Make sure that any include files referenced using double quotes
  380. ## exist in the fixed directory. This comes last since otherwise
  381. ## we might end up deleting some of these files "because they don't
  382. ## need any change."
  383. set x `echo $required`
  384. shift
  385. while [ $# != 0 ]; do
  386. newreq=
  387. while [ $# != 0 ]; do
  388. # $1 is the directory to copy from,
  389. # $2 is the unfixed file,
  390. # $3 is the fixed file name.
  391. #
  392. cd ${INPUT}
  393. cd $1
  394. if [ -f $2 ] ; then
  395. if [ -r $2 ] && [ ! -r $3 ]; then
  396. cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
  397. chmod +w $3 2>/dev/null
  398. chmod a+r $3 2>/dev/null
  399. if test $VERBOSE -gt 2
  400. then echo Copied $2 ; fi
  401. for include in `egrep '^[ ]*#[ ]*include[ ]*"[^/]' $3 |
  402. sed -e 's/^[ ]*#[ ]*include[ ]*"\([^"]*\)".*$/\1/'`
  403. do
  404. dir=`echo $2 | sed -e s'|/[^/]*$||'`
  405. dir2=`echo $3 | sed -e s'|/[^/]*$||'`
  406. newreq="$newreq $1 $dir/$include $dir2/$include"
  407. done
  408. fi
  409. fi
  410. shift; shift; shift
  411. done
  412. set x $newreq
  413. shift
  414. done
  415. if test $VERBOSE -gt 2
  416. then echo 'Cleaning up DONE files.' ; fi
  417. cd $LIB
  418. # Look for files case-insensitively, for the benefit of
  419. # DOS/Windows filesystems.
  420. find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';'
  421. if test $VERBOSE -gt 1
  422. then echo 'Cleaning up unneeded directories:' ; fi
  423. cd $LIB
  424. all_dirs=`find . -type d \! -name '.' -print | sort -r`
  425. for file in $all_dirs; do
  426. if rmdir $LIB/$file > /dev/null
  427. then
  428. test $VERBOSE -gt 3 && echo " removed $file"
  429. fi
  430. done 2> /dev/null
  431. # On systems which don't support symlinks, `find' may barf
  432. # if called with "-type l" predicate. So only use that if
  433. # we know we should look for symlinks.
  434. if $LINKS; then
  435. test $VERBOSE -gt 2 && echo "Removing unused symlinks"
  436. all_dirs=`find . -type l -print`
  437. for file in $all_dirs
  438. do
  439. if test ! -d $file
  440. then
  441. rm -f $file
  442. test $VERBOSE -gt 3 && echo " removed $file"
  443. rmdir `dirname $file` > /dev/null && \
  444. test $VERBOSE -gt 3 && \
  445. echo " removed `dirname $file`"
  446. fi
  447. done 2> /dev/null
  448. fi
  449. if test $VERBOSE -gt 0
  450. then echo fixincludes is done ; fi
  451. # # # # # # # # # # # # # # # # # # # # #
  452. #
  453. # End of for INPUT directories
  454. #
  455. done
  456. #
  457. # # # # # # # # # # # # # # # # # # # # #