Skip to content

Commit 1d76340

Browse files
authored
Enable warnings for the native cc rules & symbols (#1326)
1 parent a84ab46 commit 1d76340

File tree

8 files changed

+251
-86
lines changed

8 files changed

+251
-86
lines changed

WARNINGS.md

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,25 @@ Warning categories supported by buildifier's linter:
3939
* [`name-conventions`](#name-conventions)
4040
* [`native-android`](#native-android)
4141
* [`native-build`](#native-build)
42-
* [`native-cc`](#native-cc)
42+
* [`native-cc-binary`](#native-cc-binary)
43+
* [`native-cc-common`](#native-cc-common)
44+
* [`native-cc-debug-package-info`](#native-cc-debug-package-info)
45+
* [`native-cc-fdo-prefetch-hints`](#native-cc-fdo-prefetch-hints)
46+
* [`native-cc-fdo-profile`](#native-cc-fdo-profile)
47+
* [`native-cc-import`](#native-cc-import)
48+
* [`native-cc-info`](#native-cc-info)
49+
* [`native-cc-library`](#native-cc-library)
50+
* [`native-cc-memprof-profile`](#native-cc-memprof-profile)
51+
* [`native-cc-objc-import`](#native-cc-objc-import)
52+
* [`native-cc-objc-library`](#native-cc-objc-library)
53+
* [`native-cc-propeller-optimize`](#native-cc-propeller-optimize)
4354
* [`native-cc-proto`](#native-cc-proto)
55+
* [`native-cc-shared-library`](#native-cc-shared-library)
56+
* [`native-cc-shared-library-hint-info`](#native-cc-shared-library-hint-info)
57+
* [`native-cc-shared-library-info`](#native-cc-shared-library-info)
58+
* [`native-cc-test`](#native-cc-test)
59+
* [`native-cc-toolchain`](#native-cc-toolchain)
60+
* [`native-cc-toolchain-suite`](#native-cc-toolchain-suite)
4461
* [`native-java-binary`](#native-java-binary)
4562
* [`native-java-common`](#native-java-common)
4663
* [`native-java-import`](#native-java-import)
@@ -698,18 +715,32 @@ as global symbols there.
698715

699716
--------------------------------------------------------------------------------
700717

701-
## <a name="native-cc"></a>All C++ build rules should be loaded from Starlark
718+
## <a name="native-cc-binary"></a><a name="native-cc-common"></a><a name="native-cc-debug-package-info"></a><a name="native-cc-fdo-prefetch-hints"></a><a name="native-cc-fdo-profile"></a><a name="native-cc-import"></a><a name="native-cc-info"></a><a name="native-cc-library"></a><a name="native-cc-memprof-profile"></a><a name="native-cc-objc-import"></a><a name="native-cc-objc-library"></a><a name="native-cc-propeller-optimize"></a><a name="native-cc-shared-library"></a><a name="native-cc-shared-library-hint-info"></a><a name="native-cc-shared-library-info"></a><a name="native-cc-test"></a><a name="native-cc-toolchain"></a><a name="native-cc-toolchain-suite"></a>All C++ build rules should be loaded from Starlark
702719

703-
* Category name: `native-cc`
704-
* Flag in Bazel: [`--incompatible_load_cc_rules_from_bzl`](https://github.com/bazelbuild/bazel/issues/8743)
720+
* Category names:
721+
* `native-cc-binary`
722+
* `native-cc-common`
723+
* `native-cc-debug-package-info`
724+
* `native-cc-fdo-prefetch-hints`
725+
* `native-cc-fdo-profile`
726+
* `native-cc-import`
727+
* `native-cc-info`
728+
* `native-cc-library`
729+
* `native-cc-memprof-profile`
730+
* `native-cc-objc-import`
731+
* `native-cc-objc-library`
732+
* `native-cc-propeller-optimize`
733+
* `native-cc-shared-library`
734+
* `native-cc-shared-library-hint-info`
735+
* `native-cc-shared-library-info`
736+
* `native-cc-test`
737+
* `native-cc-toolchain`
738+
* `native-cc-toolchain-suite`
739+
* Flag in Bazel: [`--incompatible_autoload_externally`](https://github.com/bazelbuild/bazel/issues/23043)
705740
* Automatic fix: yes
706-
* [Suppress the warning](#suppress): `# buildifier: disable=native-cc`
707-
708-
The CC build rules should be loaded from Starlark.
741+
* [Suppress the warning](#suppress): `# buildifier: disable=native-cc-binary`, `# buildifier: disable=native-cc-common`, `# buildifier: disable=native-cc-debug-package-info`, `# buildifier: disable=native-cc-fdo-prefetch-hints`, `# buildifier: disable=native-cc-fdo-profile`, `# buildifier: disable=native-cc-import`, `# buildifier: disable=native-cc-info`, `# buildifier: disable=native-cc-library`, `# buildifier: disable=native-cc-memprof-profile`, `# buildifier: disable=native-cc-objc-import`, `# buildifier: disable=native-cc-objc-library`, `# buildifier: disable=native-cc-propeller-optimize`, `# buildifier: disable=native-cc-shared-library`, `# buildifier: disable=native-cc-shared-library-hint-info`, `# buildifier: disable=native-cc-shared-library-info`, `# buildifier: disable=native-cc-test`, `# buildifier: disable=native-cc-toolchain`, `# buildifier: disable=native-cc-toolchain-suite`
709742

710-
Update: the plans for disabling native rules
711-
[have been postponed](https://groups.google.com/g/bazel-discuss/c/XNvpWcge4AE/m/aJ-aQzszAwAJ),
712-
at the moment it's not required to load Starlark rules.
743+
The C++ build rules should be loaded from @rules_cc.
713744

714745
--------------------------------------------------------------------------------
715746

buildifier/config/config_test.go

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,25 @@ func ExampleExample() {
7878
// "name-conventions",
7979
// "native-android",
8080
// "native-build",
81-
// "native-cc",
81+
// "native-cc-binary",
82+
// "native-cc-common",
83+
// "native-cc-debug-package-info",
84+
// "native-cc-fdo-prefetch-hints",
85+
// "native-cc-fdo-profile",
86+
// "native-cc-import",
87+
// "native-cc-info",
88+
// "native-cc-library",
89+
// "native-cc-memprof-profile",
90+
// "native-cc-objc-import",
91+
// "native-cc-objc-library",
92+
// "native-cc-propeller-optimize",
8293
// "native-cc-proto",
94+
// "native-cc-shared-library",
95+
// "native-cc-shared-library-hint-info",
96+
// "native-cc-shared-library-info",
97+
// "native-cc-test",
98+
// "native-cc-toolchain",
99+
// "native-cc-toolchain-suite",
83100
// "native-java-binary",
84101
// "native-java-common",
85102
// "native-java-import",
@@ -279,8 +296,25 @@ func TestValidate(t *testing.T) {
279296
"name-conventions",
280297
"native-android",
281298
"native-build",
282-
"native-cc",
299+
"native-cc-binary",
300+
"native-cc-common",
301+
"native-cc-debug-package-info",
302+
"native-cc-fdo-prefetch-hints",
303+
"native-cc-fdo-profile",
304+
"native-cc-import",
305+
"native-cc-info",
306+
"native-cc-library",
307+
"native-cc-memprof-profile",
308+
"native-cc-objc-import",
309+
"native-cc-objc-library",
310+
"native-cc-propeller-optimize",
283311
"native-cc-proto",
312+
"native-cc-shared-library",
313+
"native-cc-shared-library-hint-info",
314+
"native-cc-shared-library-info",
315+
"native-cc-test",
316+
"native-cc-toolchain",
317+
"native-cc-toolchain-suite",
284318
"native-java-binary",
285319
"native-java-common",
286320
"native-java-import",
@@ -361,8 +395,25 @@ func TestValidate(t *testing.T) {
361395
"name-conventions",
362396
"native-android",
363397
"native-build",
364-
"native-cc",
398+
"native-cc-binary",
399+
"native-cc-common",
400+
"native-cc-debug-package-info",
401+
"native-cc-fdo-prefetch-hints",
402+
"native-cc-fdo-profile",
403+
"native-cc-import",
404+
"native-cc-info",
405+
"native-cc-library",
406+
"native-cc-memprof-profile",
407+
"native-cc-objc-import",
408+
"native-cc-objc-library",
409+
"native-cc-propeller-optimize",
365410
"native-cc-proto",
411+
"native-cc-shared-library",
412+
"native-cc-shared-library-hint-info",
413+
"native-cc-shared-library-info",
414+
"native-cc-test",
415+
"native-cc-toolchain",
416+
"native-cc-toolchain-suite",
366417
"native-java-binary",
367418
"native-java-common",
368419
"native-java-import",
@@ -443,8 +494,25 @@ func TestValidate(t *testing.T) {
443494
"name-conventions",
444495
"native-android",
445496
"native-build",
446-
"native-cc",
497+
"native-cc-binary",
498+
"native-cc-common",
499+
"native-cc-debug-package-info",
500+
"native-cc-fdo-prefetch-hints",
501+
"native-cc-fdo-profile",
502+
"native-cc-import",
503+
"native-cc-info",
504+
"native-cc-library",
505+
"native-cc-memprof-profile",
506+
"native-cc-objc-import",
507+
"native-cc-objc-library",
508+
"native-cc-propeller-optimize",
447509
"native-cc-proto",
510+
"native-cc-shared-library",
511+
"native-cc-shared-library-hint-info",
512+
"native-cc-shared-library-info",
513+
"native-cc-test",
514+
"native-cc-toolchain",
515+
"native-cc-toolchain-suite",
448516
"native-java-binary",
449517
"native-java-common",
450518
"native-java-import",
@@ -490,7 +558,7 @@ func TestValidate(t *testing.T) {
490558
"unused-variable",
491559
"unsorted-dict-items",
492560
}},
493-
"warnings error": {options: "--warnings=native-cc,-print,-deprecated-function", wantErr: fmt.Errorf(`warning categories with modifiers ("+" or "-") can't be mixed with raw warning categories`)},
561+
"warnings error": {options: "--warnings=native-py,-print,-deprecated-function", wantErr: fmt.Errorf(`warning categories with modifiers ("+" or "-") can't be mixed with raw warning categories`)},
494562
} {
495563
t.Run(name, func(t *testing.T) {
496564
c := New()

buildifier/integration_test.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,25 @@ cat > golden/.buildifier.example.json <<EOF
283283
"name-conventions",
284284
"native-android",
285285
"native-build",
286-
"native-cc",
286+
"native-cc-binary",
287+
"native-cc-common",
288+
"native-cc-debug-package-info",
289+
"native-cc-fdo-prefetch-hints",
290+
"native-cc-fdo-profile",
291+
"native-cc-import",
292+
"native-cc-info",
293+
"native-cc-library",
294+
"native-cc-memprof-profile",
295+
"native-cc-objc-import",
296+
"native-cc-objc-library",
297+
"native-cc-propeller-optimize",
287298
"native-cc-proto",
299+
"native-cc-shared-library",
300+
"native-cc-shared-library-hint-info",
301+
"native-cc-shared-library-info",
302+
"native-cc-test",
303+
"native-cc-toolchain",
304+
"native-cc-toolchain-suite",
288305
"native-java-binary",
289306
"native-java-common",
290307
"native-java-import",

tables/tables.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,8 @@ var AndroidNativeRules = []string{
228228
// AndroidLoadPath is the load path for the Starlark Android Rules.
229229
var AndroidLoadPath = "@rules_android//android:rules.bzl"
230230

231-
// CcNativeRules lists all C++ rules that are being migrated from Native to Starlark.
232-
var CcNativeRules = []string{
233-
"cc_binary",
234-
"cc_test",
235-
"cc_library",
236-
"cc_import",
237-
"fdo_prefetch_hints",
238-
"fdo_profile",
239-
"cc_toolchain",
240-
"cc_toolchain_suite",
241-
"objc_library",
242-
"objc_import",
243-
}
244-
245-
// CcLoadPath is the load path for the Starlark C++ Rules.
246-
var CcLoadPath = "@rules_cc//cc:defs.bzl"
231+
// CcLoadPathPrefix is the load path for the Starlark C++ Rules.
232+
var CcLoadPathPrefix = "@rules_cc//cc"
247233

248234
// JavaLoadPathPrefix is the load package for the Starlark Java Rules.
249235
var JavaLoadPathPrefix = "@rules_java//java"

warn/docs/warnings.textproto

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,28 @@ warnings: {
480480
}
481481

482482
warnings: {
483-
name: "native-cc"
483+
name: "native-cc-binary"
484+
name: "native-cc-common"
485+
name: "native-cc-debug-package-info"
486+
name: "native-cc-fdo-prefetch-hints"
487+
name: "native-cc-fdo-profile"
488+
name: "native-cc-import"
489+
name: "native-cc-info"
490+
name: "native-cc-library"
491+
name: "native-cc-memprof-profile"
492+
name: "native-cc-objc-import"
493+
name: "native-cc-objc-library"
494+
name: "native-cc-propeller-optimize"
495+
name: "native-cc-shared-library"
496+
name: "native-cc-shared-library-hint-info"
497+
name: "native-cc-shared-library-info"
498+
name: "native-cc-test"
499+
name: "native-cc-toolchain"
500+
name: "native-cc-toolchain-suite"
484501
header: "All C++ build rules should be loaded from Starlark"
485-
description:
486-
"The CC build rules should be loaded from Starlark.\n\n"
487-
"Update: the plans for disabling native rules\n"
488-
"[have been postponed](https://groups.google.com/g/bazel-discuss/c/XNvpWcge4AE/m/aJ-aQzszAwAJ),\n"
489-
"at the moment it's not required to load Starlark rules."
490-
bazel_flag: "--incompatible_load_cc_rules_from_bzl"
491-
bazel_flag_link: "https://github.com/bazelbuild/bazel/issues/8743"
502+
description: "The C++ build rules should be loaded from @rules_cc."
503+
bazel_flag: "--incompatible_autoload_externally"
504+
bazel_flag_link: "https://github.com/bazelbuild/bazel/issues/23043"
492505
autofix: true
493506
}
494507

warn/warn.go

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,35 +172,52 @@ var FileWarningMap = map[string]func(f *build.File) []*LinterFinding{
172172

173173
// MultiFileWarningMap lists the warnings that run on the whole file, but may use other files.
174174
var MultiFileWarningMap = map[string]func(f *build.File, fileReader *FileReader) []*LinterFinding{
175-
"deprecated-function": deprecatedFunctionWarning,
176-
"git-repository": nativeGitRepositoryWarning,
177-
"http-archive": nativeHTTPArchiveWarning,
178-
"native-android": nativeAndroidRulesWarning,
179-
"native-cc": nativeCcRulesWarning,
180-
"native-java-binary": NativeJavaRulesWarning("java_binary"),
181-
"native-java-import": NativeJavaRulesWarning("java_import"),
182-
"native-java-library": NativeJavaRulesWarning("java_library"),
183-
"native-java-plugin": NativeJavaRulesWarning("java_plugin"),
184-
"native-java-test": NativeJavaRulesWarning("java_test"),
185-
"native-java-package-config": NativeJavaToolchainRulesWarning("java_package_configuration"),
186-
"native-java-runtime": NativeJavaToolchainRulesWarning("java_runtime"),
187-
"native-java-toolchain": NativeJavaToolchainRulesWarning("java_toolchain"),
188-
"native-java-common": NativeJavaSymbolsWarning("java_common", "java_common"),
189-
"native-java-info": NativeJavaSymbolsWarning("JavaInfo", "java_info"),
190-
"native-java-plugin-info": NativeJavaSymbolsWarning("JavaPluginInfo", "java_plugin_info"),
191-
"native-proto": NativeProtoRulesWarning("proto_library"),
192-
"native-java-proto": NativeProtoRulesWarning("java_proto_library"),
193-
"native-java-lite-proto": NativeProtoRulesWarning("java_lite_proto_library"),
194-
"native-cc-proto": NativeProtoRulesWarning("cc_proto_library"),
195-
"native-proto-lang-toolchain": nativeProtoLangToolchainWarning,
196-
"native-proto-info": nativeProtoSymbolsWarning("ProtoInfo", "proto_info.bzl"),
197-
"native-proto-common": nativeProtoSymbolsWarning("proto_common", "proto_common.bzl"),
198-
"native-proto-lang-toolchain-info": nativeProtoSymbolsWarning("ProtoLangToolchainInfo", "proto_lang_toolchain_info.bzl"),
199-
"native-py": nativePyRulesWarning,
200-
"native-sh-binary": NativeShellRulesWarning("sh_binary"),
201-
"native-sh-library": NativeShellRulesWarning("sh_library"),
202-
"native-sh-test": NativeShellRulesWarning("sh_test"),
203-
"unnamed-macro": unnamedMacroWarning,
175+
"deprecated-function": deprecatedFunctionWarning,
176+
"git-repository": nativeGitRepositoryWarning,
177+
"http-archive": nativeHTTPArchiveWarning,
178+
"native-android": nativeAndroidRulesWarning,
179+
"native-cc-binary": NativeCcRulesWarning("cc_binary"),
180+
"native-cc-import": NativeCcRulesWarning("cc_import"),
181+
"native-cc-library": NativeCcRulesWarning("cc_library"),
182+
"native-cc-objc-import": NativeCcRulesWarning("objc_import"),
183+
"native-cc-objc-library": NativeCcRulesWarning("objc_library"),
184+
"native-cc-shared-library": NativeCcRulesWarning("cc_shared_library"),
185+
"native-cc-test": NativeCcRulesWarning("cc_test"),
186+
"native-cc-toolchain": NativeCcToolchainRulesWarning("cc_toolchain"),
187+
"native-cc-toolchain-suite": NativeCcToolchainRulesWarning("cc_toolchain_suite"),
188+
"native-cc-fdo-prefetch-hints": NativeCcToolchainRulesWarning("fdo_prefetch_hints"),
189+
"native-cc-fdo-profile": NativeCcToolchainRulesWarning("fdo_profile"),
190+
"native-cc-memprof-profile": NativeCcToolchainRulesWarning("memprof_profile"),
191+
"native-cc-propeller-optimize": NativeCcToolchainRulesWarning("propeller_optimize"),
192+
"native-cc-common": NativeCcSymbolsWarning("cc_common", "cc_common"),
193+
"native-cc-debug-package-info": NativeCcSymbolsWarning("DebugPackageInfo", "debug_package_info"),
194+
"native-cc-info": NativeCcSymbolsWarning("CcInfo", "cc_info"),
195+
"native-cc-shared-library-info": NativeCcSymbolsWarning("CcSharedLibraryInfo", "cc_shared_library_info"),
196+
"native-cc-shared-library-hint-info": NativeCcSymbolsWarning("CcSharedLibraryHintInfo", "cc_shared_library_hint_info"),
197+
"native-java-binary": NativeJavaRulesWarning("java_binary"),
198+
"native-java-import": NativeJavaRulesWarning("java_import"),
199+
"native-java-library": NativeJavaRulesWarning("java_library"),
200+
"native-java-plugin": NativeJavaRulesWarning("java_plugin"),
201+
"native-java-test": NativeJavaRulesWarning("java_test"),
202+
"native-java-package-config": NativeJavaToolchainRulesWarning("java_package_configuration"),
203+
"native-java-runtime": NativeJavaToolchainRulesWarning("java_runtime"),
204+
"native-java-toolchain": NativeJavaToolchainRulesWarning("java_toolchain"),
205+
"native-java-common": NativeJavaSymbolsWarning("java_common", "java_common"),
206+
"native-java-info": NativeJavaSymbolsWarning("JavaInfo", "java_info"),
207+
"native-java-plugin-info": NativeJavaSymbolsWarning("JavaPluginInfo", "java_plugin_info"),
208+
"native-proto": NativeProtoRulesWarning("proto_library"),
209+
"native-java-proto": NativeProtoRulesWarning("java_proto_library"),
210+
"native-java-lite-proto": NativeProtoRulesWarning("java_lite_proto_library"),
211+
"native-cc-proto": NativeProtoRulesWarning("cc_proto_library"),
212+
"native-proto-lang-toolchain": nativeProtoLangToolchainWarning,
213+
"native-proto-info": nativeProtoSymbolsWarning("ProtoInfo", "proto_info.bzl"),
214+
"native-proto-common": nativeProtoSymbolsWarning("proto_common", "proto_common.bzl"),
215+
"native-proto-lang-toolchain-info": nativeProtoSymbolsWarning("ProtoLangToolchainInfo", "proto_lang_toolchain_info.bzl"),
216+
"native-py": nativePyRulesWarning,
217+
"native-sh-binary": NativeShellRulesWarning("sh_binary"),
218+
"native-sh-library": NativeShellRulesWarning("sh_library"),
219+
"native-sh-test": NativeShellRulesWarning("sh_test"),
220+
"unnamed-macro": unnamedMacroWarning,
204221
}
205222

206223
// nonDefaultWarnings contains warnings that are enabled by default because they're not applicable

warn/warn_bazel_api.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,34 @@ func nativeAndroidRulesWarning(f *build.File, fileReader *FileReader) []*LinterF
728728
return NotLoadedFunctionUsageCheck(f, fileReader, tables.AndroidNativeRules, tables.AndroidLoadPath)
729729
}
730730

731-
func nativeCcRulesWarning(f *build.File, fileReader *FileReader) []*LinterFinding {
732-
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
733-
return nil
731+
// NativeCcRulesWarning produces a warning for missing loads of cc rules
732+
func NativeCcRulesWarning(rule string) func(f *build.File, fileReader *FileReader) []*LinterFinding {
733+
return func(f *build.File, fileReader *FileReader) []*LinterFinding {
734+
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
735+
return nil
736+
}
737+
return NotLoadedFunctionUsageCheck(f, fileReader, []string{rule}, tables.CcLoadPathPrefix+":"+rule+".bzl")
738+
}
739+
}
740+
741+
// NativeCcToolchainRulesWarning produces a warning for missing loads of cc toolchain rules
742+
func NativeCcToolchainRulesWarning(rule string) func(f *build.File, fileReader *FileReader) []*LinterFinding {
743+
return func(f *build.File, fileReader *FileReader) []*LinterFinding {
744+
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
745+
return nil
746+
}
747+
return NotLoadedFunctionUsageCheck(f, fileReader, []string{rule}, tables.CcLoadPathPrefix+"/toolchains:"+rule+".bzl")
748+
}
749+
}
750+
751+
// NativeCcSymbolsWarning produces a warning for missing loads of cc top-level symbols
752+
func NativeCcSymbolsWarning(symbol string, bzlfile string) func(f *build.File, fileReader *FileReader) []*LinterFinding {
753+
return func(f *build.File, fileReader *FileReader) []*LinterFinding {
754+
if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
755+
return nil
756+
}
757+
return NotLoadedSymbolUsageCheck(f, fileReader, []string{symbol}, tables.CcLoadPathPrefix+"/common:"+bzlfile+".bzl")
734758
}
735-
return NotLoadedFunctionUsageCheck(f, fileReader, tables.CcNativeRules, tables.CcLoadPath)
736759
}
737760

738761
// NativeJavaRulesWarning produces a warning for missing loads of java rules

0 commit comments

Comments
 (0)