blob: e9da6fabe6a844fcf09328fcd494787198f4731b [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 (
Ivan Lozanoffee3342019-08-27 12:03:00 -070018 "path/filepath"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019
20 "android/soong/android"
21)
22
23type AndroidMkContext interface {
24 Name() string
25 Target() android.Target
Liz Kammer4018a8d2020-11-24 09:35:21 -080026 SubAndroidMk(*android.AndroidMkEntries, interface{})
Ivan Lozanoffee3342019-08-27 12:03:00 -070027}
28
Andrei Homescuc7767922020-08-05 06:36:19 -070029type SubAndroidMkProvider interface {
Liz Kammer4018a8d2020-11-24 09:35:21 -080030 AndroidMk(AndroidMkContext, *android.AndroidMkEntries)
Ivan Lozanoffee3342019-08-27 12:03:00 -070031}
32
Liz Kammer4018a8d2020-11-24 09:35:21 -080033func (mod *Module) SubAndroidMk(data *android.AndroidMkEntries, obj interface{}) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070034 if mod.subAndroidMkOnce == nil {
Andrei Homescuc7767922020-08-05 06:36:19 -070035 mod.subAndroidMkOnce = make(map[SubAndroidMkProvider]bool)
Ivan Lozanoffee3342019-08-27 12:03:00 -070036 }
Andrei Homescuc7767922020-08-05 06:36:19 -070037 if androidmk, ok := obj.(SubAndroidMkProvider); ok {
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 if !mod.subAndroidMkOnce[androidmk] {
39 mod.subAndroidMkOnce[androidmk] = true
40 androidmk.AndroidMk(mod, data)
41 }
42 }
43}
44
Liz Kammer4018a8d2020-11-24 09:35:21 -080045func (mod *Module) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park99644e92020-11-17 22:21:02 +090046 if mod.Properties.HideFromMake || mod.hideApexVariantFromMake {
Liz Kammer4018a8d2020-11-24 09:35:21 -080047
48 return []android.AndroidMkEntries{android.AndroidMkEntries{Disabled: true}}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040049 }
50
Liz Kammer4018a8d2020-11-24 09:35:21 -080051 ret := android.AndroidMkEntries{
Ivan Lozanoffee3342019-08-27 12:03:00 -070052 OutputFile: mod.outputFile,
53 Include: "$(BUILD_SYSTEM)/soong_rust_prebuilt.mk",
Liz Kammer4018a8d2020-11-24 09:35:21 -080054 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
55 func(entries *android.AndroidMkEntries) {
56 entries.AddStrings("LOCAL_RLIB_LIBRARIES", mod.Properties.AndroidMkRlibs...)
57 entries.AddStrings("LOCAL_DYLIB_LIBRARIES", mod.Properties.AndroidMkDylibs...)
58 entries.AddStrings("LOCAL_PROC_MACRO_LIBRARIES", mod.Properties.AndroidMkProcMacroLibs...)
59 entries.AddStrings("LOCAL_SHARED_LIBRARIES", mod.Properties.AndroidMkSharedLibs...)
60 entries.AddStrings("LOCAL_STATIC_LIBRARIES", mod.Properties.AndroidMkStaticLibs...)
Ivan Lozano6a884432020-12-02 09:15:16 -050061 entries.AddStrings("LOCAL_SOONG_LINK_TYPE", mod.makeLinkType)
Ivan Lozanoffee3342019-08-27 12:03:00 -070062 },
63 },
64 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040065
66 if mod.compiler != nil && !mod.compiler.Disabled() {
Andrei Homescuc7767922020-08-05 06:36:19 -070067 mod.SubAndroidMk(&ret, mod.compiler)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040068 } else if mod.sourceProvider != nil {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040069 // If the compiler is disabled, this is a SourceProvider.
Andrei Homescuc7767922020-08-05 06:36:19 -070070 mod.SubAndroidMk(&ret, mod.sourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -040071 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070072 ret.SubName += mod.Properties.SubName
73
Liz Kammer4018a8d2020-11-24 09:35:21 -080074 return []android.AndroidMkEntries{ret}
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
Liz Kammer4018a8d2020-11-24 09:35:21 -080077func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -070078 ctx.SubAndroidMk(ret, binary.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -070079
Jingwen Chen40fd90a2020-06-15 05:24:19 +000080 if binary.distFile.Valid() {
81 ret.DistFiles = android.MakeDefaultDistFiles(binary.distFile.Path())
82 }
83
Ivan Lozanoffee3342019-08-27 12:03:00 -070084 ret.Class = "EXECUTABLES"
Liz Kammer4018a8d2020-11-24 09:35:21 -080085 ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
86 entries.SetOptionalPath("LOCAL_PREBUILT_COVERAGE_ARCHIVE", binary.coverageOutputZipFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 })
88}
89
Liz Kammer4018a8d2020-11-24 09:35:21 -080090func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070091 test.binaryDecorator.AndroidMk(ctx, ret)
Chih-Hung Hsieh15f369e2019-11-15 04:14:44 -080092 ret.Class = "NATIVE_TESTS"
Liz Kammer4018a8d2020-11-24 09:35:21 -080093 ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
Liz Kammer57f5b332020-11-24 12:42:58 -080094 entries.AddCompatibilityTestSuites(test.Properties.Test_suites...)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070095 if test.testConfig != nil {
Liz Kammer4018a8d2020-11-24 09:35:21 -080096 entries.SetString("LOCAL_FULL_TEST_CONFIG", test.testConfig.String())
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070097 }
Liz Kammer4018a8d2020-11-24 09:35:21 -080098 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(test.Properties.Auto_gen_config, true))
99 entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", Bool(test.Properties.Test_options.Unit_test))
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700100 })
101 // TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700102}
103
Liz Kammer4018a8d2020-11-24 09:35:21 -0800104func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700105 ctx.SubAndroidMk(ret, library.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106
107 if library.rlib() {
108 ret.Class = "RLIB_LIBRARIES"
109 } else if library.dylib() {
110 ret.Class = "DYLIB_LIBRARIES"
Ivan Lozano52767be2019-10-18 14:49:46 -0700111 } else if library.static() {
112 ret.Class = "STATIC_LIBRARIES"
113 } else if library.shared() {
114 ret.Class = "SHARED_LIBRARIES"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700116
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000117 if library.distFile.Valid() {
118 ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path())
119 }
120
Liz Kammer4018a8d2020-11-24 09:35:21 -0800121 ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
122 entries.SetOptionalPath("LOCAL_PREBUILT_COVERAGE_ARCHIVE", library.coverageOutputZipFile)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123 })
124}
125
Liz Kammer4018a8d2020-11-24 09:35:21 -0800126func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700127 ctx.SubAndroidMk(ret, procMacro.baseCompiler)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128
129 ret.Class = "PROC_MACRO_LIBRARIES"
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000130 if procMacro.distFile.Valid() {
131 ret.DistFiles = android.MakeDefaultDistFiles(procMacro.distFile.Path())
132 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133
134}
135
Liz Kammer4018a8d2020-11-24 09:35:21 -0800136func (sourceProvider *BaseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700137 outFile := sourceProvider.OutputFiles[0]
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400138 ret.Class = "ETC"
139 ret.OutputFile = android.OptionalPathForPath(outFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400140 ret.SubName += sourceProvider.subName
Liz Kammer4018a8d2020-11-24 09:35:21 -0800141 ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400142 _, file := filepath.Split(outFile.String())
143 stem, suffix, _ := android.SplitFileExt(file)
Liz Kammer4018a8d2020-11-24 09:35:21 -0800144 entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
145 entries.SetString("LOCAL_MODULE_STEM", stem)
146 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400147 })
148}
149
Liz Kammer4018a8d2020-11-24 09:35:21 -0800150func (bindgen *bindgenDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Andrei Homescuc7767922020-08-05 06:36:19 -0700151 ctx.SubAndroidMk(ret, bindgen.BaseSourceProvider)
Treehugger Robot588aae72020-08-21 10:01:58 +0000152}
153
Liz Kammer4018a8d2020-11-24 09:35:21 -0800154func (proto *protobufDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Treehugger Robot588aae72020-08-21 10:01:58 +0000155 ctx.SubAndroidMk(ret, proto.BaseSourceProvider)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156}
157
Liz Kammer4018a8d2020-11-24 09:35:21 -0800158func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
Ivan Lozano2b081132020-09-08 12:46:52 -0400159 if compiler.path == (android.InstallPath{}) {
160 return
161 }
162
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200163 var unstrippedOutputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164 // Soong installation is only supported for host modules. Have Make
165 // installation trigger Soong installation.
166 if ctx.Target().Os.Class == android.Host {
167 ret.OutputFile = android.OptionalPathForPath(compiler.path)
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200168 } else if compiler.strippedOutputFile.Valid() {
169 unstrippedOutputFile = ret.OutputFile
170 ret.OutputFile = compiler.strippedOutputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700171 }
Liz Kammer4018a8d2020-11-24 09:35:21 -0800172 ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
173 entries.SetOptionalPath("LOCAL_SOONG_UNSTRIPPED_BINARY", unstrippedOutputFile)
Colin Crossff6c33d2019-10-02 16:01:35 -0700174 path, file := filepath.Split(compiler.path.ToMakePath().String())
Ivan Lozano022a73b2019-09-09 20:29:31 -0700175 stem, suffix, _ := android.SplitFileExt(file)
Liz Kammer4018a8d2020-11-24 09:35:21 -0800176 entries.SetString("LOCAL_MODULE_SUFFIX", suffix)
177 entries.SetString("LOCAL_MODULE_PATH", path)
178 entries.SetString("LOCAL_MODULE_STEM", stem)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179 })
180}