Skip to content

Commit

Permalink
fix: update find commands to support GNU and FreeBSD flavors (#804)
Browse files Browse the repository at this point in the history
The arguments to GNU `find` and FreeBSD (MacOS) `find` are slightly
different. This PR orders the options so that GNU `find` is happy. It
also only uses options that are available in both flavors.

Closes #800.
  • Loading branch information
cgrindel authored Dec 17, 2023
1 parent 2ba25de commit 9ff3554
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions swiftpkg/internal/repository_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ def _list_files_under(

# Follow symlinks and report on the actual files.
find_args = ["find", "-H", "-L", path]

# For GNU find, it is important for the global options (e.g. -maxdepth) to be
# specified BEFORE other options like -type. Also, GNU find does not support -depth <level>.
# So, we approximate it by using -mindepth and -maxdepth.
if depth != None:
depth_str = "{}".format(depth)
find_args.extend(["-mindepth", depth_str, "-maxdepth", depth_str])
if by_name != None:
find_args.extend(["-name", by_name])
if depth != None:
find_args.extend(["-depth", "{}".format(depth)])
exec_result = repository_ctx.execute(find_args, quiet = True)
if exec_result.return_code != 0:
fail("Failed to list files in %s. stderr:\n%s" % (path, exec_result.stderr))
Expand Down Expand Up @@ -79,13 +84,22 @@ def _list_directories_under(
Returns:
A `list` of path `string` values.
"""
find_args = ["find", path, "-type", "d"]
find_args = ["find", path]

# For GNU find, it is important for the global options (e.g. -maxdepth) to be
# specified BEFORE other options like -type. Also, GNU find does not support -depth <level>.
# So, we approximate it by using -mindepth and -maxdepth.
if depth != None:
depth_str = "{}".format(depth)
find_args.extend(["-mindepth", depth_str])
if max_depth == None:
find_args.extend(["-maxdepth", depth_str])
if max_depth != None:
find_args.extend(["-maxdepth", "%d" % (max_depth)])
find_args.extend(["-type", "d"])
if by_name != None:
find_args.extend(["-name", by_name])
if depth != None:
find_args.extend(["-depth", "{}".format(depth)])

exec_result = repository_ctx.execute(find_args, quiet = True)
if exec_result.return_code != 0:
fail("Failed to list directories under %s. stderr:\n%s" % (path, exec_result.stderr))
Expand Down

0 comments on commit 9ff3554

Please sign in to comment.