# This program creates make_objs.inc and make_deps.inc, which are be used by the makefile.
#
# Assumption:
# * All C/C++ and header files are placed at the root directory of the project.
# * Header files which are not placed at the above directory are included by
#   #include <foo.h>
#
# Algorithm:
# * Find all C/C++ files in the current directory and append them to LOCAL_OBJS.
# * Find all header files which are included in each C/C++ file by the pattern
#   #include "foobar.h"
#   and append them to the dependency list of the C/C++ file.

def find_all_c_and_cpp_files
  ret = Array.new
  Dir.foreach('.') { |fn| ret << fn if File.file?(fn) and fn =~ /(\.c|\.cpp)$/ }
  ret
end

def find_all_h_files(file_name)
  ret = Array.new
  buf = File.read(file_name)
  buf.each_line { |s| ret << $1 if s =~ /\#include\s+"(.+)"/ }
  ret
end

def main
  objs = "LOCAL_OBJS = \\\n"
  deps = ''
  
  find_all_c_and_cpp_files.each do |fn|
    fn =~ /^(.+)(\.c|\.cpp)$/
    base = $1
    objs << "$(TARGETDIR)\\#{base}.o \\\n"
    
    deps << "$(TARGETDIR)\\#{base}.o: #{fn}\n"
    find_all_h_files(fn).each { |h_fn| deps << "$(TARGETDIR)\\#{base}.o: #{h_fn}\n" }
  end
  
  File.open('make_objs.inc', 'w') { |f| f.write(objs) }
  File.open('make_deps.inc', 'w') { |f| f.write(deps) }
end
main

