mirror of
https://github.com/kp2pml30/ya-build.git
synced 2026-02-17 00:14:42 +04:00
support plugins, rework how config works
This commit is contained in:
parent
601c906574
commit
fc5497f039
1 changed files with 83 additions and 24 deletions
107
ya-build
107
ya-build
|
|
@ -29,14 +29,32 @@ require 'shellwords'
|
||||||
|
|
||||||
SELF_COMMAND = [RbConfig.ruby, __FILE__] + ARGV.dup
|
SELF_COMMAND = [RbConfig.ruby, __FILE__] + ARGV.dup
|
||||||
|
|
||||||
def to_ostruct(object)
|
NATIVE_LIB_EXT = (Proc.new {
|
||||||
case object
|
re = [
|
||||||
when Hash
|
[/linux/i, '.so'],
|
||||||
OpenStruct.new(Hash[object.map { |k, v| [k, to_ostruct(v)] }])
|
[/windows/i, '.dll'],
|
||||||
when Array
|
[/darwin/i, '.dylib'],
|
||||||
object.map { |x| to_ostruct(x) }
|
]
|
||||||
else
|
re.each { |r|
|
||||||
object
|
if r[0] =~ RUBY_PLATFORM
|
||||||
|
break r[1]
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}).call()
|
||||||
|
|
||||||
|
class Hash
|
||||||
|
def to_ostruct
|
||||||
|
prc = Proc.new { |rec, object|
|
||||||
|
case object
|
||||||
|
when Hash
|
||||||
|
OpenStruct.new(Hash[object.map { |k, v| [k, rec.call(rec, v)] }])
|
||||||
|
when Array
|
||||||
|
object.map { |x| rec(x) }
|
||||||
|
else
|
||||||
|
object
|
||||||
|
end
|
||||||
|
}
|
||||||
|
prc.call(prc, self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -49,9 +67,10 @@ end
|
||||||
|
|
||||||
class Target
|
class Target
|
||||||
attr_reader :trg_name, :output_file
|
attr_reader :trg_name, :output_file
|
||||||
|
|
||||||
def initialize(trg_name, dependencies)
|
def initialize(trg_name, dependencies)
|
||||||
if dependencies.nil?
|
if dependencies.nil?
|
||||||
raise "dependecies can't be nil"
|
raise "dependencies can't be nil"
|
||||||
end
|
end
|
||||||
@trg_name = trg_name
|
@trg_name = trg_name
|
||||||
if @trg_name.kind_of?(Pathname)
|
if @trg_name.kind_of?(Pathname)
|
||||||
|
|
@ -61,6 +80,10 @@ class Target
|
||||||
@dependencies = dependencies
|
@dependencies = dependencies
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_deps(*deps)
|
||||||
|
@dependencies.concat(deps)
|
||||||
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"<#{self.class.name}:#{@trg_name}>"
|
"<#{self.class.name}:#{@trg_name}>"
|
||||||
end
|
end
|
||||||
|
|
@ -167,17 +190,19 @@ class AliasTarget < Target
|
||||||
end
|
end
|
||||||
|
|
||||||
class Configurator
|
class Configurator
|
||||||
attr_reader :root_src, :root_build, :config
|
attr_reader :root_src, :root_build, :config, :all
|
||||||
|
|
||||||
def initialize(src, build, config)
|
def initialize(src, build, extra_conf)
|
||||||
|
@rules = []
|
||||||
@root_src = Pathname.new(src).realpath
|
@root_src = Pathname.new(src).realpath
|
||||||
@root_build = Pathname.new(build)
|
@root_build = Pathname.new(build)
|
||||||
@root_build.mkpath()
|
@root_build.mkpath()
|
||||||
@root_build = @root_build.realpath
|
@root_build = @root_build.realpath
|
||||||
@config = config.dup
|
|
||||||
@ya_files = []
|
@ya_files = []
|
||||||
|
|
||||||
@targets = []
|
@all = AliasTarget.new('all', [])
|
||||||
|
|
||||||
|
@targets = [@all]
|
||||||
|
|
||||||
@stack = []
|
@stack = []
|
||||||
|
|
||||||
|
|
@ -186,6 +211,21 @@ class Configurator
|
||||||
#date_format = datetime.strftime("%H:%M:%S")
|
#date_format = datetime.strftime("%H:%M:%S")
|
||||||
"#{severity.ljust(5)} #{msg}\n"
|
"#{severity.ljust(5)} #{msg}\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@config = OpenStruct.new
|
||||||
|
cnf = root_src.join('yabuild-default-conf.rb')
|
||||||
|
if cnf.exist?
|
||||||
|
@ya_files.push(cnf)
|
||||||
|
@config = self.instance_eval(cnf.read, cnf.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not extra_conf.nil?
|
||||||
|
cnf = Pathname.new(extra_conf)
|
||||||
|
if cnf.exist?
|
||||||
|
@ya_files.push(cnf)
|
||||||
|
self.instance_eval(cnf.read, cnf.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
@ -208,6 +248,13 @@ class Configurator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def eval_script(path)
|
||||||
|
script_path = cur_src.join(@stack[-1].path.join(path))
|
||||||
|
@ya_files.push(script_path)
|
||||||
|
contents = script_path.read
|
||||||
|
self.instance_eval(contents, script_path.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
private def run_last_stack()
|
private def run_last_stack()
|
||||||
path = @stack[-1].path
|
path = @stack[-1].path
|
||||||
@logger.info("configuring #{path}")
|
@logger.info("configuring #{path}")
|
||||||
|
|
@ -217,8 +264,12 @@ class Configurator
|
||||||
self.instance_eval(contents, script_path.to_s)
|
self.instance_eval(contents, script_path.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_rule(rule)
|
||||||
|
@rules.push(rule)
|
||||||
|
end
|
||||||
|
|
||||||
private def rules_str()
|
private def rules_str()
|
||||||
<<-EOF
|
beg = [<<-EOF
|
||||||
# ya-build generated, do not edit
|
# ya-build generated, do not edit
|
||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|
@ -233,7 +284,7 @@ rule CLEAN
|
||||||
|
|
||||||
rule HELP
|
rule HELP
|
||||||
command = /usr/bin/ninja -t targets
|
command = /usr/bin/ninja -t targets
|
||||||
description = All primary targets available:
|
description = All primary targets available
|
||||||
|
|
||||||
rule CUSTOM_COMMAND
|
rule CUSTOM_COMMAND
|
||||||
command = cd $WD && $COMMAND
|
command = cd $WD && $COMMAND
|
||||||
|
|
@ -246,7 +297,13 @@ rule COMPILE_C
|
||||||
rule LINK_C
|
rule LINK_C
|
||||||
command = $CC $cflags -o $out $in
|
command = $CC $cflags -o $out $in
|
||||||
|
|
||||||
|
rule COPY
|
||||||
|
command = cp $in $out
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
]
|
||||||
|
beg += @rules
|
||||||
|
beg.join("\n\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def run()
|
def run()
|
||||||
|
|
@ -256,6 +313,7 @@ EOF
|
||||||
:project => "",
|
:project => "",
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
run_last_stack()
|
run_last_stack()
|
||||||
|
|
||||||
File.write(root_build.join('rules.ninja'), rules_str)
|
File.write(root_build.join('rules.ninja'), rules_str)
|
||||||
|
|
@ -279,9 +337,17 @@ EOF
|
||||||
@targets.each { |t|
|
@targets.each { |t|
|
||||||
t.dump_rules(build_str)
|
t.dump_rules(build_str)
|
||||||
}
|
}
|
||||||
|
build_str << "default all\n"
|
||||||
File.write(root_build.join('build.ninja'), build_str)
|
File.write(root_build.join('build.ninja'), build_str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def return_target(trg, &block)
|
||||||
|
if not block.nil?
|
||||||
|
trg.instance_eval(&block)
|
||||||
|
end
|
||||||
|
trg
|
||||||
|
end
|
||||||
|
|
||||||
def project(name)
|
def project(name)
|
||||||
new_stack = @stack[-1].clone
|
new_stack = @stack[-1].clone
|
||||||
if new_stack.project == ""
|
if new_stack.project == ""
|
||||||
|
|
@ -344,10 +410,7 @@ EOF
|
||||||
trg
|
trg
|
||||||
end
|
end
|
||||||
|
|
||||||
def target_alias(name: nil, dependencies: nil)
|
def target_alias(name, *dependencies)
|
||||||
if name.nil? or dependencies.nil?
|
|
||||||
raise "all of name and dependencies must be provided"
|
|
||||||
end
|
|
||||||
name_full = @stack[-1].project
|
name_full = @stack[-1].project
|
||||||
if name_full != ""
|
if name_full != ""
|
||||||
name_full += "/"
|
name_full += "/"
|
||||||
|
|
@ -375,11 +438,7 @@ def config()
|
||||||
end
|
end
|
||||||
end.parse!(into: options)
|
end.parse!(into: options)
|
||||||
|
|
||||||
conf = OpenStruct.new
|
configurator = Configurator.new(options[:src], options[:build], options[:config])
|
||||||
if not options[:config].nil?
|
|
||||||
conf = to_ostruct(JSON.load_file(options[:config], {:allow_nan => true, :max_nesting => false}))
|
|
||||||
end
|
|
||||||
configurator = Configurator.new(options[:src], options[:build], conf)
|
|
||||||
configurator.run
|
configurator.run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue