blob: 8fca6c18fa812164d0efa0ed1966768fadf1a357 [file] [log] [blame]
Hao Chen1c8ea5b2023-10-20 23:03:45 +00001// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070018 "runtime"
Hao Chen1c8ea5b2023-10-20 23:03:45 +000019 "strings"
20 "testing"
21
22 "android/soong/android"
23)
24
25func wasGenerated(t *testing.T, m *android.TestingModule, fileName string, ruleType string) {
26 t.Helper()
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070027 ruleName := "<nil>"
28 if rule := m.MaybeOutput(fileName).Rule; rule != nil {
29 ruleName = rule.String()
30 }
Hao Chen1c8ea5b2023-10-20 23:03:45 +000031 if !strings.HasSuffix(ruleName, ruleType) {
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070032 t.Errorf("Main Cmake file wasn't generated properly, expected rule %v, found %v", ruleType, ruleName)
Hao Chen1c8ea5b2023-10-20 23:03:45 +000033 }
34}
35
36func TestEmptyCmakeSnapshot(t *testing.T) {
37 t.Parallel()
38 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
39 cc_cmake_snapshot {
40 name: "foo",
41 modules: [],
42 prebuilts: ["libc++"],
43 include_sources: true,
44 }`)
45
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070046 if runtime.GOOS != "linux" {
47 t.Skip("CMake snapshots are only supported on Linux")
48 }
49
50 snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
Hao Chen1c8ea5b2023-10-20 23:03:45 +000051
52 wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy")
53 wasGenerated(t, &snapshotModule, "foo.zip", "")
54}
55
56func TestCmakeSnapshotWithBinary(t *testing.T) {
Hao Chen1c8ea5b2023-10-20 23:03:45 +000057 t.Parallel()
58 xtra := android.FixtureAddTextFile("some/module/Android.bp", `
59 cc_binary {
60 name: "foo_binary",
61 host_supported: true,
62 cmake_snapshot_supported: true,
63 }
64 `)
65 result := android.GroupFixturePreparers(PrepareForIntegrationTestWithCc, xtra).RunTestWithBp(t, `
66 cc_cmake_snapshot {
67 name: "foo",
68 modules: [
69 "foo_binary",
70 ],
71 include_sources: true,
72 }`)
73
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070074 if runtime.GOOS != "linux" {
75 t.Skip("CMake snapshots are only supported on Linux")
76 }
77
78 snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
Hao Chen1c8ea5b2023-10-20 23:03:45 +000079
80 wasGenerated(t, &snapshotModule, "some/module/CMakeLists.txt", "rawFileCopy")
81}
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070082
83func TestCmakeSnapshotAsTestData(t *testing.T) {
84 t.Parallel()
85 result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
86 cc_test {
87 name: "foo_test",
88 gtest: false,
89 srcs: [
90 "foo_test.c",
91 ],
92 data: [
93 ":foo",
94 ],
95 target: {
96 android: {enabled: false},
97 },
98 }
99
100 cc_cmake_snapshot {
101 name: "foo",
102 modules: [],
103 prebuilts: ["libc++"],
104 include_sources: true,
105 }`)
106
107 if runtime.GOOS != "linux" {
108 t.Skip("CMake snapshots are only supported on Linux")
109 }
110
111 snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
112
113 wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy")
114 wasGenerated(t, &snapshotModule, "foo.zip", "")
115}