From 9bad9d645eddc87b213d9b6addc219d87f3dd5c6 Mon Sep 17 00:00:00 2001 From: Liz Kammer Date: Mon, 11 Oct 2021 15:40:35 -0400 Subject: [PATCH] Do not sort after subtraction. This allows labels/strings to remain in their original order. Test: go test bazel tests Change-Id: I69f575df9e4a358fee4392ae48edf4550e463efb --- bazel/properties.go | 38 +++++--------- bp2build/cc_library_conversion_test.go | 8 +-- bp2build/cc_library_static_conversion_test.go | 52 +++++++++---------- bp2build/cc_object_conversion_test.go | 4 +- 4 files changed, 45 insertions(+), 57 deletions(-) diff --git a/bazel/properties.go b/bazel/properties.go index ee32e73ac..6a06c1bab 100644 --- a/bazel/properties.go +++ b/bazel/properties.go @@ -164,48 +164,36 @@ func UniqueSortedBazelLabelList(originalLabelList LabelList) LabelList { // Subtract needle from haystack func SubtractStrings(haystack []string, needle []string) []string { // This is really a set - remainder := make(map[string]bool) - - for _, s := range haystack { - remainder[s] = true - } + needleMap := make(map[string]bool) for _, s := range needle { - delete(remainder, s) + needleMap[s] = true } var strings []string - for s, _ := range remainder { - strings = append(strings, s) + for _, s := range haystack { + if exclude := needleMap[s]; !exclude { + strings = append(strings, s) + } } - sort.SliceStable(strings, func(i, j int) bool { - return strings[i] < strings[j] - }) - return strings } // Subtract needle from haystack func SubtractBazelLabels(haystack []Label, needle []Label) []Label { // This is really a set - remainder := make(map[Label]bool) - - for _, label := range haystack { - remainder[label] = true - } - for _, label := range needle { - delete(remainder, label) + needleMap := make(map[Label]bool) + for _, s := range needle { + needleMap[s] = true } var labels []Label - for label, _ := range remainder { - labels = append(labels, label) + for _, label := range haystack { + if exclude := needleMap[label]; !exclude { + labels = append(labels, label) + } } - sort.SliceStable(labels, func(i, j int) bool { - return labels[i].Label < labels[j].Label - }) - return labels } diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index 76f83e706..236dec465 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -1669,21 +1669,21 @@ cc_library { srcs = ["base.cpp"] + select({ "//build/bazel/platforms/os:android": [ "android.cpp", - "bionic.cpp", "linux.cpp", + "bionic.cpp", ], "//build/bazel/platforms/os:darwin": ["darwin.cpp"], "//build/bazel/platforms/os:linux": [ - "linux.cpp", "linux_glibc.cpp", + "linux.cpp", ], "//build/bazel/platforms/os:linux_bionic": [ - "bionic.cpp", "linux.cpp", + "bionic.cpp", ], "//build/bazel/platforms/os:linux_musl": [ - "linux.cpp", "linux_musl.cpp", + "linux.cpp", ], "//build/bazel/platforms/os:windows": ["windows.cpp"], "//conditions:default": [], diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index 3289391e5..9f6f45047 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -641,12 +641,12 @@ cc_library_static { name = "foo_static", srcs_c = ["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ - "for-arm.c", "not-for-x86.c", + "for-arm.c", ], "//build/bazel/platforms/arch:x86": [ - "for-x86.c", "not-for-arm.c", + "for-x86.c", ], "//conditions:default": [ "not-for-arm.c", @@ -691,28 +691,28 @@ cc_library_static { name = "foo_static", srcs_c = ["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ - "for-arm.c", "not-for-arm64.c", "not-for-x86.c", "not-for-x86_64.c", + "for-arm.c", ], "//build/bazel/platforms/arch:arm64": [ - "for-arm64.c", "not-for-arm.c", "not-for-x86.c", "not-for-x86_64.c", + "for-arm64.c", ], "//build/bazel/platforms/arch:x86": [ - "for-x86.c", "not-for-arm.c", "not-for-arm64.c", "not-for-x86_64.c", + "for-x86.c", ], "//build/bazel/platforms/arch:x86_64": [ - "for-x86_64.c", "not-for-arm.c", "not-for-arm64.c", "not-for-x86.c", + "for-x86_64.c", ], "//conditions:default": [ "not-for-arm.c", @@ -875,20 +875,20 @@ cc_library_static { name = "foo_static2", srcs_c = ["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ - "for-lib32.c", "not-for-lib64.c", + "for-lib32.c", ], "//build/bazel/platforms/arch:arm64": [ - "for-lib64.c", "not-for-lib32.c", + "for-lib64.c", ], "//build/bazel/platforms/arch:x86": [ - "for-lib32.c", "not-for-lib64.c", + "for-lib32.c", ], "//build/bazel/platforms/arch:x86_64": [ - "for-lib64.c", "not-for-lib32.c", + "for-lib64.c", ], "//conditions:default": [ "not-for-lib32.c", @@ -942,36 +942,36 @@ cc_library_static { name = "foo_static3", srcs_c = ["common.c"] + select({ "//build/bazel/platforms/arch:arm": [ + "not-for-arm64.c", + "not-for-lib64.c", + "not-for-x86.c", + "not-for-x86_64.c", "for-arm.c", "for-lib32.c", - "not-for-arm64.c", - "not-for-lib64.c", - "not-for-x86.c", - "not-for-x86_64.c", ], "//build/bazel/platforms/arch:arm64": [ - "for-arm64.c", - "for-lib64.c", "not-for-arm.c", "not-for-lib32.c", "not-for-x86.c", "not-for-x86_64.c", + "for-arm64.c", + "for-lib64.c", ], "//build/bazel/platforms/arch:x86": [ - "for-lib32.c", - "for-x86.c", "not-for-arm.c", "not-for-arm64.c", "not-for-lib64.c", "not-for-x86_64.c", + "for-x86.c", + "for-lib32.c", ], "//build/bazel/platforms/arch:x86_64": [ - "for-lib64.c", - "for-x86_64.c", "not-for-arm.c", "not-for-arm64.c", "not-for-lib32.c", "not-for-x86.c", + "for-x86_64.c", + "for-lib64.c", ], "//conditions:default": [ "not-for-arm.c", @@ -1066,19 +1066,19 @@ cc_library_static { expectedBazelTargets: []string{`cc_library_static( name = "foo_static3", srcs = [ - "//dep:generated_hdr_other_pkg", - "//dep:generated_src_other_pkg", - ":generated_hdr", - ":generated_src", "common.cpp", + ":generated_hdr", + "//dep:generated_hdr_other_pkg", + ":generated_src", + "//dep:generated_src_other_pkg", ] + select({ "//build/bazel/platforms/arch:x86": [ - "//dep:generated_hdr_other_pkg_x86", "for-x86.cpp", + "//dep:generated_hdr_other_pkg_x86", ], "//conditions:default": [ - ":generated_src_not_x86", "not-for-x86.cpp", + ":generated_src_not_x86", ], }) + select({ "//build/bazel/platforms/os:android": [ diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go index 63a0b8a21..c4b276a64 100644 --- a/bp2build/cc_object_conversion_test.go +++ b/bp2build/cc_object_conversion_test.go @@ -439,13 +439,13 @@ func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) { copts = ["-fno-addrsig"], srcs = ["base.cpp"] + select({ "//build/bazel/platforms/os_arch:android_arm64": [ - "bionic_arm64.cpp", "linux_arm64.cpp", + "bionic_arm64.cpp", ], "//build/bazel/platforms/os_arch:android_x86": ["linux_x86.cpp"], "//build/bazel/platforms/os_arch:linux_bionic_arm64": [ - "bionic_arm64.cpp", "linux_arm64.cpp", + "bionic_arm64.cpp", ], "//build/bazel/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"], "//build/bazel/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"],