blob: b6f4369b04bc070e7975b265287dd6bb2c1188b1 [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",
Tomasz Wasilczykbd83c742024-06-27 10:47:40 -070041 modules_host: [],
42 modules_system: [],
43 modules_vendor: [],
Hao Chen1c8ea5b2023-10-20 23:03:45 +000044 prebuilts: ["libc++"],
45 include_sources: true,
46 }`)
47
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070048 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")
Hao Chen1c8ea5b2023-10-20 23:03:45 +000053
54 wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy")
55 wasGenerated(t, &snapshotModule, "foo.zip", "")
56}
57
58func TestCmakeSnapshotWithBinary(t *testing.T) {
Hao Chen1c8ea5b2023-10-20 23:03:45 +000059 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",
Tomasz Wasilczykbd83c742024-06-27 10:47:40 -070070 modules_system: [
Hao Chen1c8ea5b2023-10-20 23:03:45 +000071 "foo_binary",
72 ],
73 include_sources: true,
74 }`)
75
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070076 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")
Hao Chen1c8ea5b2023-10-20 23:03:45 +000081
82 wasGenerated(t, &snapshotModule, "some/module/CMakeLists.txt", "rawFileCopy")
83}
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -070084
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",
Tomasz Wasilczykbd83c742024-06-27 10:47:40 -0700104 modules_system: [],
Tomasz Wasilczykd848dcc2024-05-10 09:16:37 -0700105 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}