add Rscons.glob - close #50

This commit is contained in:
Josh Holtrop 2018-08-23 11:26:20 -04:00
parent 57de94a3fb
commit c7f66694ab
17 changed files with 64 additions and 35 deletions

View File

@ -1,5 +1,5 @@
Rscons::Environment.new do |env| Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'].sort) env.append('CPPPATH' => Rscons.glob('src/**'))
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/') env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.Program('build_dir.exe', Dir['src/**/*.c']) env.Program('build_dir.exe', Rscons.glob('src/**/*.c'))
end end

View File

@ -6,7 +6,7 @@ end
Rscons::Environment.new(echo: :command) do |env| Rscons::Environment.new(echo: :command) do |env|
env.add_builder(MyObject.new) env.add_builder(MyObject.new)
env.append('CPPPATH' => Dir['src/**/*/'].sort) env.append('CPPPATH' => Rscons.glob('src/**'))
env.add_build_hook do |build_op| env.add_build_hook do |build_op|
if build_op[:builder].name == "MyObject" && build_op[:sources].first =~ %r{one\.c} if build_op[:builder].name == "MyObject" && build_op[:sources].first =~ %r{one\.c}
build_op[:vars]["CFLAGS"] << "-O1" build_op[:vars]["CFLAGS"] << "-O1"

View File

@ -1,6 +1,6 @@
env = Rscons::Environment.new do |env| env = Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'].sort) env.append('CPPPATH' => Rscons.glob('src/**/*/'))
env.build_dir("src", "build") env.build_dir("src", "build")
env.build_root = "build_root" env.build_root = "build_root"
env.Program('build_dir.exe', Dir['src/**/*.c']) env.Program('build_dir.exe', Rscons.glob('src/**/*.c'))
end end

View File

@ -1,5 +1,5 @@
Rscons::Environment.new(echo: :command) do |env| Rscons::Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Dir['src/**/*/'].sort) env.append('CPPPATH' => Rscons.glob('src/**/*/'))
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/') env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.add_build_hook do |build_op| env.add_build_hook do |build_op|
if build_op[:target] =~ %r{build_one/.*\.o} if build_op[:target] =~ %r{build_one/.*\.o}
@ -8,5 +8,5 @@ Rscons::Environment.new(echo: :command) do |env|
build_op[:vars]["CFLAGS"] << "-O2" build_op[:vars]["CFLAGS"] << "-O2"
end end
end end
env.Program('build_hook.exe', Dir['src/**/*.c'].sort) env.Program('build_hook.exe', Rscons.glob('src/**/*.c'))
end end

View File

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

View File

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

View File

@ -1,6 +1,6 @@
Rscons::Environment.new do |env| Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'].sort) env.append('CPPPATH' => Rscons.glob('src/**'))
env.build_dir("src2", "build") env.build_dir("src2", "build")
env.build_root = "build_root" env.build_root = "build_root"
env.Program('build_dir.exe', Dir['src/**/*.c']) env.Program('build_dir.exe', Rscons.glob('src/**/*.c'))
end end

View File

@ -1,6 +1,6 @@
Rscons::Environment.new do |env| Rscons::Environment.new do |env|
env.append("CPPPATH" => Dir["src/**/*/"].sort) env.append("CPPPATH" => Rscons.glob("src/**"))
env.build_dir("src/one/", "build_one/") env.build_dir("src/one/", "build_one/")
env.build_dir("src/two", "build_two") env.build_dir("src/two", "build_two")
env.Program("build_dir.exe", Dir["src/**/*.c"]) env.Program("build_dir.exe", Rscons.glob("src/**/*.c"))
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -165,6 +165,35 @@ module Rscons
@command_executer = val @command_executer = val
end end
# Return a list of paths matching the specified pattern(s).
#
# @since 1.16.0
#
# A pattern can contain a "/**" component to recurse through directories.
# If the pattern ends with "/**" then only the recursive list of
# directories will be returned.
#
# Examples:
# - "src/**": return all directories under "src", recursively (including
# "src" itself).
# - "src/**/*": return all files and directories recursively under the src
# directory.
# - "src/**/*.c": return all .c files recursively under the src directory.
# - "dir/*/": return all directories in dir, but no files.
#
# @return [Array<String>] Paths matching the specified pattern(s).
def glob(*patterns)
require "pathname"
patterns.reduce([]) do |result, pattern|
if pattern.end_with?("/**")
pattern += "/"
end
result += Dir.glob(pattern).map do |path|
Pathname.new(path.gsub("\\", "/")).cleanpath.to_s
end
end.sort
end
private private
# Determine the number of threads to use by default. # Determine the number of threads to use by default.

View File

@ -250,8 +250,8 @@ EOF
result = run_test(rsconsfile: "carat.rb") result = run_test(rsconsfile: "carat.rb")
expect(result.stderr).to eq "" expect(result.stderr).to eq ""
expect(Set[*lines(result.stdout)]).to eq Set[ expect(Set[*lines(result.stdout)]).to eq Set[
%q{gcc -c -o build_root/one.o -MMD -MF build_root/one.mf -MT TARGET -Isrc/one/ -Isrc/two/ build_root/one.c}, %q{gcc -c -o build_root/one.o -MMD -MF build_root/one.mf -MT TARGET -Isrc -Isrc/one -Isrc/two build_root/one.c},
%q{gcc -c -o build_root/src/two/two.o -MMD -MF build_root/src/two/two.mf -MT TARGET -Isrc/one/ -Isrc/two/ src/two/two.c}, %q{gcc -c -o build_root/src/two/two.o -MMD -MF build_root/src/two/two.mf -MT TARGET -Isrc -Isrc/one -Isrc/two src/two/two.c},
%Q{gcc -o build_dir.exe build_root/src/two/two.o build_root/one.o}, %Q{gcc -o build_dir.exe build_root/src/two/two.o build_root/one.o},
] ]
end end
@ -393,8 +393,8 @@ EOF
result = run_test(rsconsfile: "build_hooks.rb") result = run_test(rsconsfile: "build_hooks.rb")
expect(result.stderr).to eq "" expect(result.stderr).to eq ""
expect(Set[*lines(result.stdout)]).to eq Set[ expect(Set[*lines(result.stdout)]).to eq Set[
'gcc -c -o build_one/one.o -MMD -MF build_one/one.mf -MT TARGET -Isrc/one/ -Isrc/two/ -O1 src/one/one.c', 'gcc -c -o build_one/one.o -MMD -MF build_one/one.mf -MT TARGET -Isrc/one -Isrc/two -O1 src/one/one.c',
'gcc -c -o build_two/two.o -MMD -MF build_two/two.mf -MT TARGET -Isrc/one/ -Isrc/two/ -O2 src/two/two.c', 'gcc -c -o build_two/two.o -MMD -MF build_two/two.mf -MT TARGET -Isrc/one -Isrc/two -O2 src/two/two.c',
'gcc -o build_hook.exe build_one/one.o build_two/two.o', 'gcc -o build_hook.exe build_one/one.o build_two/two.o',
] ]
expect(`./build_hook.exe`).to eq "Hello from two()\n" expect(`./build_hook.exe`).to eq "Hello from two()\n"
@ -894,8 +894,8 @@ EOF
result = run_test(rsconsfile: "backward_compatible_build_hooks.rb") result = run_test(rsconsfile: "backward_compatible_build_hooks.rb")
expect(result.stderr).to eq "" expect(result.stderr).to eq ""
expect(Set[*lines(result.stdout)]).to eq Set[ expect(Set[*lines(result.stdout)]).to eq Set[
'gcc -c -o one.o -MMD -MF one.mf -MT TARGET -Isrc/one/ -Isrc/two/ -O1 src/two/two.c', 'gcc -c -o one.o -MMD -MF one.mf -MT TARGET -Isrc -Isrc/one -Isrc/two -O1 src/two/two.c',
'gcc -c -o two.o -MMD -MF two.mf -MT TARGET -Isrc/one/ -Isrc/two/ -O2 src/two/two.c' 'gcc -c -o two.o -MMD -MF two.mf -MT TARGET -Isrc -Isrc/one -Isrc/two -O2 src/two/two.c'
] ]
expect(File.exists?('one.o')).to be_truthy expect(File.exists?('one.o')).to be_truthy
expect(File.exists?('two.o')).to be_truthy expect(File.exists?('two.o')).to be_truthy