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