Class: Bake::Blocks::Executable

Inherits:
BlockBase show all
Defined in:
lib/blocks/executable.rb

Instance Attribute Summary collapse

Attributes inherited from BlockBase

#block, #projectDir

Instance Method Summary collapse

Methods inherited from BlockBase

#calcFileCmd, #check_config_file, #cleanProjectDir, #config_changed?, #defaultToolchainTime, isCmdLineEqual?, prepareOutput, #printCmd, #process_console_output, #process_result, writeCmdLineFile

Constructor Details

#initialize(block, config, referencedConfigs, compileBlock) ⇒ Executable

Returns a new instance of Executable.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/blocks/executable.rb', line 11

def initialize(block, config, referencedConfigs, compileBlock)
  super(block, config, referencedConfigs)
  @compileBlock = compileBlock

  block.set_executable(self)

  calcArtifactName
  calcMapFile
  calcLinkerScript

end

Instance Attribute Details

#compileBlockObject (readonly)

Returns the value of attribute compileBlock.



9
10
11
# File 'lib/blocks/executable.rb', line 9

def compileBlock
  @compileBlock
end

Instance Method Details

#calcArtifactNameObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blocks/executable.rb', line 31

def calcArtifactName
  if not @config.artifactName.nil? and @config.artifactName.name != ""
    baseFilename = @config.artifactName.name
    baseFilename += Bake::Toolchain.outputEnding(@block.tcs) if !baseFilename.include?(".")
  else
    baseFilename = "#{@projectName}#{Bake::Toolchain.outputEnding(@block.tcs)}"
  end
  if !@config.artifactExtension.nil? && @config.artifactExtension.name != "default"
    extension = ".#{@config.artifactExtension.name}"
    if baseFilename.include?(".")
      baseFilename = baseFilename.split(".")[0...-1].join(".")
    end
    baseFilename += ".#{@config.artifactExtension.name}"
  end
  @exe_name ||= File.join([@block.output_dir, baseFilename])
  if Bake.options.abs_path_in
    @exe_name = File.expand_path(@exe_name, @projectDir)
  end
  return @exe_name
end

#calcCmdlineFileObject



52
53
54
# File 'lib/blocks/executable.rb', line 52

def calcCmdlineFile()
  @exe_name + ".cmdline"
end

#calcLinkerScriptObject



23
24
25
26
27
28
29
# File 'lib/blocks/executable.rb', line 23

def calcLinkerScript
  if Metamodel::LibraryConfig === @config
    @linker_script = nil
  else
    @linker_script = @config.linkerScript.nil? ? nil : @block.convPath(@config.linkerScript)
  end
end

#calcMapFileObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/blocks/executable.rb', line 56

def calcMapFile
  @mapfile = nil
  if Metamodel::LibraryConfig === @config
    def @config.mapFile
      Metamodel::MapFile.new
    end
  end
  if (not Bake.options.docu) and (not @config.mapFile.nil?)
    if @config.mapFile.name == ""
      @mapfile = @exe_name.chomp(File.extname(@exe_name)) + ".map"
    else
      @mapfile = @config.mapFile.name
    end
  end
end

#cleanObject



213
214
215
# File 'lib/blocks/executable.rb', line 213

def clean
  return cleanProjectDir()
end

#executeObject



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
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
# File 'lib/blocks/executable.rb', line 104

def execute
  Dir.chdir(@projectDir) do
    
    subBlocks = @block.bes.select{|d| Metamodel::Dependency === d}.map { |d| ALL_BLOCKS["#{d.name},#{d.config}"] }
    if subBlocks.any? { |d| d.result == false }
      if Bake.options.stopOnFirstError
        Blocks::Block.set_delayed_result
        return true
      else
        return false
      end
    end

    allSources = []
    (subBlocks + [@block]).each do |b|
      Dir.chdir(b.projectDir) do
        b.getCompileBlocks.each do |c|
          srcs = c.calcSources(true, true).map { |s| File.expand_path(s) }
          allSources += srcs
        end
      end
    end
    
    duplicateSources = allSources.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first)
    duplicateSources.each do |d|
      Bake.formatter.printError("Source compiled more than once: #{d}")
    end
    ExitHelper.exit(1) if duplicateSources.length > 0

    libs, linker_libs_array = LibElements.calc_linker_lib_string(@block, @block.tcs)

    cmdLineCheck = false
    cmdLineFile = calcCmdlineFile()

    return true if ignore?
    reason = needed?(libs)
    if not reason
      cmdLineCheck = true
      reason = config_changed?(cmdLineFile)
    end

    linker = @block.tcs[:LINKER]

    cmd = Utils.flagSplit(linker[:PREFIX], true)
    cmd += Utils.flagSplit(linker[:COMMAND], true) # g++
    onlyCmd = cmd

    cmd += linker[:MUST_FLAGS].split(" ")
    cmd += Bake::Utils::flagSplit(linker[:FLAGS],true)
    cmd << linker[:EXE_FLAG]
    if linker[:EXE_FLAG_SPACE]
      cmd << @exe_name
    else
      cmd[cmd.length-1] += @exe_name
    end

    cmd += @compileBlock.objects
    if @linker_script
      if linker[:SCRIPT_SPACE]
        cmd << linker[:SCRIPT] # -T
        cmd << @linker_script # xy/xy.dld
      else
        cmd << linker[:SCRIPT]+@linker_script
      end
    end
    cmd += linker[:MAP_FILE_FLAG].split(" ") if @mapfile # -Wl,-m6
    if not linker[:MAP_FILE_PIPE] and @mapfile
      cmd[cmd.length-1] << @mapfile
    end
    cmd += Bake::Utils::flagSplit(linker[:LIB_PREFIX_FLAGS],true) # "-Wl,--whole-archive "
    cmd += linker_libs_array
    cmd += Bake::Utils::flagSplit(linker[:LIB_POSTFIX_FLAGS],true) # "-Wl,--no-whole-archive "

    realCmd = Bake.options.fileCmd ? calcFileCmd(cmd, onlyCmd, @exe_name, linker) : cmd
      
    # pre print because linking can take much time
    cmdLinePrint = Bake.options.fileCmd ? realCmd.dup : cmd.dup

    # some mapfiles are printed in stdout
    outPipe = (@mapfile and linker[:MAP_FILE_PIPE]) ? "#{@mapfile}" : nil
    cmdLinePrint << "> #{outPipe}" if outPipe

    if cmdLineCheck and BlockBase.isCmdLineEqual?(cmd, cmdLineFile)
      success = true
    else
      ToCxx.linkBlock

      BlockBase.prepareOutput(File.expand_path(@exe_name,@projectDir), @block)

      printCmd(cmdLinePrint, "Linking   #{@projectName} (#{@config.name}): #{@exe_name}", reason, false)
      BlockBase.writeCmdLineFile(cmd, cmdLineFile)
      success = true
      consoleOutput = ""
      retry_linking = Bake.options.dev_features.include?("retry-linking") ? 5 : 1
      begin
        success, consoleOutput = ProcessHelper.run(realCmd, false, false, outPipe) if !Bake.options.dry
        process_result(cmdLinePrint, consoleOutput, linker[:ERROR_PARSER], nil, reason, success)
      rescue Exception
        retry_linking -= 1
        retry if !success && retry_linking > 0
        raise
      end
      check_config_file()
    end

    return success
  end
end

#ignore?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/blocks/executable.rb', line 72

def ignore?
  Bake.options.prepro
end

#needed?(libs) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/blocks/executable.rb', line 76

def needed?(libs)
  return "because linkOnly was specified" if Bake.options.linkOnly

  # exe
  return "because executable does not exist" if not File.exist?(@exe_name)

  eTime = File.mtime(@exe_name)

  # linkerscript
  if @linker_script
    return "because linker script does not exist - will most probably result in an error" if not File.exist?(@linker_script)
    return "because linker script is newer than executable" if eTime < File.mtime(@linker_script)
  end

  # sources
  @compileBlock.objects.each do |obj|
    return "because object #{obj} does not exist" if not File.exist?(obj)
    return "because object #{obj} is newer than executable" if eTime < File.mtime(obj)
  end if @compileBlock

  # libs
  libs.each do |lib|
    return "because library #{lib} does not exist" if not File.exist?(lib)
    return "because library #{lib} is newer than executable" if eTime < File.mtime(lib)
  end
  false
end