Skip to content

Commit

Permalink
feat: Add directive to disable manual tag (#632)
Browse files Browse the repository at this point in the history
This PR introduces a directive to disable the addition of the manual tag
to generated swift_library targets.

I understand the intent behind the tag and am ok with keeping it the
default. But for more advanced users who understand that they need to
pass the apple platform type configuration, it is useful to be able to
build globs of these targets without a dedicated build test target.
  • Loading branch information
AttilaTheFun authored Oct 2, 2023
1 parent 20e57bf commit 7aae6a7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions examples/pkg_manifest_minimal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tidy(
# Ignore the Swift build folder
# gazelle:exclude .build

# Omit tags = ["manual"] from the generated swift library targets
# gazelle:swift_library_tags -

gazelle_binary(
name = "gazelle_bin",
languages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ swift_library(
name = "MyLibrary",
srcs = ["World.swift"],
module_name = "MyLibrary",
tags = ["manual"],
visibility = ["//visibility:public"],
deps = ["@swiftpkg_my_local_package//:Sources_GreetingsFramework"],
)
16 changes: 16 additions & 0 deletions gazelle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"os"
"path/filepath"
"strings"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
Expand Down Expand Up @@ -127,11 +128,13 @@ func (sl *swiftLang) CheckFlags(fs *flag.FlagSet, c *config.Config) error {

const moduleNamingConventionDirective = "swift_module_naming_convention"
const defaultModuleNameDirective = "swift_default_module_name"
const swiftLibraryTags = "swift_library_tags"

func (*swiftLang) KnownDirectives() []string {
return []string{
moduleNamingConventionDirective,
defaultModuleNameDirective,
swiftLibraryTags,
}
}

Expand All @@ -148,6 +151,19 @@ func (*swiftLang) Configure(c *config.Config, rel string, f *rule.File) {
} else {
sc.ModuleNamingConvention = swiftcfg.MatchCaseModuleNamingConvention
}
case swiftLibraryTags:
var tags []string
if d.Value == "" {
// Mark swift_library targets as manual.
// We do this so that they are always built as a dependency of a target
// which can provide critical configuration information.
tags = []string{"manual"}
} else if d.Value == "-" {
tags = nil
} else {
tags = strings.Split(d.Value, ",")
}
sc.SwiftLibraryTags = tags
case defaultModuleNameDirective:
sc.DefaultModuleNames[rel] = d.Value
}
Expand Down
2 changes: 1 addition & 1 deletion gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func genRulesFromSrcFiles(sc *swiftcfg.SwiftConfig, args language.GenerateArgs)

// Generate the rules from sources:
defaultName, defaultModuleName := defaultNameAndModuleName(args)
rules = swift.RulesFromSrcs(args, srcs, defaultName, defaultModuleName)
rules = swift.RulesFromSrcs(args, srcs, defaultName, defaultModuleName, sc.SwiftLibraryTags)
result.Gen = append(result.Gen, rules...)
result.Imports = swift.Imports(result.Gen)
result.Empty = generateEmpty(args, srcs)
Expand Down
9 changes: 5 additions & 4 deletions gazelle/internal/swift/rules_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ func rulesForLibraryModule(
srcs []string,
swiftImports []string,
shouldSetVis bool,
swiftLibraryTags []string,
buildFile *rule.File,
) []*rule.Rule {
name, moduleName := ruleNameAndModuleName(buildFile, LibraryRuleKind, defaultName, defaultModuleName)
r := rule.NewRule(LibraryRuleKind, name)
setCommonSwiftAttrs(r, moduleName, srcs, swiftImports)
setVisibilityAttr(r, shouldSetVis, []string{"//visibility:public"})
// Mark swift_library targets as manual. We do this so that they are always
// built from a leaf node which can provide critical configuration
// information.
r.SetAttr("tags", []string{"manual"})
if len(swiftLibraryTags) > 0 {
r.SetAttr("tags", swiftLibraryTags)
}

return []*rule.Rule{r}
}

Expand Down
2 changes: 1 addition & 1 deletion gazelle/internal/swift/rules_from_protos.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/bazelbuild/bazel-gazelle/rule"
)

// RulesFromSrcs returns the Bazel build rule declarations for the provided source files.
// RulesFromProtos returns the Bazel build rule declarations for the provided source files.
func RulesFromProtos(args language.GenerateArgs) []*rule.Rule {

// Extract information about proto files.
Expand Down
3 changes: 2 additions & 1 deletion gazelle/internal/swift/rules_from_srcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func RulesFromSrcs(
srcs []string,
defaultName string,
defaultModuleName string,
swiftLibraryTags []string,
) []*rule.Rule {
fileInfos := swiftpkg.NewSwiftFileInfosFromRelPaths(args.Dir, srcs)
swiftImports, moduleType := collectSwiftInfo(fileInfos)
Expand All @@ -24,7 +25,7 @@ func RulesFromSrcs(
var rules []*rule.Rule
switch moduleType {
case LibraryModuleType:
rules = rulesForLibraryModule(defaultName, defaultModuleName, srcs, swiftImports, shouldSetVis, args.File)
rules = rulesForLibraryModule(defaultName, defaultModuleName, srcs, swiftImports, shouldSetVis, swiftLibraryTags, args.File)
case BinaryModuleType:
rules = rulesForBinaryModule(defaultName, defaultModuleName, srcs, swiftImports, shouldSetVis, args.File)
case TestModuleType:
Expand Down
6 changes: 6 additions & 0 deletions gazelle/internal/swiftcfg/swift_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ type SwiftConfig struct {

GenerateSwiftDepsForWorkspace bool

// The naming convention to apply to the module names derived from the directory names.
// The default behavior uses the name verbatim while PascalCase will convert snake_case to PascalCase.
ModuleNamingConvention string

// The set of tags to apply to generated swift library targets.
// Defaults to ["manual"]
SwiftLibraryTags []string

// Mapping of relative path to default module name. These values are populated from directives
// that can be applied to
DefaultModuleNames map[string]string
Expand Down

0 comments on commit 7aae6a7

Please sign in to comment.