1// 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 ( 18 "path/filepath" 19 20 "android/soong/android" 21 "android/soong/cc" 22) 23 24type AndroidMkContext interface { 25 Name() string 26 Target() android.Target 27 SubAndroidMk(*android.AndroidMkEntries, interface{}) 28} 29 30type SubAndroidMkProvider interface { 31 AndroidMk(AndroidMkContext, *android.AndroidMkEntries) 32} 33 34func (mod *Module) SubAndroidMk(data *android.AndroidMkEntries, obj interface{}) { 35 if mod.subAndroidMkOnce == nil { 36 mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool) 37 } 38 if androidmk, ok := obj.(SubAndroidMkProvider); ok { 39 if !mod.subAndroidMkOnce[androidmk] { 40 mod.subAndroidMkOnce[androidmk] = true 41 androidmk.AndroidMk(mod, data) 42 } 43 } 44} 45 46func (mod *Module) AndroidMkEntries() []android.AndroidMkEntries { 47 if mod.Properties.HideFromMake || mod.hideApexVariantFromMake { 48 49 return []android.AndroidMkEntries{android.AndroidMkEntries{Disabled: true}} 50 } 51 52 ret := android.AndroidMkEntries{ 53 OutputFile: android.OptionalPathForPath(mod.UnstrippedOutputFile()), 54 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk", 55 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 56 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 57 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...) 62 entries.AddStrings("LOCAL_SOONG_LINK_TYPE", mod.makeLinkType) 63 if mod.UseVndk() { 64 entries.SetBool("LOCAL_USE_VNDK", true) 65 } 66 67 }, 68 }, 69 } 70 71 if mod.compiler != nil && !mod.compiler.Disabled() { 72 mod.SubAndroidMk(&ret, mod.compiler) 73 } else if mod.sourceProvider != nil { 74 // If the compiler is disabled, this is a SourceProvider. 75 mod.SubAndroidMk(&ret, mod.sourceProvider) 76 } 77 78 if mod.sanitize != nil { 79 mod.SubAndroidMk(&ret, mod.sanitize) 80 } 81 82 ret.SubName += mod.Properties.RustSubName 83 ret.SubName += mod.Properties.SubName 84 85 return []android.AndroidMkEntries{ret} 86} 87 88func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 89 ctx.SubAndroidMk(ret, binary.baseCompiler) 90 91 if binary.distFile.Valid() { 92 ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path()) 93 } 94 ret.Class = "EXECUTABLES" 95} 96 97func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 98 ctx.SubAndroidMk(ret, test.binaryDecorator) 99 100 ret.Class = "NATIVE_TESTS" 101 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 if test.Properties.Data_bins != nil { 110 entries.AddStrings("LOCAL_TEST_DATA_BINS", test.Properties.Data_bins...) 111 } 112 }) 113 114 cc.AndroidMkWriteTestData(test.data, ret) 115} 116 117func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 118 benchmark.binaryDecorator.AndroidMk(ctx, ret) 119 ret.Class = "NATIVE_TESTS" 120 ret.ExtraEntries = append(ret.ExtraEntries, 121 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 122 entries.AddCompatibilityTestSuites(benchmark.Properties.Test_suites...) 123 if benchmark.testConfig != nil { 124 entries.SetString("LOCAL_FULL_TEST_CONFIG", benchmark.testConfig.String()) 125 } 126 entries.SetBool("LOCAL_NATIVE_BENCHMARK", true) 127 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(benchmark.Properties.Auto_gen_config, true)) 128 }) 129} 130 131func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 132 ctx.SubAndroidMk(ret, library.baseCompiler) 133 134 if library.rlib() { 135 ret.Class = "RLIB_LIBRARIES" 136 } else if library.dylib() { 137 ret.Class = "DYLIB_LIBRARIES" 138 } else if library.static() { 139 ret.Class = "STATIC_LIBRARIES" 140 } else if library.shared() { 141 ret.Class = "SHARED_LIBRARIES" 142 } 143 if library.distFile.Valid() { 144 ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path()) 145 } 146 ret.ExtraEntries = append(ret.ExtraEntries, 147 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 148 if library.tocFile.Valid() { 149 entries.SetString("LOCAL_SOONG_TOC", library.tocFile.String()) 150 } 151 }) 152} 153 154func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 155 ctx.SubAndroidMk(ret, procMacro.baseCompiler) 156 157 ret.Class = "PROC_MACRO_LIBRARIES" 158 if procMacro.distFile.Valid() { 159 ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path()) 160 } 161 162} 163 164func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 165 outFile := sourceProvider.OutputFiles[0] 166 ret.Class = "ETC" 167 ret.OutputFile = android.OptionalPathForPath(outFile) 168 ret.SubName += sourceProvider.subName 169 ret.ExtraEntries = append(ret.ExtraEntries, 170 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 171 _, file := filepath.Split(outFile.String()) 172 stem, suffix, _ := android.SplitFileExt(file) 173 entries.SetString("LOCAL_MODULE_SUFFIX", suffix) 174 entries.SetString("LOCAL_MODULE_STEM", stem) 175 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) 176 }) 177} 178 179func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 180 ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider) 181} 182 183func (proto *protobufDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 184 ctx.SubAndroidMk(ret, proto.BaseSourceProvider) 185} 186 187func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) { 188 if compiler.path == (android.InstallPath{}) { 189 return 190 } 191 192 if compiler.strippedOutputFile.Valid() { 193 ret.OutputFile = compiler.strippedOutputFile 194 } 195 196 ret.ExtraEntries = append(ret.ExtraEntries, 197 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 198 entries.SetPath("LOCAL_SOONG_UNSTRIPPED_BINARY", compiler.unstrippedOutputFile) 199 path, file := filepath.Split(compiler.path.String()) 200 stem, suffix, _ := android.SplitFileExt(file) 201 entries.SetString("LOCAL_MODULE_SUFFIX", suffix) 202 entries.SetString("LOCAL_MODULE_PATH", path) 203 entries.SetString("LOCAL_MODULE_STEM", stem) 204 }) 205} 206 207func (fuzz *fuzzDecorator) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { 208 ctx.SubAndroidMk(entries, fuzz.binaryDecorator) 209 210 var fuzzFiles []string 211 for _, d := range fuzz.fuzzPackagedModule.Corpus { 212 fuzzFiles = append(fuzzFiles, 213 filepath.Dir(fuzz.fuzzPackagedModule.CorpusIntermediateDir.String())+":corpus/"+d.Base()) 214 } 215 216 for _, d := range fuzz.fuzzPackagedModule.Data { 217 fuzzFiles = append(fuzzFiles, 218 filepath.Dir(fuzz.fuzzPackagedModule.DataIntermediateDir.String())+":data/"+d.Rel()) 219 } 220 221 if fuzz.fuzzPackagedModule.Dictionary != nil { 222 fuzzFiles = append(fuzzFiles, 223 filepath.Dir(fuzz.fuzzPackagedModule.Dictionary.String())+":"+fuzz.fuzzPackagedModule.Dictionary.Base()) 224 } 225 226 if fuzz.fuzzPackagedModule.Config != nil { 227 fuzzFiles = append(fuzzFiles, 228 filepath.Dir(fuzz.fuzzPackagedModule.Config.String())+":config.json") 229 } 230 231 entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, 232 entries *android.AndroidMkEntries) { 233 entries.SetBool("LOCAL_IS_FUZZ_TARGET", true) 234 if len(fuzzFiles) > 0 { 235 entries.AddStrings("LOCAL_TEST_DATA", fuzzFiles...) 236 } 237 }) 238} 239