blob: dad5892566a53796ae56774d0e8db01d6ab7fc81 [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
Chris Parsons39a16972023-06-08 14:28:51 +000022 "android/soong/ui/metrics/bp2build_metrics_proto"
Kiyoung Kim62abd122020-10-06 17:16:44 +090023 "github.com/google/blueprint/proptools"
Jiyong Parkfa616132021-04-20 11:36:40 +090024
25 "android/soong/android"
Liz Kammer45faf8f2022-06-02 16:00:54 -040026 "android/soong/bazel"
Jiyong Parkfa616132021-04-20 11:36:40 +090027 "android/soong/cc"
28 "android/soong/etc"
Kiyoung Kim62abd122020-10-06 17:16:44 +090029)
30
31var (
32 pctx = android.NewPackageContext("android/soong/linkerconfig")
33)
34
35func init() {
36 pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
Paul Duffin2a2fb662021-03-29 01:14:55 +010037 registerLinkerConfigBuildComponent(android.InitRegistrationContext)
38}
39
40func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
Liz Kammer45faf8f2022-06-02 16:00:54 -040041 ctx.RegisterModuleType("linker_config", LinkerConfigFactory)
Kiyoung Kim62abd122020-10-06 17:16:44 +090042}
43
44type linkerConfigProperties struct {
45 // source linker configuration property file
46 Src *string `android:"path"`
47
48 // If set to true, allow module to be installed to one of the partitions.
49 // Default value is true.
50 // Installable should be marked as false for APEX configuration to avoid
51 // conflicts of configuration on /system/etc directory.
52 Installable *bool
53}
54
55type linkerConfig struct {
56 android.ModuleBase
Liz Kammer45faf8f2022-06-02 16:00:54 -040057 android.BazelModuleBase
Kiyoung Kim62abd122020-10-06 17:16:44 +090058 properties linkerConfigProperties
59
60 outputFilePath android.OutputPath
61 installDirPath android.InstallPath
62}
63
64// Implement PrebuiltEtcModule interface to fit in APEX prebuilt list.
65var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
66
67func (l *linkerConfig) BaseDir() string {
68 return "etc"
69}
70
71func (l *linkerConfig) SubDir() string {
72 return ""
73}
74
75func (l *linkerConfig) OutputFile() android.OutputPath {
76 return l.outputFilePath
77}
78
Jooyung Hana0436a32021-04-15 05:11:00 +090079var _ android.OutputFileProducer = (*linkerConfig)(nil)
80
81func (l *linkerConfig) OutputFiles(tag string) (android.Paths, error) {
82 switch tag {
83 case "":
84 return android.Paths{l.outputFilePath}, nil
85 default:
86 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
87 }
88}
89
Kiyoung Kim62abd122020-10-06 17:16:44 +090090func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkfa616132021-04-20 11:36:40 +090091 input := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
92 output := android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
Kiyoung Kim62abd122020-10-06 17:16:44 +090093
Jiyong Parkfa616132021-04-20 11:36:40 +090094 builder := android.NewRuleBuilder(pctx, ctx)
95 BuildLinkerConfig(ctx, builder, input, nil, output)
96 builder.Build("conv_linker_config", "Generate linker config protobuf "+output.String())
97
98 l.outputFilePath = output
99 l.installDirPath = android.PathForModuleInstall(ctx, "etc")
Jooyung Hanc6a91ec2021-04-15 04:41:13 +0900100 if !proptools.BoolDefault(l.properties.Installable, true) {
101 l.SkipInstall()
Kiyoung Kim62abd122020-10-06 17:16:44 +0900102 }
Jooyung Hanc6a91ec2021-04-15 04:41:13 +0900103 ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900104}
105
Liz Kammer45faf8f2022-06-02 16:00:54 -0400106type linkerConfigAttributes struct {
107 Src bazel.LabelAttribute
108}
109
Chris Parsons637458d2023-09-19 20:09:00 +0000110func (l *linkerConfig) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammer45faf8f2022-06-02 16:00:54 -0400111 if l.properties.Src == nil {
112 ctx.PropertyErrorf("src", "empty src is not supported")
Chris Parsons39a16972023-06-08 14:28:51 +0000113 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
Liz Kammer45faf8f2022-06-02 16:00:54 -0400114 return
115 }
116 src := android.BazelLabelForModuleSrcSingle(ctx, *l.properties.Src)
117 targetModuleProperties := bazel.BazelTargetModuleProperties{
118 Rule_class: "linker_config",
119 Bzl_load_location: "//build/bazel/rules:linker_config.bzl",
120 }
121 ctx.CreateBazelTargetModule(
122 targetModuleProperties,
123 android.CommonAttributes{Name: l.Name()},
124 &linkerConfigAttributes{
125 Src: bazel.LabelAttribute{Value: &src},
126 })
127}
128
Jiyong Parkfa616132021-04-20 11:36:40 +0900129func BuildLinkerConfig(ctx android.ModuleContext, builder *android.RuleBuilder,
130 input android.Path, otherModules []android.Module, output android.OutputPath) {
131
132 // First, convert the input json to protobuf format
133 interimOutput := android.PathForModuleOut(ctx, "temp.pb")
134 builder.Command().
135 BuiltTool("conv_linker_config").
136 Flag("proto").
137 FlagWithInput("-s ", input).
138 FlagWithOutput("-o ", interimOutput)
139
140 // Secondly, if there's provideLibs gathered from otherModules, append them
141 var provideLibs []string
142 for _, m := range otherModules {
143 if c, ok := m.(*cc.Module); ok && cc.IsStubTarget(c) {
144 for _, ps := range c.PackagingSpecs() {
145 provideLibs = append(provideLibs, ps.FileName())
146 }
147 }
148 }
149 provideLibs = android.FirstUniqueStrings(provideLibs)
150 sort.Strings(provideLibs)
151 if len(provideLibs) > 0 {
152 builder.Command().
153 BuiltTool("conv_linker_config").
154 Flag("append").
155 FlagWithInput("-s ", interimOutput).
156 FlagWithOutput("-o ", output).
157 FlagWithArg("--key ", "provideLibs").
158 FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(provideLibs, " ")))
159 } else {
160 // If nothing to add, just cp to the final output
161 builder.Command().Text("cp").Input(interimOutput).Output(output)
162 }
163 builder.Temporary(interimOutput)
164 builder.DeleteTemporaryFiles()
165}
166
Kiyoung Kim62abd122020-10-06 17:16:44 +0900167// linker_config generates protobuf file from json file. This protobuf file will be used from
168// linkerconfig while generating ld.config.txt. Format of this file can be found from
169// https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md
Liz Kammer45faf8f2022-06-02 16:00:54 -0400170func LinkerConfigFactory() android.Module {
Kiyoung Kim62abd122020-10-06 17:16:44 +0900171 m := &linkerConfig{}
172 m.AddProperties(&m.properties)
173 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
Liz Kammer45faf8f2022-06-02 16:00:54 -0400174 android.InitBazelModule(m)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900175 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}