Class: GccCompiler

Inherits:
Compiler show all
Defined in:
lib/rakepp/gcccompiler.rb

Direct Known Subclasses

GccWin32Compiler, LinuxCompiler, OsxCompiler

Instance Attribute Summary

Attributes inherited from Compiler

#compileflags, #targetDir

Instance Method Summary collapse

Methods inherited from Compiler

#add_tasks, #initialize

Constructor Details

This class inherits a constructor from Compiler

Instance Method Details

#add_binary_lib_tasks(artifact) ⇒ Object



146
147
148
# File 'lib/rakepp/gcccompiler.rb', line 146

def add_binary_lib_tasks(artifact)
  artifact.outFile = "lib#{artifact.name}.a"
end

#add_exe_tasks(artifact) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rakepp/gcccompiler.rb', line 154

def add_exe_tasks(artifact)
  outDir = File.join(@targetDir, artifact.name)
  objects = artifact.sources.map { |source| ObjectFile.new(self, source, outDir, artifact.tr_includes).outFile }
  exesName = File.join(@targetDir, 'exes')
  artifact.outFile = File.join(exesName, artifact.name + '.exe')
  desc "Create Exe #{artifact.outFile}"
  theTask = file artifact.outFile => objects do | task |
    command = "g++ #{compileflags} -o #{artifact.outFile}"
    objects.each { |o| command += " #{o}" }
    command += start_of_libs()
    LibHelper.tr_libs(artifact.libs).each { |lib| command += add_lib(task, lib) }
    command += end_of_libs()
    sh command
    do_additional_work_for_exe(artifact)
  end

  add_transitive_library_prerequisites(theTask, artifact)

  file artifact.outFile => [exesName]
  All.add(artifact.outFile)
  directory exesName
end

#add_framework_tasks(artifact) ⇒ Object



150
151
152
# File 'lib/rakepp/gcccompiler.rb', line 150

def add_framework_tasks(artifact)
  artifact.outFile = artifact.name
end

#add_lib(task, lib) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rakepp/gcccompiler.rb', line 192

def add_lib(task, lib)
  if (lib.instance_of?(BinaryLib)) then
    return " -L#{lib.path||'/usr/lib'} -l#{lib.name} "
  elsif (lib.instance_of?(SourceLib)) then
    return "#{add_lib_prefix(lib)} -L#{targetDir}/libs -l#{lib.name} #{add_lib_suffix(lib)}"
  elsif (lib.instance_of?(SharedLib)) then
    return " #{targetDir}/libs/#{lib.name}.so "
  else
    raise "#{lib} not supported"
  end
end

#add_lib_prefix(lib) ⇒ Object



204
205
206
# File 'lib/rakepp/gcccompiler.rb', line 204

def add_lib_prefix(lib)
  return ''
end

#add_lib_suffix(lib) ⇒ Object



208
209
210
# File 'lib/rakepp/gcccompiler.rb', line 208

def add_lib_suffix(lib)
  return ''
end

#add_object_tasks(artifact) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rakepp/gcccompiler.rb', line 6

def add_object_tasks(artifact)
  outName = File.join(artifact.targetDir, artifact.source.to_o)
  outDir = File.dirname(outName)

  artifact.outFile = outName
  depFile = artifact.dep_file

  depFileTask = file depFile => artifact.source.fullPath { | task | calc_dependencies(artifact, depFile) }
  recreateDepFileTask = recreate_task(depFile, artifact, depFileTask)

  outFile = file outName => ["#{outName}.apply"] do | task |
    sh "#{compiler(artifact)} -c #{defines(artifact)} #{includes(artifact)} -o #{outName} #{artifact.source.fullPath}"
  end

  applyTask = apply_task(outFile, recreateDepFileTask, depFile)
  outFile.enhance([applyTask])
  dirs = [artifact.targetDir, outDir]
  dirs.each { |d| directory d }
  [outFile, depFileTask, recreateDepFileTask].each { |task| task.enhance(dirs) }
  All.addObject(artifact)
  All.addDepFile(depFile)
end

#add_shared_lib_tasks(artifact) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rakepp/gcccompiler.rb', line 123

def add_shared_lib_tasks(artifact)
  outDir = File.join(@targetDir, artifact.name)
  objects = artifact.sources.map { |source| ObjectFile.new(self, source, outDir, artifact.tr_includes, artifact.privateDefines).outFile }

  libsName = File.join(@targetDir, 'libs')
  outName = File.join(libsName, "#{artifact.name}.#{shared_extension()}")
  artifact.outFile = outName
  desc "Create SharedLib #{outName}"
  theTask = file outName => objects do | task |
    command = start_of_shared_lib_command(outName, artifact)
    objects.each { |o| command += " #{o}" }
    LibHelper.tr_libs(artifact.libs).each { |lib| command += add_lib(task, lib) }
    command += " -o #{outName}"
    sh command
  end

  add_transitive_library_prerequisites(theTask, artifact)

  file outName => [libsName]
  All.add(outName)
  directory libsName
end

#add_source_lib_tasks(artifact) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rakepp/gcccompiler.rb', line 102

def add_source_lib_tasks(artifact)
  outDir = File.join(@targetDir, artifact.name)

  objects = artifact.sources.map { |source| ObjectFile.new(self, source, outDir, artifact.tr_includes, artifact.privateDefines).outFile }

  libsName = File.join(@targetDir, 'libs')
  outName = File.join(libsName, "lib#{artifact.name}.a")

  artifact.outFile = outName
  desc "Create SourceLib #{outName}"
  theTask = file outName => objects do | task |
    sh objects.inject(start_of_source_lib_command(outName, artifact)) { | command, o | "#{command} #{o}" }
  end

  add_transitive_library_prerequisites(theTask, artifact)

  file outName => [libsName]
  All.add(outName)
  directory libsName
end

#add_transitive_library_prerequisites(theTask, artifact) ⇒ Object



186
187
188
189
190
# File 'lib/rakepp/gcccompiler.rb', line 186

def add_transitive_library_prerequisites(theTask, artifact)
  LibHelper.tr_libs(artifact.libs).each do |lib|
    theTask.prerequisites << lib.outFile unless lib.kind_of?(BinaryLib)
  end
end

#apply_task(outFile, recreateDepFileTask, depFile) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/rakepp/gcccompiler.rb', line 29

def apply_task(outFile, recreateDepFileTask, depFile)
  task "#{outFile}.apply" => recreateDepFileTask do |task|
    deps = YAML.load_file(depFile)
    if (deps)
      outFile.enhance(deps)
    end
    outFile.enhance([depFile])
  end
end

#calc_dependencies(artifact, taskToEnhance) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rakepp/gcccompiler.rb', line 85

def calc_dependencies(artifact, taskToEnhance)
  source = artifact.source
  command = "#{compiler(artifact)} -M #{defines(artifact)} #{includes(artifact)} #{source.fullPath}"
  deps = `#{command}`
  if deps.length == 0
    raise 'cannot calc dependencies'
  end
  deps = deps.gsub(/\\\n/,'').split()[1..-1]
  File.open(artifact.dep_file, 'wb') do |f|
    f.write(deps.to_yaml)
  end
end

#compiler(artifact) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rakepp/gcccompiler.rb', line 59

def compiler(artifact)
  if ((artifact.source.fullPath.index(".cpp") != nil) ||
      (artifact.source.fullPath.index(".cxx") != nil)) then
    return "g++ #{compileflags}"
  else
    return "gcc #{compileflags}"
  end
end

#defines(artifact) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rakepp/gcccompiler.rb', line 68

def defines(artifact)
  res = ""
  allDefines = @defines + artifact.privateDefines
  allDefines.each do |define|
    res << "-D#{define} "
  end
  return res
end

#dep_missing?(deps) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/rakepp/gcccompiler.rb', line 53

def dep_missing?(deps)
  deps.inject(false) do |memo, d|
    memo || !File.exists?(d)
  end
end

#do_additional_work_for_exe(artifact) ⇒ Object



183
184
# File 'lib/rakepp/gcccompiler.rb', line 183

def do_additional_work_for_exe(artifact)
end

#end_of_libsObject



180
181
182
# File 'lib/rakepp/gcccompiler.rb', line 180

def end_of_libs()
  return ''
end

#includes(artifact) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/rakepp/gcccompiler.rb', line 77

def includes(artifact)
  res = ""
  artifact.includes.each do |aInclude|
    res << "-I#{aInclude} "
  end
  return res
end

#recreate_task(depFile, artifact, depFileTask) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rakepp/gcccompiler.rb', line 39

def recreate_task(depFile, artifact, depFileTask)
  task "#{depFile}.recreate" do | task |
    if (!File.exists?(depFile))
      calc_dependencies(artifact, depFile)
    end
    deps = YAML.load_file(depFile)
    if (dep_missing?(deps))
      calc_dependencies(artifact, depFile)
      deps = YAML.load_file(depFile)
    end
    depFileTask.enhance(deps)
  end
end

#start_of_libsObject



177
178
179
# File 'lib/rakepp/gcccompiler.rb', line 177

def start_of_libs()
  return ''
end

#start_of_source_lib_command(outName, artifact) ⇒ Object



98
99
100
# File 'lib/rakepp/gcccompiler.rb', line 98

def start_of_source_lib_command(outName, artifact)
  return "ar -r #{outName}"
end