Class: Canoe::DepAnalyzer

Inherits:
Object
  • Object
show all
Extended by:
Err, WorkSpaceUtil
Includes:
Err, SystemCommand
Defined in:
lib/dependence.rb

Overview

This class is the key component of canoe, which offers file dependency analysis functionality.

A DepAnalyzer takes a directory as input, sources files and corresponding header files in this directory should have same name, e.g. test.cpp and test.hpp. DepAnalyzer would read every source file and recursively process user header files included in this source file to find out all user header files this source file depends on. Based on dependencies built in previous stage, DepAnalyzer determines which files should be recompiled and return these files to caller. Dependencies could be written to a file to avoid wasting time parsing all files, Depanalyzer would read from this file to construct dependencies. But if sources files included new headers or included headers are revmoed, Depanalyzer should rebuild the whole dependencies.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WorkSpaceUtil

comp_to_obj, current_workspace, extract_one_file, extract_one_file_obj, file_to_obj, src_to_obj

Methods included from Err

abort_on_err, warn_on_err

Methods included from SystemCommand

#issue_command, #run_command

Constructor Details

#initialize(dir, src_sfx = 'cpp', hdr_sfx = 'hpp') ⇒ DepAnalyzer

Returns a new instance of DepAnalyzer.



92
93
94
95
96
97
# File 'lib/dependence.rb', line 92

def initialize(dir, src_sfx = 'cpp', hdr_sfx = 'hpp')
  @dir = dir
  @deps = Hash.new []
  @source_suffix = src_sfx
  @header_suffix = hdr_sfx
end

Class Method Details

.compiling_filter(deps, build_time, src_sfx = 'cpp', hdr_sfx = 'hpp') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dependence.rb', line 34

def compiling_filter(deps, build_time, src_sfx = 'cpp', hdr_sfx = 'hpp')
  files = []
  @recompiles = {}
  deps.each_key do |k|
    @recompiles[k] = false
  end
  deps.each do |k, v|
    next if k.end_with? ".#{hdr_sfx}"

    # first analyze dependency to discover circular includes
    v.each do |f|
      next unless mark(f, build_time, deps) || mark(f.sub(".#{hdr_sfx}", ".#{src_sfx}"), build_time, deps)

      files << k
      @recompiles[k] = true
      break
    end

    next if @recompiles[k]

    if should_recompile?(k, build_time)
      files << k
      @recompiles[k] = true
    end
  end
  files
end

.read_from(filename) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dependence.rb', line 22

def read_from(filename)
  File.open(filename, 'r') do |f|
    ret = Hash.new []
    f.each_with_index do |line, i|
      entry = line.split(': ')
      abort_on_err("Bad .canoe.deps format, line #{i + 1}") unless entry.length == 2
      ret[entry[0]] = entry[1].split
    end
    ret
  end
end

Instance Method Details

#build_dependence(include_path) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dependence.rb', line 99

def build_dependence(include_path)
  files = SourceFiles.get_all(@dir) do |f|
    f.end_with?(".#{@source_suffix}") || f.end_with?(".#{@header_suffix}")
  end

  @deps = Hash.new []
  files.each do |fname|
    @deps[fname] = get_all_headers include_path, fname, @header_suffix
  end

  @deps
end

#build_to_file(include_path, filename) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dependence.rb', line 112

def build_to_file(include_path, filename)
  build_dependence include_path

  File.open(filename, 'w') do |f|
    @deps.each do |k, v|
      f.write "#{k}: #{v.join(' ')}\n"
    end
  end

  @deps
end