Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 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 cc |
| 16 | |
| 17 | import ( |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 19 | "io" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 26 | type AndroidMkContext interface { |
| 27 | Target() android.Target |
| 28 | } |
| 29 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 30 | func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) { |
Colin Cross | bc6fb16 | 2016-05-24 15:39:04 -0700 | [diff] [blame] | 31 | if c.Properties.HideFromMake { |
| 32 | ret.Disabled = true |
| 33 | return ret, nil |
| 34 | } |
| 35 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 36 | ret.OutputFile = c.outputFile |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 37 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 38 | fmt.Fprintln(w, "LOCAL_SANITIZE := never") |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 39 | if len(c.Properties.AndroidMkSharedLibs) > 0 { |
| 40 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " ")) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 41 | } |
Dan Willemsen | a96ff64 | 2016-06-07 12:34:45 -0700 | [diff] [blame] | 42 | if c.Target().Os == android.Android && c.Properties.Sdk_version != "" { |
| 43 | fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version) |
| 44 | fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none") |
| 45 | } else { |
| 46 | // These are already included in LOCAL_SHARED_LIBRARIES |
| 47 | fmt.Fprintln(w, "LOCAL_CXX_STL := none") |
| 48 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 49 | return nil |
| 50 | }) |
| 51 | |
| 52 | callSubAndroidMk := func(obj interface{}) { |
| 53 | if obj != nil { |
| 54 | if androidmk, ok := obj.(interface { |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 55 | AndroidMk(AndroidMkContext, *android.AndroidMkData) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 56 | }); ok { |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 57 | androidmk.AndroidMk(c, &ret) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | for _, feature := range c.features { |
| 63 | callSubAndroidMk(feature) |
| 64 | } |
| 65 | |
| 66 | callSubAndroidMk(c.compiler) |
| 67 | callSubAndroidMk(c.linker) |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 68 | if c.linker.installable() { |
| 69 | callSubAndroidMk(c.installer) |
| 70 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 71 | |
| 72 | return ret, nil |
| 73 | } |
| 74 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 75 | func (library *baseLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 76 | if library.static() { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 77 | ret.Class = "STATIC_LIBRARIES" |
| 78 | } else { |
| 79 | ret.Class = "SHARED_LIBRARIES" |
| 80 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 83 | func (library *libraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 84 | library.baseLinker.AndroidMk(ctx, ret) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 85 | |
Colin Cross | 7b5c22b | 2016-07-07 11:16:20 -0700 | [diff] [blame] | 86 | if !library.static() { |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 87 | library.stripper.AndroidMk(ctx, ret) |
Colin Cross | 7b5c22b | 2016-07-07 11:16:20 -0700 | [diff] [blame] | 88 | } |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 89 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 90 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | 3b1fffa | 2016-05-05 15:11:48 -0700 | [diff] [blame] | 91 | var exportedIncludes []string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 92 | for _, flag := range library.exportedFlags() { |
Dan Willemsen | 3b1fffa | 2016-05-05 15:11:48 -0700 | [diff] [blame] | 93 | if strings.HasPrefix(flag, "-I") { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 94 | exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I")) |
| 95 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 96 | } |
| 97 | if len(exportedIncludes) > 0 { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 98 | fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " ")) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 101 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext()) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 102 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 103 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 104 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 105 | return nil |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 106 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 109 | func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 110 | ret.Custom = func(w io.Writer, name, prefix string) error { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 111 | out := ret.OutputFile.Path() |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 112 | |
Dan Willemsen | 72d3993 | 2016-07-08 23:23:48 -0700 | [diff] [blame] | 113 | fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 114 | fmt.Fprintln(w, "\t$(copy-file-to-target)") |
| 115 | |
| 116 | return nil |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 117 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 120 | func (binary *binaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 121 | binary.stripper.AndroidMk(ctx, ret) |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 122 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 123 | ret.Class = "EXECUTABLES" |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 124 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 125 | fmt.Fprintln(w, "LOCAL_CXX_STL := none") |
| 126 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
Colin Cross | 3854a60 | 2016-01-11 12:49:11 -0800 | [diff] [blame] | 127 | if binary.static() { |
| 128 | fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true") |
| 129 | } |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 130 | return nil |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 131 | }) |
| 132 | } |
| 133 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 134 | func (test *testBinaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 135 | test.binaryLinker.AndroidMk(ctx, ret) |
Colin Cross | c7a38dc | 2016-07-12 13:13:09 -0700 | [diff] [blame] | 136 | if Bool(test.testLinker.Properties.Test_per_src) { |
Dan Albert | 6a04769 | 2016-07-18 17:24:47 -0700 | [diff] [blame^] | 137 | ret.SubName = "_" + test.binaryLinker.Properties.Stem |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 141 | func (library *toolchainLibraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 142 | library.baseLinker.AndroidMk(ctx, ret) |
Dan Willemsen | e7932e8 | 2016-05-16 15:56:53 -0700 | [diff] [blame] | 143 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 144 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | e7932e8 | 2016-05-16 15:56:53 -0700 | [diff] [blame] | 145 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext()) |
| 146 | fmt.Fprintln(w, "LOCAL_CXX_STL := none") |
| 147 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
| 148 | |
| 149 | return nil |
| 150 | }) |
| 151 | } |
| 152 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 153 | func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 154 | // Make only supports stripping target modules |
| 155 | if ctx.Target().Os != android.Android { |
| 156 | return |
| 157 | } |
| 158 | |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 159 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
| 160 | if stripper.StripProperties.Strip.None { |
| 161 | fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false") |
| 162 | } else if stripper.StripProperties.Strip.Keep_symbols { |
| 163 | fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols") |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 164 | } else { |
| 165 | fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info") |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return nil |
| 169 | }) |
| 170 | } |
| 171 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 172 | func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 173 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 174 | path := installer.path.RelPathString() |
Colin Cross | bf305de | 2016-03-29 17:32:06 -0700 | [diff] [blame] | 175 | dir, file := filepath.Split(path) |
| 176 | stem := strings.TrimSuffix(file, filepath.Ext(file)) |
Colin Cross | e14388b | 2016-05-03 14:08:40 -0700 | [diff] [blame] | 177 | fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir)) |
Colin Cross | bf305de | 2016-03-29 17:32:06 -0700 | [diff] [blame] | 178 | fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem) |
Colin Cross | 3854a60 | 2016-01-11 12:49:11 -0800 | [diff] [blame] | 179 | if len(installer.Properties.Symlinks) > 0 { |
| 180 | fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(installer.Properties.Symlinks, " ")) |
| 181 | } |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 182 | return nil |
| 183 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 184 | } |