blob: 1f18b4a12a2471354300ad45deaf50d876c62d69 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 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 rust
16
17import (
Ivan Lozanoffee3342019-08-27 12:03:00 -070018 "path/filepath"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019
20 "android/soong/android"
Ivan Lozano9da4aa82021-01-29 12:48:05 -050021 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24type AndroidMkContext interface {
25 Name() string
26 Target() android.Target
Liz Kammer4018a8d2020-11-24 09:35:21 -080027 SubAndroidMk(*android.AndroidMkEntries, interface{})
Ivan Lozanoffee3342019-08-27 12:03:00 -070028}
29
Andrei Homescuc7767922020-08-05 06:36:19 -070030type SubAndroidMkProvider interface {
Liz Kammer4018a8d2020-11-24 09:35:21 -080031 AndroidMk(AndroidMkContext, *android.AndroidMkEntries)
Ivan Lozanoffee3342019-08-27 12:03:00 -070032}
33
Liz Kammer4018a8d2020-11-24 09:35:21 -080034func (mod *Module) SubAndroidMk(data *android.AndroidMkEntries, obj interface{}) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070035 if mod.subAndroidMkOnce == nil {
Andrei Homescuc7767922020-08-05 06:36:19 -070036 mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool)
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 }
Andrei Homescuc7767922020-08-05 06:36:19 -070038 if androidmk, ok := obj.(SubAndroidMkProvider); ok {
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 if !mod.subAndroidMkOnce[androidmk] {
40 mod.subAndroidMkOnce[androidmk] = true
41 androidmk.AndroidMk(mod, data)
42 }
43 }
44}
45
Liz Kammer4018a8d2020-11-24 09:35:21 -080046func (mod *Module) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park99644e92020-11-17 22:21:02 +090047 if mod.Properties.HideFromMake || mod.hideApexVariantFromMake {
Liz Kammer4018a8d2020-11-24 09:35:21 -080048
49 return []android.AndroidMkEntries{android.AndroidMkEntries{Disabled: true}}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040050 }
51
Liz Kammer4018a8d2020-11-24 09:35:21 -080052 ret := android.AndroidMkEntries{
Ivan Lozano8d10fc32021-11-05 16:36:47 -040053 OutputFile: android.OptionalPathForPath(mod.UnstrippedOutputFile()),
Ivan Lozanod06cc742021-11-12 13:27:58 -050054 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Liz Kammer4018a8d2020-11-24 09:35:21 -080055 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -070056 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Liz Kammer4018a8d2020-11-24 09:35:21 -080057 entries.AddStrings("LOCAL_RLIB_LIBRARIES", mod.Properties.AndroidMkRlibs...)
58 entries.AddStrings("LOCAL_DYLIB_LIBRARIES", mod.Properties.AndroidMkDylibs...)
59 entries.AddStrings("LOCAL_PROC_MACRO_LIBRARIES", mod.Properties.AndroidMkProcMacroLibs...)
60 entries.AddStrings("LOCAL_SHARED_LIBRARIES", mod.Properties.AndroidMkSharedLibs...)
61 entries.AddStrings("LOCAL_STATIC_LIBRARIES", mod.Properties.AndroidMkStaticLibs...)
Ivan Lozano6a884432020-12-02 09:15:16 -050062 entries.AddStrings("LOCAL_SOONG_LINK_TYPE", mod.makeLinkType)
Ivan Lozanoc08897c2021-04-02 12:41:32 -040063 if mod.UseVndk() {
64 entries.SetBool("LOCAL_USE_VNDK", true)
65 }
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067 },
68 },
69 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040070
71 if mod.compiler != nil && !mod.compiler.Disabled() {
Andrei Homescuc7767922020-08-05 06:36:19 -070072 mod.SubAndroidMk(&ret, mod.compiler)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040073 } else if mod.sourceProvider != nil {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040074 // If the compiler is disabled, this is a SourceProvider.
Andrei Homescuc7767922020-08-05 06:36:19 -070075 mod.SubAndroidMk(&ret, mod.sourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040076 }
Tri Vo0a74c3e2021-04-01 13:59:27 -070077
78 if mod.sanitize != nil {
79 mod.SubAndroidMk(&ret, mod.sanitize)
80 }
81
Ivan Lozanoc08897c2021-04-02 12:41:32 -040082 ret.SubName += mod.Properties.RustSubName
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070083 ret.SubName += mod.Properties.SubName
84
Liz Kammer4018a8d2020-11-24 09:35:21 -080085 return []android.AndroidMkEntries{ret}
Ivan Lozanoffee3342019-08-27 12:03:00 -070086}
87
Liz Kammer4018a8d2020-11-24 09:35:21 -080088func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -070089 ctx.SubAndroidMk(ret, binary.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -070090
Jingwen Chen40fd90a2020-06-15 05:24:19 +000091 if binary.distFile.Valid() {
92 ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path())
93 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 ret.Class = "EXECUTABLES"
Ivan Lozanoffee3342019-08-27 12:03:00 -070095}
96
Liz Kammer4018a8d2020-11-24 09:35:21 -080097func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Ivan Lozano9da4aa82021-01-29 12:48:05 -050098 ctx.SubAndroidMk(ret, test.binaryDecorator)
99
Chih-Hung Hsieh15f369e2019-11-15 04:14:44 -0800100 ret.Class = "NATIVE_TESTS"
Colin Crossaa255532020-07-03 13:18:24 -0700101 ret.ExtraEntries = append(ret.ExtraEntries,
102 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
103 entries.AddCompatibilityTestSuites(test.Properties.Test_suites...)
104 if test.testConfig != nil {
105 entries.SetString("LOCAL_FULL_TEST_CONFIG", test.testConfig.String())
106 }
107 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(test.Properties.Auto_gen_config, true))
108 entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", Bool(test.Properties.Test_options.Unit_test))
109 })
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500110
111 cc.AndroidMkWriteTestData(test.data, ret)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700112}
113
Jakub Kotur546ccd52021-01-15 15:57:27 +0100114func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
115 benchmark.binaryDecorator.AndroidMk(ctx, ret)
116 ret.Class = "NATIVE_TESTS"
117 ret.ExtraEntries = append(ret.ExtraEntries,
118 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
119 entries.AddCompatibilityTestSuites(benchmark.Properties.Test_suites...)
120 if benchmark.testConfig != nil {
121 entries.SetString("LOCAL_FULL_TEST_CONFIG", benchmark.testConfig.String())
122 }
123 entries.SetBool("LOCAL_NATIVE_BENCHMARK", true)
124 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(benchmark.Properties.Auto_gen_config, true))
125 })
126}
127
Liz Kammer4018a8d2020-11-24 09:35:21 -0800128func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700129 ctx.SubAndroidMk(ret, library.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130
131 if library.rlib() {
132 ret.Class = "RLIB_LIBRARIES"
133 } else if library.dylib() {
134 ret.Class = "DYLIB_LIBRARIES"
Ivan Lozano52767be2019-10-18 14:49:46 -0700135 } else if library.static() {
136 ret.Class = "STATIC_LIBRARIES"
137 } else if library.shared() {
138 ret.Class = "SHARED_LIBRARIES"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000140 if library.distFile.Valid() {
141 ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path())
142 }
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400143 ret.ExtraEntries = append(ret.ExtraEntries,
144 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
145 if library.tocFile.Valid() {
146 entries.SetString("LOCAL_SOONG_TOC", library.tocFile.String())
147 }
148 })
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149}
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500150
Liz Kammer4018a8d2020-11-24 09:35:21 -0800151func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700152 ctx.SubAndroidMk(ret, procMacro.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153
154 ret.Class = "PROC_MACRO_LIBRARIES"
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000155 if procMacro.distFile.Valid() {
156 ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path())
157 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158
159}
160
Liz Kammer4018a8d2020-11-24 09:35:21 -0800161func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700162 outFile := sourceProvider.OutputFiles[0]
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400163 ret.Class = "ETC"
164 ret.OutputFile = android.OptionalPathForPath(outFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400165 ret.SubName += sourceProvider.subName
Colin Crossaa255532020-07-03 13:18:24 -0700166 ret.ExtraEntries = append(ret.ExtraEntries,
167 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
168 _, file := filepath.Split(outFile.String())
169 stem, suffix, _ := android.SplitFileExt(file)
170 entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
171 entries.SetString("LOCAL_MODULE_STEM", stem)
172 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
173 })
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400174}
175
Liz Kammer4018a8d2020-11-24 09:35:21 -0800176func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700177 ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider)
Treehugger Robot588aae72020-08-21 10:01:58 +0000178}
179
Liz Kammer4018a8d2020-11-24 09:35:21 -0800180func (proto *protobufDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Treehugger Robot588aae72020-08-21 10:01:58 +0000181 ctx.SubAndroidMk(ret, proto.BaseSourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400182}
183
Liz Kammer4018a8d2020-11-24 09:35:21 -0800184func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Ivan Lozano2b081132020-09-08 12:46:52 -0400185 if compiler.path == (android.InstallPath{}) {
186 return
187 }
188
Colin Cross77435572021-11-08 12:37:01 -0800189 if compiler.strippedOutputFile.Valid() {
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200190 ret.OutputFile = compiler.strippedOutputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400192
Colin Crossaa255532020-07-03 13:18:24 -0700193 ret.ExtraEntries = append(ret.ExtraEntries,
194 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400195 entries.SetPath("LOCAL_SOONG_UNSTRIPPED_BINARY", compiler.unstrippedOutputFile)
Colin Crossaa255532020-07-03 13:18:24 -0700196 path, file := filepath.Split(compiler.path.ToMakePath().String())
197 stem, suffix, _ := android.SplitFileExt(file)
198 entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
199 entries.SetString("LOCAL_MODULE_PATH", path)
200 entries.SetString("LOCAL_MODULE_STEM", stem)
201 })
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202}
hamzehc651b522021-04-29 12:50:47 -0700203
204func (fuzz *fuzzDecorator) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
205 ctx.SubAndroidMk(entries, fuzz.binaryDecorator)
206
207 var fuzzFiles []string
hamzeh41ad8812021-07-07 14:00:07 -0700208 for _, d := range fuzz.fuzzPackagedModule.Corpus {
hamzehc651b522021-04-29 12:50:47 -0700209 fuzzFiles = append(fuzzFiles,
hamzeh41ad8812021-07-07 14:00:07 -0700210 filepath.Dir(fuzz.fuzzPackagedModule.CorpusIntermediateDir.String())+":corpus/"+d.Base())
hamzehc651b522021-04-29 12:50:47 -0700211 }
212
hamzeh41ad8812021-07-07 14:00:07 -0700213 for _, d := range fuzz.fuzzPackagedModule.Data {
hamzehc651b522021-04-29 12:50:47 -0700214 fuzzFiles = append(fuzzFiles,
hamzeh41ad8812021-07-07 14:00:07 -0700215 filepath.Dir(fuzz.fuzzPackagedModule.DataIntermediateDir.String())+":data/"+d.Rel())
hamzehc651b522021-04-29 12:50:47 -0700216 }
217
hamzeh41ad8812021-07-07 14:00:07 -0700218 if fuzz.fuzzPackagedModule.Dictionary != nil {
hamzehc651b522021-04-29 12:50:47 -0700219 fuzzFiles = append(fuzzFiles,
hamzeh41ad8812021-07-07 14:00:07 -0700220 filepath.Dir(fuzz.fuzzPackagedModule.Dictionary.String())+":"+fuzz.fuzzPackagedModule.Dictionary.Base())
hamzehc651b522021-04-29 12:50:47 -0700221 }
222
hamzeh41ad8812021-07-07 14:00:07 -0700223 if fuzz.fuzzPackagedModule.Config != nil {
hamzehc651b522021-04-29 12:50:47 -0700224 fuzzFiles = append(fuzzFiles,
hamzeh41ad8812021-07-07 14:00:07 -0700225 filepath.Dir(fuzz.fuzzPackagedModule.Config.String())+":config.json")
hamzehc651b522021-04-29 12:50:47 -0700226 }
227
228 entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext,
229 entries *android.AndroidMkEntries) {
230 entries.SetBool("LOCAL_IS_FUZZ_TARGET", true)
231 if len(fuzzFiles) > 0 {
232 entries.AddStrings("LOCAL_TEST_DATA", fuzzFiles...)
233 }
234 })
235}