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



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

def C.ar
  @@ar
end

.ar=(ar) ⇒ Object



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

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

.ccObject



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

def C.cc
  @@compiler
end

.cc=(cc) ⇒ Object



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

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

.cflagsObject



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

def C.cflags
  @@cflags
end

.cflags=(flags) ⇒ Object



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

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

.cleanObject



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

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

.ldflagsObject



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

def C.ldflags
  @@ldflags
end

.ldflags=(flags) ⇒ Object



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

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

.library(name, files) ⇒ Object



54
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
# File 'lib/rake/c.rb', line 54

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' 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}"
        run "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



94
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
# File 'lib/rake/c.rb', line 94

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}"
        run "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



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

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