blob: fda0a25799907fe1758ae4239cf94a88efd8638a [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 (
18 "fmt"
19 "io"
20 "path/filepath"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "strings"
22
23 "android/soong/android"
24)
25
26type AndroidMkContext interface {
27 Name() string
28 Target() android.Target
Andrei Homescuc7767922020-08-05 06:36:19 -070029 SubAndroidMk(*android.AndroidMkData, interface{})
Ivan Lozanoffee3342019-08-27 12:03:00 -070030}
31
Andrei Homescuc7767922020-08-05 06:36:19 -070032type SubAndroidMkProvider interface {
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 AndroidMk(AndroidMkContext, *android.AndroidMkData)
34}
35
Andrei Homescuc7767922020-08-05 06:36:19 -070036func (mod *Module) SubAndroidMk(data *android.AndroidMkData, obj interface{}) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 if mod.subAndroidMkOnce == nil {
Andrei Homescuc7767922020-08-05 06:36:19 -070038 mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool)
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 }
Andrei Homescuc7767922020-08-05 06:36:19 -070040 if androidmk, ok := obj.(SubAndroidMkProvider); ok {
Ivan Lozanoffee3342019-08-27 12:03:00 -070041 if !mod.subAndroidMkOnce[androidmk] {
42 mod.subAndroidMkOnce[androidmk] = true
43 androidmk.AndroidMk(mod, data)
44 }
45 }
46}
47
48func (mod *Module) AndroidMk() android.AndroidMkData {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040049 if mod.Properties.HideFromMake {
50 return android.AndroidMkData{
51 Disabled: true,
52 }
53 }
54
Ivan Lozanoffee3342019-08-27 12:03:00 -070055 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 Lozano26ecd6c2020-07-31 13:40:31 -040078
79 if mod.compiler != nil && !mod.compiler.Disabled() {
Andrei Homescuc7767922020-08-05 06:36:19 -070080 mod.SubAndroidMk(&ret, mod.compiler)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040081 } else if mod.sourceProvider != nil {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040082 // If the compiler is disabled, this is a SourceProvider.
Andrei Homescuc7767922020-08-05 06:36:19 -070083 mod.SubAndroidMk(&ret, mod.sourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040084 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070085 ret.SubName += mod.Properties.SubName
86
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 return ret
88}
89
90func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Andrei Homescuc7767922020-08-05 06:36:19 -070091 ctx.SubAndroidMk(ret, binary.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -070092
Jingwen Chen40fd90a2020-06-15 05:24:19 +000093 if binary.distFile.Valid() {
94 ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path())
95 }
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 ret.Class = "EXECUTABLES"
Ivan Lozanoffee3342019-08-27 12:03:00 -070098 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
99 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", binary.unstrippedOutputFile.String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400100 if binary.coverageOutputZipFile.Valid() {
101 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+binary.coverageOutputZipFile.String())
102 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700103 })
104}
105
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700106func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700107 test.binaryDecorator.AndroidMk(ctx, ret)
Chih-Hung Hsieh15f369e2019-11-15 04:14:44 -0800108 ret.Class = "NATIVE_TESTS"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700109 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
110 if len(test.Properties.Test_suites) > 0 {
111 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
112 strings.Join(test.Properties.Test_suites, " "))
113 }
114 if test.testConfig != nil {
115 fmt.Fprintln(w, "LOCAL_FULL_TEST_CONFIG :=", test.testConfig.String())
116 }
Dan Shi2468d012020-01-06 15:47:57 -0800117 if !BoolDefault(test.Properties.Auto_gen_config, true) {
118 fmt.Fprintln(w, "LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG := true")
119 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700120 })
121 // TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700122}
123
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700125 ctx.SubAndroidMk(ret, library.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126
127 if library.rlib() {
128 ret.Class = "RLIB_LIBRARIES"
129 } else if library.dylib() {
130 ret.Class = "DYLIB_LIBRARIES"
Ivan Lozano52767be2019-10-18 14:49:46 -0700131 } else if library.static() {
132 ret.Class = "STATIC_LIBRARIES"
133 } else if library.shared() {
134 ret.Class = "SHARED_LIBRARIES"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700136
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000137 if library.distFile.Valid() {
138 ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path())
139 }
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
142 if !library.rlib() {
143 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", library.unstrippedOutputFile.String())
144 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400145 if library.coverageOutputZipFile.Valid() {
146 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+library.coverageOutputZipFile.String())
147 }
148
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149 })
150}
151
152func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700153 ctx.SubAndroidMk(ret, procMacro.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154
155 ret.Class = "PROC_MACRO_LIBRARIES"
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000156 if procMacro.distFile.Valid() {
157 ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path())
158 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159
160}
161
Andrei Homescuc7767922020-08-05 06:36:19 -0700162func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
163 outFile := sourceProvider.OutputFile
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400164 ret.Class = "ETC"
165 ret.OutputFile = android.OptionalPathForPath(outFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400166 ret.SubName += sourceProvider.subName
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400167 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
168 _, file := filepath.Split(outFile.String())
169 stem, suffix, _ := android.SplitFileExt(file)
170 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
171 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
172 })
173}
174
175func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700176 ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400177 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
178 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
179 })
180}
181
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
183 // Soong installation is only supported for host modules. Have Make
184 // installation trigger Soong installation.
185 if ctx.Target().Os.Class == android.Host {
186 ret.OutputFile = android.OptionalPathForPath(compiler.path)
187 }
188 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700189 path, file := filepath.Split(compiler.path.ToMakePath().String())
Ivan Lozano022a73b2019-09-09 20:29:31 -0700190 stem, suffix, _ := android.SplitFileExt(file)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
Colin Crossff6c33d2019-10-02 16:01:35 -0700192 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
194 })
195}