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", |
| 41 | modules: [], |
| 42 | prebuilts: ["libc++"], |
| 43 | include_sources: true, |
| 44 | }`) |
| 45 | |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 46 | 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 Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 51 | |
| 52 | wasGenerated(t, &snapshotModule, "CMakeLists.txt", "rawFileCopy") |
| 53 | wasGenerated(t, &snapshotModule, "foo.zip", "") |
| 54 | } |
| 55 | |
| 56 | func TestCmakeSnapshotWithBinary(t *testing.T) { |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 57 | 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 Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 74 | 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 Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 79 | |
| 80 | wasGenerated(t, &snapshotModule, "some/module/CMakeLists.txt", "rawFileCopy") |
| 81 | } |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 82 | |
| 83 | func 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 | } |