Class: Cxx::RubyDsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projects, build_dir, toolchain_name, base_dir = '.', &option_block) ⇒ RubyDsl

Returns a new instance of RubyDsl.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cxx.rb', line 25

def initialize(projects, build_dir, toolchain_name, base_dir='.', &option_block)
  @build_dir = build_dir
  option_block.call if option_block
  toolchain = Cxxproject::Toolchain::Provider[toolchain_name]
  raise "no provider with name \"#{toolchain_name}\" found" unless toolchain
  @base_dir = base_dir
  cd(@base_dir, :verbose => false) do
    @projects = projects.to_a
  end

  Rake::application.deriveIncludes = true

  initialize_logging(build_dir)
  @all_tasks = instantiate_tasks(toolchain, build_dir)

  create_generic_tasks
  create_console_colorization
  create_multitask
  create_dont_bail_on_first_task
  describe_clean_task

  load_nontoolchain_plugins
end

Instance Attribute Details

#all_tasksObject

Returns the value of attribute all_tasks.



23
24
25
# File 'lib/cxx.rb', line 23

def all_tasks
  @all_tasks
end

#base_dirObject

Returns the value of attribute base_dir.



23
24
25
# File 'lib/cxx.rb', line 23

def base_dir
  @base_dir
end

#build_dirObject

Returns the value of attribute build_dir.



23
24
25
# File 'lib/cxx.rb', line 23

def build_dir
  @build_dir
end

Instance Method Details

#check_for_project_configsObject



159
160
161
162
163
164
165
# File 'lib/cxx.rb', line 159

def check_for_project_configs
  cd(@base_dir, :verbose => false) do
    @projects.each do |p|
      abort "project config #{p} cannot be found in #{Dir.pwd}!" unless File.exists?(p)
    end
  end
end

#create_console_colorizationObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/cxx.rb', line 91

def create_console_colorization
  # default is on
  Cxxproject::ColorizingFormatter.enabled = true
  desc 'Toggle colorization of console output (use true|t|yes|y|1|on for true ... everything else is false)'
  task :toggle_colorize, :on_off do |t, args|
    arg = args[:on_off] || 'false'
    on_off = arg.match(/(true|t|yes|y|1|on)$/) != nil
    Cxxproject::ColorizingFormatter.enabled = on_off
  end
end

#create_dont_bail_on_first_taskObject



74
75
76
77
78
79
# File 'lib/cxx.rb', line 74

def create_dont_bail_on_first_task
  desc 'dont bail on first error'
  task :dont_bail_on_first_error do
    Rake::Task.bail_on_first_error = false
  end
end

#create_filter_task(basename) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cxx.rb', line 120

def create_filter_task(basename)
  task :filter, :filter do |t, args|
    filter = ".*"
    if args[:filter]
      filter = "#{args[:filter]}"
    end
    filter = Regexp.new("#{basename}#{filter}")
    Rake::Task.tasks.each do |to_check|
      name = to_check.name
      if ("#{basename}:filter" != name)
        match = filter.match(name)
        if match
          to_check.invoke
        end
      end
    end
  end
end

#create_filter_task_with_namespace(basename) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cxx.rb', line 108

def create_filter_task_with_namespace(basename)
  if basename
    desc "invoke #{basename} with filter"
    namespace basename do
      create_filter_task("#{basename}:")
    end
  else
    desc 'invoke with filter'
    create_filter_task('')
  end
end

#create_generic_tasksObject



102
103
104
105
106
# File 'lib/cxx.rb', line 102

def create_generic_tasks
  tasks = [:lib, :exe, :run]
  tasks << nil
  tasks.each { |i| create_filter_task_with_namespace(i) }
end

#create_multitaskObject



81
82
83
84
85
86
87
88
89
# File 'lib/cxx.rb', line 81

def create_multitask
  desc 'set parallelization of multitask'
  task :multitask, :threads do |t, args|
    arg = args.threads
    if arg
      Rake::application.max_parallel_tasks = arg.to_i
    end
  end
end

#define_project_info_taskObject



205
206
207
208
209
210
211
212
# File 'lib/cxx.rb', line 205

def define_project_info_task
  desc "shows your defined projects"
  task :project_info do
    Cxxproject::ALL_BUILDING_BLOCKS.each_value do |bb|
      pp bb
    end
  end
end

#describe_clean_taskObject



70
71
72
# File 'lib/cxx.rb', line 70

def describe_clean_task
  Rake::Task[:clean].add_description('clean')
end

#eval_file(b, project_file) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cxx.rb', line 187

def eval_file(b, project_file)
  loadContext = EvalContext.new
  project_text = File.read(File.basename(project_file))
  begin
    loadContext.eval_project(project_text, project_file, Dir.pwd)
  rescue Exception => e
    puts "problems with #{File.join(b, project_file)} in dir: #{Dir.pwd}"
    puts project_text
    raise e
  end

  loadContext.all_blocks.each do |block|
    block.
      set_project_dir(Dir.pwd).
      set_config_files([Dir.pwd + "/" + project_file])
  end
end

#initialize_logging(build_dir) ⇒ Object



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

def initialize_logging(build_dir)
  @log = Logger.new(STDOUT)
  @log.formatter = proc { |severity, datetime, progname, msg|
    "#{severity}: #{msg}\n"
  }
  # Logger loglevels: fatal, error, warn, info, debug
  # Rake --verbose -> info
  # Rake --trace -> debug
  @log.level = Logger::ERROR
  @log.level = Logger::INFO if RakeFileUtils.verbose == true
  @log.level = Logger::DEBUG if Rake::application.options.trace
  @log.debug "initializing for build_dir: \"#{build_dir}\", base_dir: \"#{@base_dir}\""
end

#instantiate_tasks(toolchain, build_dir) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cxx.rb', line 139

def instantiate_tasks(toolchain, build_dir)
  check_for_project_configs

  if @log.debug?
    @log.debug "project_configs:"
    @projects.each { |c| @log.debug " *  #{c}" }
  end
  register_projects()
  Cxxproject::ALL_BUILDING_BLOCKS.values.each do |block|
    prepare_block(block, toolchain, build_dir)
  end
  Cxxproject::ALL_BUILDING_BLOCKS.values.inject([]) do |memo,block|
    @log.debug "creating tasks for block: #{block.name}/taskname: #{block.get_task_name} (#{block})"
    if block.name != block.get_task_name
      task block.name => block.get_task_name # create task with simple name of buildinblock
    end
    memo << block.convert_to_rake()
  end
end

#load_nontoolchain_pluginsObject



49
50
51
52
53
54
# File 'lib/cxx.rb', line 49

def load_nontoolchain_plugins
  registry = Frazzle::Registry.new('cxxproject', '_', '-')
  registry.get_all_plugins.select { |name|name.index('toolchain') == nil }.each do |plugin|
    registry.load_plugin(plugin, Cxxproject::PluginContext.new(self, Cxxproject::ALL_BUILDING_BLOCKS, @log))
  end
end

#prepare_block(block, toolchain, build_dir) ⇒ Object



167
168
169
170
171
# File 'lib/cxx.rb', line 167

def prepare_block(block, toolchain, build_dir)
  block.set_tcs(toolchain) unless block.has_tcs?
  block.set_output_dir(Dir.pwd + "/" + build_dir)
  block.complete_init()
end

#register_projectsObject



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cxx.rb', line 173

def register_projects()
  cd(@base_dir,:verbose => false) do |b|
    @projects.each_with_index do |project_file, i|
      @log.debug "register project #{project_file}"
      dirname = File.dirname(project_file)
      @log.debug "dirname for project was: #{dirname}"
      cd(dirname,:verbose => false) do | base_dir |
        @log.debug "register project #{project_file} from within directory: #{Dir.pwd}"
        eval_file(b, File.basename(project_file))
      end
    end
  end
end