1// Copyright (C) 2017 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 hidl
16
17import (
18	"errors"
19	"path/filepath"
20	"regexp"
21	"strings"
22)
23
24type fqName struct {
25	packageComponents []string
26	minor             string
27	major             string
28}
29
30// See system/tools/hidl/FQName.h
31var component = "[a-zA-Z_][a-zA-Z_0-9]*"
32var pkg = "(" + component + "(?:[.]" + component + ")*)"
33var digits = "([0-9]+)"
34var re_package = regexp.MustCompile("^" + pkg + "@" + digits + "." + digits + "$")
35
36func parseFqName(f string) (*fqName, error) {
37	matches := re_package.FindAllStringSubmatch(f, 3)
38
39	if matches == nil {
40		return nil, errors.New("Poorly formed hal interface name: '" + f + "' must match '" + re_package.String() + "'")
41	}
42
43	ret := fqName{
44		packageComponents: strings.Split(matches[0][1], "."),
45		major:             matches[0][2],
46		minor:             matches[0][3],
47	}
48
49	return &ret, nil
50}
51
52func (f *fqName) inPackage(pkg string) bool {
53	components := strings.Split(pkg, ".")
54
55	if len(components) > len(f.packageComponents) {
56		return false
57	}
58
59	for i, v := range components {
60		if f.packageComponents[i] != v {
61			return false
62		}
63	}
64
65	return true
66}
67
68func (f *fqName) pkg() string {
69	return strings.Join(f.packageComponents, ".")
70}
71func (f *fqName) version() string {
72	return f.major + "." + f.minor
73}
74func (f *fqName) sanitizedVersion() string {
75	return "V" + f.major + "_" + f.minor
76}
77func (f *fqName) string() string {
78	return f.pkg() + "@" + f.version()
79}
80func (f *fqName) sanitizedString() string {
81	return f.pkg() + "-V" + f.version() // NOT sanitizedVersion
82}
83
84func (f *fqName) dir() string {
85	return filepath.Join(filepath.Join(f.packageComponents...), f.version()) + "/"
86}
87func (f *fqName) sanitizedDir() string {
88	return filepath.Join(filepath.Join(f.packageComponents...), f.sanitizedVersion()) + "/"
89}
90
91func (f *fqName) fileGroupName() string {
92	return f.string() + "_hal"
93}
94func (f *fqName) sourcesName() string {
95	return f.string() + "_genc++"
96}
97func (f *fqName) headersName() string {
98	return f.string() + "_genc++_headers"
99}
100func (f *fqName) javaName() string {
101	return f.sanitizedString() + "-java"
102}
103func (f *fqName) javaSharedName() string {
104	return f.sanitizedString() + "-java-shallow"
105}
106func (f *fqName) javaSourcesName() string {
107	return f.sanitizedString() + "-java_gen_java"
108}
109func (f *fqName) javaConstantsName() string {
110	return f.sanitizedString() + "-java-constants"
111}
112func (f *fqName) javaConstantsSourcesName() string {
113	return f.sanitizedString() + "-java-constants_gen_java"
114}
115func (f *fqName) adapterName() string {
116	return f.string() + "-adapter"
117}
118func (f *fqName) adapterSourcesName() string {
119	return f.string() + "-adapter_genc++"
120}
121func (f *fqName) adapterHelperName() string {
122	return f.string() + "-adapter-helper"
123}
124func (f *fqName) adapterHelperSourcesName() string {
125	return f.string() + "-adapter-helper_genc++"
126}
127func (f *fqName) adapterHelperHeadersName() string {
128	return f.string() + "-adapter-helper_genc++_headers"
129}
130
131func (f *fqName) vtsSpecName() string {
132	return f.string() + "-vts.spec"
133}
134func (f *fqName) vtsDriverSourcesName() string {
135	return f.string() + "-vts.driver_genc++"
136}
137func (f *fqName) vtsDriverHeadersName() string {
138	return f.string() + "-vts.driver_genc++_headers"
139}
140func (f *fqName) vtsDriverName() string {
141	return f.string() + "-vts.driver"
142}
143func (f *fqName) vtsProfilerSourcesName() string {
144	return f.string() + "-vts.profiler_genc++"
145}
146func (f *fqName) vtsProfilerHeadersName() string {
147	return f.string() + "-vts.profiler_genc++_headers"
148}
149func (f *fqName) vtsProfilerName() string {
150	return f.string() + "-vts.profiler"
151}
152func (f *fqName) vtsFuzzerName() string {
153	return f.string() + "-vts.fuzzer"
154}
155
156func (f *fqName) lintName() string {
157	return f.string() + "-hidl-lint"
158}
159
160func (f *fqName) inheritanceHierarchyName() string {
161	return f.string() + "-inheritance-hierarchy"
162}
163