blob: 4f1ef9d666fc81377c5a928fa57e7093822708c5 [file] [log] [blame]
Kiyoung Kim62abd122020-10-06 17:16:44 +09001// 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
15package linkerconfig
16
17import (
Jiyong Parkfa616132021-04-20 11:36:40 +090018 "sort"
19 "strings"
Kiyoung Kim62abd122020-10-06 17:16:44 +090020
21 "github.com/google/blueprint/proptools"
Jiyong Parkfa616132021-04-20 11:36:40 +090022
23 "android/soong/android"
24 "android/soong/cc"
25 "android/soong/etc"
Kiyoung Kim62abd122020-10-06 17:16:44 +090026)
27
28var (
29 pctx = android.NewPackageContext("android/soong/linkerconfig")
30)
31
32func init() {
33 pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
Paul Duffin2a2fb662021-03-29 01:14:55 +010034 registerLinkerConfigBuildComponent(android.InitRegistrationContext)
35}
36
37func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
Liz Kammer45faf8f2022-06-02 16:00:54 -040038 ctx.RegisterModuleType("linker_config", LinkerConfigFactory)
Kiyoung Kim62abd122020-10-06 17:16:44 +090039}
40
41type 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
52type 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.
61var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
62
63func (l *linkerConfig) BaseDir() string {
64 return "etc"
65}
66
67func (l *linkerConfig) SubDir() string {
68 return ""
69}
70
71func (l *linkerConfig) OutputFile() android.OutputPath {
72 return l.outputFilePath
73}
74
75func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkfa616132021-04-20 11:36:40 +090076 input := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
77 output := android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
Kiyoung Kim62abd122020-10-06 17:16:44 +090078
Cole Faustfee27012024-12-13 14:10:31 -080079 BuildLinkerConfig(ctx, android.Paths{input}, nil, nil, output)
Jiyong Parkfa616132021-04-20 11:36:40 +090080
81 l.outputFilePath = output
82 l.installDirPath = android.PathForModuleInstall(ctx, "etc")
Jooyung Hanc6a91ec2021-04-15 04:41:13 +090083 if !proptools.BoolDefault(l.properties.Installable, true) {
84 l.SkipInstall()
Kiyoung Kim62abd122020-10-06 17:16:44 +090085 }
Jooyung Hanc6a91ec2021-04-15 04:41:13 +090086 ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -070087
88 ctx.SetOutputFiles(android.Paths{l.outputFilePath}, "")
Kiyoung Kim62abd122020-10-06 17:16:44 +090089}
90
Cole Faustfee27012024-12-13 14:10:31 -080091func BuildLinkerConfig(
92 ctx android.ModuleContext,
93 inputs android.Paths,
Yu Liu68a70b72025-01-08 22:54:44 +000094 provideModules []android.ModuleProxy,
95 requireModules []android.ModuleProxy,
Cole Faustfee27012024-12-13 14:10:31 -080096 output android.WritablePath,
97) {
Jiyong Parkfa616132021-04-20 11:36:40 +090098 // First, convert the input json to protobuf format
Cole Faustfee27012024-12-13 14:10:31 -080099 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Parkfa616132021-04-20 11:36:40 +0900100 interimOutput := android.PathForModuleOut(ctx, "temp.pb")
Spandan Das92631882024-10-28 22:49:38 +0000101 cmd := builder.Command().
Jiyong Parkfa616132021-04-20 11:36:40 +0900102 BuiltTool("conv_linker_config").
103 Flag("proto").
Spandan Das92631882024-10-28 22:49:38 +0000104 Flag("--force")
105 for _, input := range inputs {
106 cmd.FlagWithInput("-s ", input)
107 }
108 cmd.FlagWithOutput("-o ", interimOutput)
Jiyong Parkfa616132021-04-20 11:36:40 +0900109
Kiyoung Kimee599d62024-03-22 18:00:40 +0900110 // Secondly, if there's provideLibs gathered from provideModules, append them
Jiyong Parkfa616132021-04-20 11:36:40 +0900111 var provideLibs []string
Kiyoung Kimee599d62024-03-22 18:00:40 +0900112 for _, m := range provideModules {
Yu Liu68a70b72025-01-08 22:54:44 +0000113 ccInfo, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider)
114 if ok && (cc.IsStubTarget(android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider)) || ccInfo.HasLlndkStubs) {
Yu Liud46e5ae2024-08-15 18:46:17 +0000115 for _, ps := range android.OtherModuleProviderOrDefault(
Yu Liu68a70b72025-01-08 22:54:44 +0000116 ctx, m, android.InstallFilesProvider).PackagingSpecs {
Jiyong Parkfa616132021-04-20 11:36:40 +0900117 provideLibs = append(provideLibs, ps.FileName())
118 }
119 }
120 }
121 provideLibs = android.FirstUniqueStrings(provideLibs)
122 sort.Strings(provideLibs)
Kiyoung Kimee599d62024-03-22 18:00:40 +0900123
124 var requireLibs []string
125 for _, m := range requireModules {
Yu Liu68a70b72025-01-08 22:54:44 +0000126 if _, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider); ok {
127 if android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider).HasStubsVariants &&
128 !android.OtherModuleProviderOrDefault(ctx, m, android.CommonModuleInfoKey).Host {
129 name := ctx.OtherModuleName(m)
130 if ccInfo, ok := android.OtherModuleProvider(ctx, m, cc.CcInfoProvider); ok && ccInfo.LinkerInfo != nil && ccInfo.LinkerInfo.ImplementationModuleName != nil {
131 name = *ccInfo.LinkerInfo.ImplementationModuleName
132 }
133 requireLibs = append(requireLibs, name+".so")
134 }
Kiyoung Kimee599d62024-03-22 18:00:40 +0900135 }
136 }
137
138 requireLibs = android.FirstUniqueStrings(requireLibs)
139 sort.Strings(requireLibs)
140
Jiyong Parkfa616132021-04-20 11:36:40 +0900141 if len(provideLibs) > 0 {
Kiyoung Kimee599d62024-03-22 18:00:40 +0900142 prevOutput := interimOutput
143 interimOutput = android.PathForModuleOut(ctx, "temp_provideLibs.pb")
Jiyong Parkfa616132021-04-20 11:36:40 +0900144 builder.Command().
145 BuiltTool("conv_linker_config").
146 Flag("append").
Kiyoung Kimee599d62024-03-22 18:00:40 +0900147 FlagWithInput("-s ", prevOutput).
148 FlagWithOutput("-o ", interimOutput).
Jiyong Parkfa616132021-04-20 11:36:40 +0900149 FlagWithArg("--key ", "provideLibs").
150 FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(provideLibs, " ")))
Kiyoung Kimee599d62024-03-22 18:00:40 +0900151 builder.Temporary(prevOutput)
Jiyong Parkfa616132021-04-20 11:36:40 +0900152 }
Kiyoung Kimee599d62024-03-22 18:00:40 +0900153 if len(requireLibs) > 0 {
154 prevOutput := interimOutput
155 interimOutput = android.PathForModuleOut(ctx, "temp_requireLibs.pb")
156 builder.Command().
157 BuiltTool("conv_linker_config").
158 Flag("append").
159 FlagWithInput("-s ", prevOutput).
160 FlagWithOutput("-o ", interimOutput).
161 FlagWithArg("--key ", "requireLibs").
162 FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(requireLibs, " ")))
163 builder.Temporary(prevOutput)
164 }
165
166 // cp to the final output
167 builder.Command().Text("cp").Input(interimOutput).Output(output)
168
Jiyong Parkfa616132021-04-20 11:36:40 +0900169 builder.Temporary(interimOutput)
170 builder.DeleteTemporaryFiles()
Cole Faustfee27012024-12-13 14:10:31 -0800171 builder.Build("conv_linker_config_"+output.String(), "Generate linker config protobuf "+output.String())
Jiyong Parkfa616132021-04-20 11:36:40 +0900172}
173
Kiyoung Kim62abd122020-10-06 17:16:44 +0900174// linker_config generates protobuf file from json file. This protobuf file will be used from
175// linkerconfig while generating ld.config.txt. Format of this file can be found from
Elliott Hughes10363162024-01-09 22:02:03 +0000176// https://android.googlesource.com/platform/system/linkerconfig/+/main/README.md
Liz Kammer45faf8f2022-06-02 16:00:54 -0400177func LinkerConfigFactory() android.Module {
Kiyoung Kim62abd122020-10-06 17:16:44 +0900178 m := &linkerConfig{}
179 m.AddProperties(&m.properties)
180 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
181 return m
182}
183
184func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries {
185 installable := proptools.BoolDefault(l.properties.Installable, true)
186 return []android.AndroidMkEntries{android.AndroidMkEntries{
187 Class: "ETC",
188 OutputFile: android.OptionalPathForPath(l.outputFilePath),
189 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700190 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800191 entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.String())
Kiyoung Kim62abd122020-10-06 17:16:44 +0900192 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base())
193 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900194 },
195 },
196 }}
197}