Advertisement
aepokh

creqs

Jul 4th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 1.08 KB | None | 0 0
  1. #!/usr/bin/awk -f
  2.  
  3. # creqs: scan a c source file and determine the
  4. #   compiler flags necessary, e.g. -lm for <math.h>
  5.  
  6. # r(h): return the required command switch to link the header `h'
  7. # this would be a switch but apparently i cannot write
  8. # syntactically correct switches in awk!
  9. function r(h) {
  10.     if (h == "math.h")
  11.         return "-lm"
  12.     if (h == "ncurses.h")
  13.         return "-lncurses"
  14.     if (h ~ /^SDL/)
  15.         return "-l" substr(h, 5, length(h)-6)
  16.         # length(h)-6 to trim trailing .h
  17.     return ""
  18. }
  19.  
  20. {   if (match($0, "^#[ \t]*include[ \t]*[\"<][ \t]*")) {
  21.         header = substr($0, 1+RLENGTH); # trim #include < from left of line
  22.         if (!match(header, "[ \">]")) {
  23.             # is there a better way than | "cat 1>&2" to print to stderr?
  24.             printf "creqs: malformed #include directive: `%s'\n", $0 | "cat 1>&2"
  25.             next
  26.         }
  27.         header = substr(header, 1, RSTART-1); # trim to the actual name of the header
  28.         if (switch = r(header))
  29.             reqs = reqs? reqs " " switch: switch
  30.     }
  31. }
  32.  
  33. END { print reqs }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement