Class: Bake::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#referencedConfigsObject (readonly)

Returns the value of attribute referencedConfigs.



6
7
8
# File 'lib/bake/config/loader.rb', line 6

def referencedConfigs
  @referencedConfigs
end

Instance Method Details

#bailOutVer(reqVersion) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/bake/config/loader.rb', line 69

def bailOutVer(reqVersion)
  text1 = (reqVersion.minimum.empty? ? "" : "minimum = #{reqVersion.minimum}")
  text2 = ((reqVersion.minimum.empty? or reqVersion.maximum.empty?) ? "" : ", ")
  text3 = (reqVersion.maximum.empty? ? "" : "maximum = #{reqVersion.maximum}")
  Bake.formatter.printError("Not compatible with installed bake version: #{text1 + text2 + text3}", reqVersion)
  ExitHelper.exit(1)
end

#checkRootsObject



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/bake/config/loader.rb', line 253

def checkRoots()
  @potentialProjs = []
  Bake.options.roots.each do |r|
    if (r.length == 3 && r.include?(":/"))
      r = r + Bake.options.main_project_name # glob would not work otherwise on windows (ruby bug?)
    end
    r = r+"/**{,/*/**}/Project.meta"
    @potentialProjs.concat(Dir.glob(r))
  end
  
  @potentialProjs = @potentialProjs.uniq.sort
end

#checkVer(reqVersion) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bake/config/loader.rb', line 77

def checkVer(reqVersion)
  return if reqVersion.nil?
  min = reqVersion.minimum.split(".")
  max = reqVersion.maximum.split(".")
  cur = Bake::Version.number.split(".")
  
  if !checkVerFormat(min) or !checkVerFormat(max)
    Bake.formatter.printError("Version must be <major>.<minor>.<patch> whereas minor and patch are optional and all numbers >= 0.", reqVersion)
    ExitHelper.exit(1)
  end
  
  [min,max,cur].each { |arr| arr.map! {|x| x.to_i} }
  min.each_with_index do |v,i|
    break if v < cur[i]
    bailOutVer(reqVersion) if v > cur[i]
  end
  max.each_with_index do |v,i|
    break if v > cur[i]
    bailOutVer(reqVersion) if v < cur[i]
  end
end

#checkVerFormat(ver) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/bake/config/loader.rb', line 60

def checkVerFormat(ver)
  return true if ver.empty?
  return false if ver.length > 3
  ver.each do |v|
    return false if not /\A\d+\z/.match(v)
  end
  true
end

#filterStep(step, globalFilterStr) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/bake/config/loader.rb', line 267

def filterStep(step, globalFilterStr)
  
  # 1st prio: explicit single filter
  if step.filter != "" 
    return true if  Bake.options.exclude_filter.include?step.filter
    return false if Bake.options.include_filter.include?step.filter
  end 

  # 2nd prio: explicit global filter
  if globalFilterStr != nil
    return true if  Bake.options.exclude_filter.include?globalFilterStr
    return false if Bake.options.include_filter.include?globalFilterStr
  end      

  # 3nd prio: default
  return true if step.default == "off"
  false      
end

#filterStepsObject



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/bake/config/loader.rb', line 286

def filterSteps
  @referencedConfigs.each do |projName, configs|
    configs.each do |config|
      config.startupSteps.step  = config.startupSteps.step.delete_if  { |step| filterStep(step, "STARTUP") }  if config.startupSteps
      config.preSteps.step      = config.preSteps.step.delete_if      { |step| filterStep(step, "PRE") }  if config.preSteps
      config.postSteps.step     = config.postSteps.step.delete_if     { |step| filterStep(step, "POST") } if config.postSteps
      config.exitSteps.step     = config.exitSteps.step.delete_if     { |step| filterStep(step, "EXIT") }  if config.exitSteps
      if Metamodel::CustomConfig === config and config.step
        config.step = nil if filterStep(config.step, nil)
      end
    end
  end
end

#getFullProject(configs, configname) ⇒ Object

note: configs is never empty



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
41
# File 'lib/bake/config/loader.rb', line 8

def getFullProject(configs, configname) # note: configs is never empty
  
  if (configname == "")
    if configs[0].parent.default != ""
      configname = configs[0].parent.default
    else
      Bake.formatter.printError("No default config specified", configs[0].file_name) 
      ExitHelper.exit(1)
    end
  end
  
  config = nil
  configs.each do |c|
    if c.name == configname
      if config
        Bake.formatter.printError("Config '#{configname}' found more than once",config.file_name)
        ExitHelper.exit(1)
      end
      config = c
    end
  end 
  
  if not config
    Bake.formatter.printError("Config '#{configname}' not found", configs[0].file_name)
    ExitHelper.exit(1)
  end
  
  if config.extends != ""
    parent,parentConfigName = getFullProject(configs, config.extends)
    MergeConfig.new(config, parent).merge()
  end
  
  [config, configname]
end

#loadObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/bake/config/loader.rb', line 300

def load()
  @loader = Loader.new
  cache = CacheAccess.new()
  @referencedConfigs = cache.load_cache unless Bake.options.nocache

  # cache invalid or forced to reload
  if @referencedConfigs.nil?
    loadMainMeta
    checkRoots
    while dep = @depsPending.shift
      loadMeta(dep)
    end
   
    filterSteps
    
    cache.write_cache(@project_files, @referencedConfigs)
  end
  
end

#loadMainMetaObject



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
# File 'lib/bake/config/loader.rb', line 223

def loadMainMeta()
  mainMeta = Bake.options.main_dir+"/Project.meta"
  if not File.exist?(mainMeta)
    Bake.formatter.printError("Error: #{mainMeta} not found")
    ExitHelper.exit(1)
  end
    
  @project_files = []
  
  configs = loadProjMeta(mainMeta)
  @loadedConfigs = {}
  @loadedConfigs[Bake.options.main_project_name] = configs
  
  if (Bake.options.build_config == "" and configs[0].parent.default == "")
    ConfigNames.print(configs, nil, mainMeta)
  end
  
  config, Bake.options.build_config = getFullProject(configs,Bake.options.build_config)
  @referencedConfigs = {}
  @referencedConfigs[Bake.options.main_project_name] = [config]
    
  if config.defaultToolchain == nil
    Bake.formatter.printError("Main project configuration must contain DefaultToolchain", config)
    ExitHelper.exit(1)
  end
  
  validateDependencies(config)
  @depsPending = config.dependency
end

#loadMeta(dep) ⇒ Object



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
# File 'lib/bake/config/loader.rb', line 163

def loadMeta(dep)
  dep_subbed = dep.name.gsub(/\\/,"/")
  if dep_subbed.include?":" or dep_subbed.include?"../" or dep_subbed.start_with?"/" or dep_subbed.end_with?"/"
    Bake.formatter.printError("#{dep.name}  is invalid", dep)
    ExitHelper.exit(1)
  end
  dep_path, dismiss, dep_name = dep_subbed.rpartition("/")
  
  # file not loaded yet
  if not @loadedConfigs.include?dep_name
    
    pmeta_filenames = []
      
    @potentialProjs.each do |pp|
      if pp.include?("/" + dep_subbed + "/Project.meta") or pp == (dep_subbed + "/Project.meta") 
        pmeta_filenames << pp
      end
    end
  
    if pmeta_filenames.empty?
      Bake.formatter.printError("#{dep.name}/Project.meta not found", dep)
      ExitHelper.exit(1)
    end
    
    if pmeta_filenames.length > 1
      Bake.formatter.printWarning("Project #{dep.name} exists more than once", dep)
      chosen = " (chosen)"
      pmeta_filenames.each do |f|
        Bake.formatter.printWarning("  #{File.dirname(f)}#{chosen}")
        chosen = ""
      end
    end
    
    @loadedConfigs[dep_name] = loadProjMeta(pmeta_filenames[0])
  else
    folder = @loadedConfigs[dep_name][0].get_project_dir
    if not folder.include?dep_subbed
      Bake.formatter.printError("Cannot load #{dep.name}, because #{folder} already loaded", dep)
      ExitHelper.exit(1)
    end
    
  end
        
  # get config
  config, dep.config = getFullProject(@loadedConfigs[dep_name], dep.config)
  dep.name = dep_name
  
  # config not referenced yet
  if not @referencedConfigs.include?dep_name
    @referencedConfigs[dep_name] = [config] 
  elsif @referencedConfigs[dep_name].index { |c| c.name == dep.config } == nil
    @referencedConfigs[dep_name] << config
  else
    return
  end
  
  validateDependencies(config)
  @depsPending += config.dependency
end

#loadProjMeta(filename) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/bake/config/loader.rb', line 99

def loadProjMeta(filename)
  
  symlinkCheck(filename)
  
  @project_files << filename
  f = @loader.load(filename)

  config = nil
  
  if f.root_elements.length != 1 or not Metamodel::Project === f.root_elements[0]
    Bake.formatter.printError("Config file must have exactly one 'Project' element as root element", filename)
    ExitHelper.exit(1)
  end
  
  reqVersion = f.root_elements[0].getRequiredBakeVersion

  checkVer(reqVersion)
 
  configs = f.root_elements[0].getConfig
  
  if configs.length == 0
    Bake.formatter.printError("No config found", filename)
    ExitHelper.exit(1)
  end
  
  configs.each do |config|
    if config.respond_to?("toolchain") and config.toolchain
      config.toolchain.compiler.each do |c|
        if not c.internalDefines.nil? and c.internalDefines != ""
          Bake.formatter.printError("InternalDefines only allowed in DefaultToolchain", c.internalDefines)
          ExitHelper.exit(1)
        end
      end
    end
    
    config.includeDir.each do |inc|
      if not ["front", "back", ""].include?inc.inject
        Bake.formatter.printError("inject of IncludeDir must be 'front' or 'back'", inc) 
        ExitHelper.exit(1)
      end
      if not ["front", "back", ""].include?inc.infix
        Bake.formatter.printError("infix of IncludeDir must be 'front' or 'back'", inc) 
        ExitHelper.exit(1)
      end
      if (inc.infix != "" and inc.inject != "")
        Bake.formatter.printError("IncludeDir must have inject OR infix (deprecated)", inc) 
        ExitHelper.exit(1)
      end
    end if config.respond_to?("includeDir")
  end
  configs
end

#symlinkCheck(filename) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bake/config/loader.rb', line 43

def symlinkCheck(filename)
  dirOfProjMeta = File.dirname(filename)
  Dir.chdir(dirOfProjMeta) do
    if Dir.pwd != dirOfProjMeta and File.dirname(Dir.pwd) != File.dirname(dirOfProjMeta)
      isSym = false
      begin
        isSym = File.symlink?(dirOfProjMeta)
      rescue
      end
      if isSym
        Bake.formatter.printError("Symlinks only allowed with the same parent dir as the target: #{dirOfProjMeta} --> #{Dir.pwd}", filename)
        ExitHelper.exit(1)
      end
    end
  end
end

#validateDependencies(config) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/bake/config/loader.rb', line 153

def validateDependencies(config)
  config.dependency.each do |dep|
    if dep.name.include?"$" or dep.config.include?"$"
      Bake.formatter.printError("No variables allowed in Dependency definition", dep)
      ExitHelper.exit(1)
    end
    dep.name = config.parent.name if dep.name == ""
  end
end