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
15package aidl
16
17import (
18	"android/soong/android"
19	"strings"
20)
21
22// wrap(p, a, s) = [p + v + s for v in a]
23func 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// wrapFunc(p, a, s, f) = [p + f(v) + s for v in a]
32func 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
40// concat(a...) = sum((i for i in a), [])
41func 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.
55func 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
65func 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}
75