Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use_repo(
"compilation_test_include_prefix_unit_group",
"compilation_test_includes",
"compilation_test_index_globs",
"compilation_test_index_globs_excluded",
"compilation_test_keep-assigned-groups",
"compilation_test_keep_deps",
"compilation_test_kind_name_collisions",
Expand Down
63 changes: 32 additions & 31 deletions language/cc/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func transformIncludePath(libRel, stripIncludePrefix, includePrefix, hdrRel stri
// If the attribute is a list of strings, it returns the list. If the attribute
// is a glob, it expands the glob patterns relative to dir and returns the
// resulting paths.
func readListOrGlob(config *config.Config, r *rule.Rule, dir, attrName string) ([]string, error) {
func readListOrGlob(config *config.Config, r *rule.Rule, pkg, attrName string) ([]string, error) {
// Fast path: plain list of strings in the BUILD file.
if ss := r.AttrStrings(attrName); ss != nil {
return ss, nil
Expand All @@ -187,7 +187,7 @@ func readListOrGlob(config *config.Config, r *rule.Rule, dir, attrName string) (
return nil, nil
}
if globValue, ok := rule.ParseGlobExpr(expr); ok {
return expandGlob(config, dir, globValue)
return expandGlob(config, pkg, globValue)
}
return nil, nil
}
Expand All @@ -198,7 +198,7 @@ func readListOrGlob(config *config.Config, r *rule.Rule, dir, attrName string) (
// are sorted in lexicographical order. It does not use I/O, it uses cached
// directory info obtained from walk.GetDirInfo so it might panic if the
// directory was not walked before.
func expandGlob(config *config.Config, dir string, glob rule.GlobValue) ([]string, error) {
func expandGlob(config *config.Config, pkg string, glob rule.GlobValue) ([]string, error) {
if len(glob.Patterns) == 0 {
return nil, nil
}
Expand All @@ -220,42 +220,43 @@ func expandGlob(config *config.Config, dir string, glob rule.GlobValue) ([]strin
excludePatterns := validatedPatterns(glob.Excludes)

// Traverse the file tree using walk.GetDirInfo and collect all matching files
matched := []string{}
var matched []string
var traverse func(string)
traverse = func(relativePath string) {
di, err := walk.GetDirInfo(relativePath)
traverse = func(current_subdir string) {
di, err := walk.GetDirInfo(current_subdir)
if err != nil {
return // swallow errors
}
if relativePath != "" {
// When walking the subdirectories, we need to exclude dirs containg BUILD files
if slices.ContainsFunc(di.RegularFiles, config.IsValidBuildFileName) {
return // BUILD file found, stop walking
}

// When walking the subdirectories, we need to exclude dirs containing BUILD files
if current_subdir != pkg && slices.ContainsFunc(di.RegularFiles, config.IsValidBuildFileName) {
return // BUILD file found, stop walking
}
for _, file := range di.RegularFiles {
path := filepath.Join(relativePath, file)
// Check matches include pattern
if !slices.ContainsFunc(
includePatterns,
func(pattern string) bool { return doublestar.MatchUnvalidated(pattern, path) },
) {
continue // not included
}
// Check matched exclude pattern
if slices.ContainsFunc(
excludePatterns,
func(pattern string) bool { return doublestar.MatchUnvalidated(pattern, path) },
) {
continue // excluded
}
matched = append(matched, path)

pkgRelativeSubdir, err := filepath.Rel(pkg, current_subdir)
if err != nil {
log.Panicf("gazelle_cc: failed to get relative path of %s to %s: %v", current_subdir, pkg, err)
}
for _, dir := range di.Subdirs {
traverse(filepath.Join(relativePath, dir))

fileMatcher := func(file string) (pkgRelativeFile string, ok bool) {
pkgRelativeFile = filepath.Join(pkgRelativeSubdir, file)
globMatcher := func(pattern string) bool { return doublestar.MatchUnvalidated(pattern, pkgRelativeFile) }
ok = slices.ContainsFunc(includePatterns, globMatcher) && !slices.ContainsFunc(excludePatterns, globMatcher)
return
}

matched = slices.Grow(matched, len(di.RegularFiles))
matched = slices.AppendSeq(matched, collections.FilterMapSeq(slices.Values(di.RegularFiles), fileMatcher))

// Traverse the subdirectories
for _, subdir := range di.Subdirs {
traverse(filepath.Join(current_subdir, subdir))
}
}
traverse(dir)

// Start traversal from the package directory
traverse(pkg)

sort.Strings(matched)
return matched, nil
}
2 changes: 1 addition & 1 deletion language/cc/testdata/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ COMPILABLE_TEST_DIRS = [paths.dirname(p) for p in glob(
"cycle-in-existing-units_no_merge/**",
"deps_external/**",
"deps_index/**",
"index_globs/**",
"index_globs_excluded/**",
"kind_name_collisions/**",

# Sources with syntax errors won't compile.
Expand Down
4 changes: 4 additions & 0 deletions language/cc/testdata/index_globs/BUILD.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

# gazelle:cc_group unit

cc_library(
name = "library",
hdrs = glob(
["include/**/*.h"],
["include/private/*.h"]),
visibility = ["//visibility:public"],
)

cc_library(
Expand All @@ -13,4 +16,5 @@ cc_library(
include = ["include/private/*.h"],
exclude = ["include/**/excluded_*.h"]
),
visibility = ["//visibility:public"],
)
4 changes: 4 additions & 0 deletions language/cc/testdata/index_globs/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

# gazelle:cc_group unit

cc_library(
name = "library",
hdrs = glob(
["include/**/*.h"],
["include/private/*.h"]),
visibility = ["//visibility:public"],
)

cc_library(
Expand All @@ -13,4 +16,5 @@ cc_library(
include = ["include/private/*.h"],
exclude = ["include/**/excluded_*.h"]
),
visibility = ["//visibility:public"],
)
Comment thread
mateusz-olczyk marked this conversation as resolved.
Outdated
Binary file not shown.
9 changes: 7 additions & 2 deletions language/cc/testdata/index_globs/include/with_build/BUILD.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "library_with_build",
hdrs = ["with_build.h"],
)
hdrs = glob(["**/*.h"]),
include_prefix = "include/with_build",
strip_include_prefix = "subdir",
visibility = ["//visibility:public"],
)
9 changes: 9 additions & 0 deletions language/cc/testdata/index_globs/include/with_build/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "library_with_build",
hdrs = glob(["**/*.h"]),
include_prefix = "include/with_build",
strip_include_prefix = "subdir",
visibility = ["//visibility:public"],
)
6 changes: 0 additions & 6 deletions language/cc/testdata/index_globs/src/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "usage_excluded",
srcs = ["usage_excluded.cc"],
visibility = ["//visibility:public"],
)

cc_library(
name = "usage_lib",
srcs = ["usage_lib.cc"],
Expand Down
16 changes: 16 additions & 0 deletions language/cc/testdata/index_globs_excluded/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# gazelle:cc_group unit

cc_library(
name = "library",
hdrs = glob(
["include/**/*.h"],
["include/private/*.h"]),
)

cc_library(
name = "library_private",
hdrs = glob(
include = ["include/private/*.h"],
exclude = ["include/**/excluded_*.h"]
),
)
16 changes: 16 additions & 0 deletions language/cc/testdata/index_globs_excluded/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# gazelle:cc_group unit

cc_library(
name = "library",
hdrs = glob(
["include/**/*.h"],
["include/private/*.h"]),
)

cc_library(
name = "library_private",
hdrs = glob(
include = ["include/private/*.h"],
exclude = ["include/**/excluded_*.h"]
),
)
Empty file.
1 change: 1 addition & 0 deletions language/cc/testdata/index_globs_excluded/arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cc_library(
name = "library_with_build",
hdrs = glob(["*.h"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cc_library(
name = "library_with_build",
hdrs = glob(["*.h"]),
)
35 changes: 35 additions & 0 deletions language/cc/testdata/index_globs_excluded/src/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "usage_excluded",
srcs = ["usage_excluded.cc"],
visibility = ["//visibility:public"],
)

cc_library(
name = "usage_lib",
srcs = ["usage_lib.cc"],
implementation_deps = ["//:library"],
visibility = ["//visibility:public"],
)

cc_library(
name = "usage_lib-ext",
srcs = ["usage_lib-ext.cc"],
implementation_deps = ["//:library"],
visibility = ["//visibility:public"],
)

cc_library(
name = "usage_private",
srcs = ["usage_private.cc"],
implementation_deps = ["//:library_private"],
visibility = ["//visibility:public"],
)

cc_library(
name = "usage_with_build",
srcs = ["usage_with_build.cc"],
implementation_deps = ["//include/with_build:library_with_build"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "include/contrib/lib-ext.h"
1 change: 1 addition & 0 deletions language/cc/testdata/index_globs_excluded/src/usage_lib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "include/lib.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "include/private/private.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "include/with_build/with_build.h"
Loading