blob: babbf9123f69bdbd5250a7d4306295409faee7b3 [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
29 subAndroidMk(*android.AndroidMkData, interface{})
30}
31
32type subAndroidMkProvider interface {
33 AndroidMk(AndroidMkContext, *android.AndroidMkData)
34}
35
36func (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
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",
Ivan Lozano4fef93c2020-07-08 08:39:44 -040058 SubName: mod.subName,
Ivan Lozanoffee3342019-08-27 12:03:00 -070059 Extra: []android.AndroidMkExtraFunc{
60 func(w io.Writer, outputFile android.Path) {
61 if len(mod.Properties.AndroidMkRlibs) > 0 {
62 fmt.Fprintln(w, "LOCAL_RLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkRlibs, " "))
63 }
64 if len(mod.Properties.AndroidMkDylibs) > 0 {
65 fmt.Fprintln(w, "LOCAL_DYLIB_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkDylibs, " "))
66 }
67 if len(mod.Properties.AndroidMkProcMacroLibs) > 0 {
68 fmt.Fprintln(w, "LOCAL_PROC_MACRO_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkProcMacroLibs, " "))
69 }
70 if len(mod.Properties.AndroidMkSharedLibs) > 0 {
71 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkSharedLibs, " "))
72 }
73 if len(mod.Properties.AndroidMkStaticLibs) > 0 {
74 fmt.Fprintln(w, "LOCAL_STATIC_LIBRARIES := "+strings.Join(mod.Properties.AndroidMkStaticLibs, " "))
75 }
76 },
77 },
78 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -040079 if mod.compiler != nil {
80 mod.subAndroidMk(&ret, mod.compiler)
81 } else if mod.sourceProvider != nil {
82 mod.subAndroidMk(&ret, mod.sourceProvider)
83 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070084 ret.SubName += mod.Properties.SubName
85
Ivan Lozanoffee3342019-08-27 12:03:00 -070086 return ret
87}
88
89func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
90 ctx.subAndroidMk(ret, binary.baseCompiler)
91
Jingwen Chen40fd90a2020-06-15 05:24:19 +000092 if binary.distFile.Valid() {
93 ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path())
94 }
95
Ivan Lozanoffee3342019-08-27 12:03:00 -070096 ret.Class = "EXECUTABLES"
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
98 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", binary.unstrippedOutputFile.String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040099 if binary.coverageOutputZipFile.Valid() {
100 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+binary.coverageOutputZipFile.String())
101 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102 })
103}
104
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700105func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700106 test.binaryDecorator.AndroidMk(ctx, ret)
Chih-Hung Hsieh15f369e2019-11-15 04:14:44 -0800107 ret.Class = "NATIVE_TESTS"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700108 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 Shi2468d012020-01-06 15:47:57 -0800116 if !BoolDefault(test.Properties.Auto_gen_config, true) {
117 fmt.Fprintln(w, "LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG := true")
118 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700119 })
120 // TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700121}
122
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
124 ctx.subAndroidMk(ret, library.baseCompiler)
125
126 if library.rlib() {
127 ret.Class = "RLIB_LIBRARIES"
128 } else if library.dylib() {
129 ret.Class = "DYLIB_LIBRARIES"
Ivan Lozano52767be2019-10-18 14:49:46 -0700130 } else if library.static() {
131 ret.Class = "STATIC_LIBRARIES"
132 } else if library.shared() {
133 ret.Class = "SHARED_LIBRARIES"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700135
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000136 if library.distFile.Valid() {
137 ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path())
138 }
139
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
141 if !library.rlib() {
142 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", library.unstrippedOutputFile.String())
143 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400144 if library.coverageOutputZipFile.Valid() {
145 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+library.coverageOutputZipFile.String())
146 }
147
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 })
149}
150
151func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
152 ctx.subAndroidMk(ret, procMacro.baseCompiler)
153
154 ret.Class = "PROC_MACRO_LIBRARIES"
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000155 if procMacro.distFile.Valid() {
156 ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path())
157 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158
159}
160
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400161func (sourceProvider *baseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
162 outFile := sourceProvider.outputFile
163 ret.Class = "ETC"
164 ret.OutputFile = android.OptionalPathForPath(outFile)
165 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
166 _, file := filepath.Split(outFile.String())
167 stem, suffix, _ := android.SplitFileExt(file)
168 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
169 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
170 })
171}
172
173func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
174 ctx.subAndroidMk(ret, bindgen.baseSourceProvider)
175 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
176 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
177 })
178}
179
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
181 // Soong installation is only supported for host modules. Have Make
182 // installation trigger Soong installation.
183 if ctx.Target().Os.Class == android.Host {
184 ret.OutputFile = android.OptionalPathForPath(compiler.path)
185 }
186 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700187 path, file := filepath.Split(compiler.path.ToMakePath().String())
Ivan Lozano022a73b2019-09-09 20:29:31 -0700188 stem, suffix, _ := android.SplitFileExt(file)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
Colin Crossff6c33d2019-10-02 16:01:35 -0700190 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
192 })
193}