Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame^] | 1 | // Copyright (C) 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "path/filepath" |
| 21 | "strings" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | "android/soong/cc" |
| 25 | "android/soong/java" |
| 26 | |
| 27 | "github.com/google/blueprint/proptools" |
| 28 | ) |
| 29 | |
| 30 | func (a *apexBundle) AndroidMk() android.AndroidMkData { |
| 31 | if a.properties.HideFromMake { |
| 32 | return android.AndroidMkData{ |
| 33 | Disabled: true, |
| 34 | } |
| 35 | } |
| 36 | writers := []android.AndroidMkData{} |
| 37 | writers = append(writers, a.androidMkForType()) |
| 38 | return android.AndroidMkData{ |
| 39 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 40 | for _, data := range writers { |
| 41 | data.Custom(w, name, prefix, moduleDir, data) |
| 42 | } |
| 43 | }} |
| 44 | } |
| 45 | |
| 46 | func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string { |
| 47 | moduleNames := []string{} |
| 48 | apexType := a.properties.ApexType |
| 49 | // To avoid creating duplicate build rules, run this function only when primaryApexType is true |
| 50 | // to install symbol files in $(PRODUCT_OUT}/apex. |
| 51 | // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex. |
| 52 | if !a.primaryApexType && apexType != flattenedApex { |
| 53 | return moduleNames |
| 54 | } |
| 55 | |
| 56 | for _, fi := range a.filesInfo { |
| 57 | if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | if !android.InList(fi.moduleName, moduleNames) { |
| 62 | moduleNames = append(moduleNames, fi.moduleName) |
| 63 | } |
| 64 | |
| 65 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 66 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 67 | fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName) |
| 68 | // /apex/<apex_name>/{lib|framework|...} |
| 69 | pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir) |
| 70 | if apexType == flattenedApex { |
| 71 | // /system/apex/<name>/{lib|framework|...} |
| 72 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(), |
| 73 | apexName, fi.installDir)) |
| 74 | if a.primaryApexType { |
| 75 | fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated) |
| 76 | } |
| 77 | if len(fi.symlinks) > 0 { |
| 78 | fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " ")) |
| 79 | } |
| 80 | |
| 81 | if fi.module != nil && fi.module.NoticeFile().Valid() { |
| 82 | fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String()) |
| 83 | } |
| 84 | } else { |
| 85 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated) |
| 86 | } |
| 87 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String()) |
| 88 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake()) |
| 89 | if fi.module != nil { |
| 90 | archStr := fi.module.Target().Arch.ArchType.String() |
| 91 | host := false |
| 92 | switch fi.module.Target().Os.Class { |
| 93 | case android.Host: |
| 94 | if fi.module.Target().Arch.ArchType != android.Common { |
| 95 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
| 96 | } |
| 97 | host = true |
| 98 | case android.HostCross: |
| 99 | if fi.module.Target().Arch.ArchType != android.Common { |
| 100 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
| 101 | } |
| 102 | host = true |
| 103 | case android.Device: |
| 104 | if fi.module.Target().Arch.ArchType != android.Common { |
| 105 | fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
| 106 | } |
| 107 | } |
| 108 | if host { |
| 109 | makeOs := fi.module.Target().Os.String() |
| 110 | if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic { |
| 111 | makeOs = "linux" |
| 112 | } |
| 113 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs) |
| 114 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 115 | } |
| 116 | } |
| 117 | if fi.class == javaSharedLib { |
| 118 | javaModule := fi.module.(*java.Library) |
| 119 | // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore |
| 120 | // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise |
| 121 | // we will have foo.jar.jar |
| 122 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar")) |
| 123 | fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String()) |
| 124 | fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String()) |
| 125 | fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String()) |
| 126 | fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false") |
| 127 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk") |
| 128 | } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest { |
| 129 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base()) |
| 130 | if cc, ok := fi.module.(*cc.Module); ok { |
| 131 | if cc.UnstrippedOutputFile() != nil { |
| 132 | fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String()) |
| 133 | } |
| 134 | cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w) |
| 135 | if cc.CoverageOutputFile().Valid() { |
| 136 | fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String()) |
| 137 | } |
| 138 | } |
| 139 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk") |
| 140 | } else { |
| 141 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base()) |
| 142 | // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex |
| 143 | if a.primaryApexType && fi.builtFile == a.manifestPbOut && len(a.compatSymlinks) > 0 { |
| 144 | fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && ")) |
| 145 | } |
| 146 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 147 | } |
| 148 | } |
| 149 | return moduleNames |
| 150 | } |
| 151 | |
| 152 | func (a *apexBundle) androidMkForType() android.AndroidMkData { |
| 153 | return android.AndroidMkData{ |
| 154 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 155 | moduleNames := []string{} |
| 156 | apexType := a.properties.ApexType |
| 157 | if a.installable() { |
| 158 | apexName := proptools.StringDefault(a.properties.Apex_name, name) |
| 159 | moduleNames = a.androidMkForFiles(w, apexName, moduleDir) |
| 160 | } |
| 161 | |
| 162 | if apexType == flattenedApex { |
| 163 | // Only image APEXes can be flattened. |
| 164 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 165 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 166 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) |
| 167 | if len(moduleNames) > 0 { |
| 168 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " ")) |
| 169 | } |
| 170 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 171 | fmt.Fprintln(w, "$(LOCAL_INSTALLED_MODULE): .KATI_IMPLICIT_OUTPUTS :=", a.outputFile.String()) |
| 172 | |
| 173 | } else { |
| 174 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 175 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 176 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) |
| 177 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class? |
| 178 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String()) |
| 179 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String()) |
| 180 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix()) |
| 181 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable()) |
| 182 | if len(moduleNames) > 0 { |
| 183 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " ")) |
| 184 | } |
| 185 | if len(a.externalDeps) > 0 { |
| 186 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " ")) |
| 187 | } |
| 188 | var postInstallCommands []string |
| 189 | if a.prebuiltFileToDelete != "" { |
| 190 | postInstallCommands = append(postInstallCommands, "rm -rf "+ |
| 191 | filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete)) |
| 192 | } |
| 193 | // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD |
| 194 | postInstallCommands = append(postInstallCommands, a.compatSymlinks...) |
| 195 | if len(postInstallCommands) > 0 { |
| 196 | fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && ")) |
| 197 | } |
| 198 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 199 | |
| 200 | if apexType == imageApex { |
| 201 | fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String()) |
| 202 | } |
| 203 | } |
| 204 | }} |
| 205 | } |