Add 'build' DSL method.

Disallow processing Environments until configuration is performed.
This commit is contained in:
Josh Holtrop 2018-12-17 22:07:50 -05:00
parent 5720662b7c
commit f8e6666a2c
90 changed files with 637 additions and 434 deletions

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.CFile("lexer.c", "lexer.l") Rscons::Environment.new do |env|
env.CFile("parser.c", "parser.y") env.CFile("lexer.c", "lexer.l")
env.CFile("parser.c", "parser.y")
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.CFile("file.c", "foo.bar") Rscons::Environment.new do |env|
env.CFile("file.c", "foo.bar")
end
end end

View File

@ -1,10 +1,12 @@
debug = Rscons::Environment.new(echo: :command) do |env| build do
env['CFLAGS'] = '-O2' debug = Rscons::Environment.new(echo: :command) do |env|
env['CPPFLAGS'] = '-DSTRING="Debug Version"' env['CFLAGS'] = '-O2'
env.Program('program-debug.exe', Dir['src/*.c']) env['CPPFLAGS'] = '-DSTRING="Debug Version"'
end env.Program('program-debug.exe', Dir['src/*.c'])
end
release = debug.clone do |env| release = debug.clone do |env|
env["CPPFLAGS"] = '-DSTRING="Release Version"' env["CPPFLAGS"] = '-DSTRING="Release Version"'
env.Program('program-release.exe', Dir['src/*.c']) env.Program('program-release.exe', Dir['src/*.c'])
end
end end

View File

@ -1,14 +1,16 @@
env1 = Rscons::Environment.new(echo: :command) do |env| build do
env['CFLAGS'] = '-O2' env1 = Rscons::Environment.new(echo: :command) do |env|
env.add_build_hook do |build_op| env['CFLAGS'] = '-O2'
build_op[:vars]['CPPFLAGS'] = '-DSTRING="Hello"' env.add_build_hook do |build_op|
build_op[:vars]['CPPFLAGS'] = '-DSTRING="Hello"'
end
env.add_post_build_hook do |build_op|
$stdout.puts "post #{build_op[:target]}"
end
env.Program('program.exe', Dir['src/*.c'])
end end
env.add_post_build_hook do |build_op|
$stdout.puts "post #{build_op[:target]}"
end
env.Program('program.exe', Dir['src/*.c'])
end
env2 = env1.clone do |env| env2 = env1.clone do |env|
env.Program('program2.exe', Dir['src/*.c']) env.Program('program2.exe', Dir['src/*.c'])
end
end end

View File

@ -2,6 +2,8 @@ configure do
check_c_header "not___found.h", fail: false, set_define: "HAVE_NOT___FOUND_H" check_c_header "not___found.h", fail: false, set_define: "HAVE_NOT___FOUND_H"
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new(echo: :command) do |env|
env.Object("simple.o", "simple.c")
end
end end

View File

@ -2,6 +2,8 @@ configure do
check_c_header "string.h", set_define: "HAVE_STRING_H" check_c_header "string.h", set_define: "HAVE_STRING_H"
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new(echo: :command) do |env|
env.Object("simple.o", "simple.c")
end
end end

View File

@ -2,6 +2,8 @@ configure do
check_cfg program: "my-config" check_cfg program: "my-config"
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.Program("myconfigtest", "simple.c") Rscons::Environment.new(echo: :command) do |env|
env.Program("myconfigtest", "simple.c")
end
end end

View File

@ -2,6 +2,8 @@ configure do
check_cfg package: "mypackage" check_cfg package: "mypackage"
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.Program("myconfigtest", "simple.c") Rscons::Environment.new(echo: :command) do |env|
env.Program("myconfigtest", "simple.c")
end
end end

View File

@ -0,0 +1,4 @@
Environment.new do |env|
env.Object("simple.o", "simple.cc")
env.process
end

View File

@ -3,6 +3,8 @@ configure do
check_c_header "stdio.h", set_define: "HAVE_STDIO_H" check_c_header "stdio.h", set_define: "HAVE_STDIO_H"
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new(echo: :command) do |env|
env.Object("simple.o", "simple.c")
end
end end

View File

@ -9,8 +9,10 @@ EOF
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(MySource.new) Rscons::Environment.new do |env|
env.MySource('inc.h', []) env.add_builder(MySource.new)
env.Program('program.exe', Dir['*.c']) env.MySource('inc.h', [])
env.Program('program.exe', Dir['*.c'])
end
end end

View File

@ -1,10 +1,12 @@
Rscons::Environment.new do |env| build do
env.Command("inc.c", Rscons::Environment.new do |env|
[], env.Command("inc.c",
"CMD" => %w[ruby gen.rb ${_TARGET}], [],
"CMD_DESC" => "Generating") "CMD" => %w[ruby gen.rb ${_TARGET}],
env["build_root"] = env.build_root "CMD_DESC" => "Generating")
env["inc_c"] = "inc.c" env["build_root"] = env.build_root
env.build_after("${build_root}/program.o", "${inc_c}") env["inc_c"] = "inc.c"
env.Program("program.exe", ["program.c", "inc.c"]) env.build_after("${build_root}/program.o", "${inc_c}")
env.Program("program.exe", ["program.c", "inc.c"])
end
end end

View File

@ -9,10 +9,12 @@ EOF
end end
end end
env = Rscons::Environment.new do |env| build do
env["hdr"] = "inc.h" env = Rscons::Environment.new do |env|
env["src"] = "program.c" env["hdr"] = "inc.h"
env.add_builder(MySource.new) env["src"] = "program.c"
env.MySource('${hdr}') env.add_builder(MySource.new)
env.Program('program.exe', "${src}") env.MySource('${hdr}')
env.Program('program.exe', "${src}")
end
end end

View File

@ -9,20 +9,22 @@ EOF
end end
end end
e1 = Rscons::Environment.new do |env| build do
env.add_builder(MySource.new) e1 = Rscons::Environment.new do |env|
env["one"] = "5" env.add_builder(MySource.new)
env[:cfg] = {val: "9"} env["one"] = "5"
env["two"] = lambda do |args| env[:cfg] = {val: "9"}
args[:env][:cfg][:val] env["two"] = lambda do |args|
args[:env][:cfg][:val]
end
env["the_value"] = lambda do |args|
"${one}${two}78"
end
end end
env["the_value"] = lambda do |args|
"${one}${two}78"
end
end
e1.clone do |env| e1.clone do |env|
env[:cfg][:val] = "6" env[:cfg][:val] = "6"
env.MySource('inc.h', []) env.MySource('inc.h', [])
env.Program('program.exe', Dir['*.c']) env.Program('program.exe', Dir['*.c'])
end
end end

View File

@ -12,8 +12,10 @@ class CHGen < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(CHGen.new) Rscons::Environment.new do |env|
env.CHGen("inc.c", ["program.c"]) env.add_builder(CHGen.new)
env.Program("program.exe", %w[program.c inc.c]) env.CHGen("inc.c", ["program.c"])
env.Program("program.exe", %w[program.c inc.c])
end
end end

View File

@ -1,14 +1,16 @@
Rscons::Environment.new do |env| build do
env["build_root"] = env.build_root Rscons::Environment.new do |env|
env["inc_h"] = "inc.h" env["build_root"] = env.build_root
env["inc_h"] = "inc.h"
env.Copy("copy_inc.h", "${inc_h}") env.Copy("copy_inc.h", "${inc_h}")
env.depends("${build_root}/program.o", "${inc_h}") env.depends("${build_root}/program.o", "${inc_h}")
env.Program("program.exe", ["program.c", "inc.c"]) env.Program("program.exe", ["program.c", "inc.c"])
env.Command("inc.c", env.Command("inc.c",
[], [],
"CMD" => %w[ruby gen.rb ${_TARGET}], "CMD" => %w[ruby gen.rb ${_TARGET}],
"CMD_DESC" => "Generating") "CMD_DESC" => "Generating")
env.produces("inc.c", "inc.h") env.produces("inc.c", "inc.h")
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.Program("hello-d.exe", Rscons.glob("*.d")) Rscons::Environment.new(echo: :command) do |env|
env.Program("hello-d.exe", Rscons.glob("*.d"))
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Program('header.exe', Dir['*.c']) Rscons::Environment.new do |env|
env.Program('header.exe', Dir['*.c'])
end
end end

View File

@ -1,15 +1,17 @@
Rscons::Environment.new do |env| build do
require 'json' Rscons::Environment.new do |env|
require 'yaml' require 'json'
env.add_builder(:JsonToYaml) do |target, sources, cache, env, vars| require 'yaml'
unless cache.up_to_date?(target, :JsonToYaml, sources, env) env.add_builder(:JsonToYaml) do |target, sources, cache, env, vars|
cache.mkdir_p(File.dirname(target)) unless cache.up_to_date?(target, :JsonToYaml, sources, env)
File.open(target, 'w') do |f| cache.mkdir_p(File.dirname(target))
f.write(YAML.dump(JSON.load(IO.read(sources.first)))) File.open(target, 'w') do |f|
f.write(YAML.dump(JSON.load(IO.read(sources.first))))
end
cache.register_build(target, :JsonToYaml, sources, env)
end end
cache.register_build(target, :JsonToYaml, sources, env) target
end end
target env.JsonToYaml('foo.yml', 'foo.json')
end end
env.JsonToYaml('foo.yml', 'foo.json')
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.Program('library.exe', ['lib.a', 'three.c']) Rscons::Environment.new(echo: :command) do |env|
env.Library("lib.a", ['one.c', 'two.c'], 'CPPFLAGS' => ['-Dmake_lib']) env.Program('library.exe', ['lib.a', 'three.c'])
env.Library("lib.a", ['one.c', 'two.c'], 'CPPFLAGS' => ['-Dmake_lib'])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env["ARCMD"] = %w[ar rcf ${_TARGET} ${_SOURCES}] Rscons::Environment.new(echo: :command) do |env|
env.Library("lib.a", Rscons.glob("*.c")) env["ARCMD"] = %w[ar rcf ${_TARGET} ${_SOURCES}]
env.Library("lib.a", Rscons.glob("*.c"))
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Preprocess("pp", "foo.h") Rscons::Environment.new do |env|
env.Preprocess("pp", "foo.h")
end
end end

View File

@ -1,11 +1,13 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/lib" Rscons::Environment.new do |env|
libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.c")) env["CPPPATH"] << "src/lib"
env.Program("test-shared.exe", libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.c"))
Rscons.glob("src/*.c"), env.Program("test-shared.exe",
"LIBPATH" => %w[.], Rscons.glob("src/*.c"),
"LIBS" => %w[mine]) "LIBPATH" => %w[.],
env.build_after("test-shared.exe", libmine.to_s) "LIBS" => %w[mine])
env.Program("test-static.exe", env.build_after("test-shared.exe", libmine.to_s)
Rscons.glob("src/**/*.c")) env.Program("test-static.exe",
Rscons.glob("src/**/*.c"))
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new do |env| build do
File.open("foo.xyz", "wb") do |fh| Rscons::Environment.new do |env|
fh.puts("hi") File.open("foo.xyz", "wb") do |fh|
fh.puts("hi")
end
env.SharedObject("foo.o", "foo.xyz")
end end
env.SharedObject("foo.o", "foo.xyz")
end end

View File

@ -1,11 +1,13 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/lib" Rscons::Environment.new do |env|
libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.cc")) env["CPPPATH"] << "src/lib"
env.Program("test-shared.exe", libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.cc"))
Rscons.glob("src/*.cc"), env.Program("test-shared.exe",
"LIBPATH" => %w[.], Rscons.glob("src/*.cc"),
"LIBS" => %w[mine]) "LIBPATH" => %w[.],
env.build_after("test-shared.exe", libmine.to_s) "LIBS" => %w[mine])
env.Program("test-static.exe", env.build_after("test-shared.exe", libmine.to_s)
Rscons.glob("src/**/*.cc")) env.Program("test-static.exe",
Rscons.glob("src/**/*.cc"))
end
end end

View File

@ -1,9 +1,11 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/lib" Rscons::Environment.new do |env|
libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.d")) env["CPPPATH"] << "src/lib"
env.Program("test-shared.exe", libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.d"))
Rscons.glob("src/*.c"), env.Program("test-shared.exe",
"LIBPATH" => %w[.], Rscons.glob("src/*.c"),
"LIBS" => %w[mine]) "LIBPATH" => %w[.],
env.build_after("test-shared.exe", libmine.to_s) "LIBS" => %w[mine])
env.build_after("test-shared.exe", libmine.to_s)
end
end end

View File

@ -1,12 +1,14 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/lib" Rscons::Environment.new do |env|
env["SHLD"] = "gcc" env["CPPPATH"] << "src/lib"
libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.c")) env["SHLD"] = "gcc"
env.Program("test-shared.exe", libmine = env.SharedLibrary("mine", Rscons.glob("src/lib/*.c"))
Rscons.glob("src/*.c"), env.Program("test-shared.exe",
"LIBPATH" => %w[.], Rscons.glob("src/*.c"),
"LIBS" => %w[mine]) "LIBPATH" => %w[.],
env.build_after("test-shared.exe", libmine.to_s) "LIBS" => %w[mine])
env.Program("test-static.exe", env.build_after("test-shared.exe", libmine.to_s)
Rscons.glob("src/**/*.c")) env.Program("test-static.exe",
Rscons.glob("src/**/*.c"))
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Program('simple.exe', Dir['*.c']) Rscons::Environment.new do |env|
env.Program('simple.exe', Dir['*.c'])
end
end end

View File

@ -1,13 +1,15 @@
Rscons::Environment.new do |env| build do
tempdir = ENV["TEMP"] || ENV["TMP"] || "/tmp" Rscons::Environment.new do |env|
source_file = File.join(tempdir, "abs.c") tempdir = ENV["TEMP"] || ENV["TMP"] || "/tmp"
File.open(source_file, "w") do |fh| source_file = File.join(tempdir, "abs.c")
fh.puts(<<-EOF) File.open(source_file, "w") do |fh|
int main() fh.puts(<<-EOF)
{ int main()
return 29; {
} return 29;
EOF }
EOF
end
env.Program("abs.exe", source_file)
end end
env.Program("abs.exe", source_file)
end end

View File

@ -12,10 +12,11 @@ class MyObject < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(MyObject.new) Rscons::Environment.new do |env|
File.open("test.xyz", "w") do |fh| env.add_builder(MyObject.new)
fh.puts <<EOF File.open("test.xyz", "w") do |fh|
fh.puts <<EOF
#include <stdio.h> #include <stdio.h>
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
@ -23,6 +24,7 @@ int main(int argc, char * argv[])
return 0; return 0;
} }
EOF EOF
env.Program("test", "test.xyz") env.Program("test", "test.xyz")
end
end end
end end

View File

@ -8,15 +8,17 @@ class MyProgram < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(MyProgram.new) Rscons::Environment.new do |env|
env.Object("simple.o", "simple.c") env.add_builder(MyProgram.new)
File.open("two.c", "wb") do |fh| env.Object("simple.o", "simple.c")
fh.puts <<-EOF File.open("two.c", "wb") do |fh|
void two(void) fh.puts <<-EOF
{ void two(void)
} {
EOF }
EOF
end
env.MyProgram("simple.exe", ["simple.o", "two.c"])
end end
env.MyProgram("simple.exe", ["simple.o", "two.c"])
end end

View File

@ -3,7 +3,9 @@ class TestBuilder < Rscons::Builder
target target
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(TestBuilder.new) Rscons::Environment.new do |env|
env.TestBuilder("file") env.add_builder(TestBuilder.new)
env.TestBuilder("file")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env["LIBS"] += ["c"] Rscons::Environment.new do |env|
env.Program('simple.exe', Dir['*.c']) env["LIBS"] += ["c"]
env.Program('simple.exe', Dir['*.c'])
end
end end

View File

@ -25,10 +25,12 @@ class DebugBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(DebugBuilder.new) Rscons::Environment.new do |env|
if Rscons.vars["new_user_dep"] env.add_builder(DebugBuilder.new)
env.depends("foo.o", "new_dep") if Rscons.vars["new_user_dep"]
env.depends("foo.o", "new_dep")
end
env.DebugBuilder("foo.o", "simple.c")
end end
env.DebugBuilder("foo.o", "simple.c")
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Copy("simple.copy", "simple.c") Rscons::Environment.new do |env|
env.Copy("simple.copy", "simple.c")
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new do |env|
env.process env.Object("simple.o", "simple.c")
env["LDCMD"] = %w[gcc -o ${_TARGET} simple.o] env.process
env.Program('simple.exe', []) env["LDCMD"] = %w[gcc -o ${_TARGET} simple.o]
env.Program('simple.exe', [])
end
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new do |env|
env["LDCMD"] = %w[gcc -o ${_TARGET} simple.o] env.Object("simple.o", "simple.c")
env.Program('simple.exe', ["simple.o"]) env["LDCMD"] = %w[gcc -o ${_TARGET} simple.o]
env.Program('simple.exe', ["simple.o"])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new do |env|
env.Object("two.o", "two.c") env.Object("simple.o", "simple.c")
env.Object("two.o", "two.c")
end
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
target = env.Program("simple.exe", "simple.c") Rscons::Environment.new do |env|
user_deps = File.read("user_deps", mode: "rb").split(" ") target = env.Program("simple.exe", "simple.c")
target.depends(*user_deps) user_deps = File.read("user_deps", mode: "rb").split(" ")
target.depends(*user_deps)
end
end end

View File

@ -14,7 +14,9 @@ class TestBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(TestBuilder.new) Rscons::Environment.new do |env|
env.TestBuilder("foo") env.add_builder(TestBuilder.new)
env.TestBuilder("foo")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.Program("simple.exe", "simple.c") Rscons::Environment.new do |env|
env.clear_targets env.Program("simple.exe", "simple.c")
env.clear_targets
end
end end

View File

@ -1,7 +1,9 @@
base_env = Rscons::Environment.new do |env| build do
env.n_threads = 165 base_env = Rscons::Environment.new do |env|
env.n_threads = 165
end
my_env = base_env.clone
puts my_env.n_threads
end end
my_env = base_env.clone
puts my_env.n_threads

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env["LD"] = "gcc" Rscons::Environment.new(echo: :command) do |env|
env.Program('simple.exe', Dir['*.c']) env["LD"] = "gcc"
env.Program('simple.exe', Dir['*.c'])
end
end end

View File

@ -1,7 +1,9 @@
Rscons::Environment.new do |env| build do
command = %W[gcc -o ${_TARGET} ${_SOURCES}] Rscons::Environment.new do |env|
env.Command("simple.exe", command = %W[gcc -o ${_TARGET} ${_SOURCES}]
"simple.c", env.Command("simple.exe",
"CMD" => command, "simple.c",
"CMD_DESC" => "BuildIt") "CMD" => command,
"CMD_DESC" => "BuildIt")
end
end end

View File

@ -1,8 +1,10 @@
Rscons::Environment.new do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new do |env|
env.Command("simple.txt", env.Object("simple.o", "simple.c")
"simple.o", env.Command("simple.txt",
"CMD" => %w[objdump --disassemble --source ${_SOURCES}], "simple.o",
"CMD_STDOUT" => "${_TARGET}", "CMD" => %w[objdump --disassemble --source ${_SOURCES}],
"CMD_DESC" => "My Disassemble") "CMD_STDOUT" => "${_TARGET}",
"CMD_DESC" => "My Disassemble")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env["sources"] = Rscons.glob("*.c") Rscons::Environment.new do |env|
env.Program("simple.exe", "${sources}") env["sources"] = Rscons.glob("*.c")
env.Program("simple.exe", "${sources}")
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Directory("teh_dir") Rscons::Environment.new do |env|
env.Directory("teh_dir")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.Object("simple.o", "simple.c") Rscons::Environment.new do |env|
env.Disassemble("simple.txt", "simple.o") env.Object("simple.o", "simple.c")
env.Disassemble("simple.txt", "simple.o")
end
end end

View File

@ -1,5 +1,7 @@
env = Rscons::Environment.new do |env| build do
env["CFLAGS"] += %w[-O2 -fomit-frame-pointer] env = Rscons::Environment.new do |env|
env[:foo] = :bar env["CFLAGS"] += %w[-O2 -fomit-frame-pointer]
env[:foo] = :bar
end
env.dump
end end
env.dump

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
env.Command("foo", "bar") Rscons::Environment.new do |env|
env.Command("bar", "baz") env.Command("foo", "bar")
env.Command("baz", "foo") env.Command("bar", "baz")
env.Command("baz", "foo")
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Command("foo", "foo") Rscons::Environment.new do |env|
env.Command("foo", "foo")
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new do |env| build do
File.open("foo.xyz", "wb") do |fh| Rscons::Environment.new do |env|
fh.puts("hi") File.open("foo.xyz", "wb") do |fh|
fh.puts("hi")
end
env.Object("foo.o", "foo.xyz")
end end
env.Object("foo.o", "foo.xyz")
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new(echo: :command) do |env| build do
env["LD"] = "gcc" Rscons::Environment.new(echo: :command) do |env|
env["LIBPATH"] += ["libdir"] env["LD"] = "gcc"
env.Program('simple.exe', Dir['*.c']) env["LIBPATH"] += ["libdir"]
env.Program('simple.exe', Dir['*.c'])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.Object("simple.o", "simple.c", Rscons::Environment.new(echo: :command) do |env|
"CCCMD" => %w[${CC} -c -o ${_TARGET} -Dfoobar ${_SOURCES}]) env.Object("simple.o", "simple.c",
"CCCMD" => %w[${CC} -c -o ${_TARGET} -Dfoobar ${_SOURCES}])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env["DEPFILESUFFIX"] = ".deppy" Rscons::Environment.new(echo: :command) do |env|
env.Object("simple.o", "simple.c") env["DEPFILESUFFIX"] = ".deppy"
env.Object("simple.o", "simple.c")
end
end end

View File

@ -1,11 +1,13 @@
Rscons::Environment.new do |env| build do
env.add_builder(:Checker) do |target, sources, cache, env, vars| Rscons::Environment.new do |env|
unless cache.up_to_date?(target, :Checker, sources, env) env.add_builder(:Checker) do |target, sources, cache, env, vars|
puts "Checker #{sources.first}" if env.echo != :off unless cache.up_to_date?(target, :Checker, sources, env)
cache.register_build(target, :Checker, sources, env) puts "Checker #{sources.first}" if env.echo != :off
cache.register_build(target, :Checker, sources, env)
end
target
end end
target env.Program("simple.exe", "simple.c")
env.Checker(:checker, "simple.exe")
end end
env.Program("simple.exe", "simple.c")
env.Checker(:checker, "simple.exe")
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.Preprocess("simplepp.c", "simple.c") Rscons::Environment.new do |env|
env.Program("simple.exe", "simplepp.c") env.Preprocess("simplepp.c", "simple.c")
env.Program("simple.exe", "simplepp.c")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env["PROGSUFFIX"] = ".out" Rscons::Environment.new do |env|
env.Program("simple", Dir["*.c"]) env["PROGSUFFIX"] = ".out"
env.Program("simple", Dir["*.c"])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env["MYSUFFIX"] = ".out" Rscons::Environment.new do |env|
env.Program("simple${MYSUFFIX}", Dir["*.c"]) env["MYSUFFIX"] = ".out"
env.Program("simple${MYSUFFIX}", Dir["*.c"])
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Program("simple", Dir["*.c"], "PROGSUFFIX" => ".xyz") Rscons::Environment.new do |env|
env.Program("simple", Dir["*.c"], "PROGSUFFIX" => ".xyz")
end
end end

View File

@ -1,8 +1,10 @@
Rscons::Environment.new do |env| build do
env.Program("simple.exe", Dir["*.c"]) Rscons::Environment.new do |env|
env.add_build_hook do |build_op| env.Program("simple.exe", Dir["*.c"])
if build_op[:target].end_with?(".o") env.add_build_hook do |build_op|
env.Disassemble("#{build_op[:target]}.txt", build_op[:target]) if build_op[:target].end_with?(".o")
env.Disassemble("#{build_op[:target]}.txt", build_op[:target])
end
end end
end end
end end

View File

@ -5,8 +5,10 @@ class MyObject < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(MyObject.new) Rscons::Environment.new do |env|
env.MyObject("simple.o", "simple.c") env.add_builder(MyObject.new)
env.Program("simple.exe", "simple.o") env.MyObject("simple.o", "simple.c")
env.Program("simple.exe", "simple.o")
end
end end

View File

@ -1,17 +1,19 @@
class MyCommand < Rscons::Builder build do
def run(target, sources, cache, env, vars) class MyCommand < Rscons::Builder
vars = vars.merge({ def run(target, sources, cache, env, vars)
"_TARGET" => target, vars = vars.merge({
"_SOURCES" => sources, "_TARGET" => target,
}) "_SOURCES" => sources,
command = env.build_command("${CMD}", vars) })
cmd_desc = vars["CMD_DESC"] || "MyCommand" command = env.build_command("${CMD}", vars)
standard_build("#{cmd_desc} #{target}", target, command, sources, env, cache) cmd_desc = vars["CMD_DESC"] || "MyCommand"
standard_build("#{cmd_desc} #{target}", target, command, sources, env, cache)
end
end
Rscons::Environment.new do |env|
env.add_builder(MyCommand.new)
command = %w[gcc -c -o ${_TARGET} ${_SOURCES}]
env.MyCommand("simple.o", "simple.c", "CMD" => command)
end end
end end
Rscons::Environment.new do |env|
env.add_builder(MyCommand.new)
command = %w[gcc -c -o ${_TARGET} ${_SOURCES}]
env.MyCommand("simple.o", "simple.c", "CMD" => command)
end

View File

@ -18,11 +18,13 @@ class NonThreadedTestBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(ThreadedTestBuilder.new) Rscons::Environment.new do |env|
env.add_builder(NonThreadedTestBuilder.new) env.add_builder(ThreadedTestBuilder.new)
env.ThreadedTestBuilder("a") env.add_builder(NonThreadedTestBuilder.new)
env.ThreadedTestBuilder("b") env.ThreadedTestBuilder("a")
env.ThreadedTestBuilder("c") env.ThreadedTestBuilder("b")
env.NonThreadedTestBuilder("d") env.ThreadedTestBuilder("c")
env.NonThreadedTestBuilder("d")
end
end end

View File

@ -14,9 +14,11 @@ class TestBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(TestBuilder.new) Rscons::Environment.new do |env|
env.TestBuilder("one", [], "wait_time" => "3") env.add_builder(TestBuilder.new)
env.TestBuilder("two", [], "wait_time" => "0") env.TestBuilder("one", [], "wait_time" => "3")
env.depends("two", "one") env.TestBuilder("two", [], "wait_time" => "0")
env.depends("two", "one")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
program = env.Program("simple.exe", Dir["*.c"]) Rscons::Environment.new do |env|
env.depends(program, "program.ld") program = env.Program("simple.exe", Dir["*.c"])
env.depends(program, "program.ld")
end
end end

View File

@ -11,10 +11,12 @@ class Fail < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.add_builder(Fail.new) Rscons::Environment.new do |env|
4.times do |i| env.add_builder(Fail.new)
wait_time = i + 1 4.times do |i|
env.Fail("foo_#{wait_time}", [], "wait_time" => wait_time.to_s) wait_time = i + 1
env.Fail("foo_#{wait_time}", [], "wait_time" => wait_time.to_s)
end
end end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Program('simple.exe', Dir['*.cc']) Rscons::Environment.new do |env|
env.Program('simple.exe', Dir['*.cc'])
end
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
env["CXXSUFFIX"] = %w[.cccc .cc] Rscons::Environment.new do |env|
env["CXXFLAGS"] += %w[-x c++] env["CXXSUFFIX"] = %w[.cccc .cc]
env.Program("simple.exe", Dir["*.cc"] + Dir["*.cccc"]) env["CXXFLAGS"] += %w[-x c++]
env.Program("simple.exe", Dir["*.cc"] + Dir["*.cccc"])
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new do |env| build do
env.Preprocess("simplepp.cc", "simple.cc") Rscons::Environment.new do |env|
env.Program("simple.exe", "simplepp.cc") env.Preprocess("simplepp.cc", "simple.cc")
env.Program("simple.exe", "simplepp.cc")
end
end end

View File

@ -1,4 +1,6 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.Object("one.o", "one.c", 'CPPFLAGS' => ['-DONE']) Rscons::Environment.new(echo: :command) do |env|
env.Program('two_sources.exe', ['one.o', 'two.c']) env.Object("one.o", "one.c", 'CPPFLAGS' => ['-DONE'])
env.Program('two_sources.exe', ['one.o', 'two.c'])
end
end end

View File

@ -1,7 +1,9 @@
Rscons::Environment.new do |env| build do
env["ASSUFFIX"] = %w[.ssss .sss] Rscons::Environment.new do |env|
env["CFLAGS"] += %w[-S] env["ASSUFFIX"] = %w[.ssss .sss]
env.Object("one.ssss", "one.c", "CPPFLAGS" => ["-DONE"]) env["CFLAGS"] += %w[-S]
env.Object("two.sss", "two.c") env.Object("one.ssss", "one.c", "CPPFLAGS" => ["-DONE"])
env.Program("two_sources.exe", %w[one.ssss two.sss], "ASFLAGS" => env["ASFLAGS"] + %w[-x assembler]) env.Object("two.sss", "two.c")
env.Program("two_sources.exe", %w[one.ssss two.sss], "ASFLAGS" => env["ASFLAGS"] + %w[-x assembler])
end
end end

View File

@ -10,10 +10,12 @@ class StrictBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.add_builder(StrictBuilder.new) Rscons::Environment.new(echo: :command) do |env|
env.Object("one.o", "one.c", "CCFLAGS" => %w[-DONE]) env.add_builder(StrictBuilder.new)
env.Object("two.o", "two.c") env.Object("one.o", "one.c", "CCFLAGS" => %w[-DONE])
sources = File.read("sources", mode: "rb").split(" ") env.Object("two.o", "two.c")
env.StrictBuilder("program.exe", sources) sources = File.read("sources", mode: "rb").split(" ")
env.StrictBuilder("program.exe", sources)
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new() do |env| build do
env["LIBSUFFIX"] = %w[.aaaa .aaa] Rscons::Environment.new() do |env|
env.Library("one.aaaa", "one.c", "CPPFLAGS" => ["-DONE"]) env["LIBSUFFIX"] = %w[.aaaa .aaa]
env.Library("two.aaa", "two.c") env.Library("one.aaaa", "one.c", "CPPFLAGS" => ["-DONE"])
env.Program("two_sources.exe", %w[one.aaaa two.aaa]) env.Library("two.aaa", "two.c")
env.Program("two_sources.exe", %w[one.aaaa two.aaa])
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new do |env| build do
env["OBJSUFFIX"] = %w[.oooo .ooo] Rscons::Environment.new do |env|
env.Object("one.oooo", "one.c", "CPPFLAGS" => ["-DONE"]) env["OBJSUFFIX"] = %w[.oooo .ooo]
env.Object("two.ooo", "two.c") env.Object("one.oooo", "one.c", "CPPFLAGS" => ["-DONE"])
env.Program("two_sources.exe", %w[one.oooo two.ooo]) env.Object("two.ooo", "two.c")
env.Program("two_sources.exe", %w[one.oooo two.ooo])
end
end end

View File

@ -1,22 +1,24 @@
class MyObject < Rscons::Builder class MyObject < Rscons::Builder
def run(target, sources, cache, env, vars) def run(target, sources, cache, env, vars)
env.run_builder(env.builders["Object"], target, sources, cache, vars) env.run_builder(env.builders["Object"], target, sources, cache, vars)
end end
end end
Rscons::Environment.new(echo: :command) do |env| build do
env.add_builder(MyObject.new) Rscons::Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Rscons.glob('src/**')) env.add_builder(MyObject.new)
env.add_build_hook do |build_op| env.append('CPPPATH' => Rscons.glob('src/**'))
if build_op[:builder].name == "MyObject" && build_op[:sources].first =~ %r{one\.c} env.add_build_hook do |build_op|
build_op[:vars]["CFLAGS"] << "-O1" if build_op[:builder].name == "MyObject" && build_op[:sources].first =~ %r{one\.c}
build_op[:sources] = ['src/two/two.c'] build_op[:vars]["CFLAGS"] << "-O1"
elsif build_op[:builder].name == "MyObject" && build_op[:target] =~ %r{two\.o} build_op[:sources] = ['src/two/two.c']
new_vars = build_op[:vars].clone elsif build_op[:builder].name == "MyObject" && build_op[:target] =~ %r{two\.o}
new_vars["CFLAGS"] << "-O2" new_vars = build_op[:vars].clone
build_op[:vars] = new_vars new_vars["CFLAGS"] << "-O2"
end build_op[:vars] = new_vars
end end
env.MyObject('one.o', 'src/one/one.c') end
env.MyObject('two.o', 'src/two/two.c') env.MyObject('one.o', 'src/one/one.c')
end env.MyObject('two.o', 'src/two/two.c')
end
end

View File

@ -1,11 +1,13 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.append('CPPPATH' => Rscons.glob('src/**/*/')) Rscons::Environment.new(echo: :command) do |env|
env.add_build_hook do |build_op| env.append('CPPPATH' => Rscons.glob('src/**/*/'))
if File.basename(build_op[:target]) == "one.o" env.add_build_hook do |build_op|
build_op[:vars]["CFLAGS"] << "-O1" if File.basename(build_op[:target]) == "one.o"
elsif File.basename(build_op[:target]) == "two.o" build_op[:vars]["CFLAGS"] << "-O1"
build_op[:vars]["CFLAGS"] << "-O2" elsif File.basename(build_op[:target]) == "two.o"
build_op[:vars]["CFLAGS"] << "-O2"
end
end end
env.Program('build_hook.exe', Rscons.glob('src/**/*.c'))
end end
env.Program('build_hook.exe', Rscons.glob('src/**/*.c'))
end end

View File

@ -1,7 +1,9 @@
Rscons::Environment.new(echo: :command) do |env| build do
env.append('CPPPATH' => Rscons.glob('src/**').sort) Rscons::Environment.new(echo: :command) do |env|
FileUtils.mkdir_p(env.build_root) env.append('CPPPATH' => Rscons.glob('src/**').sort)
FileUtils.mv("src/one/one.c", env.build_root) FileUtils.mkdir_p(env.build_root)
env.Object("^/one.o", "^/one.c") FileUtils.mv("src/one/one.c", env.build_root)
env.Program("program.exe", Rscons.glob('src/**/*.c') + ["^/one.o"]) env.Object("^/one.o", "^/one.c")
env.Program("program.exe", Rscons.glob('src/**/*.c') + ["^/one.o"])
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Copy("inst.exe", "copy.rb") Rscons::Environment.new do |env|
env.Copy("inst.exe", "copy.rb")
end
end end

View File

@ -1,6 +1,8 @@
Rscons::Environment.new do |env| build do
env["CSUFFIX"] = %w[.yargh .c] Rscons::Environment.new do |env|
env["CFLAGS"] += %w[-x c] env["CSUFFIX"] = %w[.yargh .c]
env["CPPPATH"] += Rscons.glob("src/**") env["CFLAGS"] += %w[-x c]
env.Program("program.exe", Rscons.glob("src/**/*.{c,yargh}")) env["CPPPATH"] += Rscons.glob("src/**")
env.Program("program.exe", Rscons.glob("src/**/*.{c,yargh}"))
end
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
env.echo = :command Rscons::Environment.new do |env|
env.Install("inst.exe", "install.rb") env.echo = :command
end env.Install("inst.exe", "install.rb")
end
end

View File

@ -6,8 +6,10 @@ class MyBuilder < Rscons::Builder
end end
end end
Rscons::Environment.new do |env| build do
env.echo = :command Rscons::Environment.new do |env|
env.add_builder(MyBuilder.new) env.echo = :command
env.MyBuilder("foo") env.add_builder(MyBuilder.new)
env.MyBuilder("foo")
end
end end

View File

@ -1,3 +1,5 @@
Rscons::Environment.new do |env| build do
env.Install("inst.exe", "install.rb") Rscons::Environment.new do |env|
env.Install("inst.exe", "install.rb")
end
end end

View File

@ -1,9 +1,11 @@
Rscons::Environment.new do |env| build do
env.Directory("inst") Rscons::Environment.new do |env|
env.Install("inst", "install_directory.rb") env.Directory("inst")
env.Install("inst", "install_directory.rb")
env.Install("noexist/src", "src") env.Install("noexist/src", "src")
env.Directory("exist/src") env.Directory("exist/src")
env.Install("exist/src", "src") env.Install("exist/src", "src")
end
end end

View File

@ -1,5 +1,7 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/two" Rscons::Environment.new do |env|
env.Object("one.o", "src/one/one.c") env["CPPPATH"] << "src/two"
env.Object("one.o", "src/two/two.c") env.Object("one.o", "src/one/one.c")
env.Object("one.o", "src/two/two.c")
end
end end

View File

@ -1,10 +1,12 @@
Rscons::Environment.new do |env| build do
env["CPPPATH"] << "src/two" Rscons::Environment.new do |env|
env.Object("one.o", "src/one/one.c") env["CPPPATH"] << "src/two"
env.add_post_build_hook do |build_op| env.Object("one.o", "src/one/one.c")
if build_op[:target] == "one.o" env.add_post_build_hook do |build_op|
env["MODULE"] = "two" if build_op[:target] == "one.o"
env.Object("${MODULE}.o", "src/${MODULE}/${MODULE}.c") env["MODULE"] = "two"
env.Object("${MODULE}.o", "src/${MODULE}/${MODULE}.c")
end
end end
end end
end end

View File

@ -68,6 +68,7 @@ module Rscons
# Exit code. # Exit code.
def build(options) def build(options)
begin begin
@script.build
Environment.environments.each do |env| Environment.environments.each do |env|
env.process env.process
end end

View File

@ -15,6 +15,11 @@ module Rscons
# All Environments. # All Environments.
attr_reader :environments attr_reader :environments
# Initialize class instance variables.
def class_init
@environments = []
end
# Get an ID for a new Environment. This is a monotonically increasing # Get an ID for a new Environment. This is a monotonically increasing
# integer. # integer.
# #
@ -77,8 +82,7 @@ module Rscons
end end
end end
@echo = options[:echo] || :short @echo = options[:echo] || :short
# TODO: remove the || "build" below when autoconf is turned on for build test specs @build_root = "#{Cache.instance.configuration_data["build_dir"]}/e.#{@id}"
@build_root = "#{Cache.instance.configuration_data["build_dir"] || "build"}/e.#{@id}"
load_configuration_data! load_configuration_data!
if block_given? if block_given?
@ -273,6 +277,9 @@ module Rscons
# @return [void] # @return [void]
def process def process
cache = Cache.instance cache = Cache.instance
unless cache.configuration_data["configured"]
raise "Project must be configured before processing an Environment"
end
failure = nil failure = nil
begin begin
while @job_set.size > 0 or @threaded_commands.size > 0 while @job_set.size > 0 or @threaded_commands.size > 0
@ -1083,4 +1090,6 @@ module Rscons
end end
end end
Environment.class_init
end end

View File

@ -19,6 +19,11 @@ module Rscons
@script.autoconf = autoconf @script.autoconf = autoconf
end end
# Enter build block.
def build(&block)
@script.operations["build"] = block
end
# Enter configuration block. # Enter configuration block.
def configure(&block) def configure(&block)
@script.operations["configure"] = block @script.operations["configure"] = block
@ -81,6 +86,13 @@ module Rscons
Dsl.new(self).instance_eval(script_contents, path, 1) Dsl.new(self).instance_eval(script_contents, path, 1)
end end
# Perform build operation.
def build
if build_proc = @operations["build"]
build_proc.call
end
end
# Perform configure operation. # Perform configure operation.
def configure(configure_op) def configure(configure_op)
if operation_lambda = @operations["configure"] if operation_lambda = @operations["configure"]

View File

@ -1886,6 +1886,13 @@ EOF
expect(result.stdout).to match /Checking for C compiler\.\.\. not found/ expect(result.stdout).to match /Checking for C compiler\.\.\. not found/
expect(result.status).to_not eq 0 expect(result.status).to_not eq 0
end end
it "exits with an error if configuration has not been performed before attempting to process an environment" do
test_dir "configure"
result = run_rscons(rsconscript: "error_env_process_before_configure.rb")
expect(result.stderr).to match /Project must be configured before processing an Environment/
expect(result.status).to_not eq 0
end
end end
end end