find_cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/ksh
  2. # Time-stamp: <2010-09-08 13:35:25 sager>
  3. #
  4. # Find all the C pre-processor flags used in the current TM5 base, projects
  5. # and branches. Does not look into ".svn", "TM", and "release" (since they
  6. # are frozen) directories.
  7. #
  8. # Look for:
  9. # "#ifdef with_", "#ifdef without_", "#ifndef with_",
  10. # "#ifndef without_" , "#if defined (with"
  11. #
  12. # flags, and in $PWD.
  13. #
  14. # The script looks down $1 if argument is passed, else down $PWD.
  15. # It also accounts for stuff like : #if defined(with_...) || defined(with_...)
  16. #
  17. # See TIP at end of the script.
  18. #
  19. # Examples : # to look down $PWD
  20. # --------- find_cpp
  21. #
  22. # # to look down ~/TM5, and save result:
  23. # find_cpp ~/TM5 > ~/ctm5-cpp
  24. #-----------------
  25. # search function
  26. #-----------------
  27. function gsvn {
  28. find . -name .svn -prune -o -name release -prune -o -name TM -prune -o \
  29. -name '*.F90' -o -name '*.h' | xargs egrep "$@"
  30. }
  31. #-----------------
  32. # search function #2 : same but also skips mdf* and tmm* files, as well as
  33. # obsolete projects
  34. #-----------------
  35. function gsvn_2 {
  36. find . -name .svn -prune -o -name release -prune -o -name TM -prune \
  37. -o -name ipcc -prune -o -name aero -prune \
  38. -o \( -name '*.F90' ! -name tmm* ! -name mdf* \) | xargs egrep "$@"
  39. }
  40. #----------
  41. # Argument
  42. #----------
  43. [[ -d $1 ]] || set -- $PWD ; cd $1
  44. #---------------------------
  45. # Get list of C-preprocessor
  46. #---------------------------
  47. # Note - Could probably speed things up by combining all 3 searches into 1
  48. # search:
  49. # gsvn_2 -i "^[[:blank:]]*#if.*def.*with" > ~/findcpptem.txt
  50. #
  51. # and then (the tricky part) combine the 2 sed into one. Leave it for future
  52. # exercise...
  53. #---------------------------
  54. # Look for : #ifdef and #ifndef
  55. gsvn_2 -i "^[[:blank:]]*#ifn?def with(out)?_[:word:]*" > ~/findcpptemp1.txt
  56. # Look for : #if defined
  57. gsvn_2 -i "^[[:blank:]]*#if defined *\(with(out)?_" | \
  58. sed "s/#if defined *(\([^|]*\)).*/#ifdef \1/" >> ~/findcpptemp1.txt
  59. # Look for : "|| defined" after a #if defined
  60. gsvn_2 -i "^[[:blank:]]*#if defined.*\|\|" | \
  61. sed "s/#if defined.*defined(\([^|]*\)).*/#ifdef \1/" >> ~/findcpptemp1.txt
  62. awk '{print $2}' ~/findcpptemp1.txt | sort | uniq -c > ~/findcpptemp2.txt
  63. #-----------------------------------------------
  64. # Get number of files using each C-preprocessor
  65. #-----------------------------------------------
  66. echo " NFiles NOccur Cpp-flag"
  67. { while read nn cppflag
  68. do
  69. nfiles=$(grep "$cppflag\>" ~/findcpptemp1.txt | awk -F ":" '{print $1}' | \
  70. sort | uniq | wc -l)
  71. printf '%7i %7i %s\n' $nfiles $nn $cppflag
  72. done } < ~/findcpptemp2.txt
  73. #---------------------------
  74. # Clean up
  75. #---------------------------
  76. rm -f ~/findcpptemp*.txt
  77. #-------------------------------------------
  78. # TIP : add gsvn to your shell resource file (.bashrc, .user_kshrc, ...)
  79. #-------------------------------------------
  80. # gsvn is as powerful as egrep, since it is a wrapper for the
  81. # latter. So you can pass any option flag accepted by egrep.
  82. #
  83. # For example, to find which Fortran routines is using "with_lapack":
  84. #
  85. # gsvn -il "with_lapack"
  86. #
  87. # or better:
  88. #
  89. # gsvn -il "^[[:blank:]]*#if.*def.* with_lapack\>" | uniq
  90. #
  91. # To proove that the module user_output_cf is only used within an #ifdef with_cf_output:
  92. #
  93. # gsvn -inC 1 "^[[:blank:]]*use *user_output_cf"