blob: 0e2bea340ade1bf2ada0ebaf2c01a8cf4c9eaacc [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",
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 }
78
79 mod.subAndroidMk(&ret, mod.compiler)
80
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070081 ret.SubName += mod.Properties.SubName
82
Ivan Lozanoffee3342019-08-27 12:03:00 -070083 return ret
84}
85
86func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
87 ctx.subAndroidMk(ret, binary.baseCompiler)
88
89 ret.Class = "EXECUTABLES"
90 ret.DistFile = binary.distFile
91 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
92 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", binary.unstrippedOutputFile.String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040093 if binary.coverageOutputZipFile.Valid() {
94 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+binary.coverageOutputZipFile.String())
95 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070096 })
97}
98
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070099func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700100 test.binaryDecorator.AndroidMk(ctx, ret)
Chih-Hung Hsieh15f369e2019-11-15 04:14:44 -0800101 ret.Class = "NATIVE_TESTS"
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800102 ret.SubName = test.getMutatedModuleSubName(ctx.Name())
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700103 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
104 if len(test.Properties.Test_suites) > 0 {
105 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
106 strings.Join(test.Properties.Test_suites, " "))
107 }
108 if test.testConfig != nil {
109 fmt.Fprintln(w, "LOCAL_FULL_TEST_CONFIG :=", test.testConfig.String())
110 }
Dan Shi2468d012020-01-06 15:47:57 -0800111 if !BoolDefault(test.Properties.Auto_gen_config, true) {
112 fmt.Fprintln(w, "LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG := true")
113 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700114 })
115 // TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700116}
117
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
119 ctx.subAndroidMk(ret, library.baseCompiler)
120
121 if library.rlib() {
122 ret.Class = "RLIB_LIBRARIES"
123 } else if library.dylib() {
124 ret.Class = "DYLIB_LIBRARIES"
Ivan Lozano52767be2019-10-18 14:49:46 -0700125 } else if library.static() {
126 ret.Class = "STATIC_LIBRARIES"
127 } else if library.shared() {
128 ret.Class = "SHARED_LIBRARIES"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700130
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131 ret.DistFile = library.distFile
132 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
133 if !library.rlib() {
134 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", library.unstrippedOutputFile.String())
135 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400136 if library.coverageOutputZipFile.Valid() {
137 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE := "+library.coverageOutputZipFile.String())
138 }
139
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 })
141}
142
143func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
144 ctx.subAndroidMk(ret, procMacro.baseCompiler)
145
146 ret.Class = "PROC_MACRO_LIBRARIES"
147 ret.DistFile = procMacro.distFile
148
149}
150
151func (compiler *baseCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
152 // Soong installation is only supported for host modules. Have Make
153 // installation trigger Soong installation.
154 if ctx.Target().Os.Class == android.Host {
155 ret.OutputFile = android.OptionalPathForPath(compiler.path)
156 }
157 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700158 path, file := filepath.Split(compiler.path.ToMakePath().String())
Ivan Lozano022a73b2019-09-09 20:29:31 -0700159 stem, suffix, _ := android.SplitFileExt(file)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
Colin Crossff6c33d2019-10-02 16:01:35 -0700161 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
163 })
164}