Class: Daedalus::Compiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cc, cxx, ldshared, ldsharedxx, logger, blueprint) ⇒ Compiler

Returns a new instance of Compiler.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/daedalus.rb', line 141

def initialize(cc, cxx, ldshared, ldsharedxx, logger, blueprint)
  @cc = cc
  @cxx = cxx
  @ldshared = ldshared
  @ldsharedxx = ldsharedxx
  @cflags = []
  @cxxflags = []
  @ldflags = []
  @libraries = []
  @pre_link_cmds = []
  @log = logger
  @blueprint = blueprint

  @mod_times = Hash.new do |h,k|
    h[k] = (File.exist?(k) ? File.mtime(k) : Time.at(0))
  end

  @sha1_mtimes = {}
  @sha1s = Hash.new do |h,k|
    if File.exist?(k)
      @log.verbose "computing SHA1: #{k}"
      @sha1_mtimes[k] = File.mtime(k)
      h[k] = Digest::SHA1.file(k).hexdigest
    else
      h[k] = ""
    end
  end

  @mtime_only = false
end

Instance Attribute Details

#ccObject (readonly)

Returns the value of attribute cc.



186
187
188
# File 'lib/daedalus.rb', line 186

def cc
  @cc
end

#cflagsObject (readonly)

Returns the value of attribute cflags.



186
187
188
# File 'lib/daedalus.rb', line 186

def cflags
  @cflags
end

#cxxObject (readonly)

Returns the value of attribute cxx.



186
187
188
# File 'lib/daedalus.rb', line 186

def cxx
  @cxx
end

#cxxflagsObject (readonly)

Returns the value of attribute cxxflags.



186
187
188
# File 'lib/daedalus.rb', line 186

def cxxflags
  @cxxflags
end

#ldflagsObject (readonly)

Returns the value of attribute ldflags.



186
187
188
# File 'lib/daedalus.rb', line 186

def ldflags
  @ldflags
end

#ldsharedObject (readonly)

Returns the value of attribute ldshared.



186
187
188
# File 'lib/daedalus.rb', line 186

def ldshared
  @ldshared
end

#ldsharedxxObject (readonly)

Returns the value of attribute ldsharedxx.



186
187
188
# File 'lib/daedalus.rb', line 186

def ldsharedxx
  @ldsharedxx
end

#logObject (readonly)

Returns the value of attribute log.



186
187
188
# File 'lib/daedalus.rb', line 186

def log
  @log
end

#mtime_onlyObject

Returns the value of attribute mtime_only.



172
173
174
# File 'lib/daedalus.rb', line 172

def mtime_only
  @mtime_only
end

#pathObject (readonly)

Returns the value of attribute path.



186
187
188
# File 'lib/daedalus.rb', line 186

def path
  @path
end

Instance Method Details

#add_library(lib) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/daedalus.rb', line 189

def add_library(lib)
  if f = lib.cflags
    @cflags = f + @cflags
    @cflags.uniq!
  end

  if f = lib.ldflags
    @ldflags = f + @ldflags
    @ldflags.uniq!
  end
end


201
202
203
# File 'lib/daedalus.rb', line 201

def add_pre_link(cmd, &b)
  @pre_link_cmds << [cmd, b]
end

#ar(library, objects) ⇒ Object



257
258
259
260
261
# File 'lib/daedalus.rb', line 257

def ar(library, objects)
  @log.show "AR", library
  @log.command "ar rv #{library} #{objects.join(' ')}"
  @log.command "ranlib #{library}"
end

#c_compile(source, object) ⇒ Object



227
228
229
230
# File 'lib/daedalus.rb', line 227

def c_compile(source, object)
  @log.show "CC", source
  @log.command "#{@cc} #{@cflags.join(' ')} -c -o #{object} #{source}"
end

#calculate_deps(path) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/daedalus.rb', line 268

def calculate_deps(path)
  dirs = header_directories() + ["/usr/include"]
  flags = @cflags.join(' ')
  begin
    dep = DependencyGrapher.new @cc, [path], dirs, flags
    dep.process

    # This is a quick work-around for a craptastic bug that I can't figure
    # out. Sometimes this raises an exception saying it can't find a file
    # which is pretty obviously there. I've been unable to figure out
    # what causes this and thus how to fix.
    #
    # So as a temporary measure, if an exception is raised, I'm going to
    # just do it again. Previous results have shown that this should
    # work the 2nd time even though the first time failed.
    #
    # I know this sounds silly, but we need some fix for this.
  rescue Exception
    dep = DependencyGrapher.new @cc, [path], dirs, flags
    dep.process
  end

  dep.sources.first.dependencies.sort
end

#compile(source, object) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/daedalus.rb', line 219

def compile(source, object)
  if source =~ /\.cpp$/
    cxx_compile(source, object)
  else
    c_compile(source, object)
  end
end

#cxx_compile(source, object) ⇒ Object



232
233
234
235
# File 'lib/daedalus.rb', line 232

def cxx_compile(source, object)
  @log.show "CXX", source
  @log.command "#{@cxx} #{@cflags.join(' ')} #{@cxxflags.join(' ')} -c -o #{object} #{source}"
end

#header_directoriesObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/daedalus.rb', line 174

def header_directories
  dirs = []
  @cflags.each do |fl|
    fl.split(/\s+/).each do |part|
      if part.index("-I") == 0
        dirs << part[2..-1]
      end
    end
  end
  dirs
end


252
253
254
255
# File 'lib/daedalus.rb', line 252

def link(path, files)
  @log.show "LD", path
  @log.command "#{@cxx} -o #{path} #{files.join(' ')} #{@libraries.join(' ')} #{@ldflags.join(' ')}"
end


263
264
265
266
# File 'lib/daedalus.rb', line 263

def link_shared(library, objects)
  @log.show "LDSHARED", library
  @log.command "#{@ldsharedxx} #{objects.join(' ')} -o #{library}"
end

#mtime(path) ⇒ Object



205
206
207
# File 'lib/daedalus.rb', line 205

def mtime(path)
  @mod_times[path]
end


237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/daedalus.rb', line 237

def pre_link(files)
  @pre_link_cmds.each do |cmd, blk|
    file_list = files

    if cmd.index("%objects%")
      if blk
        file_list = blk.call file_list
      end
      cmd = cmd.gsub(/%objects%/, file_list.join(" "))
    end

    @log.command cmd
  end
end

#sha1(path) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/daedalus.rb', line 209

def sha1(path)
  if @sha1s.key?(path)
    if File.mtime(path) > @sha1_mtimes[path]
      @sha1s.delete(path)
    end
  end

  @sha1s[path]
end