Module: L::C

Defined in:
lib/rub/l/c.rb

Overview

C Library

Defined Under Namespace

Modules: Compiler, CompilerClang, CompilerGCC Classes: TargetCSource, TargetGeneratedHeader

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(src) ⇒ Set<Pathname>

Compile source files.

Parameters:

  • src (Set<Pathname,String>, Array<Pathname,String>, Pathname, String)

    The source files to compile and generated headers.

  • opt (Options)

    An options object.

Returns:

  • (Set<Pathname>)

    The resulting object files.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rub/l/c.rb', line 271

def self.compile(src)
	src = R::Tool.make_set_paths src
	
	headers = Set.new
	src.keep_if do |s|
		if s.extname.match /[H]/i
		   headers << s
		   false
		else
			true
		end
	end

	src.map! do |s|
		out = R::Env.out_dir + 'l/c/' + C.unique_segment(self) + "#{s.basename}.o"
		
		R.find_target(s) or TargetCSource.new(self, s, headers)
		::C.generator(s, compiler.compile_command(self, s, out), out, desc:"Compiling")
	end
	src.flatten!
	src
end

.compiler=(name) ⇒ Object



42
43
44
# File 'lib/rub/l/c.rb', line 42

def self.compiler=(name)
	@compiler = get_compiler name
end

.compilersHash{Symbol=>Compiler}

All available compilers.

Returns:



32
# File 'lib/rub/l/c.rb', line 32

cattr_accessor :compilers

.debugtrue, false

Default debug symbols setting.

This determines if the compiler should produce debugging symbols.

Returns:

  • (true, false)


85
# File 'lib/rub/l/c.rb', line 85

cattr_accessor :debug

.defineHash{String=>String,true,nil}

A list of macros to define. ‘nil` can be used to undefine a macro.

Returns:

  • (Hash{String=>String,true,nil})


114
# File 'lib/rub/l/c.rb', line 114

cattr_accessor :define

.generate_header(name, vals) ⇒ Object

Generate a header

Generates a header with information in it.



443
444
445
446
447
448
449
450
451
452
453
# File 'lib/rub/l/c.rb', line 443

def self.generate_header(name, vals)
	h = C.unique_path("#{name}.h", vals)
	c = C.unique_path("#{name}.c", vals)
	
	t = TargetGeneratedHeader.new(self, name, h, c, vals)
	t.register
	
	include_dirs << h.dirname
	
	t.output
end

.get_compiler(name) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/rub/l/c.rb', line 236

def self.get_compiler(name)
	if name.is_a? Symbol
		compilers[name]
	else
		name
	end
end

.include_dirsArray<Pathname>

A list of directories to search for header files.

These paths are searched in order.

Returns:

  • (Array<Pathname>)


102
# File 'lib/rub/l/c.rb', line 102

cattr_accessor :include_dirs

.initialize_copy(s) ⇒ Object



469
470
471
472
473
474
475
# File 'lib/rub/l/c.rb', line 469

def self.initialize_copy(s)
	super
	
	self.include_dirs = s.include_dirs.dup
	self.libs = s.libs.dup
	self.define = s.define.dup
end

.libsArray<String,Pathname>

A list of libraries to link.

Returns:

  • (Array<String,Pathname>)


108
# File 'lib/rub/l/c.rb', line 108

cattr_accessor :libs

.optimizeSymbol

Default optimization level.

This takes one of four optimization levels. The actual optimization done is linker dependant. For example, some linker may not have any optimization so all levels will be equivalent.

One of the following:

:none

Perform no optimization. This should be fast and debuggable.

:some

Perform light optimization that is pretty fast.

:full

Perform a high level of optimization producing a fast binary. this may considerably slow down compilation.

:max

Perform all available optimizations. These may be experimental and very slow.

This value defaults to :full if D:debug is set, otherwise :none.

Returns:

  • (Symbol)


64
# File 'lib/rub/l/c.rb', line 64

cattr_accessor :optimize

.optimize_forSymbol

Default optimization goal.

This determines what the compiler should optimize for if it has the option.

One of the following:

:size

The compiler should focus on creating a small binary.

:speed

The compiler should focus on creating a fast binary.

Returns:

  • (Symbol)


77
# File 'lib/rub/l/c.rb', line 77

cattr_accessor :optimize_for

.profiletrue, false

Default profile symbols setting.

This determines if the compiler should produce code suitable for profiling.

Returns:

  • (true, false)


94
# File 'lib/rub/l/c.rb', line 94

cattr_accessor :profile

.program(src, name) ⇒ Pathname

Compile and link an executable.

Parameters:

  • src (Set<Pathname,String>, Array<Pathname,String>, Pathname, String)

    The source files to compile and generated headers.

  • name (Pathname, String)

    The basename of the output file.

Returns:

  • (Pathname)

    The resulting executable.



461
462
463
464
465
466
467
# File 'lib/rub/l/c.rb', line 461

def self.program(src, name)
	obj = compile(src)
	linker = L::LD.clone
	
	linker.set_linker compiler.linker
	linker.link(obj, libs, name, format: :exe)
end

.to_c_identifier(s) ⇒ Object



363
364
365
366
367
# File 'lib/rub/l/c.rb', line 363

def self.to_c_identifier(s)
	s.delete('`!@#$%^&*()+=[]{};"\'<>?')
	 .gsub(/[\~\-\\\|\:\,\.\/]/, '_')
	 .gsub(/^[0-9]/, '_\0')
end

Instance Method Details

#compilerObject

Compiler

The compiler to use.

See Also:



41
# File 'lib/rub/l/c.rb', line 41

cattr_reader :compiler