lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
package configs
 
import (
   "strings"
   "fmt"
   "os"
 
   "github.com/google/blueprint"
   "github.com/google/blueprint/proptools"
 
   "android/soong/android"
)
 
var (
   pctx = android.NewPackageContext("android/soong/kernel/configs")
 
   kconfigXmlFixupRule = pctx.AndroidStaticRule("kconfig_xml_fixup", blueprint.RuleParams{
       Command:     `${kconfigXmlFixupCmd} --input ${in} --output-version ${outputVersion} --output-matrix ${out}`,
       CommandDeps:  []string{"${kconfigXmlFixupCmd}"},
       Description: "kconfig_xml_fixup ${in}",
   }, "outputVersion")
 
   assembleVintfRule = pctx.AndroidStaticRule("assemble_vintf", blueprint.RuleParams{
       Command:     `${assembleVintfCmd} ${flags} -i ${in} -o ${out}`,
       CommandDeps:  []string{"${assembleVintfCmd}"},
       Description: "assemble_vintf -i ${in}",
   }, "flags")
)
 
type KernelConfigProperties struct {
   // list of source files that should be named "android-base.config" for common requirements
   // and "android-base-foo.config" for requirements on condition CONFIG_FOO=y.
   Srcs []string
 
   // metadata XML file that contains minlts and complex conditional requirements.
   Meta *string
}
 
type KernelConfigRule struct {
   android.ModuleBase
   properties KernelConfigProperties
 
   outputPath android.WritablePath
}
 
func init() {
   pctx.HostBinToolVariable("assembleVintfCmd", "assemble_vintf")
   pctx.HostBinToolVariable("kconfigXmlFixupCmd", "kconfig_xml_fixup")
   android.RegisterModuleType("kernel_config", kernelConfigFactory)
}
 
func kernelConfigFactory() android.Module {
   g := &KernelConfigRule{}
   g.AddProperties(&g.properties)
   android.InitAndroidModule(g)
   return g
}
 
func (g *KernelConfigRule) OutputPath() android.Path {
   return g.outputPath
}
 
func (g *KernelConfigRule) DepsMutator(ctx android.BottomUpMutatorContext) {
   android.ExtractSourcesDeps(ctx, g.properties.Srcs)
   android.ExtractSourceDeps(ctx, g.properties.Meta)
}
 
func (g *KernelConfigRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
   g.outputPath = android.PathForModuleOut(ctx, "matrix.xml")
   genVersion := android.PathForModuleGen(ctx, "version.txt")
   genConditionals := android.PathForModuleGen(ctx, "conditional.xml")
   inputMeta := android.PathForModuleSrc(ctx, proptools.String(g.properties.Meta))
 
   var prop_path android.Path
   var plat_srcs string
   var plat_path string
   var platform  string
 
   platform = ctx.Config().VendorConfig("vendor").String("platform")
   if len(platform) > 0 {
       for i := 0; i < len(g.properties.Srcs); i++ {
           prop_path = android.PathForModuleSrc(ctx, "")
           plat_srcs = platform + "/" + g.properties.Srcs[i]
           plat_path = fmt.Sprintf("%s/%s", prop_path, plat_srcs)
           _, err := os.Stat(plat_path)
           if err == nil {
               g.properties.Srcs[i] = plat_srcs
               fmt.Printf("kernel-config-check: Use %s reference file: %s\n", platform, plat_path)
           }
       }
   }
 
   if proptools.String(g.properties.Meta) == "" {
       ctx.PropertyErrorf("kernel_config", "Missing meta field")
   }
 
   ctx.Build(pctx, android.BuildParams{
       Rule:           kconfigXmlFixupRule,
       Description:    "Fixup kernel config meta",
       Input:          inputMeta,
       Output:         genConditionals,
       ImplicitOutput: genVersion,
       Args: map[string]string{
           "outputVersion": genVersion.String(),
       },
   })
 
   var kernelArg string
   inputConfigs := android.PathsForModuleSrc(ctx, g.properties.Srcs)
   implicitInputs := append(inputConfigs, genVersion)
   if len(inputConfigs) > 0 {
       kernelArg = "--kernel=$$(cat " + genVersion.String() + "):" +
           strings.Join(inputConfigs.Strings(), ":")
   }
 
   ctx.Build(pctx, android.BuildParams{
       Rule:        assembleVintfRule,
       Description: "Framework Compatibility Matrix kernel fragment",
       Input:       genConditionals,
       Implicits:   implicitInputs,
       Output:      g.outputPath,
       Args: map[string]string{
           "flags": kernelArg,
       },
   })
 
}