blob: f2cbc65bd2a0ba943cdfa8dee2b57e7a63568e0f [file] [log] [blame]
Spandan Dasc32e0462024-11-05 17:55:39 +00001// Copyright 2024 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 android
16
17import (
Cole Faust996b5222024-11-06 10:32:06 -080018 "github.com/google/blueprint"
Spandan Dasc32e0462024-11-05 17:55:39 +000019 "github.com/google/blueprint/proptools"
20)
21
Cole Faust996b5222024-11-06 10:32:06 -080022var (
23 mergeAndRemoveComments = pctx.AndroidStaticRule("merge_and_remove_comments",
24 blueprint.RuleParams{
25 Command: "cat $in | grep -v '#' > $out",
26 },
27 )
28 androidInfoTxtToProp = pctx.AndroidStaticRule("android_info_txt_to_prop",
29 blueprint.RuleParams{
30 Command: "grep 'require version-' $in | sed -e 's/require version-/ro.build.expect./g' > $out",
31 },
32 )
33)
34
Spandan Dasc32e0462024-11-05 17:55:39 +000035type androidInfoProperties struct {
36 // Name of output file. Defaults to module name
37 Stem *string
38
39 // Paths of board-info.txt files.
40 Board_info_files []string `android:"path"`
41
42 // Name of bootloader board. If board_info_files is empty, `board={bootloader_board_name}` will
43 // be printed to output. Ignored if board_info_files is not empty.
44 Bootloader_board_name *string
45}
46
47type androidInfoModule struct {
48 ModuleBase
49
50 properties androidInfoProperties
51}
52
53func (p *androidInfoModule) GenerateAndroidBuildActions(ctx ModuleContext) {
54 if len(p.properties.Board_info_files) > 0 && p.properties.Bootloader_board_name != nil {
55 ctx.ModuleErrorf("Either Board_info_files or Bootloader_board_name should be set. Please remove one of them\n")
56 return
57 }
Cole Faust996b5222024-11-06 10:32:06 -080058 androidInfoTxtName := proptools.StringDefault(p.properties.Stem, ctx.ModuleName()+".txt")
59 androidInfoTxt := PathForModuleOut(ctx, androidInfoTxtName)
Spandan Dasc32e0462024-11-05 17:55:39 +000060 androidInfoProp := androidInfoTxt.ReplaceExtension(ctx, "prop")
Jihoon Kangd56da322025-01-08 01:57:24 +000061 timestamp := PathForModuleOut(ctx, "timestamp")
Spandan Dasc32e0462024-11-05 17:55:39 +000062
Spandan Dasc32e0462024-11-05 17:55:39 +000063 if boardInfoFiles := PathsForModuleSrc(ctx, p.properties.Board_info_files); len(boardInfoFiles) > 0 {
Cole Faust996b5222024-11-06 10:32:06 -080064 ctx.Build(pctx, BuildParams{
Jihoon Kangd56da322025-01-08 01:57:24 +000065 Rule: mergeAndRemoveComments,
66 Inputs: boardInfoFiles,
67 Output: androidInfoTxt,
68 Validation: timestamp,
Cole Faust996b5222024-11-06 10:32:06 -080069 })
Spandan Dasc32e0462024-11-05 17:55:39 +000070 } else if bootloaderBoardName := proptools.String(p.properties.Bootloader_board_name); bootloaderBoardName != "" {
Jihoon Kangd56da322025-01-08 01:57:24 +000071 WriteFileRule(ctx, androidInfoTxt, "board="+bootloaderBoardName, timestamp)
Spandan Dasc32e0462024-11-05 17:55:39 +000072 } else {
Jihoon Kangd56da322025-01-08 01:57:24 +000073 WriteFileRule(ctx, androidInfoTxt, "", timestamp)
Spandan Dasc32e0462024-11-05 17:55:39 +000074 }
75
Spandan Dasc32e0462024-11-05 17:55:39 +000076 // Create android_info.prop
Cole Faust996b5222024-11-06 10:32:06 -080077 ctx.Build(pctx, BuildParams{
78 Rule: androidInfoTxtToProp,
79 Input: androidInfoTxt,
80 Output: androidInfoProp,
81 })
Spandan Dasc32e0462024-11-05 17:55:39 +000082
83 ctx.SetOutputFiles(Paths{androidInfoProp}, "")
Jihoon Kangd56da322025-01-08 01:57:24 +000084
85 builder := NewRuleBuilder(pctx, ctx)
86 builder.Command().Text("touch").Output(timestamp)
87 if !ctx.Config().KatiEnabled() {
88 cpPath := PathForModuleInPartitionInstall(ctx, "").Join(ctx, androidInfoTxtName)
89 builder.Command().
90 Text("rsync").
91 Flag("-a").
92 Input(androidInfoTxt).
93 Text(cpPath.String())
94 }
95 builder.Build("copy_android_info", "Copy android-info.txt")
Spandan Dasc32e0462024-11-05 17:55:39 +000096}
97
98// android_info module generate a file named android-info.txt that contains various information
99// about the device we're building for. This file is typically packaged up with everything else.
100func AndroidInfoFactory() Module {
101 module := &androidInfoModule{}
102 module.AddProperties(&module.properties)
Jihoon Kangd56da322025-01-08 01:57:24 +0000103 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Spandan Dasc32e0462024-11-05 17:55:39 +0000104 return module
105}