xref: /aosp_15_r20/build/soong/cc/cmake_snapshot_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// 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 (
18	"runtime"
19	"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()
27	ruleName := "<nil>"
28	if rule := m.MaybeOutput(fileName).Rule; rule != nil {
29		ruleName = rule.String()
30	}
31	if !strings.HasSuffix(ruleName, ruleType) {
32		t.Errorf("Main Cmake file wasn't generated properly, expected rule %v, found %v", ruleType, ruleName)
33	}
34}
35
36func TestEmptyCmakeSnapshot(t *testing.T) {
37	t.Parallel()
38	result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
39		cc_cmake_snapshot {
40			name: "foo",
41			modules_host: [],
42			modules_system: [],
43			modules_vendor: [],
44			prebuilts: ["libc++"],
45			include_sources: true,
46		}`)
47
48	if runtime.GOOS != "linux" {
49		t.Skip("CMake snapshots are only supported on Linux")
50	}
51
52	snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
53
54	wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy")
55	wasGenerated(t, &snapshotModule, "foo.zip", "")
56}
57
58func TestCmakeSnapshotWithBinary(t *testing.T) {
59	t.Parallel()
60	xtra := android.FixtureAddTextFile("some/module/Android.bp", `
61		cc_binary {
62			name: "foo_binary",
63			host_supported: true,
64			cmake_snapshot_supported: true,
65		}
66	`)
67	result := android.GroupFixturePreparers(PrepareForIntegrationTestWithCc, xtra).RunTestWithBp(t, `
68		cc_cmake_snapshot {
69			name: "foo",
70			modules_system: [
71				"foo_binary",
72			],
73			include_sources: true,
74		}`)
75
76	if runtime.GOOS != "linux" {
77		t.Skip("CMake snapshots are only supported on Linux")
78	}
79
80	snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
81
82	wasGenerated(t, &snapshotModule, "some/module/CMakeLists.txt", "rawFileCopy")
83}
84
85func TestCmakeSnapshotAsTestData(t *testing.T) {
86	t.Parallel()
87	result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
88		cc_test {
89			name: "foo_test",
90			gtest: false,
91			srcs: [
92				"foo_test.c",
93			],
94			data: [
95				":foo",
96			],
97			target: {
98				android: {enabled: false},
99			},
100		}
101
102		cc_cmake_snapshot {
103			name: "foo",
104			modules_system: [],
105			prebuilts: ["libc++"],
106			include_sources: true,
107		}`)
108
109	if runtime.GOOS != "linux" {
110		t.Skip("CMake snapshots are only supported on Linux")
111	}
112
113	snapshotModule := result.ModuleForTests("foo", "linux_glibc_x86_64")
114
115	wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy")
116	wasGenerated(t, &snapshotModule, "foo.zip", "")
117}
118