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" |
| 20 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | pctx = android.NewPackageContext("android/soong/linkerconfig") |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config") |
Paul Duffin | 2a2fb66 | 2021-03-29 01:14:55 +0100 | [diff] [blame] | 30 | registerLinkerConfigBuildComponent(android.InitRegistrationContext) |
| 31 | } |
| 32 | |
| 33 | func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) { |
| 34 | ctx.RegisterModuleType("linker_config", linkerConfigFactory) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | type linkerConfigProperties struct { |
| 38 | // source linker configuration property file |
| 39 | Src *string `android:"path"` |
| 40 | |
| 41 | // If set to true, allow module to be installed to one of the partitions. |
| 42 | // Default value is true. |
| 43 | // Installable should be marked as false for APEX configuration to avoid |
| 44 | // conflicts of configuration on /system/etc directory. |
| 45 | Installable *bool |
| 46 | } |
| 47 | |
| 48 | type linkerConfig struct { |
| 49 | android.ModuleBase |
| 50 | properties linkerConfigProperties |
| 51 | |
| 52 | outputFilePath android.OutputPath |
| 53 | installDirPath android.InstallPath |
| 54 | } |
| 55 | |
| 56 | // Implement PrebuiltEtcModule interface to fit in APEX prebuilt list. |
| 57 | var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil) |
| 58 | |
| 59 | func (l *linkerConfig) BaseDir() string { |
| 60 | return "etc" |
| 61 | } |
| 62 | |
| 63 | func (l *linkerConfig) SubDir() string { |
| 64 | return "" |
| 65 | } |
| 66 | |
| 67 | func (l *linkerConfig) OutputFile() android.OutputPath { |
| 68 | return l.outputFilePath |
| 69 | } |
| 70 | |
| 71 | func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 72 | inputFile := android.PathForModuleSrc(ctx, android.String(l.properties.Src)) |
| 73 | l.outputFilePath = android.PathForModuleOut(ctx, "linker.config.pb").OutputPath |
| 74 | l.installDirPath = android.PathForModuleInstall(ctx, "etc") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 75 | linkerConfigRule := android.NewRuleBuilder(pctx, ctx) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 76 | linkerConfigRule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 77 | BuiltTool("conv_linker_config"). |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 78 | Flag("proto"). |
| 79 | FlagWithInput("-s ", inputFile). |
| 80 | FlagWithOutput("-o ", l.outputFilePath) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 81 | linkerConfigRule.Build("conv_linker_config", |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 82 | "Generate linker config protobuf "+l.outputFilePath.String()) |
| 83 | |
| 84 | if proptools.BoolDefault(l.properties.Installable, true) { |
| 85 | ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // linker_config generates protobuf file from json file. This protobuf file will be used from |
| 90 | // linkerconfig while generating ld.config.txt. Format of this file can be found from |
| 91 | // https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md |
| 92 | func linkerConfigFactory() android.Module { |
| 93 | m := &linkerConfig{} |
| 94 | m.AddProperties(&m.properties) |
| 95 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst) |
| 96 | return m |
| 97 | } |
| 98 | |
| 99 | func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries { |
| 100 | installable := proptools.BoolDefault(l.properties.Installable, true) |
| 101 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 102 | Class: "ETC", |
| 103 | OutputFile: android.OptionalPathForPath(l.outputFilePath), |
| 104 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 105 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 106 | entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.ToMakePath().String()) |
| 107 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base()) |
| 108 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable) |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 109 | entries.SetString("LINKER_CONFIG_PATH_"+l.Name(), l.OutputFile().String()) |
Kiyoung Kim | 62abd12 | 2020-10-06 17:16:44 +0900 | [diff] [blame] | 110 | }, |
| 111 | }, |
| 112 | }} |
| 113 | } |