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 |
| 29 | subAndroidMk(*android.AndroidMkData, interface{}) |
| 30 | } |
| 31 | |
| 32 | type subAndroidMkProvider interface { |
| 33 | AndroidMk(AndroidMkContext, *android.AndroidMkData) |
| 34 | } |
| 35 | |
| 36 | func (mod *Module) subAndroidMk(data *android.AndroidMkData, obj interface{}) { |
| 37 | if mod.subAndroidMkOnce == nil { |
| 38 | mod.subAndroidMkOnce = make(map[subAndroidMkProvider]bool) |
| 39 | } |
| 40 | if androidmk, ok := obj.(subAndroidMkProvider); ok { |
| 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 { |
| 49 | ret := android.AndroidMkData{ |
| 50 | OutputFile: mod.outputFile, |
| 51 | Include: "$(BUILD_SYSTEM)/soong_rust_prebuilt.mk", |
| 52 | Extra: []android.AndroidMkExtraFunc{ |
| 53 | func(w io.Writer, outputFile android.Path) { |
| 54 | if len(mod.Properties.AndroidMkRlibs) > 0 { |
| 55 | fmt.Fprintln(w, "LOCAL_RLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkRlibs, " ")) |
| 56 | } |
| 57 | if len(mod.Properties.AndroidMkDylibs) > 0 { |
| 58 | fmt.Fprintln(w, "LOCAL_DYLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkDylibs, " ")) |
| 59 | } |
| 60 | if len(mod.Properties.AndroidMkProcMacroLibs) > 0 { |
| 61 | fmt.Fprintln(w, "LOCAL_PROC_MACRO_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkProcMacroLibs, " ")) |
| 62 | } |
| 63 | if len(mod.Properties.AndroidMkSharedLibs) > 0 { |
| 64 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkSharedLibs, " ")) |
| 65 | } |
| 66 | if len(mod.Properties.AndroidMkStaticLibs) > 0 { |
| 67 | fmt.Fprintln(w, "LOCAL_STATIC_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkStaticLibs, " ")) |
| 68 | } |
| 69 | }, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | mod.subAndroidMk(&ret, mod.compiler) |
| 74 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 75 | ret.SubName += mod.Properties.SubName |
| 76 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | return ret |
| 78 | } |
| 79 | |
| 80 | func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 81 | ctx.subAndroidMk(ret, binary.baseCompiler) |
| 82 | |
| 83 | ret.Class = "EXECUTABLES" |
| 84 | ret.DistFile = binary.distFile |
| 85 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
| 86 | fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", binary.unstrippedOutputFile.String()) |
| 87 | }) |
| 88 | } |
| 89 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 90 | func (test *testBinaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 91 | test.binaryDecorator.AndroidMk(ctx, ret) |
| 92 | ret.SubName = "_" + String(test.baseCompiler.Properties.Stem) |
| 93 | } |
| 94 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 95 | func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 96 | ctx.subAndroidMk(ret, library.baseCompiler) |
| 97 | |
| 98 | if library.rlib() { |
| 99 | ret.Class = "RLIB_LIBRARIES" |
| 100 | } else if library.dylib() { |
| 101 | ret.Class = "DYLIB_LIBRARIES" |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 102 | } else if library.static() { |
| 103 | ret.Class = "STATIC_LIBRARIES" |
| 104 | } else if library.shared() { |
| 105 | ret.Class = "SHARED_LIBRARIES" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 106 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 107 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 108 | ret.DistFile = library.distFile |
| 109 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
| 110 | if !library.rlib() { |
| 111 | fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", library.unstrippedOutputFile.String()) |
| 112 | } |
| 113 | }) |
| 114 | } |
| 115 | |
| 116 | func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 117 | ctx.subAndroidMk(ret, procMacro.baseCompiler) |
| 118 | |
| 119 | ret.Class = "PROC_MACRO_LIBRARIES" |
| 120 | ret.DistFile = procMacro.distFile |
| 121 | |
| 122 | } |
| 123 | |
| 124 | func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 125 | // Soong installation is only supported for host modules. Have Make |
| 126 | // installation trigger Soong installation. |
| 127 | if ctx.Target().Os.Class == android.Host { |
| 128 | ret.OutputFile = android.OptionalPathForPath(compiler.path) |
| 129 | } |
| 130 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 131 | path, file := filepath.Split(compiler.path.ToMakePath().String()) |
Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 132 | stem, suffix, _ := android.SplitFileExt(file) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 133 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 134 | fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 135 | fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem) |
| 136 | }) |
| 137 | } |