Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 18 | "runtime" |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | func wasGenerated(t *testing.T, m *android.TestingModule, fileName string, ruleType string) { |
| 26 | t.Helper() |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 27 | ruleName := "<nil>" |
| 28 | if rule := m.MaybeOutput(fileName).Rule; rule != nil { |
| 29 | ruleName = rule.String() |
| 30 | } |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 31 | if !strings.HasSuffix(ruleName, ruleType) { |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 32 | t.Errorf("Main Cmake file wasn't generated properly, expected rule %v, found %v", ruleType, ruleName) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestEmptyCmakeSnapshot(t *testing.T) { |
| 37 | t.Parallel() |
| 38 | result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, ` |
| 39 | cc_cmake_snapshot { |
| 40 | name: "foo", |
Tomasz Wasilczyk | bd83c74 | 2024-06-27 10:47:40 -0700 | [diff] [blame] | 41 | modules_host: [], |
| 42 | modules_system: [], |
| 43 | modules_vendor: [], |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 44 | prebuilts: ["libc++"], |
| 45 | include_sources: true, |
| 46 | }`) |
| 47 | |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 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") |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 53 | |
| 54 | wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy") |
| 55 | wasGenerated(t, &snapshotModule, "foo.zip", "") |
| 56 | } |
| 57 | |
| 58 | func TestCmakeSnapshotWithBinary(t *testing.T) { |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 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", |
Tomasz Wasilczyk | bd83c74 | 2024-06-27 10:47:40 -0700 | [diff] [blame] | 70 | modules_system: [ |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 71 | "foo_binary", |
| 72 | ], |
| 73 | include_sources: true, |
| 74 | }`) |
| 75 | |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 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") |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 81 | |
| 82 | wasGenerated(t, &snapshotModule, "some/module/CMakeLists.txt", "rawFileCopy") |
| 83 | } |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 84 | |
| 85 | func 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 Wasilczyk | bd83c74 | 2024-06-27 10:47:40 -0700 | [diff] [blame] | 104 | modules_system: [], |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 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 | } |