class MoonRabbit::Makefile

Attributes

compiles[RW]
files[RW]

Public Class Methods

new( &block ) click to toggle source
# File lib/moon_rabbit.rb, line 7
def initialize( &block )
  @files = {
    :path =>      "",
  }
  
  @compiles = {
    :compiler     => "",
    :main_target  => "",
    :srcs         => [],
    :obj_dir      => ".",
    :options      => [],
  }
  
  @links = {
    :static_libs  => [],
    :options      => [],
  }
  
  instance_eval( &block )
end

Public Instance Methods

add( makefile ) click to toggle source
# File lib/moon_rabbit.rb, line 28
def add( makefile )
  makefile.files.each{|key, value|
    @files[ key ] = value
  }
  
  makefile.compiles.each{|key, value|
    if @compiles[ key ].instance_of?( String )
      @compiles[ key ] = value
    elsif @compiles[ key ].instance_of?( Array )
      @compiles[ key ].concat value
    end
  }
  
  makefile.links.each{|key, value|
    @links[ key ].concat value
  }
end
change_ext( file_path, ext ) click to toggle source
# File lib/moon_rabbit.rb, line 175
def change_ext( file_path, ext )
  "#{File.dirname( file_path )}/#{File.basename( file_path, '.*' )}#{ext}"
end
compile_option( compile_option ) click to toggle source
# File lib/moon_rabbit.rb, line 72
def compile_option( compile_option )
  @compiles[ :options ].push compile_option
end
compile_options( compile_options ) click to toggle source
# File lib/moon_rabbit.rb, line 76
def compile_options( compile_options )
  compile_options.each{|compile_option|
    self.compile_option compile_option
  }
end
compiler( compiler ) click to toggle source
# File lib/moon_rabbit.rb, line 50
def compiler( compiler )
  @compiles[ :compiler ] = compiler
end
main_target( main_target ) click to toggle source
# File lib/moon_rabbit.rb, line 54
def main_target( main_target )
  @compiles[ :main_target ] = main_target
end
obj_dir( obj_dir ) click to toggle source
# File lib/moon_rabbit.rb, line 68
def obj_dir( obj_dir )
  @compiles[ :obj_dir ] = obj_dir
end
output() click to toggle source
# File lib/moon_rabbit.rb, line 102
    def output
      objs = []
      deps = []
      @compiles[ :srcs ].each{|src|
        obj = "#{@compiles[ :obj_dir ]}/#{change_ext( src, '.o' )}"
        objs.push obj
        deps.push change_ext( obj, ".d" )
      }
      src_ext = File.extname( @compiles[ :srcs ].first )
      main_target_ext = File.extname( @compiles[ :main_target ] )
      
      open( @files[ :path ], "wb" ){|f|
        f.puts <<EOS
override COMPILER        += #{@compiles[ :compiler ]}
override COMPILE_OPTIONS += #{@compiles[ :options ].join( " " )}
override LINK_OPTIONS    += #{@links[ :options ].join( " " )}
override STATIC_LIBS     += #{@links[ :static_libs ].join( " " )}
RM                        = rm -f
MKDIR                     = mkdir -p
MAIN_TARGET               = #{@compiles[ :main_target ]}
SRCS                      = #{@compiles[ :srcs ].join( " " )}
OBJ_DIR                   = #{@compiles[ :obj_dir ]}
OBJS                      = #{objs.join( " " )}
DEPS                      = #{deps.join( " " )}

.PHONY: all clean

all: $(MAIN_TARGET)

clean:
        $(RM) $(MAIN_TARGET)
        $(RM) $(OBJS)
        $(RM) $(DEPS)

EOS
        
        f.puts <<EOS
$(OBJ_DIR)/%.o: %#{src_ext}
        @[ -e $(dir $@) ] || $(MKDIR) $(dir $@)
        
        $(COMPILER) $(COMPILE_OPTIONS) -c $< -o $@
        
        @$(COMPILER) $(COMPILE_OPTIONS) -MM -MG -MP $< \\
                | sed "s/.*\\.o/$(subst /,\\/,$@) $(subst /,\\/,$(patsubst %.o,%.d,$@))/g" > $(patsubst %.o,%.d,$@); \\
                [ -s $(patsubst %.o,%.d,$@) ] || $(RM) $(patsubst %.o,%.d,$@)

-include $(DEPS)

EOS
        
        case main_target_ext
        when ".a"
          f.puts <<EOS
AR = ar r

$(MAIN_TARGET): $(OBJS)
        @[ -e $(dir $@) ] || $(MKDIR) $(dir $@)
        
        $(AR) $@ $^

EOS
        else
          f.puts <<EOS
$(MAIN_TARGET): $(OBJS) $(STATIC_LIBS)
        @[ -e $(dir $@) ] || $(MKDIR) $(dir $@)
        
        $(COMPILER) $(LINK_OPTIONS) -o $@ $^

EOS
        end
      }
    end
path( path ) click to toggle source
# File lib/moon_rabbit.rb, line 46
def path( path )
  @files[ :path ] = path
end
src( src ) click to toggle source
# File lib/moon_rabbit.rb, line 58
def src( src )
  @compiles[ :srcs ].push src
end
srcs( srcs ) click to toggle source
# File lib/moon_rabbit.rb, line 62
def srcs( srcs )
  srcs.each{|src|
    self.src src
  }
end
static_lib( static_lib ) click to toggle source
# File lib/moon_rabbit.rb, line 82
def static_lib( static_lib )
  @links[ :static_libs ].push static_lib
end
static_libs( static_libs ) click to toggle source
# File lib/moon_rabbit.rb, line 86
def static_libs( static_libs )
  static_libs.each{|static_lib|
    self.static_lib static_lib
  }
end