blob: cb17feedd59b16b5cd0a4fcdd17a896f74ed5a63 [file] [log] [blame]
Jiyong Park58c518b2018-05-12 22:29:12 +09001// Copyright 2018 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
15package java
16
17import (
18 "android/soong/android"
19 "sort"
Jiyong Park58c518b2018-05-12 22:29:12 +090020 "strings"
21
22 "github.com/google/blueprint/proptools"
23)
24
Jiyong Park58c518b2018-05-12 22:29:12 +090025func init() {
Paul Duffina48f7582019-12-19 11:25:19 +000026 RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext)
27}
Jiyong Park58c518b2018-05-12 22:29:12 +090028
Paul Duffina48f7582019-12-19 11:25:19 +000029func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) {
30 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
31
32 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090033 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Park58c518b2018-05-12 22:29:12 +090034 })
35}
36
Sundong Ahn27eecb92018-06-21 13:47:17 +090037type prebuiltApisProperties struct {
38 // list of api version directories
39 Api_dirs []string
40}
41
Jiyong Park58c518b2018-05-12 22:29:12 +090042type prebuiltApis struct {
43 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090044 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090045}
46
Jiyong Park58c518b2018-05-12 22:29:12 +090047func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
48 // no need to implement
49}
50
Sundong Ahna01c2a52018-06-07 21:42:16 +090051func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
52 elements := strings.Split(path, "/")
53
54 apiver = elements[0]
55 scope = elements[1]
Sundong Ahna01c2a52018-06-07 21:42:16 +090056
57 module = strings.TrimSuffix(elements[2], ".jar")
58 return
59}
60
Sundong Ahn054b19a2018-10-19 13:46:09 +090061func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090062 elements := strings.Split(path, "/")
Sundong Ahn054b19a2018-10-19 13:46:09 +090063 apiver = elements[0]
Jiyong Park58c518b2018-05-12 22:29:12 +090064
65 scope = elements[1]
66 if scope != "public" && scope != "system" && scope != "test" {
67 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
68 return
69 }
70
71 // elements[2] is string literal "api". skipping.
72 module = strings.TrimSuffix(elements[3], ".txt")
73 return
74}
75
Sundong Ahna01c2a52018-06-07 21:42:16 +090076func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
77 props := struct {
78 Name *string
79 Jars []string
80 Sdk_version *string
81 Installable *bool
82 }{}
Sundong Ahn27eecb92018-06-21 13:47:17 +090083 props.Name = proptools.StringPtr(mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module)
Sundong Ahna01c2a52018-06-07 21:42:16 +090084 props.Jars = append(props.Jars, path)
85 // TODO(hansson): change to scope after migration is done.
86 props.Sdk_version = proptools.StringPtr("current")
87 props.Installable = proptools.BoolPtr(false)
88
Colin Cross84dfc3d2019-09-25 11:33:01 -070089 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +090090}
91
Jiyong Park58c518b2018-05-12 22:29:12 +090092func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
93 fgName := module + ".api." + scope + "." + apiver
94 filegroupProps := struct {
95 Name *string
96 Srcs []string
97 }{}
98 filegroupProps.Name = proptools.StringPtr(fgName)
99 filegroupProps.Srcs = []string{path}
Colin Cross84dfc3d2019-09-25 11:33:01 -0700100 mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900101}
102
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900103func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900104 mydir := mctx.ModuleDir() + "/"
Sundong Ahn27eecb92018-06-21 13:47:17 +0900105 var files []string
106 for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
107 for _, scope := range []string{"public", "system", "test", "core"} {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900108 vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900109 if err != nil {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900110 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900111 }
112 files = append(files, vfiles...)
113 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900114 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900115 return files
116}
117
118func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
119 mydir := mctx.ModuleDir() + "/"
120 // <apiver>/<scope>/<module>.jar
121 files := getPrebuiltFiles(mctx, "*.jar")
Sundong Ahna01c2a52018-06-07 21:42:16 +0900122
123 for _, f := range files {
124 // create a Import module for each jar file
125 localPath := strings.TrimPrefix(f, mydir)
126 module, apiver, scope := parseJarPath(mctx, localPath)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900127 createImport(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900128 }
129}
130
131func prebuiltApiFiles(mctx android.TopDownMutatorContext) {
132 mydir := mctx.ModuleDir() + "/"
133 // <apiver>/<scope>/api/<module>.txt
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900134 files := getPrebuiltFiles(mctx, "api/*.txt")
135
Sundong Ahna01c2a52018-06-07 21:42:16 +0900136 if len(files) == 0 {
137 mctx.ModuleErrorf("no api file found under %q", mydir)
138 }
139
140 // construct a map to find out the latest api file path
141 // for each (<module>, <scope>) pair.
142 type latestApiInfo struct {
143 module string
144 scope string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900145 apiver string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900146 path string
147 }
148 m := make(map[string]latestApiInfo)
149
150 for _, f := range files {
151 // create a filegroup for each api txt file
152 localPath := strings.TrimPrefix(f, mydir)
153 module, apiver, scope := parseApiFilePath(mctx, localPath)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900154 createFilegroup(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900155
156 // find the latest apiver
157 key := module + "." + scope
158 info, ok := m[key]
159 if !ok {
160 m[key] = latestApiInfo{module, scope, apiver, localPath}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900161 } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) &&
162 strings.Compare(apiver, info.apiver) > 0) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900163 info.apiver = apiver
164 info.path = localPath
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900165 m[key] = info
Sundong Ahna01c2a52018-06-07 21:42:16 +0900166 }
167 }
168 // create filegroups for the latest version of (<module>, <scope>) pairs
169 // sort the keys in order to make build.ninja stable
170 keys := make([]string, 0, len(m))
171 for k := range m {
172 keys = append(keys, k)
173 }
174 sort.Strings(keys)
175 for _, k := range keys {
176 info := m[k]
177 createFilegroup(mctx, info.module, info.scope, "latest", info.path)
178 }
179}
180
Inseob Kimc0907f12019-02-08 21:00:45 +0900181func PrebuiltApisMutator(mctx android.TopDownMutatorContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +0900182 if _, ok := mctx.Module().(*prebuiltApis); ok {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900183 prebuiltApiFiles(mctx)
184 prebuiltSdkStubs(mctx)
Jiyong Park58c518b2018-05-12 22:29:12 +0900185 }
186}
187
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700188// prebuilt_apis is a meta-module that generates filegroup modules for all
189// API txt files found under the directory where the Android.bp is located.
190// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
191// generates a filegroup module named <module>-api.<scope>.<ver>.
192//
193// It also creates <module>-api.<scope>.latest for the latest <ver>.
Inseob Kimc0907f12019-02-08 21:00:45 +0900194func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900195 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900196 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900197 android.InitAndroidModule(module)
198 return module
199}