Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 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 aidl |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | // wrap(p, a, s) = [p + v + s for v in a] |
| 23 | func wrap(prefix string, strs []string, suffix string) []string { |
| 24 | ret := make([]string, len(strs)) |
| 25 | for i, v := range strs { |
| 26 | ret[i] = prefix + v + suffix |
| 27 | } |
| 28 | return ret |
| 29 | } |
| 30 | |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame^] | 31 | // wrapFunc(p, a, s, f) = [p + f(v) + s for v in a] |
| 32 | func wrapFunc(prefix string, strs []string, suffix string, f func(string) string) []string { |
| 33 | ret := make([]string, len(strs)) |
| 34 | for i, v := range strs { |
| 35 | ret[i] = prefix + f(v) + suffix |
| 36 | } |
| 37 | return ret |
| 38 | } |
| 39 | |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 40 | // concat(a...) = sum((i for i in a), []) |
| 41 | func concat(sstrs ...[]string) []string { |
| 42 | var ret []string |
| 43 | for _, v := range sstrs { |
| 44 | ret = append(ret, v...) |
| 45 | } |
| 46 | return ret |
| 47 | } |
| 48 | |
| 49 | // baseDir is the directory where the package name starts. e.g. For an AIDL fil |
| 50 | // mymodule/aidl_src/com/android/IFoo.aidl, baseDir is mymodule/aidl_src given that the package name is |
| 51 | // com.android. The build system however don't know the package name without actually reading the AIDL file. |
| 52 | // Therefore, we rely on the user to correctly set the base directory via following two methods: |
| 53 | // 1) via the 'path' property of filegroup or |
| 54 | // 2) via `local_include_dir' of the aidl_interface module. |
| 55 | func getBaseDir(ctx android.ModuleContext, src android.Path, aidlRoot android.Path) string { |
| 56 | // By default, we try to get 1) by reading Rel() of the input path. |
| 57 | baseDir := strings.TrimSuffix(src.String(), src.Rel()) |
| 58 | // However, if 2) is set and it's more specific (i.e. deeper) than 1), we use 2). |
| 59 | if strings.HasPrefix(aidlRoot.String(), baseDir) { |
| 60 | baseDir = aidlRoot.String() |
| 61 | } |
| 62 | return baseDir |
| 63 | } |
| 64 | |
| 65 | func fixRustName(name string) string { |
| 66 | return strings.Map(func(r rune) rune { |
| 67 | switch r { |
| 68 | case '-', '.': |
| 69 | return '_' |
| 70 | default: |
| 71 | return r |
| 72 | } |
| 73 | }, name) |
| 74 | } |