Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 1 | // Copyright (C) 2020 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 linkerconfig |
| 16 | |
| 17 | import ( |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 18 | "sort" |
| 19 | "strings" |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint/proptools" |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
| 24 | "android/soong/cc" |
| 25 | "android/soong/etc" |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | pctx = android.NewPackageContext("android/soong/linkerconfig") |
| 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config") |
Paul Duffin | 2a2fb66 | 2021-03-29 01:14:55 +0100 | [diff] [blame] | 34 | registerLinkerConfigBuildComponent(android.InitRegistrationContext) |
| 35 | } |
| 36 | |
| 37 | func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) { |
Liz Kammer | 45faf8f | 2022-06-02 16:00:54 -0400 | [diff] [blame] | 38 | ctx.RegisterModuleType("linker_config", LinkerConfigFactory) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type linkerConfigProperties struct { |
| 42 | // source linker configuration property file |
| 43 | Src *string `android:"path"` |
| 44 | |
| 45 | // If set to true, allow module to be installed to one of the partitions. |
| 46 | // Default value is true. |
| 47 | // Installable should be marked as false for APEX configuration to avoid |
| 48 | // conflicts of configuration on /system/etc directory. |
| 49 | Installable *bool |
| 50 | } |
| 51 | |
| 52 | type linkerConfig struct { |
| 53 | android.ModuleBase |
| 54 | properties linkerConfigProperties |
| 55 | |
| 56 | outputFilePath android.OutputPath |
| 57 | installDirPath android.InstallPath |
| 58 | } |
| 59 | |
| 60 | // Implement PrebuiltEtcModule interface to fit in APEX prebuilt list. |
| 61 | var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil) |
| 62 | |
| 63 | func (l *linkerConfig) BaseDir() string { |
| 64 | return "etc" |
| 65 | } |
| 66 | |
| 67 | func (l *linkerConfig) SubDir() string { |
| 68 | return "" |
| 69 | } |
| 70 | |
| 71 | func (l *linkerConfig) OutputFile() android.OutputPath { |
| 72 | return l.outputFilePath |
| 73 | } |
| 74 | |
| 75 | func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 76 | input := android.PathForModuleSrc(ctx, android.String(l.properties.Src)) |
| 77 | output := android.PathForModuleOut(ctx, "linker.config.pb").OutputPath |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 78 | |
Cole Faust | fee2701 | 2024-12-13 14:10:31 -0800 | [diff] [blame] | 79 | BuildLinkerConfig(ctx, android.Paths{input}, nil, nil, output) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 80 | |
| 81 | l.outputFilePath = output |
| 82 | l.installDirPath = android.PathForModuleInstall(ctx, "etc") |
Jooyung Han | c6a91ec | 2021-04-15 04:41:13 +0900 | [diff] [blame] | 83 | if !proptools.BoolDefault(l.properties.Installable, true) { |
| 84 | l.SkipInstall() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 85 | } |
Jooyung Han | c6a91ec | 2021-04-15 04:41:13 +0900 | [diff] [blame] | 86 | ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath) |
mrziwang | e2346b8 | 2024-06-10 15:09:45 -0700 | [diff] [blame] | 87 | |
| 88 | ctx.SetOutputFiles(android.Paths{l.outputFilePath}, "") |
Yu Liu | 0a37d42 | 2025-02-13 02:05:00 +0000 | [diff] [blame] | 89 | |
| 90 | etc.SetCommonPrebuiltEtcInfo(ctx, l) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Cole Faust | fee2701 | 2024-12-13 14:10:31 -0800 | [diff] [blame] | 93 | func BuildLinkerConfig( |
| 94 | ctx android.ModuleContext, |
| 95 | inputs android.Paths, |
Yu Liu | 68a70b7 | 2025-01-08 22:54:44 +0000 | [diff] [blame] | 96 | provideModules []android.ModuleProxy, |
| 97 | requireModules []android.ModuleProxy, |
Cole Faust | fee2701 | 2024-12-13 14:10:31 -0800 | [diff] [blame] | 98 | output android.WritablePath, |
| 99 | ) { |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 100 | // First, convert the input json to protobuf format |
Cole Faust | fee2701 | 2024-12-13 14:10:31 -0800 | [diff] [blame] | 101 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 102 | interimOutput := android.PathForModuleOut(ctx, "temp.pb") |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 103 | cmd := builder.Command(). |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 104 | BuiltTool("conv_linker_config"). |
| 105 | Flag("proto"). |
Spandan Das | 9263188 | 2024-10-28 22:49:38 +0000 | [diff] [blame] | 106 | Flag("--force") |
| 107 | for _, input := range inputs { |
| 108 | cmd.FlagWithInput("-s ", input) |
| 109 | } |
| 110 | cmd.FlagWithOutput("-o ", interimOutput) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 111 | |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 112 | // Secondly, if there's provideLibs gathered from provideModules, append them |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 113 | var provideLibs []string |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 114 | for _, m := range provideModules { |
Yu Liu | 68a70b7 | 2025-01-08 22:54:44 +0000 | [diff] [blame] | 115 | ccInfo, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider) |
| 116 | if ok && (cc.IsStubTarget(android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider)) || ccInfo.HasLlndkStubs) { |
Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 117 | for _, ps := range android.OtherModuleProviderOrDefault( |
Yu Liu | 68a70b7 | 2025-01-08 22:54:44 +0000 | [diff] [blame] | 118 | ctx, m, android.InstallFilesProvider).PackagingSpecs { |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 119 | provideLibs = append(provideLibs, ps.FileName()) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | provideLibs = android.FirstUniqueStrings(provideLibs) |
| 124 | sort.Strings(provideLibs) |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 125 | |
| 126 | var requireLibs []string |
| 127 | for _, m := range requireModules { |
Yu Liu | 68a70b7 | 2025-01-08 22:54:44 +0000 | [diff] [blame] | 128 | if _, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider); ok { |
| 129 | if android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider).HasStubsVariants && |
| 130 | !android.OtherModuleProviderOrDefault(ctx, m, android.CommonModuleInfoKey).Host { |
| 131 | name := ctx.OtherModuleName(m) |
| 132 | if ccInfo, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider); ok && ccInfo.LinkerInfo != nil && ccInfo.LinkerInfo.ImplementationModuleName != nil { |
| 133 | name = *ccInfo.LinkerInfo.ImplementationModuleName |
| 134 | } |
| 135 | requireLibs = append(requireLibs, name+".so") |
| 136 | } |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | requireLibs = android.FirstUniqueStrings(requireLibs) |
| 141 | sort.Strings(requireLibs) |
| 142 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 143 | if len(provideLibs) > 0 { |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 144 | prevOutput := interimOutput |
| 145 | interimOutput = android.PathForModuleOut(ctx, "temp_provideLibs.pb") |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 146 | builder.Command(). |
| 147 | BuiltTool("conv_linker_config"). |
| 148 | Flag("append"). |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 149 | FlagWithInput("-s ", prevOutput). |
| 150 | FlagWithOutput("-o ", interimOutput). |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 151 | FlagWithArg("--key ", "provideLibs"). |
| 152 | FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(provideLibs, " "))) |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 153 | builder.Temporary(prevOutput) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 154 | } |
Kiyoung Kim | ee599d6 | 2024-03-22 18:00:40 +0900 | [diff] [blame] | 155 | if len(requireLibs) > 0 { |
| 156 | prevOutput := interimOutput |
| 157 | interimOutput = android.PathForModuleOut(ctx, "temp_requireLibs.pb") |
| 158 | builder.Command(). |
| 159 | BuiltTool("conv_linker_config"). |
| 160 | Flag("append"). |
| 161 | FlagWithInput("-s ", prevOutput). |
| 162 | FlagWithOutput("-o ", interimOutput). |
| 163 | FlagWithArg("--key ", "requireLibs"). |
| 164 | FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(requireLibs, " "))) |
| 165 | builder.Temporary(prevOutput) |
| 166 | } |
| 167 | |
| 168 | // cp to the final output |
| 169 | builder.Command().Text("cp").Input(interimOutput).Output(output) |
| 170 | |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 171 | builder.Temporary(interimOutput) |
| 172 | builder.DeleteTemporaryFiles() |
Cole Faust | fee2701 | 2024-12-13 14:10:31 -0800 | [diff] [blame] | 173 | builder.Build("conv_linker_config_"+output.String(), "Generate linker config protobuf "+output.String()) |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 176 | // linker_config generates protobuf file from json file. This protobuf file will be used from |
| 177 | // linkerconfig while generating ld.config.txt. Format of this file can be found from |
Elliott Hughes | 1036316 | 2024-01-09 22:02:03 +0000 | [diff] [blame] | 178 | // https://android.googlesource.com/platform/system/linkerconfig/+/main/README.md |
Liz Kammer | 45faf8f | 2022-06-02 16:00:54 -0400 | [diff] [blame] | 179 | func LinkerConfigFactory() android.Module { |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 180 | m := &linkerConfig{} |
| 181 | m.AddProperties(&m.properties) |
| 182 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst) |
| 183 | return m |
| 184 | } |
| 185 | |
| 186 | func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries { |
| 187 | installable := proptools.BoolDefault(l.properties.Installable, true) |
| 188 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 189 | Class: "ETC", |
| 190 | OutputFile: android.OptionalPathForPath(l.outputFilePath), |
| 191 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 192 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 193 | entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.String()) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 194 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base()) |
| 195 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 196 | }, |
| 197 | }, |
| 198 | }} |
| 199 | } |