Globs are regular expressions, which define regular grammars, so glob matching is equivalent to solving a containment problem for a regular language. This requires a finite state machine, so the problem is solvable without recursion.
To illustrate: the glob "a/b/*/c/**/d" is the same as regex "a/b/[^/]+/c(/.+)+/d"
Unfortunately Skylark doesn't support regex matching on strings, so right now you have to implement glob matching on your own. Or, if you can, use native.glob() if your input paths are in a filegroup -- that'd be by far the easiest solution.
I admit implementing glob matching in pure Skylark is painfully complicated, I spent a good couple hours on it now but I don't have the tenacity to finish. My naive algorithm is this:
1. Split up the glob patterns at "/"s, then create groups by splitting at the "**"s. Example: "a/b/*/c/**/d/**/e/f" --> [a, b, *, c, **, d, **, e, f] --> [[a,b,*, c], [d], [e,f]]
2. Find all occurrences of the first elements of each group, that gives you a list of lists. These are the potential starting positions of the groups in the input.
3. Try to match each group from each starting position, e.g. if "a" (in the first group) was found at positions [0, 2, 3], then check if the next character ("b") is at positions [1, 3, 4]. Remove starting positions that are dead ends. Do this for the starting positions of all groups. In the end you have the list of starting positions where each group was fully found.
4. Finally you need to create an ascending sequence of the starting positions, and see if the groups could fit fully, leaving at least one element for the "**" inbetween. In other words, if the first group ([a,b,*,c]) was found in positions 0 and 8, then the next group can only start at 0+4+1=5 or at 8+4+1=13, to fulfill the "a/b/*/c/**" fragment of the pattern. So if the second group was found at positions [5, 10], you know that the input cannot possibly match, but if it was found on [5, 10, 15], then you know that it may. You need to do this aligning for all groups.
Good luck!