Class: CppEngine::Compiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/cpp_engine/compiler.rb', line 3

def flags
  @flags
end

#header_dirsObject

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_dirsObject

Returns the value of attribute lib_dirs.



3
4
5
# File 'lib/cpp_engine/compiler.rb', line 3

def lib_dirs
  @lib_dirs
end

#libsObject

Returns the value of attribute libs.



3
4
5
# File 'lib/cpp_engine/compiler.rb', line 3

def libs
  @libs
end

#obj_dirObject

Returns the value of attribute obj_dir.



3
4
5
# File 'lib/cpp_engine/compiler.rb', line 3

def obj_dir
  @obj_dir
end

#productObject

Returns the value of attribute product.



3
4
5
# File 'lib/cpp_engine/compiler.rb', line 3

def product
  @product
end

#source_filesObject

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

#compileObject



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


27
28
29
# File 'lib/cpp_engine/compiler.rb', line 27

def link
	execute link_command(@objs,@product)
end


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

Returns:

  • (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