Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native-cc fix to buildifier. #676

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions WARNINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Warning categories supported by buildifier's linter:
* [name-conventions](#name-conventions)
* [native-android](#native-android)
* [native-build](#native-build)
* [native-cc](#native-cc)
* [native-java](#native-java)
* [native-package](#native-package)
* [no-effect](#no-effect)
Expand Down Expand Up @@ -479,6 +480,17 @@ there.

--------------------------------------------------------------------------------

## <a name="native-cc"></a>All C++ build rules should be loaded from Starlark

* Category name: `native-cc`
* Flag in Bazel: [`--incompatible_load_cc_rules_from_bzl`](https://github.com/bazelbuild/bazel/issues/8743)
* Automatic fix: yes

The C++ build rules should be loaded from Starlark. The native rules [will be
disabled](https://github.com/bazelbuild/bazel/issues/8743).

--------------------------------------------------------------------------------

## <a name="native-java"></a>All Java build rules should be loaded from Starlark

* Category name: `native-java`
Expand Down
18 changes: 18 additions & 0 deletions tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ var AndroidNativeRules = []string{
// AndroidLoadPath is the load path for the Starlark Android Rules.
var AndroidLoadPath = "@rules_android//android:rules.bzl"

// CcNativeRules lists all C++ rules that are being migrated from Native to Starlark.
var CcNativeRules = []string{
"cc_binary",
"cc_test",
"cc_library",
"cc_import",
"cc_proto_library",
"fdo_prefetch_hints",
"fdo_profile",
"cc_toolchain",
"cc_toolchain_suite",
"objc_library",
"objc_import",
}

// CcLoadPath is the load path for the Starlark C++ Rules.
var CcLoadPath = "@rules_cc//cc:defs.bzl"

// JavaNativeRules lists all Java rules that are being migrated from Native to Starlark.
var JavaNativeRules = []string{
"java_binary",
Expand Down
1 change: 1 addition & 0 deletions warn/warn.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var FileWarningMap = map[string]func(f *build.File) []*LinterFinding{
"name-conventions": nameConventionsWarning,
"native-android": nativeAndroidRulesWarning,
"native-build": nativeInBuildFilesWarning,
"native-cc": nativeCcRulesWarning,
"native-java": nativeJavaRulesWarning,
"native-package": nativePackageWarning,
"no-effect": noEffectWarning,
Expand Down
7 changes: 7 additions & 0 deletions warn/warn_bazel_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,13 @@ func nativeAndroidRulesWarning(f *build.File) []*LinterFinding {
return notLoadedFunctionUsageCheck(f, tables.AndroidNativeRules, tables.AndroidLoadPath)
}

func nativeCcRulesWarning(f *build.File) []*LinterFinding {
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
return nil
}
return notLoadedFunctionUsageCheck(f, tables.CcNativeRules, tables.CcLoadPath)
}

func nativeJavaRulesWarning(f *build.File) []*LinterFinding {
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
return nil
Expand Down
52 changes: 52 additions & 0 deletions warn/warn_bazel_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,58 @@ android_binary()
scopeBzl|scopeBuild)
}

func TestNativeCcWarning(t *testing.T) {
checkFindingsAndFix(t, "native-cc", `
"""My file"""

def macro():
cc_library()
native.cc_binary()
cc_test()
cc_proto_library()
native.fdo_prefetch_hints()
native.objc_library()
objc_import()
cc_toolchain()
native.cc_toolchain_suite()

fdo_profile()
cc_import()
`, fmt.Sprintf(`
"""My file"""

load(%q, "cc_binary", "cc_import", "cc_library", "cc_proto_library", "cc_test", "cc_toolchain", "cc_toolchain_suite", "fdo_prefetch_hints", "fdo_profile", "objc_import", "objc_library")

def macro():
cc_library()
cc_binary()
cc_test()
cc_proto_library()
fdo_prefetch_hints()
objc_library()
objc_import()
cc_toolchain()
cc_toolchain_suite()

fdo_profile()
cc_import()
`, tables.CcLoadPath),
[]string{
fmt.Sprintf(`:4: Function "cc_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:5: Function "cc_binary" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:6: Function "cc_test" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:7: Function "cc_proto_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:8: Function "fdo_prefetch_hints" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:9: Function "objc_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:10: Function "objc_import" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:11: Function "cc_toolchain" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:12: Function "cc_toolchain_suite" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:14: Function "fdo_profile" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
fmt.Sprintf(`:15: Function "cc_import" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
},
scopeBzl|scopeBuild)
}

func TestNativeJavaWarning(t *testing.T) {
checkFindingsAndFix(t, "native-java", `
"""My file"""
Expand Down