Class: Bake::Subst

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

Class Method Summary collapse

Class Method Details

.itute(config, projName, isMainProj, toolchain, loadedConfig, configTcMap) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
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
93
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
# File 'lib/bake/subst.rb', line 42

def self.itute(config, projName, isMainProj, toolchain, loadedConfig, configTcMap)
  @@lazy = true
  @@config = config
  @@toolchain = toolchain
  @@loadedConfig = loadedConfig
  @@configTcMap = configTcMap
  
  @@configName = config.name
  @@projDir = config.parent.get_project_dir
  @@projName = projName
  @@resolvedVars = 0
  @@configFilename = config.file_name
  
  @@artifactName = ""
  if Metamodel::ExecutableConfig === config
    if not config.artifactName.nil?
      @@artifactName = config.artifactName.name
    elsif config.defaultToolchain != nil
      basedOnToolchain = Bake::Toolchain::Provider[config.defaultToolchain.basedOn]
      if basedOnToolchain != nil
        @@artifactName = projName+basedOnToolchain[:LINKER][:OUTPUT_ENDING]
      end
    end
  end
  
  if isMainProj
    @@userVarMap = {}
  else
    @@userVarMap = @@userVarMapMain.clone
  end
  
  config.set.each do |s|
 
    if (s.value != "" and s.cmd != "")
      Bake.formatter.printError("value and cmd attributes must be used exclusively", s)
      ExitHelper.exit(1)
    end
    
    if (s.value != "")
      setName = substString(s.name, s)
      if (setName.empty?)
        Bake.formatter.printWarning("Name of variable must not be empty - variable will be ignored", s)
      else
        @@userVarMap[s.name] = substString(s.value, s)
        ENV[s.name] = @@userVarMap[s.name] if s.env
      end
    else
      cmd_result = false
      consoleOutput = ""
      begin
        Dir.chdir(@@projDir) do
          cmd = [substString(s.cmd, s)]
          cmd_result, consoleOutput = ProcessHelper.run(cmd)
          @@userVarMap[s.name] = consoleOutput.chomp
          ENV[s.name] = @@userVarMap[s.name] if s.env
        end
      rescue Exception=>e
        consoleOutput = e.message
      end
      if (cmd_result == false)
        Bake.formatter.printWarning("Command not successful, variable #{s.name} will be set to \"\" (#{consoleOutput.chomp}).", s)
        @@userVarMap[s.name] = ""
        ENV[s.name] = "" if s.env
      end          
    end
    
  end
  
  @@userVarMapMain = @@userVarMap.clone if isMainProj
 
  3.times {
    subst(config);
    substToolchain(toolchain)
  }
  
  @@resolvedVars = 0
  lastFoundInVar = -1 
  100.times do
    subst(config)
    break if @@resolvedVars == 0 or (@@resolvedVars >= lastFoundInVar and lastFoundInVar >= 0)
    lastFoundInVar = @@resolvedVars 
  end      
  if (@@resolvedVars > 0)
    Bake.formatter.printError("Cyclic variable substitution detected", config.file_name)
    ExitHelper.exit(1)
  end
  
end

.lazyPathesObject

this is done lazy because usually there is no need to calculate that



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bake/subst.rb', line 8

def self.lazyPathes
  return unless @@lazy
  
  cppCmd = @@toolchain[:COMPILER][:CPP][:COMMAND]
  cCmd = @@toolchain[:COMPILER][:C][:COMMAND]
  asmCmd = @@toolchain[:COMPILER][:ASM][:COMMAND]
  archiverCmd = @@toolchain[:ARCHIVER][:COMMAND]
  linkerCmd = @@toolchain[:LINKER][:COMMAND]
    
  if @@config.toolchain
    linkerCmd = @@config.toolchain.linker.command if @@config.toolchain.linker and @@config.toolchain.linker.command != ""
    archiverCmd = @@config.toolchain.archiver.command if @@config.toolchain.linker and @@config.toolchain.archiver.command != ""
    @@config.toolchain.compiler.each do |c|
      if c.command != ""
        if c.ctype == :CPP
          cppCmd = c.command
        elsif c.ctype == :C
          cCmd = c.command
        elsif c.ctype == :ASM
          asmCmd = c.command
        end
      end
    end
  end 
  
  @@cppExe      = File.which(cppCmd) 
  @@cExe        = File.which(cCmd)
  @@asmExe      = File.which(asmCmd)
  @@archiverExe = File.which(archiverCmd)
  @@linkerExe   = File.which(linkerCmd)
  
  @@lazy = false
end

.subst(elem) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/bake/subst.rb', line 289

def self.subst(elem)
  elem.class.ecore.eAllAttributes_derived.each do |a|
    next if a.name == "file_name" or a.name == "line_number"
    return if Metamodel::Set === elem.class
    return if Metamodel::DefaultToolchain === elem
    return if Metamodel::Toolchain === elem.class
    next if a.eType.name != "EString" 
    substStr = substString(elem.getGeneric(a.name), elem)
    elem.setGeneric(a.name, substStr)
  end

  childsRefs = elem.class.ecore.eAllReferences.select{|r| r.containment}
  childsRefs.each do |c|
    elem.getGenericAsArray(c.name).each { |child| subst(child) }
  end     
end

.substString(str, elem = nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/bake/subst.rb', line 131

def self.substString(str, elem=nil)
  substStr = ""
  posSubst = 0
  while (true)
    posStart = str.index("$(", posSubst)
    break if posStart.nil?
    posEnd = str.index(")", posStart)
    break if posEnd.nil?
    posStartSub = str.index("$(", posStart+1)
    if (not posStartSub.nil? and posStartSub < posEnd) # = nested vars
      newStr = str[0,posStartSub] + substString(str[posStartSub..posEnd],elem)
      if (str.length + 1 > posEnd)
        str = newStr + str[posEnd+1..-1]
      else
        str = newStr
      end
      next
    end
    
    substStr << str[posSubst..posStart-1] if posStart>0
  
    @@resolvedVars += 1
    var = str[posStart+2..posEnd-1]

    splittedVar = var.split(",")
    
    if Bake.options.vars.has_key?(var)
      substStr << Bake.options.vars[var]  
    elsif @@userVarMap.has_key?(var)
      substStr << @@userVarMap[var]       
    elsif var == "MainConfigName"
      substStr << Bake.options.build_config
    elsif var == "MainProjectName"
      substStr << Bake.options.main_project_name
    elsif var == "MainProjectDir"
      substStr << Bake.options.main_dir
    elsif var == "ConfigName"
     substStr << @@configName
    elsif var == "ProjectName"
      substStr << @@projName
    elsif var == "ProjectDir"
      substStr << @@projDir
    elsif var == "OutputDir" or (splittedVar.length == 3 and splittedVar[0] == "OutputDir")
      if (var == "OutputDir")
        out_proj_name = @@projName
        out_conf_name = @@configName
      else
        out_proj_name = splittedVar[1].strip
        out_conf_name = splittedVar[2].strip
      end
      if @@loadedConfig.referencedConfigs.has_key?out_proj_name
        configs = @@loadedConfig.referencedConfigs[out_proj_name]
        config = configs.select {|c| c.name == out_conf_name }.first
        if config
           out_dir = nil
          if (config.toolchain and config.toolchain.outputDir and config.toolchain.outputDir != "")
            out_dir = config.toolchain.outputDir
          else
            out_dir = @@configTcMap[config][:OUTPUT_DIR]
          end
          if not out_dir
            if out_proj_name == Bake.options.main_project_name and out_conf_name == Bake.options.build_config
              out_dir = "build_" + Bake.options.build_config
            else
              out_dir = "build_" + out_conf_name + "_" + Bake.options.main_project_name + "_" + Bake.options.build_config
            end
          end
          out_dir = substString(out_dir, elem)
          if File.is_absolute?(out_dir)
            substStr << out_dir
          else
            substStr << Pathname.new(File.rel_from_to_project(@@projDir,config.get_project_dir,true)  + out_dir).cleanpath.to_s
          end
        else
          if Bake.options.verbose > 0
            msg = "Substitute variable '$(#{var})' with empty string, because config #{out_conf_name} not found for project #{out_proj_name}"
            Bake.formatter.printInfo(msg, elem ? elem : @@config)
          end
        end
      else
        if Bake.options.verbose > 0
          msg = "Substitute variable '$(#{var})' with empty string, because project #{out_proj_name} not found"
          Bake.formatter.printInfo(msg, elem ? elem : @@config)
        end
      end
    elsif splittedVar.length > 1 and splittedVar[0] == "OutputDir"
      if Bake.options.verbose > 0
        msg = "Substitute variable '$(#{var})' with empty string, because syntax of complex variable OutputDir is not $(OutputDir,<project name>,<config name>)"
        Bake.formatter.printInfo(msg, elem ? elem : @@config)
      end
    elsif var == "Time"
      substStr << Time.now.to_s
    elsif var == "Hostname"
      substStr << Socket.gethostname
    elsif var == "ArtifactName"
      substStr << @@artifactName
    elsif var == "ArtifactNameBase"
      substStr << @@artifactName.chomp(File.extname(@@artifactName))
    elsif var == "CPPPath"
      self.lazyPathes
      substStr << @@cppExe
    elsif var == "CPath"
      self.lazyPathes
      substStr << @@cExe
    elsif var == "ASMPath"
      self.lazyPathes
      substStr << @@asmExe
    elsif var == "ArchiverPath"
      self.lazyPathes
      substStr << @@archiverExe
    elsif var == "LinkerPath"
      self.lazyPathes
      substStr << @@linkerExe
    elsif var == "Roots"
      substStr << "___ROOTS___"
    elsif var == "/"
      substStr << File::SEPARATOR
    elsif var == ":"
      substStr << File::PATH_SEPARATOR
    elsif ENV[var]
      substStr << ENV[var]
    else
      if Bake.options.verbose >= 2
        msg = "Substitute variable '$(#{var})' with empty string"
        if elem
          Bake.formatter.printInfo(msg, elem)
        else
          Bake.formatter.printInfo(msg +  " in the toolchain", @@config)
        end
      end
    end
  
    posSubst = posEnd + 1
  end
  substStr << str[posSubst..-1]
  substStr
end

.substToolchain(elem) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/bake/subst.rb', line 269

def self.substToolchain(elem)
  if Hash === elem
    elem.each do |k, e|
      if Hash === e or Array === e
        substToolchain(e)
      elsif String === e
        elem[k] = substString(e)
      end
    end
  elsif Array === elem
    elem.each_with_index do |e, i|
      if Hash === e or Array === e
        substToolchain(e)
      elsif String === e
        elem[i] = substString(e)
      end
    end
  end
end