Add find_files builtin, use it to fix find_and_copy implementation
The macro find-and-copy finds all the files in the given source tree that
match the given filename patten and create <source>:<dest> pair with the
same relative path in the destination tree.
Bug: 193540681
Test: rbcrun build/make/tests/run.rbc
Change-Id: Ic4315ce2fab7a7791ab55dd9eed039205a1c721a
diff --git a/tools/rbcrun/host.go b/tools/rbcrun/host.go
index b3dd499..7f4f72d 100644
--- a/tools/rbcrun/host.go
+++ b/tools/rbcrun/host.go
@@ -16,6 +16,7 @@
import (
"fmt"
+ "io/fs"
"os"
"os/exec"
"path/filepath"
@@ -170,6 +171,46 @@
return makeStringList(files), nil
}
+// find(top, pattern, only_files = 0) returns all the paths under 'top'
+// whose basename matches 'pattern' (which is a shell's glob pattern).
+// If 'only_files' is non-zero, only the paths to the regular files are
+// returned. The returned paths are relative to 'top'.
+func find(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
+ kwargs []starlark.Tuple) (starlark.Value, error) {
+ var top, pattern string
+ var onlyFiles int
+ if err := starlark.UnpackArgs(b.Name(), args, kwargs,
+ "top", &top, "pattern", &pattern, "only_files?", &onlyFiles); err != nil {
+ return starlark.None, err
+ }
+ top = filepath.Clean(top)
+ pattern = filepath.Clean(pattern)
+ // Go's filepath.Walk is slow, consider using OS's find
+ var res []string
+ err := filepath.WalkDir(top, func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ if d != nil && d.IsDir() {
+ return fs.SkipDir
+ } else {
+ return nil
+ }
+ }
+ relPath := strings.TrimPrefix(path, top)
+ if len(relPath) > 0 && relPath[0] == os.PathSeparator {
+ relPath = relPath[1:]
+ }
+ // Do not return top-level dir
+ if len(relPath) == 0 {
+ return nil
+ }
+ if matched, err := filepath.Match(pattern, d.Name()); err == nil && matched && (onlyFiles == 0 || d.Type().IsRegular()) {
+ res = append(res, relPath)
+ }
+ return nil
+ })
+ return makeStringList(res), err
+}
+
// shell(command) runs OS shell with given command and returns back
// its output the same way as Make's $(shell ) function. The end-of-lines
// ("\n" or "\r\n") are replaced with " " in the result, and the trailing
@@ -226,6 +267,8 @@
"rblf_env": structFromEnv(os.Environ()),
// To convert makefile's $(wildcard foo)
"rblf_file_exists": starlark.NewBuiltin("rblf_file_exists", fileExists),
+ // To convert find-copy-subdir and product-copy-files-by pattern
+ "rblf_find_files": starlark.NewBuiltin("rblf_find_files", find),
// To convert makefile's $(filter ...)/$(filter-out)
"rblf_regex": starlark.NewBuiltin("rblf_regex", regexMatch),
// To convert makefile's $(shell cmd)