class MCBuild

Public Class Methods

clean(path) click to toggle source
# File lib/mcbuild.rb, line 102
def self.clean(path)
        begin
                FileUtils.rm_rf(path)
        rescue Exception => e
                puts e
        end
end
detect_machine() click to toggle source
# 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_macro() click to toggle source
# File lib/mcbuild.rb, line 63
def self.detect_machine_macro
        "-D__"+MCBuild.detect_machine+"__"
end
new(dir) click to toggle source
# 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
no_command(valid_args) { |valid_args| ... } click to toggle source
# 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

Public Instance Methods

add_flag(flag) click to toggle source
# File lib/mcbuild.rb, line 261
def add_flag(flag)
        @flags += ' ' + flag
        self
end
archive_exe() click to toggle source
# 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_lib() click to toggle source
# 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_so() click to toggle source
# 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_command() click to toggle source
# File lib/mcbuild.rb, line 422
def archiver_command
        cmd = @archiver
end
clean() click to toggle source
# File lib/mcbuild.rb, line 321
def clean
        MCBuild.clean("#{@d}/#{@outpath}")
        self
end
command(arg) { || ... } click to toggle source
# File lib/mcbuild.rb, line 93
def command(arg)
        ARGV.each do |_arg|
                if _arg==arg
                        yield
                end
        end
        self
end
compile() click to toggle source
# 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) click to toggle source
# 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_command() click to toggle source
# 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_headers() click to toggle source
# 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=[]) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/mcbuild.rb, line 276
def disable_warning(warning)
        @flags += ' -Wno-' + warning
        self
end
disable_warnings(warnings) click to toggle source
# File lib/mcbuild.rb, line 281
def disable_warnings(warnings)
        warnings.each { |warn|
                self.disable_warning(warn)
        }
        self
end
done() click to toggle source
# File lib/mcbuild.rb, line 509
def done
        puts "---------- build finished ----------".green
        puts "#{self.export_lib_path}/#{name}".green
end
enable_warning(warning) click to toggle source
# File lib/mcbuild.rb, line 288
def enable_warning(warning)
        @flags += ' -W' + warning
        self
end
enable_warnings(warnings) click to toggle source
# File lib/mcbuild.rb, line 293
def enable_warnings(warnings)
        warnings.each { |warn|
                self.enable_warning(warn)
        }
        self
end
error_log(err) click to toggle source
# File lib/mcbuild.rb, line 117
def error_log(err)
        puts "Error[#{@d}]: ".red + err.red
        self
end
excludes() click to toggle source
# File lib/mcbuild.rb, line 182
def excludes
        @excludes
end
export_include_path() click to toggle source
# File lib/mcbuild.rb, line 166
def export_include_path
        @include_path
end
export_lib_path() click to toggle source
# File lib/mcbuild.rb, line 170
def export_lib_path
        @lib_path
end
headers() click to toggle source
# File lib/mcbuild.rb, line 178
def headers
        @headers
end
include(folders) click to toggle source
# File lib/mcbuild.rb, line 110
def include(folders)
        folders.each { |f|
                require_relative @d+'/'+f+'/settings.rb'
        }
        self
end
info() click to toggle source
# 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_command() click to toggle source
# 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
name() click to toggle source
# File lib/mcbuild.rb, line 174
def name
        @name
end
prehash(string) click to toggle source
# File lib/mcbuild.rb, line 350
def prehash(string)
        self
end
prepare() click to toggle source
# 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
print(valid_args) click to toggle source
remove_slash(str) click to toggle source

remove the last slash from path

# 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
run() click to toggle source
# File lib/mcbuild.rb, line 505
def run
        system("#{self.export_lib_path}/#{name}")
end
set_aext(aext) click to toggle source
# File lib/mcbuild.rb, line 236
def set_aext(aext)
        @aext = aext
        self
end
set_arch(arch) click to toggle source
# File lib/mcbuild.rb, line 246
def set_arch(arch)
        @mach = arch
        self
end
set_archiver(archiver) click to toggle source
# File lib/mcbuild.rb, line 201
def set_archiver(archiver)
        @archiver = archiver
        self
end
set_compiler(compiler) click to toggle source
# File lib/mcbuild.rb, line 196
def set_compiler(compiler)
        @compiler = compiler
        self
end
set_dependency(libs=[]) click to toggle source
# 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) click to toggle source
# File lib/mcbuild.rb, line 271
def set_excludes(excludes)
        @excludes = Array.new(excludes)
        self
end
set_export_include_path(incpath) click to toggle source
# File lib/mcbuild.rb, line 186
def set_export_include_path(incpath)
        @include_path = incpath
        self
end
set_export_lib_path(libpath) click to toggle source
# File lib/mcbuild.rb, line 191
def set_export_lib_path(libpath)
        @lib_path = libpath
        self
end
set_fext(fext) click to toggle source
# File lib/mcbuild.rb, line 226
def set_fext(fext)
        @fext = fext
        self
end
set_flags(flags) click to toggle source
# File lib/mcbuild.rb, line 256
def set_flags(flags)
        @flags = flags
        self
end
set_headers(headers) click to toggle source
# File lib/mcbuild.rb, line 216
def set_headers(headers)
        @headers = headers
        self
end
set_name(name) click to toggle source
# File lib/mcbuild.rb, line 221
def set_name(name)
        @name = name
        self
end
set_oext(oext) click to toggle source
# File lib/mcbuild.rb, line 231
def set_oext(oext)
        @oext = oext
        self
end
set_outpath(outpath) click to toggle source
# File lib/mcbuild.rb, line 266
def set_outpath(outpath)
        @outpath = outpath
        self
end
set_position_independent_code(pic) click to toggle source
# File lib/mcbuild.rb, line 211
def set_position_independent_code(pic)
        @position_independent_code = pic
        self
end
set_std(std) click to toggle source
# File lib/mcbuild.rb, line 251
def set_std(std)
        @std = std
        self
end
set_sysroot(root) click to toggle source
# File lib/mcbuild.rb, line 206
def set_sysroot(root)
        @sysroot = root
        self
end
set_target(target) click to toggle source
# File lib/mcbuild.rb, line 241
def set_target(target)
        @target = target
        self
end