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 | |
| 31 | // concat(a...) = sum((i for i in a), []) |
| 32 | func concat(sstrs ...[]string) []string { |
| 33 | var ret []string |
| 34 | for _, v := range sstrs { |
| 35 | ret = append(ret, v...) |
| 36 | } |
| 37 | return ret |
| 38 | } |
| 39 | |
| 40 | // baseDir is the directory where the package name starts. e.g. For an AIDL fil |
| 41 | // mymodule/aidl_src/com/android/IFoo.aidl, baseDir is mymodule/aidl_src given that the package name is |
| 42 | // com.android. The build system however don't know the package name without actually reading the AIDL file. |
| 43 | // Therefore, we rely on the user to correctly set the base directory via following two methods: |
| 44 | // 1) via the 'path' property of filegroup or |
| 45 | // 2) via `local_include_dir' of the aidl_interface module. |
| 46 | func getBaseDir(ctx android.ModuleContext, src android.Path, aidlRoot android.Path) string { |
| 47 | // By default, we try to get 1) by reading Rel() of the input path. |
| 48 | baseDir := strings.TrimSuffix(src.String(), src.Rel()) |
| 49 | // However, if 2) is set and it's more specific (i.e. deeper) than 1), we use 2). |
| 50 | if strings.HasPrefix(aidlRoot.String(), baseDir) { |
| 51 | baseDir = aidlRoot.String() |
| 52 | } |
| 53 | return baseDir |
| 54 | } |
| 55 | |
| 56 | func fixRustName(name string) string { |
| 57 | return strings.Map(func(r rune) rune { |
| 58 | switch r { |
| 59 | case '-', '.': |
| 60 | return '_' |
| 61 | default: |
| 62 | return r |
| 63 | } |
| 64 | }, name) |
| 65 | } |