Class: C

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/c.rb

Constant Summary collapse

@@cflags =
'-Wall'
@@ldflags =
''
@@compiler =
ENV['CC'] || 'cc'
@@ar =
ENV['AR'] || 'ar'
@@builddir =
'build'
@@sourcedir =
'src'
@@libs =
''

Class Method Summary collapse

Class Method Details

.arObject



31
32
33
# File 'lib/rake/c.rb', line 31

def C.ar
  @@ar
end

.ar=(ar) ⇒ Object



35
36
37
# File 'lib/rake/c.rb', line 35

def C.ar= ar
  @@ar = ar
end

.ccObject



23
24
25
# File 'lib/rake/c.rb', line 23

def C.cc
  @@compiler
end

.cc=(cc) ⇒ Object



27
28
29
# File 'lib/rake/c.rb', line 27

def C.cc= cc
  @@compiler = cc
end

.cflagsObject



39
40
41
# File 'lib/rake/c.rb', line 39

def C.cflags
  @@cflags
end

.cflags=(flags) ⇒ Object



43
44
45
# File 'lib/rake/c.rb', line 43

def C.cflags= flags
  @@cflags = flags
end

.cleanObject



19
20
21
# File 'lib/rake/c.rb', line 19

def C.clean
  run "rm -rf \"#{@@builddir}\""
end

.ldflagsObject



47
48
49
# File 'lib/rake/c.rb', line 47

def C.ldflags
  @@ldflags
end

.ldflags=(flags) ⇒ Object



51
52
53
# File 'lib/rake/c.rb', line 51

def C.ldflags= flags
  @@ldflags = flags
end

.library(name, files) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rake/c.rb', line 55

def C.library name, files
  @@libs += " \"-I#{@@sourcedir}/#{name}\""
  cflags = @@cflags
  thelib = "#{@@builddir}/lib#{name}.a"
  objects = Array.new
  files.each do |f|
    type = /\.[a-zA-Z0-9]+$/.match(f).to_s
    if type == '.c' || type == '.cc' || type == '.cpp' || type == '.c++' || type == '.m' || type == '.mm' then
      o = f.ext('.o')
      d = f.ext('.deps.rb')
      theobject = "#{@@builddir}/objects/#{name}/#{o}"
      thedeps = "#{@@builddir}/objects/#{name}/#{d}"
      thesource = "#{@@sourcedir}/#{name}/#{f}"
      objects.push theobject
      Rake::FileTask.define_task theobject => [thesource] do
        puts "[CC] #{theobject}"
        FileUtils.mkdir_p "#{@@builddir}/objects/#{name}"
        xxx = run "#{@@compiler} #{cflags}#{@@libs} -M \"#{thesource}\""
        xx = []
        File.open(thedeps, 'w') do |f|
          f.puts '# This file was automatically generated. Do not edit!'
          xxx.scan(/\s([^\s\\]+)/) do |x| xx.push x[0] end
          f.puts "Rake::FileTask.define_task #{theobject.dump} => #{xx}"
        end
        run "#{@@compiler} #{cflags}#{@@libs} -c -o \"#{theobject}\" \"#{thesource}\""
      end
      load thedeps if File.file? thedeps
      Rake::FileTask.define_task thelib => [theobject]
    else
      objects.push f
    end
  end
  Rake::FileTask.define_task thelib do
    puts "[AR] #{thelib}"
    run "#{@@ar} rcs \"#{thelib}\" \"#{objects.join '" "'}\""
  end
  Rake::Task.define_task :default => thelib
  Rake::Task.define_task :clean => :c_clean
end

.program(name, files) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rake/c.rb', line 95

def C.program name, files
  cflags = @@cflags
  theprogram = "#{@@builddir}/#{name}"
  objects = Array.new
  files.each do |f|
    type = /\.[a-zA-Z0-9]+$/.match(f).to_s
    if type == '.c' || type == '.cc' || type == '.cpp' then
      o = f.ext('.o')
      d = f.ext('.deps.rb')
      theobject = "#{@@builddir}/objects/#{o}"
      thedeps = "#{@@builddir}/objects/#{d}"
      thesource = "#{@@sourcedir}/#{f}"
      objects.push theobject
      Rake::FileTask.define_task theobject => [thesource] do
        puts "[CC] #{theobject}"
        FileUtils.mkdir_p "#{@@builddir}/objects"
        xxx = run "#{@@compiler} #{cflags}#{@@libs} -M \"#{thesource}\""
        xx = []
        File.open(thedeps, 'w') do |f|
          f.puts '# This file was automatically generated. Do not edit!'
          xxx.scan(/\s([^\s\\]+)/) do |x| xx.push x[0] end
          f.puts "Rake::FileTask.define_task #{theobject.dump} => #{xx}"
        end
        run "#{@@compiler} #{cflags}#{@@libs} -c -o \"#{theobject}\" \"#{thesource}\""
      end
      load thedeps if File.file? thedeps
      Rake::FileTask.define_task theprogram => [theobject]
    elsif type == '.a' then
      thefile = "#{@@builddir}/#{f}"
      Rake::FileTask.define_task theprogram => [thefile]
      objects.push thefile
    else
      objects.push f
    end
  end
  Rake::FileTask.define_task theprogram do
    puts "[LD] #{theprogram}"
    run "#{@@compiler} #{@@ldflags} \"-L#{@@builddir}\" -o \"#{theprogram}\" \"#{objects.join '" "'}\""
  end
  Rake::Task.define_task :default => theprogram
  Rake::Task.define_task :clean => :c_clean
end

.run(cmd) ⇒ Object



13
14
15
16
17
# File 'lib/rake/c.rb', line 13

def C.run cmd
  r = `#{cmd}`
  raise "Error: '#{cmd}'" unless $?.success?
  return r
end