5.2 Example Build Scripts5.2.1 Example: Building a C Programbuild do Environment.new do |env| env["CFLAGS"] << "-Wall" env.Program("program", glob("src/**/*.c")) end end 5.2.2 Example: Building a D Programbuild do Environment.new do |env| env["DFLAGS"] << "-Wall" env.Program("program", glob("src/**/*.d")) end end 5.2.3 Example: Cloning an Environmentbuild do main_env = Environment.new do |env| env["CFLAGS"] = ["-DSOME_DEFINE", "-O3"] env["LIBS"] = ["SDL"] env.Program("program", glob("src/**/*.cc")) end debug_env = main_env.clone do |env| env["CFLAGS"] -= ["-O3"] env["CFLAGS"] += ["-g", "-O0"] env.Program("program-debug", glob("src/**/*.cc")) end end 5.2.4 Example: Custom Builderclass GenerateFoo < Builder def run(options) target, cache = options.values_at(:target, :cache) cache.mkdir_p(File.dirname(target)) File.open(target, "w") do |fh| fh.puts <<EOF #define GENERATED 42 EOF end target end end build do Environment.new do |env| env.add_builder(GenerateFoo) env.GenerateFoo("foo.h", []) env.Program("a.out", glob("*.c")) end end 5.2.5 Example: Using different compilation flags for some sourcesbuild do Environment.new do |env| env["CFLAGS"] = ["-O3", "-Wall"] env.add_build_hook do |build_op| if build_op[:target] =~ %r{build/third-party} build_op[:vars]["CFLAGS"] -= ["-Wall"] end end env.Program("program", glob("**/*.cc")) end end 5.2.6 Example: Creating a static librarybuild do Environment.new do |env| env.Library("mylib.a", glob("src/**/*.c")) end end 5.2.7 Example: Creating a C++ parser source from a Yacc/Bison input filebuild do Environment.new do |env| env.CFile("^/parser.tab.cc", "parser.yy") end end
|