Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "path/filepath" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | type AndroidMkContext interface { |
| 27 | Name() string |
| 28 | Target() android.Target |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 29 | SubAndroidMk(*android.AndroidMkData, interface{}) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 32 | type SubAndroidMkProvider interface { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | AndroidMk(AndroidMkContext, *android.AndroidMkData) |
| 34 | } |
| 35 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 36 | func (mod *Module) SubAndroidMk(data *android.AndroidMkData, obj interface{}) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 37 | if mod.subAndroidMkOnce == nil { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 38 | mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 39 | } |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 40 | if androidmk, ok := obj.(SubAndroidMkProvider); ok { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 41 | if !mod.subAndroidMkOnce[androidmk] { |
| 42 | mod.subAndroidMkOnce[androidmk] = true |
| 43 | androidmk.AndroidMk(mod, data) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func (mod *Module) AndroidMk() android.AndroidMkData { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 49 | if mod.Properties.HideFromMake { |
| 50 | return android.AndroidMkData{ |
| 51 | Disabled: true, |
| 52 | } |
| 53 | } |
| 54 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | ret := android.AndroidMkData{ |
| 56 | OutputFile: mod.outputFile, |
| 57 | Include: "$(BUILD_SYSTEM)/soong_rust_prebuilt.mk", |
| 58 | Extra: []android.AndroidMkExtraFunc{ |
| 59 | func(w io.Writer, outputFile android.Path) { |
| 60 | if len(mod.Properties.AndroidMkRlibs) > 0 { |
| 61 | fmt.Fprintln(w, "LOCAL_RLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkRlibs, " ")) |
| 62 | } |
| 63 | if len(mod.Properties.AndroidMkDylibs) > 0 { |
| 64 | fmt.Fprintln(w, "LOCAL_DYLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkDylibs, " ")) |
| 65 | } |
| 66 | if len(mod.Properties.AndroidMkProcMacroLibs) > 0 { |
| 67 | fmt.Fprintln(w, "LOCAL_PROC_MACRO_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkProcMacroLibs, " ")) |
| 68 | } |
| 69 | if len(mod.Properties.AndroidMkSharedLibs) > 0 { |
| 70 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkSharedLibs, " ")) |
| 71 | } |
| 72 | if len(mod.Properties.AndroidMkStaticLibs) > 0 { |
| 73 | fmt.Fprintln(w, "LOCAL_STATIC_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkStaticLibs, " ")) |
| 74 | } |
| 75 | }, |
| 76 | }, |
| 77 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 78 | |
| 79 | if mod.compiler != nil && !mod.compiler.Disabled() { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 80 | mod.SubAndroidMk(&ret, mod.compiler) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 81 | } else if mod.sourceProvider != nil { |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 82 | // If the compiler is disabled, this is a SourceProvider. |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 83 | mod.SubAndroidMk(&ret, mod.sourceProvider) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 84 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 85 | ret.SubName += mod.Properties.SubName |
| 86 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | return ret |
| 88 | } |
| 89 | |
| 90 | func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 91 | ctx.SubAndroidMk(ret, binary.baseCompiler) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 92 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 93 | if binary.distFile.Valid() { |
| 94 | ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path()) |
| 95 | } |
| 96 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 97 | ret.Class = "EXECUTABLES" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 98 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 99 | if binary.coverageOutputZipFile.Valid() { |
| 100 | fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+binary.coverageOutputZipFile.String()) |
| 101 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 102 | }) |
| 103 | } |
| 104 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 105 | func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 106 | test.binaryDecorator.AndroidMk(ctx, ret) |
Chih-Hung Hsieh | 15f369e | 2019-11-15 04:14:44 -0800 | [diff] [blame] | 107 | ret.Class = "NATIVE_TESTS" |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 108 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
| 109 | if len(test.Properties.Test_suites) > 0 { |
| 110 | fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=", |
| 111 | strings.Join(test.Properties.Test_suites, " ")) |
| 112 | } |
| 113 | if test.testConfig != nil { |
| 114 | fmt.Fprintln(w, "LOCAL_FULL_TEST_CONFIG :=", test.testConfig.String()) |
| 115 | } |
Dan Shi | 2468d01 | 2020-01-06 15:47:57 -0800 | [diff] [blame] | 116 | if !BoolDefault(test.Properties.Auto_gen_config, true) { |
| 117 | fmt.Fprintln(w, "LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG := true") |
| 118 | } |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 119 | if Bool(test.Properties.Test_options.Unit_test) { |
| 120 | fmt.Fprintln(w, "LOCAL_IS_UNIT_TEST := true") |
| 121 | } |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 122 | }) |
| 123 | // TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret) |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 126 | func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 127 | ctx.SubAndroidMk(ret, library.baseCompiler) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 128 | |
| 129 | if library.rlib() { |
| 130 | ret.Class = "RLIB_LIBRARIES" |
| 131 | } else if library.dylib() { |
| 132 | ret.Class = "DYLIB_LIBRARIES" |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 133 | } else if library.static() { |
| 134 | ret.Class = "STATIC_LIBRARIES" |
| 135 | } else if library.shared() { |
| 136 | ret.Class = "SHARED_LIBRARIES" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 137 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 138 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 139 | if library.distFile.Valid() { |
| 140 | ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path()) |
| 141 | } |
| 142 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 143 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 144 | if library.coverageOutputZipFile.Valid() { |
| 145 | fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+library.coverageOutputZipFile.String()) |
| 146 | } |
| 147 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 148 | }) |
| 149 | } |
| 150 | |
| 151 | func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 152 | ctx.SubAndroidMk(ret, procMacro.baseCompiler) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | |
| 154 | ret.Class = "PROC_MACRO_LIBRARIES" |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 155 | if procMacro.distFile.Valid() { |
| 156 | ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path()) |
| 157 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 158 | |
| 159 | } |
| 160 | |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 161 | func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 162 | outFile := sourceProvider.OutputFiles[0] |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 163 | ret.Class = "ETC" |
| 164 | ret.OutputFile = android.OptionalPathForPath(outFile) |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 165 | ret.SubName += sourceProvider.subName |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 166 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
| 167 | _, file := filepath.Split(outFile.String()) |
| 168 | stem, suffix, _ := android.SplitFileExt(file) |
| 169 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix) |
| 170 | fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 171 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 172 | }) |
| 173 | } |
| 174 | |
| 175 | func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 176 | ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | func (proto *protobufDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 180 | ctx.SubAndroidMk(ret, proto.BaseSourceProvider) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 183 | func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 184 | if compiler.path == (android.InstallPath{}) { |
| 185 | return |
| 186 | } |
| 187 | |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 188 | var unstrippedOutputFile android.OptionalPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 189 | // Soong installation is only supported for host modules. Have Make |
| 190 | // installation trigger Soong installation. |
| 191 | if ctx.Target().Os.Class == android.Host { |
| 192 | ret.OutputFile = android.OptionalPathForPath(compiler.path) |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 193 | } else if compiler.strippedOutputFile.Valid() { |
| 194 | unstrippedOutputFile = ret.OutputFile |
| 195 | ret.OutputFile = compiler.strippedOutputFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | } |
| 197 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 198 | if compiler.strippedOutputFile.Valid() { |
| 199 | fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", unstrippedOutputFile) |
| 200 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 201 | path, file := filepath.Split(compiler.path.ToMakePath().String()) |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 202 | stem, suffix, _ := android.SplitFileExt(file) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 203 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 204 | fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 205 | fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem) |
| 206 | }) |
| 207 | } |