xref: /aosp_15_r20/build/soong/filesystem/bootconfig.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2024 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage filesystem
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
19*333d2b36SAndroid Build Coastguard Worker	"strings"
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
22*333d2b36SAndroid Build Coastguard Worker)
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Workerfunc init() {
25*333d2b36SAndroid Build Coastguard Worker	android.RegisterModuleType("bootconfig", BootconfigModuleFactory)
26*333d2b36SAndroid Build Coastguard Worker	pctx.Import("android/soong/android")
27*333d2b36SAndroid Build Coastguard Worker}
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Workertype bootconfigProperty struct {
30*333d2b36SAndroid Build Coastguard Worker	// List of bootconfig parameters that will be written as a line separated list in the output
31*333d2b36SAndroid Build Coastguard Worker	// file.
32*333d2b36SAndroid Build Coastguard Worker	Boot_config []string
33*333d2b36SAndroid Build Coastguard Worker	// Path to the file that contains the list of bootconfig parameters. This will be appended
34*333d2b36SAndroid Build Coastguard Worker	// to the output file, after the entries in boot_config.
35*333d2b36SAndroid Build Coastguard Worker	Boot_config_file *string `android:"path"`
36*333d2b36SAndroid Build Coastguard Worker}
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Workertype BootconfigModule struct {
39*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Worker	properties bootconfigProperty
42*333d2b36SAndroid Build Coastguard Worker}
43*333d2b36SAndroid Build Coastguard Worker
44*333d2b36SAndroid Build Coastguard Worker// bootconfig module generates the `vendor-bootconfig.img` file, which lists the bootconfig
45*333d2b36SAndroid Build Coastguard Worker// parameters and can be passed as a `--vendor_bootconfig` value in mkbootimg invocation.
46*333d2b36SAndroid Build Coastguard Workerfunc BootconfigModuleFactory() android.Module {
47*333d2b36SAndroid Build Coastguard Worker	module := &BootconfigModule{}
48*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&module.properties)
49*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
50*333d2b36SAndroid Build Coastguard Worker	return module
51*333d2b36SAndroid Build Coastguard Worker}
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Workerfunc (m *BootconfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
54*333d2b36SAndroid Build Coastguard Worker	bootConfig := m.properties.Boot_config
55*333d2b36SAndroid Build Coastguard Worker	bootConfigFileStr := proptools.String(m.properties.Boot_config_file)
56*333d2b36SAndroid Build Coastguard Worker	if len(bootConfig) == 0 && len(bootConfigFileStr) == 0 {
57*333d2b36SAndroid Build Coastguard Worker		return
58*333d2b36SAndroid Build Coastguard Worker	}
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Worker	var bootConfigFile android.Path
61*333d2b36SAndroid Build Coastguard Worker	if len(bootConfigFileStr) > 0 {
62*333d2b36SAndroid Build Coastguard Worker		bootConfigFile = android.PathForModuleSrc(ctx, bootConfigFileStr)
63*333d2b36SAndroid Build Coastguard Worker	}
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Worker	outputPath := android.PathForModuleOut(ctx, ctx.ModuleName(), "vendor-bootconfig.img")
66*333d2b36SAndroid Build Coastguard Worker	bootConfigOutput := android.PathForModuleOut(ctx, ctx.ModuleName(), "bootconfig.txt")
67*333d2b36SAndroid Build Coastguard Worker	android.WriteFileRule(ctx, bootConfigOutput, strings.Join(bootConfig, "\n"))
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker	bcFiles := android.Paths{bootConfigOutput}
70*333d2b36SAndroid Build Coastguard Worker	if bootConfigFile != nil {
71*333d2b36SAndroid Build Coastguard Worker		bcFiles = append(bcFiles, bootConfigFile)
72*333d2b36SAndroid Build Coastguard Worker	}
73*333d2b36SAndroid Build Coastguard Worker	ctx.Build(pctx, android.BuildParams{
74*333d2b36SAndroid Build Coastguard Worker		Rule:        android.Cat,
75*333d2b36SAndroid Build Coastguard Worker		Description: "concatenate bootconfig parameters",
76*333d2b36SAndroid Build Coastguard Worker		Inputs:      bcFiles,
77*333d2b36SAndroid Build Coastguard Worker		Output:      outputPath,
78*333d2b36SAndroid Build Coastguard Worker	})
79*333d2b36SAndroid Build Coastguard Worker	ctx.SetOutputFiles(android.Paths{outputPath}, "")
80*333d2b36SAndroid Build Coastguard Worker}
81