diff options
author | Rupert Shuttleworth <ruperts@google.com> | 2021-04-06 20:06:21 +0000 |
---|---|---|
committer | Rupert Shuttleworth <ruperts@google.com> | 2021-04-07 16:05:44 +0000 |
commit | b815168474b9a906997690b37ab9b3c203613155 (patch) | |
tree | 4ff9508355716f66ffddeba7cbee4b6e1dc73726 /bazel | |
parent | 85822e9b5f872601da4a68c8a09c5e9484493e7f (diff) | |
download | soong-b815168474b9a906997690b37ab9b3c203613155.tar.gz |
Support arch variations for export_system_include_dirs in cc_library_headers bp2build converter.
Test: Added unit test
Test: bp2build-sync.py write; bazel build //bionic/... works for more cc_library_static targets (in a parent CL)
Change-Id: Ib487216a4bcbc52958ff948722dae347b0d8b606
Diffstat (limited to 'bazel')
-rw-r--r-- | bazel/properties.go | 63 | ||||
-rw-r--r-- | bazel/properties_test.go | 76 |
2 files changed, 139 insertions, 0 deletions
diff --git a/bazel/properties.go b/bazel/properties.go index 2440ca13f..148386f6a 100644 --- a/bazel/properties.go +++ b/bazel/properties.go @@ -79,6 +79,63 @@ func UniqueBazelLabelList(originalLabelList LabelList) LabelList { return uniqueLabelList } +// 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 + } + for _, s := range needle { + delete(remainder, s) + } + + var strings []string + for s, _ := range remainder { + 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) + } + + var labels []Label + for label, _ := range remainder { + labels = append(labels, label) + } + + sort.SliceStable(labels, func(i, j int) bool { + return labels[i].Label < labels[j].Label + }) + + return labels +} + +// Subtract needle from haystack +func SubtractBazelLabelList(haystack LabelList, needle LabelList) LabelList { + var result LabelList + result.Includes = SubtractBazelLabels(haystack.Includes, needle.Includes) + // NOTE: Excludes are intentionally not subtracted + result.Excludes = haystack.Excludes + return result +} + const ( // ArchType names in arch.go ARCH_ARM = "arm" @@ -257,6 +314,12 @@ type StringListAttribute struct { OsValues stringListOsValues } +// MakeStringListAttribute initializes a StringListAttribute with the non-arch specific value. +func MakeStringListAttribute(value []string) StringListAttribute { + // NOTE: These strings are not necessarily unique or sorted. + return StringListAttribute{Value: value} +} + // Arch-specific string_list typed Bazel attribute values. This should correspond // to the types of architectures supported for compilation in arch.go. type stringListArchValues struct { diff --git a/bazel/properties_test.go b/bazel/properties_test.go index 0fcb90427..56840efbe 100644 --- a/bazel/properties_test.go +++ b/bazel/properties_test.go @@ -46,6 +46,82 @@ func TestUniqueBazelLabels(t *testing.T) { } } +func TestSubtractStrings(t *testing.T) { + testCases := []struct { + haystack []string + needle []string + expectedResult []string + }{ + { + haystack: []string{ + "a", + "b", + "c", + }, + needle: []string{ + "a", + }, + expectedResult: []string{ + "b", "c", + }, + }, + } + for _, tc := range testCases { + actualResult := SubtractStrings(tc.haystack, tc.needle) + if !reflect.DeepEqual(tc.expectedResult, actualResult) { + t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult) + } + } +} + +func TestSubtractBazelLabelList(t *testing.T) { + testCases := []struct { + haystack LabelList + needle LabelList + expectedResult LabelList + }{ + { + haystack: LabelList{ + Includes: []Label{ + {Label: "a"}, + {Label: "b"}, + {Label: "c"}, + }, + Excludes: []Label{ + {Label: "x"}, + {Label: "y"}, + {Label: "z"}, + }, + }, + needle: LabelList{ + Includes: []Label{ + {Label: "a"}, + }, + Excludes: []Label{ + {Label: "z"}, + }, + }, + // NOTE: Excludes are intentionally not subtracted + expectedResult: LabelList{ + Includes: []Label{ + {Label: "b"}, + {Label: "c"}, + }, + Excludes: []Label{ + {Label: "x"}, + {Label: "y"}, + {Label: "z"}, + }, + }, + }, + } + for _, tc := range testCases { + actualResult := SubtractBazelLabelList(tc.haystack, tc.needle) + if !reflect.DeepEqual(tc.expectedResult, actualResult) { + t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult) + } + } +} func TestUniqueBazelLabelList(t *testing.T) { testCases := []struct { originalLabelList LabelList |