blob: 241cac6917e7eaa99e5da7dcb4eeeb780da730ac [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 (
18 "android/soong/android"
19 "android/soong/etc"
Jooyung Hana0436a32021-04-15 05:11:00 +090020 "fmt"
Kiyoung Kim62abd122020-10-06 17:16:44 +090021
22 "github.com/google/blueprint/proptools"
23)
24
25var (
26 pctx = android.NewPackageContext("android/soong/linkerconfig")
27)
28
29func init() {
30 pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
Paul Duffin2a2fb662021-03-29 01:14:55 +010031 registerLinkerConfigBuildComponent(android.InitRegistrationContext)
32}
33
34func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("linker_config", linkerConfigFactory)
Kiyoung Kim62abd122020-10-06 17:16:44 +090036}
37
38type linkerConfigProperties struct {
39 // source linker configuration property file
40 Src *string `android:"path"`
41
42 // If set to true, allow module to be installed to one of the partitions.
43 // Default value is true.
44 // Installable should be marked as false for APEX configuration to avoid
45 // conflicts of configuration on /system/etc directory.
46 Installable *bool
47}
48
49type linkerConfig struct {
50 android.ModuleBase
51 properties linkerConfigProperties
52
53 outputFilePath android.OutputPath
54 installDirPath android.InstallPath
55}
56
57// Implement PrebuiltEtcModule interface to fit in APEX prebuilt list.
58var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
59
60func (l *linkerConfig) BaseDir() string {
61 return "etc"
62}
63
64func (l *linkerConfig) SubDir() string {
65 return ""
66}
67
68func (l *linkerConfig) OutputFile() android.OutputPath {
69 return l.outputFilePath
70}
71
Jooyung Hana0436a32021-04-15 05:11:00 +090072var _ android.OutputFileProducer = (*linkerConfig)(nil)
73
74func (l *linkerConfig) OutputFiles(tag string) (android.Paths, error) {
75 switch tag {
76 case "":
77 return android.Paths{l.outputFilePath}, nil
78 default:
79 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
80 }
81}
82
Kiyoung Kim62abd122020-10-06 17:16:44 +090083func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
84 inputFile := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
85 l.outputFilePath = android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
86 l.installDirPath = android.PathForModuleInstall(ctx, "etc")
Colin Crossf1a035e2020-11-16 17:32:30 -080087 linkerConfigRule := android.NewRuleBuilder(pctx, ctx)
Kiyoung Kim62abd122020-10-06 17:16:44 +090088 linkerConfigRule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080089 BuiltTool("conv_linker_config").
Kiyoung Kim62abd122020-10-06 17:16:44 +090090 Flag("proto").
91 FlagWithInput("-s ", inputFile).
92 FlagWithOutput("-o ", l.outputFilePath)
Colin Crossf1a035e2020-11-16 17:32:30 -080093 linkerConfigRule.Build("conv_linker_config",
Kiyoung Kim62abd122020-10-06 17:16:44 +090094 "Generate linker config protobuf "+l.outputFilePath.String())
95
Jooyung Hanc6a91ec2021-04-15 04:41:13 +090096 if !proptools.BoolDefault(l.properties.Installable, true) {
97 l.SkipInstall()
Kiyoung Kim62abd122020-10-06 17:16:44 +090098 }
Jooyung Hanc6a91ec2021-04-15 04:41:13 +090099 ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
Kiyoung Kim62abd122020-10-06 17:16:44 +0900100}
101
102// linker_config generates protobuf file from json file. This protobuf file will be used from
103// linkerconfig while generating ld.config.txt. Format of this file can be found from
104// https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md
105func linkerConfigFactory() android.Module {
106 m := &linkerConfig{}
107 m.AddProperties(&m.properties)
108 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
109 return m
110}
111
112func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries {
113 installable := proptools.BoolDefault(l.properties.Installable, true)
114 return []android.AndroidMkEntries{android.AndroidMkEntries{
115 Class: "ETC",
116 OutputFile: android.OptionalPathForPath(l.outputFilePath),
117 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700118 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Kiyoung Kim62abd122020-10-06 17:16:44 +0900119 entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.ToMakePath().String())
120 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base())
121 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900122 entries.SetString("LINKER_CONFIG_PATH_"+l.Name(), l.OutputFile().String())
Kiyoung Kim62abd122020-10-06 17:16:44 +0900123 },
124 },
125 }}
126}