blob: 98aa40805bceb8a6c14d830f9ec8537f547c103c [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 (
Jooyung Hana0436a32021-04-15 05:11:00 +090018 "fmt"
Jiyong Parkfa616132021-04-20 11:36:40 +090019 "sort"
20 "strings"
Kiyoung Kim62abd122020-10-06 17:16:44 +090021
22 "github.com/google/blueprint/proptools"
Jiyong Parkfa616132021-04-20 11:36:40 +090023
24 "android/soong/android"
25 "android/soong/cc"
26 "android/soong/etc"
Kiyoung Kim62abd122020-10-06 17:16:44 +090027)
28
29var (
30 pctx = android.NewPackageContext("android/soong/linkerconfig")
31)
32
33func init() {
34 pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
Paul Duffin2a2fb662021-03-29 01:14:55 +010035 registerLinkerConfigBuildComponent(android.InitRegistrationContext)
36}
37
38func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
Liz Kammer45faf8f2022-06-02 16:00:54 -040039 ctx.RegisterModuleType("linker_config", LinkerConfigFactory)
Kiyoung Kim62abd122020-10-06 17:16:44 +090040}
41
42type linkerConfigProperties struct {
43 // source linker configuration property file
44 Src *string `android:"path"`
45
46 // If set to true, allow module to be installed to one of the partitions.
47 // Default value is true.
48 // Installable should be marked as false for APEX configuration to avoid
49 // conflicts of configuration on /system/etc directory.
50 Installable *bool
51}
52
53type linkerConfig struct {
54 android.ModuleBase
55 properties linkerConfigProperties
56
57 outputFilePath android.OutputPath
58 installDirPath android.InstallPath
59}
60
61// Implement PrebuiltEtcModule interface to fit in APEX prebuilt list.
62var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
63
64func (l *linkerConfig) BaseDir() string {
65 return "etc"
66}
67
68func (l *linkerConfig) SubDir() string {
69 return ""
70}
71
72func (l *linkerConfig) OutputFile() android.OutputPath {
73 return l.outputFilePath
74}
75
Jooyung Hana0436a32021-04-15 05:11:00 +090076var _ android.OutputFileProducer = (*linkerConfig)(nil)
77
78func (l *linkerConfig) OutputFiles(tag string) (android.Paths, error) {
79 switch tag {
80 case "":
81 return android.Paths{l.outputFilePath}, nil
82 default:
83 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
84 }
85}
86
Kiyoung Kim62abd122020-10-06 17:16:44 +090087func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkfa616132021-04-20 11:36:40 +090088 input := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
89 output := android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
Kiyoung Kim62abd122020-10-06 17:16:44 +090090
Jiyong Parkfa616132021-04-20 11:36:40 +090091 builder := android.NewRuleBuilder(pctx, ctx)
Kiyoung Kimee599d62024-03-22 18:00:40 +090092 BuildLinkerConfig(ctx, builder, input, nil, nil, output)
Jiyong Parkfa616132021-04-20 11:36:40 +090093 builder.Build("conv_linker_config", "Generate linker config protobuf "+output.String())
94
95 l.outputFilePath = output
96 l.installDirPath = android.PathForModuleInstall(ctx, "etc")
Jooyung Hanc6a91ec2021-04-15 04:41:13 +090097 if !proptools.BoolDefault(l.properties.Installable, true) {
98 l.SkipInstall()
Kiyoung Kim62abd122020-10-06 17:16:44 +090099 }
Jooyung Hanc6a91ec2021-04-15 04:41:13 +0900100 ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900101}
102
Jiyong Parkfa616132021-04-20 11:36:40 +0900103func BuildLinkerConfig(ctx android.ModuleContext, builder *android.RuleBuilder,
Kiyoung Kimee599d62024-03-22 18:00:40 +0900104 input android.Path, provideModules []android.Module, requireModules []android.Module, output android.OutputPath) {
Jiyong Parkfa616132021-04-20 11:36:40 +0900105
106 // First, convert the input json to protobuf format
107 interimOutput := android.PathForModuleOut(ctx, "temp.pb")
108 builder.Command().
109 BuiltTool("conv_linker_config").
110 Flag("proto").
111 FlagWithInput("-s ", input).
112 FlagWithOutput("-o ", interimOutput)
113
Kiyoung Kimee599d62024-03-22 18:00:40 +0900114 // Secondly, if there's provideLibs gathered from provideModules, append them
Jiyong Parkfa616132021-04-20 11:36:40 +0900115 var provideLibs []string
Kiyoung Kimee599d62024-03-22 18:00:40 +0900116 for _, m := range provideModules {
Jiyong Parkfa616132021-04-20 11:36:40 +0900117 if c, ok := m.(*cc.Module); ok && cc.IsStubTarget(c) {
118 for _, ps := range c.PackagingSpecs() {
119 provideLibs = append(provideLibs, ps.FileName())
120 }
121 }
122 }
123 provideLibs = android.FirstUniqueStrings(provideLibs)
124 sort.Strings(provideLibs)
Kiyoung Kimee599d62024-03-22 18:00:40 +0900125
126 var requireLibs []string
127 for _, m := range requireModules {
128 if c, ok := m.(*cc.Module); ok && c.HasStubsVariants() && !c.Host() {
129 requireLibs = append(requireLibs, c.ImplementationModuleName(ctx)+".so")
130 }
131 }
132
133 requireLibs = android.FirstUniqueStrings(requireLibs)
134 sort.Strings(requireLibs)
135
Jiyong Parkfa616132021-04-20 11:36:40 +0900136 if len(provideLibs) > 0 {
Kiyoung Kimee599d62024-03-22 18:00:40 +0900137 prevOutput := interimOutput
138 interimOutput = android.PathForModuleOut(ctx, "temp_provideLibs.pb")
Jiyong Parkfa616132021-04-20 11:36:40 +0900139 builder.Command().
140 BuiltTool("conv_linker_config").
141 Flag("append").
Kiyoung Kimee599d62024-03-22 18:00:40 +0900142 FlagWithInput("-s ", prevOutput).
143 FlagWithOutput("-o ", interimOutput).
Jiyong Parkfa616132021-04-20 11:36:40 +0900144 FlagWithArg("--key ", "provideLibs").
145 FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(provideLibs, " ")))
Kiyoung Kimee599d62024-03-22 18:00:40 +0900146 builder.Temporary(prevOutput)
Jiyong Parkfa616132021-04-20 11:36:40 +0900147 }
Kiyoung Kimee599d62024-03-22 18:00:40 +0900148 if len(requireLibs) > 0 {
149 prevOutput := interimOutput
150 interimOutput = android.PathForModuleOut(ctx, "temp_requireLibs.pb")
151 builder.Command().
152 BuiltTool("conv_linker_config").
153 Flag("append").
154 FlagWithInput("-s ", prevOutput).
155 FlagWithOutput("-o ", interimOutput).
156 FlagWithArg("--key ", "requireLibs").
157 FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(requireLibs, " ")))
158 builder.Temporary(prevOutput)
159 }
160
161 // cp to the final output
162 builder.Command().Text("cp").Input(interimOutput).Output(output)
163
Jiyong Parkfa616132021-04-20 11:36:40 +0900164 builder.Temporary(interimOutput)
165 builder.DeleteTemporaryFiles()
166}
167
Kiyoung Kim62abd122020-10-06 17:16:44 +0900168// linker_config generates protobuf file from json file. This protobuf file will be used from
169// linkerconfig while generating ld.config.txt. Format of this file can be found from
Elliott Hughes10363162024-01-09 22:02:03 +0000170// https://android.googlesource.com/platform/system/linkerconfig/+/main/README.md
Liz Kammer45faf8f2022-06-02 16:00:54 -0400171func LinkerConfigFactory() android.Module {
Kiyoung Kim62abd122020-10-06 17:16:44 +0900172 m := &linkerConfig{}
173 m.AddProperties(&m.properties)
174 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
175 return m
176}
177
178func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries {
179 installable := proptools.BoolDefault(l.properties.Installable, true)
180 return []android.AndroidMkEntries{android.AndroidMkEntries{
181 Class: "ETC",
182 OutputFile: android.OptionalPathForPath(l.outputFilePath),
183 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700184 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800185 entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.String())
Kiyoung Kim62abd122020-10-06 17:16:44 +0900186 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base())
187 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900188 },
189 },
190 }}
191}