xref: /aosp_15_r20/build/soong/android/vintf_data.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 android
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"fmt"
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 Workerconst (
25*333d2b36SAndroid Build Coastguard Worker	deviceCmType          = "device_cm"
26*333d2b36SAndroid Build Coastguard Worker	systemManifestType    = "system_manifest"
27*333d2b36SAndroid Build Coastguard Worker	productManifestType   = "product_manifest"
28*333d2b36SAndroid Build Coastguard Worker	systemExtManifestType = "system_ext_manifest"
29*333d2b36SAndroid Build Coastguard Worker	vendorManifestType    = "vendor_manifest"
30*333d2b36SAndroid Build Coastguard Worker	odmManifestType       = "odm_manifest"
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker	defaultDcm               = "system/libhidl/vintfdata/device_compatibility_matrix.default.xml"
33*333d2b36SAndroid Build Coastguard Worker	defaultSystemManifest    = "system/libhidl/vintfdata/manifest.xml"
34*333d2b36SAndroid Build Coastguard Worker	defaultSystemExtManifest = "system/libhidl/vintfdata/system_ext_manifest.default.xml"
35*333d2b36SAndroid Build Coastguard Worker)
36*333d2b36SAndroid Build Coastguard Worker
37*333d2b36SAndroid Build Coastguard Workertype vintfDataProperties struct {
38*333d2b36SAndroid Build Coastguard Worker	// Optional name for the installed file. If unspecified it will be manifest.xml by default.
39*333d2b36SAndroid Build Coastguard Worker	Filename *string
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Worker	// Type of the vintf data type, the allowed type are device_compatibility_matrix, system_manifest,
42*333d2b36SAndroid Build Coastguard Worker	// product_manifest, and system_ext_manifest.
43*333d2b36SAndroid Build Coastguard Worker	Type *string
44*333d2b36SAndroid Build Coastguard Worker}
45*333d2b36SAndroid Build Coastguard Worker
46*333d2b36SAndroid Build Coastguard Workertype vintfDataRule struct {
47*333d2b36SAndroid Build Coastguard Worker	ModuleBase
48*333d2b36SAndroid Build Coastguard Worker
49*333d2b36SAndroid Build Coastguard Worker	properties vintfDataProperties
50*333d2b36SAndroid Build Coastguard Worker
51*333d2b36SAndroid Build Coastguard Worker	installDirPath InstallPath
52*333d2b36SAndroid Build Coastguard Worker	outputFilePath Path
53*333d2b36SAndroid Build Coastguard Worker	noAction       bool
54*333d2b36SAndroid Build Coastguard Worker}
55*333d2b36SAndroid Build Coastguard Worker
56*333d2b36SAndroid Build Coastguard Workerfunc init() {
57*333d2b36SAndroid Build Coastguard Worker	registerVintfDataComponents(InitRegistrationContext)
58*333d2b36SAndroid Build Coastguard Worker}
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Workerfunc registerVintfDataComponents(ctx RegistrationContext) {
61*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("vintf_data", vintfDataFactory)
62*333d2b36SAndroid Build Coastguard Worker}
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Worker// vintf_fragment module processes vintf fragment file and installs under etc/vintf/manifest.
65*333d2b36SAndroid Build Coastguard Workerfunc vintfDataFactory() Module {
66*333d2b36SAndroid Build Coastguard Worker	m := &vintfDataRule{}
67*333d2b36SAndroid Build Coastguard Worker	m.AddProperties(
68*333d2b36SAndroid Build Coastguard Worker		&m.properties,
69*333d2b36SAndroid Build Coastguard Worker	)
70*333d2b36SAndroid Build Coastguard Worker	InitAndroidArchModule(m, DeviceSupported, MultilibFirst)
71*333d2b36SAndroid Build Coastguard Worker
72*333d2b36SAndroid Build Coastguard Worker	return m
73*333d2b36SAndroid Build Coastguard Worker}
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Workerfunc (m *vintfDataRule) GenerateAndroidBuildActions(ctx ModuleContext) {
76*333d2b36SAndroid Build Coastguard Worker	builder := NewRuleBuilder(pctx, ctx)
77*333d2b36SAndroid Build Coastguard Worker	gensrc := PathForModuleOut(ctx, "manifest.xml")
78*333d2b36SAndroid Build Coastguard Worker	assembleVintfEnvs := []string{}
79*333d2b36SAndroid Build Coastguard Worker	inputPaths := make(Paths, 0)
80*333d2b36SAndroid Build Coastguard Worker
81*333d2b36SAndroid Build Coastguard Worker	switch proptools.String(m.properties.Type) {
82*333d2b36SAndroid Build Coastguard Worker	case deviceCmType:
83*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, fmt.Sprintf("BOARD_SYSTEMSDK_VERSIONS=\"%s\"", strings.Join(ctx.DeviceConfig().SystemSdkVersions(), " ")))
84*333d2b36SAndroid Build Coastguard Worker
85*333d2b36SAndroid Build Coastguard Worker		deviceMatrixs := PathsForSource(ctx, ctx.Config().DeviceMatrixFile())
86*333d2b36SAndroid Build Coastguard Worker		if len(deviceMatrixs) > 0 {
87*333d2b36SAndroid Build Coastguard Worker			inputPaths = append(inputPaths, deviceMatrixs...)
88*333d2b36SAndroid Build Coastguard Worker		} else {
89*333d2b36SAndroid Build Coastguard Worker			inputPaths = append(inputPaths, PathForSource(ctx, defaultDcm))
90*333d2b36SAndroid Build Coastguard Worker		}
91*333d2b36SAndroid Build Coastguard Worker	case systemManifestType:
92*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, fmt.Sprintf("PLATFORM_SYSTEMSDK_VERSIONS=\"%s\"", strings.Join(ctx.DeviceConfig().PlatformSystemSdkVersions(), " ")))
93*333d2b36SAndroid Build Coastguard Worker
94*333d2b36SAndroid Build Coastguard Worker		inputPaths = append(inputPaths, PathForSource(ctx, defaultSystemManifest))
95*333d2b36SAndroid Build Coastguard Worker		systemManifestFiles := PathsForSource(ctx, ctx.Config().SystemManifestFile())
96*333d2b36SAndroid Build Coastguard Worker		if len(systemManifestFiles) > 0 {
97*333d2b36SAndroid Build Coastguard Worker			inputPaths = append(inputPaths, systemManifestFiles...)
98*333d2b36SAndroid Build Coastguard Worker		}
99*333d2b36SAndroid Build Coastguard Worker	case productManifestType:
100*333d2b36SAndroid Build Coastguard Worker		productManifestFiles := PathsForSource(ctx, ctx.Config().ProductManifestFiles())
101*333d2b36SAndroid Build Coastguard Worker		// Only need to generate the manifest if PRODUCT_MANIFEST_FILES not defined.
102*333d2b36SAndroid Build Coastguard Worker		if len(productManifestFiles) == 0 {
103*333d2b36SAndroid Build Coastguard Worker			m.noAction = true
104*333d2b36SAndroid Build Coastguard Worker			return
105*333d2b36SAndroid Build Coastguard Worker		}
106*333d2b36SAndroid Build Coastguard Worker
107*333d2b36SAndroid Build Coastguard Worker		inputPaths = append(inputPaths, productManifestFiles...)
108*333d2b36SAndroid Build Coastguard Worker	case systemExtManifestType:
109*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, fmt.Sprintf("PROVIDED_VNDK_VERSIONS=\"%s\"", strings.Join(ctx.DeviceConfig().ExtraVndkVersions(), " ")))
110*333d2b36SAndroid Build Coastguard Worker
111*333d2b36SAndroid Build Coastguard Worker		inputPaths = append(inputPaths, PathForSource(ctx, defaultSystemExtManifest))
112*333d2b36SAndroid Build Coastguard Worker		systemExtManifestFiles := PathsForSource(ctx, ctx.Config().SystemExtManifestFiles())
113*333d2b36SAndroid Build Coastguard Worker		if len(systemExtManifestFiles) > 0 {
114*333d2b36SAndroid Build Coastguard Worker			inputPaths = append(inputPaths, systemExtManifestFiles...)
115*333d2b36SAndroid Build Coastguard Worker		}
116*333d2b36SAndroid Build Coastguard Worker	case vendorManifestType:
117*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, fmt.Sprintf("BOARD_SEPOLICY_VERS=\"%s\"", ctx.DeviceConfig().BoardSepolicyVers()))
118*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, fmt.Sprintf("PRODUCT_ENFORCE_VINTF_MANIFEST=%t", *ctx.Config().productVariables.Enforce_vintf_manifest))
119*333d2b36SAndroid Build Coastguard Worker		deviceManifestFiles := PathsForSource(ctx, ctx.Config().DeviceManifestFiles())
120*333d2b36SAndroid Build Coastguard Worker		// Only need to generate the manifest if DEVICE_MANIFEST_FILE is defined.
121*333d2b36SAndroid Build Coastguard Worker		if len(deviceManifestFiles) == 0 {
122*333d2b36SAndroid Build Coastguard Worker			m.noAction = true
123*333d2b36SAndroid Build Coastguard Worker			return
124*333d2b36SAndroid Build Coastguard Worker		}
125*333d2b36SAndroid Build Coastguard Worker
126*333d2b36SAndroid Build Coastguard Worker		inputPaths = append(inputPaths, deviceManifestFiles...)
127*333d2b36SAndroid Build Coastguard Worker	case odmManifestType:
128*333d2b36SAndroid Build Coastguard Worker		assembleVintfEnvs = append(assembleVintfEnvs, "VINTF_IGNORE_TARGET_FCM_VERSION=true")
129*333d2b36SAndroid Build Coastguard Worker		odmManifestFiles := PathsForSource(ctx, ctx.Config().OdmManifestFiles())
130*333d2b36SAndroid Build Coastguard Worker		// Only need to generate the manifest if ODM_MANIFEST_FILES is defined.
131*333d2b36SAndroid Build Coastguard Worker		if len(odmManifestFiles) == 0 {
132*333d2b36SAndroid Build Coastguard Worker			m.noAction = true
133*333d2b36SAndroid Build Coastguard Worker			return
134*333d2b36SAndroid Build Coastguard Worker		}
135*333d2b36SAndroid Build Coastguard Worker
136*333d2b36SAndroid Build Coastguard Worker		inputPaths = append(inputPaths, odmManifestFiles...)
137*333d2b36SAndroid Build Coastguard Worker	default:
138*333d2b36SAndroid Build Coastguard Worker		panic(fmt.Errorf("For %s: The attribute 'type' value only allowed device_cm, system_manifest, product_manifest, system_ext_manifest!", ctx.Module().Name()))
139*333d2b36SAndroid Build Coastguard Worker	}
140*333d2b36SAndroid Build Coastguard Worker
141*333d2b36SAndroid Build Coastguard Worker	// Process vintf fragment source file with assemble_vintf tool
142*333d2b36SAndroid Build Coastguard Worker	builder.Command().
143*333d2b36SAndroid Build Coastguard Worker		Flags(assembleVintfEnvs).
144*333d2b36SAndroid Build Coastguard Worker		BuiltTool("assemble_vintf").
145*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("-i ", strings.Join(inputPaths.Strings(), ":")).
146*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("-o ", gensrc)
147*333d2b36SAndroid Build Coastguard Worker
148*333d2b36SAndroid Build Coastguard Worker	builder.Build("assemble_vintf", "Process vintf data "+gensrc.String())
149*333d2b36SAndroid Build Coastguard Worker
150*333d2b36SAndroid Build Coastguard Worker	m.installDirPath = PathForModuleInstall(ctx, "etc", "vintf")
151*333d2b36SAndroid Build Coastguard Worker	m.outputFilePath = gensrc
152*333d2b36SAndroid Build Coastguard Worker
153*333d2b36SAndroid Build Coastguard Worker	installFileName := "manifest.xml"
154*333d2b36SAndroid Build Coastguard Worker	if filename := proptools.String(m.properties.Filename); filename != "" {
155*333d2b36SAndroid Build Coastguard Worker		installFileName = filename
156*333d2b36SAndroid Build Coastguard Worker	}
157*333d2b36SAndroid Build Coastguard Worker
158*333d2b36SAndroid Build Coastguard Worker	ctx.InstallFile(m.installDirPath, installFileName, gensrc)
159*333d2b36SAndroid Build Coastguard Worker}
160*333d2b36SAndroid Build Coastguard Worker
161*333d2b36SAndroid Build Coastguard Worker// Make this module visible to AndroidMK so it can be referenced from modules defined from Android.mk files
162*333d2b36SAndroid Build Coastguard Workerfunc (m *vintfDataRule) AndroidMkEntries() []AndroidMkEntries {
163*333d2b36SAndroid Build Coastguard Worker	if m.noAction {
164*333d2b36SAndroid Build Coastguard Worker		return []AndroidMkEntries{}
165*333d2b36SAndroid Build Coastguard Worker	}
166*333d2b36SAndroid Build Coastguard Worker
167*333d2b36SAndroid Build Coastguard Worker	return []AndroidMkEntries{{
168*333d2b36SAndroid Build Coastguard Worker		Class:      "ETC",
169*333d2b36SAndroid Build Coastguard Worker		OutputFile: OptionalPathForPath(m.outputFilePath),
170*333d2b36SAndroid Build Coastguard Worker	}}
171*333d2b36SAndroid Build Coastguard Worker}
172