Class: CppEngine::Compiler
- Inherits:
-
Object
- Object
- CppEngine::Compiler
- Defined in:
- lib/cpp_engine/compiler.rb
Instance Attribute Summary collapse
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#header_dirs ⇒ Object
Returns the value of attribute header_dirs.
-
#lib_dirs ⇒ Object
Returns the value of attribute lib_dirs.
-
#libs ⇒ Object
Returns the value of attribute libs.
-
#obj_dir ⇒ Object
Returns the value of attribute obj_dir.
-
#product ⇒ Object
Returns the value of attribute product.
-
#source_files ⇒ Object
Returns the value of attribute source_files.
Instance Method Summary collapse
- #compile ⇒ Object
- #compile_command(source_file) ⇒ Object
- #dependent_headers(source_path) ⇒ Object
- #execute(command) ⇒ Object
- #get_obj_path(source_path) ⇒ Object
-
#initialize(params = {}) ⇒ Compiler
constructor
A new instance of Compiler.
- #link ⇒ Object
- #link_command(objs, product) ⇒ Object
- #outdated?(source_path) ⇒ Boolean
Constructor Details
#initialize(params = {}) ⇒ Compiler
Returns a new instance of Compiler.
4 5 6 7 8 9 10 |
# File 'lib/cpp_engine/compiler.rb', line 4 def initialize params={} self.flags = params[:flags] || [] self.header_dirs = params[:header_dirs] || [] self.lib_dirs = params[:lib_dirs] || [] self.libs = params[:libs] || [] self.product = params[:product] || "executable" end |
Instance Attribute Details
#flags ⇒ Object
Returns the value of attribute flags.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def flags @flags end |
#header_dirs ⇒ Object
Returns the value of attribute header_dirs.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def header_dirs @header_dirs end |
#lib_dirs ⇒ Object
Returns the value of attribute lib_dirs.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def lib_dirs @lib_dirs end |
#libs ⇒ Object
Returns the value of attribute libs.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def libs @libs end |
#obj_dir ⇒ Object
Returns the value of attribute obj_dir.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def obj_dir @obj_dir end |
#product ⇒ Object
Returns the value of attribute product.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def product @product end |
#source_files ⇒ Object
Returns the value of attribute source_files.
3 4 5 |
# File 'lib/cpp_engine/compiler.rb', line 3 def source_files @source_files end |
Instance Method Details
#compile ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/cpp_engine/compiler.rb', line 17 def compile @objs=[] source_files.each{|source_file| @objs<<get_obj_path(source_file) if outdated? source_file execute compile_command(source_file) end } end |
#compile_command(source_file) ⇒ Object
31 32 33 34 |
# File 'lib/cpp_engine/compiler.rb', line 31 def compile_command source_file header_dirs_flags=header_dirs.map{|dir| "-I#{dir}"} return "g++ -c #{(flags+header_dirs_flags).join(" ")} #{source_file}" end |
#dependent_headers(source_path) ⇒ Object
55 56 57 58 |
# File 'lib/cpp_engine/compiler.rb', line 55 def dependent_headers source_path result= `g++ -MM #{source_path}` return result.gsub(/\w+\.o:\s+/,"").gsub(/(\n)|(\\)/,"").split(/\s+/).select{|f| f=~/(\.h$)|(.hpp$)/} end |
#execute(command) ⇒ Object
12 13 14 15 |
# File 'lib/cpp_engine/compiler.rb', line 12 def execute command puts command system command end |
#get_obj_path(source_path) ⇒ Object
60 61 62 |
# File 'lib/cpp_engine/compiler.rb', line 60 def get_obj_path source_path return source_path.sub(".cpp",".o") end |
#link ⇒ Object
27 28 29 |
# File 'lib/cpp_engine/compiler.rb', line 27 def link execute link_command(@objs,@product) end |
#link_command(objs, product) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/cpp_engine/compiler.rb', line 36 def link_command objs, product lib_dirs_flags = lib_dirs.map{|dir| "-L#{dir}"} lib_flags = libs.map{|lib| "-l#{lib}"} return "g++ #{(flags+lib_dirs_flags+lib_flags).join(" ")} #{objs.join(" ")} -o #{product}" end |
#outdated?(source_path) ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cpp_engine/compiler.rb', line 44 def outdated? source_path obj=get_obj_path source_path return true if !File.exists?(obj) return true if File.mtime(source_path)>File.mtime(obj) dependent_headers(source_path).each{|header| return true if File.mtime(header)>File.mtime(obj) } return false end |