include_optimize.rb 930 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/ruby -w
  2. def write_file_without_lines(source, lines, without_lines)
  3. out = File.new(source, 'w')
  4. lines.each_with_index { |v, i|
  5. if without_lines.member? i
  6. out.print "//", v
  7. else
  8. out.print v
  9. end
  10. }
  11. out.close()
  12. end
  13. def optimize(source, target)
  14. lines = File.new(source).readlines()
  15. includes = []
  16. lines.each_with_index { |v, i|
  17. if v =~ /^#include/
  18. # puts v
  19. includes << i
  20. end
  21. }
  22. unneeded_includes = []
  23. includes.each{|i|
  24. # puts i
  25. write_file_without_lines(source, lines, [i])
  26. ret = system("scons", "-u", target)
  27. if ret
  28. puts "INCUNNEEDED #{source} #{lines[i].chop}"
  29. unneeded_includes << i
  30. end
  31. }
  32. write_file_without_lines(source, lines, unneeded_includes)
  33. end
  34. ARGV.each{ |filename|
  35. source = filename
  36. target = "build/#{filename.gsub(/\..pp$/, '.o')}"
  37. puts "#{source} => #{target}"
  38. optimize(source, target)
  39. }
  40. # EOF #