fixinc.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 $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*)
  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. for file in $all_dirs; do
  233. rm -rf $LIB/$file
  234. if [ ! -d $LIB/$file ]
  235. then mkdir $LIB/$file
  236. fi
  237. done
  238. mkdir $LIB/root
  239. # # # # # # # # # # # # # # # # # # # # #
  240. #
  241. # treetops gets an alternating list
  242. # of old directories to copy
  243. # and the new directories to copy to.
  244. treetops=". ${LIB}"
  245. if $LINKS; then
  246. if test $VERBOSE -gt 1
  247. then echo 'Making symbolic directory links' ; fi
  248. cwd=`${PWDCMD}`
  249. for sym_link in $search_dirs; do
  250. cd ${INPUT}
  251. dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`
  252. # In case $dest is relative, get to ${sym_link}'s dir first.
  253. #
  254. cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`
  255. # Check that the target directory exists.
  256. # Redirections changed to avoid bug in sh on Ultrix.
  257. #
  258. (cd $dest) > /dev/null 2>&1
  259. if [ $? = 0 ]; then
  260. cd $dest
  261. # full_dest_dir gets the dir that the link actually leads to.
  262. #
  263. full_dest_dir=`${PWDCMD}`
  264. # Canonicalize ${INPUT} now to minimize the time an
  265. # automounter has to change the result of ${PWDCMD}.
  266. #
  267. cinput=`cd ${INPUT}; ${PWDCMD}`
  268. # If a link points to ., make a similar link to .
  269. #
  270. if [ ${full_dest_dir} = ${cinput} ]; then
  271. if test $VERBOSE -gt 2
  272. then echo ${sym_link} '->' . ': Making self link' ; fi
  273. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  274. ln -s . ${LIB}/${sym_link} > /dev/null 2>&1
  275. # If link leads back into ${INPUT},
  276. # make a similar link here.
  277. #
  278. elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
  279. # Y gets the actual target dir name, relative to ${INPUT}.
  280. y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
  281. # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
  282. dots=`echo "${sym_link}" |
  283. sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
  284. if test $VERBOSE -gt 2
  285. then echo ${sym_link} '->' $dots$y ': Making local link' ; fi
  286. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  287. ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1
  288. else
  289. # If the link is to a dir $target outside ${INPUT},
  290. # repoint the link at ${INPUT}/root$target
  291. # and process $target into ${INPUT}/root$target
  292. # treat this directory as if it actually contained the files.
  293. #
  294. if test $VERBOSE -gt 2
  295. then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
  296. fi
  297. if [ -d $LIB/root${full_dest_dir} ]
  298. then true
  299. else
  300. dirname=root${full_dest_dir}/
  301. dirmade=.
  302. cd $LIB
  303. while [ x$dirname != x ]; do
  304. component=`echo $dirname | sed -e 's|/.*$||'`
  305. mkdir $component >/dev/null 2>&1
  306. cd $component
  307. dirmade=$dirmade/$component
  308. dirname=`echo $dirname | sed -e 's|[^/]*/||'`
  309. done
  310. fi
  311. # Duplicate directory structure created in ${LIB}/${sym_link} in new
  312. # root area.
  313. #
  314. for file2 in $all_dirs; do
  315. case $file2 in
  316. ${sym_link}/*)
  317. dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
  318. sed -n "s|^${sym_link}/||p"`
  319. if test $VERBOSE -gt 2
  320. then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi
  321. if [ -d ${dupdir} ]
  322. then true
  323. else
  324. mkdir ${dupdir}
  325. fi
  326. ;;
  327. *)
  328. ;;
  329. esac
  330. done
  331. # Get the path from ${LIB} to ${sym_link}, accounting for symlinks.
  332. #
  333. parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
  334. libabs=`cd ${LIB}; ${PWDCMD}`
  335. file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
  336. # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
  337. #
  338. dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
  339. rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
  340. ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
  341. treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
  342. fi
  343. fi
  344. done
  345. fi
  346. # # # # # # # # # # # # # # # # # # # # #
  347. #
  348. required=
  349. set x $treetops
  350. shift
  351. while [ $# != 0 ]; do
  352. # $1 is an old directory to copy, and $2 is the new directory to copy to.
  353. #
  354. SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
  355. export SRCDIR
  356. FIND_BASE=$1
  357. export FIND_BASE
  358. shift
  359. DESTDIR=`cd $1;${PWDCMD}`
  360. export DESTDIR
  361. shift
  362. # The same dir can appear more than once in treetops.
  363. # There's no need to scan it more than once.
  364. #
  365. if [ -f ${DESTDIR}/DONE ]
  366. then continue ; fi
  367. touch ${DESTDIR}/DONE
  368. if test $VERBOSE -gt 1
  369. then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi
  370. # Check files which are symlinks as well as those which are files.
  371. #
  372. cd ${INPUT}
  373. required="$required `if $LINKS; then
  374. find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
  375. else
  376. find ${FIND_BASE}/. -name '*.h' -type f -print
  377. fi | \
  378. sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
  379. ${FIXINCL}`"
  380. done
  381. ## Make sure that any include files referenced using double quotes
  382. ## exist in the fixed directory. This comes last since otherwise
  383. ## we might end up deleting some of these files "because they don't
  384. ## need any change."
  385. set x `echo $required`
  386. shift
  387. while [ $# != 0 ]; do
  388. newreq=
  389. while [ $# != 0 ]; do
  390. # $1 is the directory to copy from,
  391. # $2 is the unfixed file,
  392. # $3 is the fixed file name.
  393. #
  394. cd ${INPUT}
  395. cd $1
  396. if [ -f $2 ] ; then
  397. if [ -r $2 ] && [ ! -r $3 ]; then
  398. cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
  399. chmod +w $3 2>/dev/null
  400. chmod a+r $3 2>/dev/null
  401. if test $VERBOSE -gt 2
  402. then echo Copied $2 ; fi
  403. for include in `egrep '^[ ]*#[ ]*include[ ]*"[^/]' $3 |
  404. sed -e 's/^[ ]*#[ ]*include[ ]*"\([^"]*\)".*$/\1/'`
  405. do
  406. dir=`echo $2 | sed -e s'|/[^/]*$||'`
  407. dir2=`echo $3 | sed -e s'|/[^/]*$||'`
  408. newreq="$newreq $1 $dir/$include $dir2/$include"
  409. done
  410. fi
  411. fi
  412. shift; shift; shift
  413. done
  414. set x $newreq
  415. shift
  416. done
  417. if test $VERBOSE -gt 2
  418. then echo 'Cleaning up DONE files.' ; fi
  419. cd $LIB
  420. # Look for files case-insensitively, for the benefit of
  421. # DOS/Windows filesystems.
  422. find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';'
  423. if test $VERBOSE -gt 1
  424. then echo 'Cleaning up unneeded directories:' ; fi
  425. cd $LIB
  426. all_dirs=`find . -type d \! -name '.' -print | sort -r`
  427. for file in $all_dirs; do
  428. if rmdir $LIB/$file > /dev/null
  429. then
  430. test $VERBOSE -gt 3 && echo " removed $file"
  431. fi
  432. done 2> /dev/null
  433. # On systems which don't support symlinks, `find' may barf
  434. # if called with "-type l" predicate. So only use that if
  435. # we know we should look for symlinks.
  436. if $LINKS; then
  437. test $VERBOSE -gt 2 && echo "Removing unused symlinks"
  438. all_dirs=`find . -type l -print`
  439. for file in $all_dirs
  440. do
  441. if test ! -d $file
  442. then
  443. rm -f $file
  444. test $VERBOSE -gt 3 && echo " removed $file"
  445. rmdir `dirname $file` > /dev/null && \
  446. test $VERBOSE -gt 3 && \
  447. echo " removed `dirname $file`"
  448. fi
  449. done 2> /dev/null
  450. fi
  451. if test $VERBOSE -gt 0
  452. then echo fixincludes is done ; fi
  453. # # # # # # # # # # # # # # # # # # # # #
  454. #
  455. # End of for INPUT directories
  456. #
  457. done
  458. #
  459. # # # # # # # # # # # # # # # # # # # # #