Class: MCBuild

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ MCBuild

Returns a new instance of MCBuild.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mcbuild.rb', line 135

def initialize(dir)
	@compiler = "cc"
	@sysroot = ''
	@target = ''
	@mach = ''
	@position_independent_code = false
	@flags = MCBuild.detect_machine_macro + " -Wno-unused-command-line-argument"

	@archiver = "ar"

	@d = remove_slash(dir)

	@name = "mcdefault"

	@fext = ".c"
	@oext = ".o"
	@aext = ".a"
	@std  = "c99"
	@outpath = "_build"

	@headers    = []
	@excludes   = []
	@dependency = []

	@include_path = "#{@d}/#{@outpath}/archive/include"
	@lib_path     = "#{@d}/#{@outpath}/archive/lib"

	@compile_arg = " -I#{self.export_include_path}"
	@link_arg    = " -L#{self.export_lib_path}"
end

Class Method Details

.clean(path) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/mcbuild.rb', line 102

def self.clean(path)
	begin
		FileUtils.rm_rf(path)
	rescue Exception => e
		puts e
	end
end

.detect_machineObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/mcbuild.rb', line 52

def self.detect_machine
	ret = MCConfig.x86_64
	mach = %x[uname -m].strip
	MCConfig.support_machines.each { |supportm|
		if mach.include? supportm
			ret = supportm
		end
	}
	ret
end

.detect_machine_macroObject



63
64
65
# File 'lib/mcbuild.rb', line 63

def self.detect_machine_macro
	"-D__"+MCBuild.detect_machine+"__"
end

.no_command(valid_args) {|valid_args| ... } ⇒ Object

Yields:

  • (valid_args)


67
68
69
70
71
72
73
74
75
76
# File 'lib/mcbuild.rb', line 67

def self.no_command(valid_args)
	ARGV.each { |arg|
		valid_args.each { |va|
			if va == arg
				return
			end
		}
	}
	yield valid_args
end

Instance Method Details

#add_flag(flag) ⇒ Object



261
262
263
264
# File 'lib/mcbuild.rb', line 261

def add_flag(flag)
	@flags += ' ' + flag
	self
end

#archive_exeObject



478
479
480
481
482
483
# File 'lib/mcbuild.rb', line 478

def archive_exe
	cmd = "#{self.linker_command} -o #{self.export_lib_path}/#{@name} #{@d}/#{@outpath}/*#{@oext} #{@link_arg}"
	#puts(cmd)
	system(cmd)
	self
end

#archive_libObject



457
458
459
460
461
462
# File 'lib/mcbuild.rb', line 457

def archive_lib
	cmd = "#{self.archiver_command} -r #{self.export_lib_path}/lib#{@name}#{@aext} #{@d}/#{@outpath}/*#{@oext}"
	#puts(cmd)
	system(cmd)
	self
end

#archive_soObject



464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/mcbuild.rb', line 464

def archive_so
	if @position_independent_code
		cmd = "#{self.compiler_command} -shared -Wl,-soname,lib#{@name}.so"
		cmd += " -o #{self.export_lib_path}/lib#{@name}.so #{@d}/#{@outpath}/*#{@oext}"
		cmd += " -Wl,--whole-archive #{@link_arg} -Wl,--no-whole-archive"
		#puts(cmd)
		system(cmd)
		self
	else
		error_log('please use set_shared(true) before archive so')
		nil
	end
end

#archiver_commandObject



422
423
424
# File 'lib/mcbuild.rb', line 422

def archiver_command
	cmd = @archiver
end

#cleanObject



321
322
323
324
# File 'lib/mcbuild.rb', line 321

def clean
	MCBuild.clean("#{@d}/#{@outpath}")
	self
end

#command(arg) ⇒ Object



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

def command(arg)
	ARGV.each do |_arg|
		if _arg==arg
			yield
		end
	end
	self
end

#compileObject



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/mcbuild.rb', line 436

def compile
	self.clean
	self.prepare
	begin
		Find.find(@d) { |file|
			ext = File.extname(file)
			base = File.basename(file, ext)
			if ext == ".c" || ext == ".asm" || ext == ".S"
				if (@excludes != nil) && (@excludes.include? base)
					puts "exclude: #{base}".magenta
				else
					compile_file(file)
				end
			end
		}
	rescue Exception => e
		error_log(e)
	end
	self
end

#compile_file(file) ⇒ Object



426
427
428
429
430
431
432
433
434
# File 'lib/mcbuild.rb', line 426

def compile_file(file)
	ext = File.extname(file)
	base = File.basename(file, ext)
	cmd = compiler_command
	cmd += " -c -o #{@d}/#{@outpath}/#{base}#{@oext} #{file} #{@compile_arg}"
	#puts(cmd)
	system(cmd)
	self
end

#compiler_commandObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/mcbuild.rb', line 389

def compiler_command
	cmd = @compiler
	cmd += " -std=#{@std}"
	if @sysroot != ''
		cmd += " --sysroot #{@sysroot}"
	end
	if @target != ''
		cmd += " -target #{@target}"
	end
	if @mach != ''
		cmd += " -arch #{@mach}"
	end
	if @position_independent_code
		cmd += " -fPIC"
	end
	cmd += " #{@flags}"
	cmd
end

#copy_headersObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/mcbuild.rb', line 354

def copy_headers
	begin
		Find.find(@d) { |header|
			base = File.basename(header)
			if @headers.include? base
				puts "copying-> #{header}".cyan
				FileUtils.cp("#{header}", "#{@d}/#{@outpath}/archive/include")
			end
		}
	rescue Exception => e
		puts e
	end
	self
end

#copy_headers_all(except = []) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/mcbuild.rb', line 369

def copy_headers_all(except=[])
	begin
		Find.find(@d) { |header|
			ext = File.extname(header)
			base = File.basename(header)
			if (ext == ".h") && (!header.include? self.export_include_path)
				if (except != nil) && (except.include? base)
					puts "except: #{base}".magenta
				else
					puts "copying-> #{header}".cyan
					FileUtils.cp("#{header}", self.export_include_path)
				end
			end
		}
	rescue Exception => e
		puts e
	end
	self
end

#copy_lib_to(path) ⇒ Object



485
486
487
488
489
490
491
492
493
# File 'lib/mcbuild.rb', line 485

def copy_lib_to(path)
	begin
		FileUtils.mkdir_p("#{@d}/#{path}")
		FileUtils.cp("#{self.export_lib_path}/lib#{@name}#{@aext}", "#{@d}/#{path}")
	rescue Exception => e
		error_log(e)
	end
	self
end

#copy_so_to(path) ⇒ Object



495
496
497
498
499
500
501
502
503
# File 'lib/mcbuild.rb', line 495

def copy_so_to(path)
	begin
		FileUtils.mkdir_p("#{@d}/#{path}")
		FileUtils.cp("#{self.export_lib_path}/lib#{@name}.so", "#{@d}/#{path}")
	rescue Exception => e
		error_log(e)
	end
	self
end

#disable_warning(warning) ⇒ Object



276
277
278
279
# File 'lib/mcbuild.rb', line 276

def disable_warning(warning)
	@flags += ' -Wno-' + warning
	self
end

#disable_warnings(warnings) ⇒ Object



281
282
283
284
285
286
# File 'lib/mcbuild.rb', line 281

def disable_warnings(warnings)
	warnings.each { |warn|
		self.disable_warning(warn)
	}
	self
end

#doneObject



509
510
511
512
# File 'lib/mcbuild.rb', line 509

def done
	puts "---------- build finished ----------".green
	puts "#{self.export_lib_path}/#{name}".green
end

#enable_warning(warning) ⇒ Object



288
289
290
291
# File 'lib/mcbuild.rb', line 288

def enable_warning(warning)
	@flags += ' -W' + warning
	self
end

#enable_warnings(warnings) ⇒ Object



293
294
295
296
297
298
# File 'lib/mcbuild.rb', line 293

def enable_warnings(warnings)
	warnings.each { |warn|
		self.enable_warning(warn)
	}
	self
end

#error_log(err) ⇒ Object



117
118
119
120
# File 'lib/mcbuild.rb', line 117

def error_log(err)
	puts "Error[#{@d}]: ".red + err.red
	self
end

#excludesObject



182
183
184
# File 'lib/mcbuild.rb', line 182

def excludes
	@excludes
end

#export_include_pathObject



166
167
168
# File 'lib/mcbuild.rb', line 166

def export_include_path
	@include_path
end

#export_lib_pathObject



170
171
172
# File 'lib/mcbuild.rb', line 170

def export_lib_path
	@lib_path
end

#headersObject



178
179
180
# File 'lib/mcbuild.rb', line 178

def headers
	@headers
end

#include(folders) ⇒ Object



110
111
112
113
114
115
# File 'lib/mcbuild.rb', line 110

def include(folders)
	folders.each { |f|
		require_relative @d+'/'+f+'/settings.rb'
	}
	self
end

#infoObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/mcbuild.rb', line 300

def info
	puts "Monk-C compiler use settings:"
	puts "--------------------------------"
	puts "         CPU target -> #{@target}"
	puts "         C standard -> #{@std}"
	puts "               name -> #{@name}"
	puts "           compiler -> #{@compiler}"
	puts " filename extension -> #{@fext}"
	puts "   output extension -> #{@oext}"
	puts "  archive extension -> #{@aext}"
	puts "      compile flags -> #{@flags}".yellow
	puts "         source dir -> #{@d}"
	puts "           excludes -> #{self.excludes}"
	puts "--------------------------------"
	puts "C compiler infos:"
	puts "--------------------------------"
	system("#{@compiler} --version")
	puts "--------------------------------"
	self
end

#linker_commandObject



408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/mcbuild.rb', line 408

def linker_command
	cmd = @compiler
	if @sysroot != ''
		cmd += " --sysroot #{@sysroot}"
	end
	if @target != ''
		cmd += " -target #{@target}"
	end
	if @mach != ''
		cmd += " -arch #{@mach}"
	end
	cmd
end

#nameObject



174
175
176
# File 'lib/mcbuild.rb', line 174

def name
	@name
end

#prehash(string) ⇒ Object



350
351
352
# File 'lib/mcbuild.rb', line 350

def prehash(string)
	self
end

#prepareObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/mcbuild.rb', line 326

def prepare
	begin
		FileUtils.rm_rf   "#{@d}/#{@outpath}"
		FileUtils.mkdir_p "#{@d}/#{@outpath}/archive/include"
		FileUtils.mkdir_p "#{@d}/#{@outpath}/archive/lib"
		if @headers.count != 0
			self.copy_headers
		else
			self.copy_headers_all
		end
	rescue Exception => e
		puts e
	end
	self
end


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mcbuild.rb', line 78

def print(valid_args)
	ARGV.each { |arg|
		valid_args.each { |va|
			if va == arg
				return
			end
		}
	}
	puts "usage:".bold.cyan
	valid_args.each { |arg|
		puts "./build.rb #{arg}".green
	}
	self
end

#remove_slash(str) ⇒ Object

remove the last slash from path



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mcbuild.rb', line 123

def remove_slash(str)
	arr = str.split('/')
	ret = arr.first
	arr.delete_at(0)
	arr.each { |s|
		if s != ''
			ret += "/#{s}"
		end
	}
	ret
end

#runObject



505
506
507
# File 'lib/mcbuild.rb', line 505

def run
	system("#{self.export_lib_path}/#{name}")
end

#set_aext(aext) ⇒ Object



236
237
238
239
# File 'lib/mcbuild.rb', line 236

def set_aext(aext)
	@aext = aext
	self
end

#set_arch(arch) ⇒ Object



246
247
248
249
# File 'lib/mcbuild.rb', line 246

def set_arch(arch)
	@mach = arch
	self
end

#set_archiver(archiver) ⇒ Object



201
202
203
204
# File 'lib/mcbuild.rb', line 201

def set_archiver(archiver)
	@archiver = archiver
	self
end

#set_compiler(compiler) ⇒ Object



196
197
198
199
# File 'lib/mcbuild.rb', line 196

def set_compiler(compiler)
	@compiler = compiler
	self
end

#set_dependency(libs = []) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/mcbuild.rb', line 342

def set_dependency(libs=[])
	libs.each { |lib|
		@compile_arg += " -I#{lib.export_include_path} -L#{lib.export_lib_path} -l#{lib.name}"
		@link_arg    += " -I#{lib.export_include_path} -L#{lib.export_lib_path} -l#{lib.name}"
	}
	self
end

#set_excludes(excludes) ⇒ Object



271
272
273
274
# File 'lib/mcbuild.rb', line 271

def set_excludes(excludes)
	@excludes = Array.new(excludes)
	self
end

#set_export_include_path(incpath) ⇒ Object



186
187
188
189
# File 'lib/mcbuild.rb', line 186

def set_export_include_path(incpath)
	@include_path = incpath
	self
end

#set_export_lib_path(libpath) ⇒ Object



191
192
193
194
# File 'lib/mcbuild.rb', line 191

def set_export_lib_path(libpath)
	@lib_path = libpath
	self
end

#set_fext(fext) ⇒ Object



226
227
228
229
# File 'lib/mcbuild.rb', line 226

def set_fext(fext)
	@fext = fext
	self
end

#set_flags(flags) ⇒ Object



256
257
258
259
# File 'lib/mcbuild.rb', line 256

def set_flags(flags)
	@flags = flags
	self
end

#set_headers(headers) ⇒ Object



216
217
218
219
# File 'lib/mcbuild.rb', line 216

def set_headers(headers)
	@headers = headers
	self
end

#set_name(name) ⇒ Object



221
222
223
224
# File 'lib/mcbuild.rb', line 221

def set_name(name)
	@name = name
	self
end

#set_oext(oext) ⇒ Object



231
232
233
234
# File 'lib/mcbuild.rb', line 231

def set_oext(oext)
	@oext = oext
	self
end

#set_outpath(outpath) ⇒ Object



266
267
268
269
# File 'lib/mcbuild.rb', line 266

def set_outpath(outpath)
	@outpath = outpath
	self
end

#set_position_independent_code(pic) ⇒ Object



211
212
213
214
# File 'lib/mcbuild.rb', line 211

def set_position_independent_code(pic)
	@position_independent_code = pic
	self
end

#set_std(std) ⇒ Object



251
252
253
254
# File 'lib/mcbuild.rb', line 251

def set_std(std)
	@std = std
	self
end

#set_sysroot(root) ⇒ Object



206
207
208
209
# File 'lib/mcbuild.rb', line 206

def set_sysroot(root)
	@sysroot = root
	self
end

#set_target(target) ⇒ Object



241
242
243
244
# File 'lib/mcbuild.rb', line 241

def set_target(target)
	@target = target
	self
end