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 ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/etc" |
Jooyung Han | a0436a3 | 2021-04-15 05:11:00 +0900 | [diff] [blame] | 20 | "fmt" |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | pctx = android.NewPackageContext("android/soong/linkerconfig") |
| 27 | ) |
| 28 | |
| 29 | func init() { |
| 30 | pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config") |
Paul Duffin | 2a2fb66 | 2021-03-29 01:14:55 +0100 | [diff] [blame] | 31 | registerLinkerConfigBuildComponent(android.InitRegistrationContext) |
| 32 | } |
| 33 | |
| 34 | func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) { |
| 35 | ctx.RegisterModuleType("linker_config", linkerConfigFactory) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type 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 | |
| 49 | type 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. |
| 58 | var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil) |
| 59 | |
| 60 | func (l *linkerConfig) BaseDir() string { |
| 61 | return "etc" |
| 62 | } |
| 63 | |
| 64 | func (l *linkerConfig) SubDir() string { |
| 65 | return "" |
| 66 | } |
| 67 | |
| 68 | func (l *linkerConfig) OutputFile() android.OutputPath { |
| 69 | return l.outputFilePath |
| 70 | } |
| 71 | |
Jooyung Han | a0436a3 | 2021-04-15 05:11:00 +0900 | [diff] [blame] | 72 | var _ android.OutputFileProducer = (*linkerConfig)(nil) |
| 73 | |
| 74 | func (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 Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 83 | func (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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 87 | linkerConfigRule := android.NewRuleBuilder(pctx, ctx) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 88 | linkerConfigRule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 89 | BuiltTool("conv_linker_config"). |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 90 | Flag("proto"). |
| 91 | FlagWithInput("-s ", inputFile). |
| 92 | FlagWithOutput("-o ", l.outputFilePath) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 93 | linkerConfigRule.Build("conv_linker_config", |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 94 | "Generate linker config protobuf "+l.outputFilePath.String()) |
| 95 | |
Jooyung Han | c6a91ec | 2021-04-15 04:41:13 +0900 | [diff] [blame] | 96 | if !proptools.BoolDefault(l.properties.Installable, true) { |
| 97 | l.SkipInstall() |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 98 | } |
Jooyung Han | c6a91ec | 2021-04-15 04:41:13 +0900 | [diff] [blame] | 99 | ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 100 | } |
| 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 |
| 105 | func linkerConfigFactory() android.Module { |
| 106 | m := &linkerConfig{} |
| 107 | m.AddProperties(&m.properties) |
| 108 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst) |
| 109 | return m |
| 110 | } |
| 111 | |
| 112 | func (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 Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 118 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 119 | 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 Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 122 | entries.SetString("LINKER_CONFIG_PATH_"+l.Name(), l.OutputFile().String()) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 123 | }, |
| 124 | }, |
| 125 | }} |
| 126 | } |