Class: Makefile
- Inherits:
-
Object
- Object
- Makefile
- Defined in:
- lib/makeconf/makefile.rb
Overview
A Makefile is a collection of targets and rules used to build software.
Instance Method Summary collapse
- #add_dependency(target, depends) ⇒ Object
- #add_rule(target, rule) ⇒ Object
- #add_target(object, depends = [], rules = []) ⇒ Object
-
#clean(path) ⇒ Object
Add a file to be removed during ‘make clean’ TODO: optimize by eliminating multiple rm(1) fork/exec.
-
#define_variable(lval, op, rval) ⇒ Object
Define a variable within the Makefile.
-
#distclean(path) ⇒ Object
Add a file to be removed during ‘make distclean’ TODO: optimize by eliminating multiple rm(1) fork/exec.
-
#distribute(path) ⇒ Object
Add a file to the tarball during ‘make dist’.
-
#initialize ⇒ Makefile
constructor
Object constructor.
- #make_dist(project, version) ⇒ Object
- #merge!(src) ⇒ Object
- #target(object) ⇒ Object
- #to_s ⇒ Object
- #write(path) ⇒ Object
Constructor Details
#initialize ⇒ Makefile
Object constructor.
6 7 8 9 10 11 12 13 |
# File 'lib/makeconf/makefile.rb', line 6 def initialize @vars = {} @targets = {} %w[all check clean distclean install uninstall distdir].each do |x| add_target(x) end end |
Instance Method Details
#add_dependency(target, depends) ⇒ Object
79 80 81 82 |
# File 'lib/makeconf/makefile.rb', line 79 def add_dependency(target,depends) add_target(target, [depends], []) unless @targets.has_key? target @targets[target].add_dependency(depends) end |
#add_rule(target, rule) ⇒ Object
46 47 48 49 |
# File 'lib/makeconf/makefile.rb', line 46 def add_rule(target, rule) add_target(target, [], []) unless @targets.has_key? target @targets[target].add_rule(rule) end |
#add_target(object, depends = [], rules = []) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/makeconf/makefile.rb', line 38 def add_target(object,depends = [], rules = []) if object.kind_of?(Target) @targets[object.objs] = object else @targets[object] = Target.new(object,depends,rules) end end |
#clean(path) ⇒ Object
Add a file to be removed during ‘make clean’ TODO: optimize by eliminating multiple rm(1) fork/exec
69 70 71 |
# File 'lib/makeconf/makefile.rb', line 69 def clean(path) add_rule('clean', Platform.rm(Platform.pathspec(path))) end |
#define_variable(lval, op, rval) ⇒ Object
Define a variable within the Makefile
16 17 18 19 20 |
# File 'lib/makeconf/makefile.rb', line 16 def define_variable(lval,op,rval) throw "invalid arguments" if lval.nil? or op.nil? throw "variable `#{lval}' is undefined" if rval.nil? @vars[lval] = [ op, rval ] end |
#distclean(path) ⇒ Object
Add a file to be removed during ‘make distclean’ TODO: optimize by eliminating multiple rm(1) fork/exec
75 76 77 |
# File 'lib/makeconf/makefile.rb', line 75 def distclean(path) add_rule('distclean', Platform.rm(Platform.pathspec(path))) end |
#distribute(path) ⇒ Object
Add a file to the tarball during ‘make dist’
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/makeconf/makefile.rb', line 52 def distribute(path) path = [ path ] if path.kind_of? String path.each do |src| # FIXME: support Windows backslashery if src =~ /\// dst = '$(distdir)/' + File.dirname(src) @targets['distdir'].mkdir(dst) @targets['distdir'].cp(src, dst) else @targets['distdir'].cp(src, '$(distdir)') end end end |
#make_dist(project, version) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/makeconf/makefile.rb', line 84 def make_dist(project,version) distdir = project + '-' + version.to_s # XXX-FIXME this should come from the Project distfile = project + '-' + version.to_s + '.tar.gz' tg = Target.new(distfile) tg.add_rule(Platform.rmdir(distdir)) tg.add_rule("mkdir " + distdir) tg.add_rule('$(MAKE) distdir distdir=' + distdir) if Platform.is_windows? # FIXME - Not implemented yet else tg.add_rule("rm -rf #{distdir}.tar #{distdir}.tar.gz") tg.add_rule("tar cf #{distdir}.tar #{distdir}") tg.add_rule("gzip #{distdir}.tar") tg.add_rule("rm -rf #{distdir}") clean("#{distdir}.tar.gz") end @targets[distfile] = tg end |
#merge!(src) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/makeconf/makefile.rb', line 26 def merge!(src) throw 'invalid argument' unless src.is_a?(Makefile) @vars.merge!(src.vars) src.targets.each do |k,v| if targets.has_key?(k) targets[k].merge!(v) else targets[k] = (v) end end end |
#target(object) ⇒ Object
22 23 24 |
# File 'lib/makeconf/makefile.rb', line 22 def target(object) @targets[object] end |
#to_s ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/makeconf/makefile.rb', line 106 def to_s res = '' @vars.sort.each { |x,y| res += [x, y[0], y[1]].join(' ') + "\n" } res += "\n\n" res += "default: all\n" targets.each { |x,y| throw "#{x} is broken" unless y.is_a? Target } @targets.sort.each { |x,y| res += y.to_s } res end |
#write(path) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/makeconf/makefile.rb', line 116 def write(path) f = File.open(path, 'w') f.print "# AUTOMATICALLY GENERATED -- DO NOT EDIT\n" f.print self.to_s f.close end |