1// Copyright 2023 Google Inc. All rights reserved.
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 build_flags
16
17import (
18	"android/soong/android"
19
20	"github.com/google/blueprint"
21)
22
23var (
24	pctx = android.NewPackageContext("android/soong/aconfig/build_flags")
25
26	// For build_flag_declarations: Generate cache file
27	buildFlagRule = pctx.AndroidStaticRule("build-flag-declarations",
28		blueprint.RuleParams{
29			Command: `${buildFlagDeclarations} ` +
30				` ${declarations}` +
31				` --format pb` +
32				` --output ${out}.tmp` +
33				` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
34			CommandDeps: []string{
35				"${buildFlagDeclarations}",
36			},
37			Restat: true,
38		}, "release_version", "declarations")
39
40	buildFlagTextRule = pctx.AndroidStaticRule("build-flag-declarations-text",
41		blueprint.RuleParams{
42			Command: `${buildFlagDeclarations} --format=textproto` +
43				` --intermediate ${in}` +
44				` --format textproto` +
45				` --output ${out}.tmp` +
46				` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
47			CommandDeps: []string{
48				"${buildFlagDeclarations}",
49			},
50			Restat: true,
51		})
52
53	allDeclarationsRule = pctx.AndroidStaticRule("all-build-flag-declarations-dump",
54		blueprint.RuleParams{
55			Command: `${buildFlagDeclarations} ${intermediates} --format pb --output ${out}`,
56			CommandDeps: []string{
57				"${buildFlagDeclarations}",
58			},
59		}, "intermediates")
60
61	allDeclarationsRuleTextProto = pctx.AndroidStaticRule("All_build_flag_declarations_dump_textproto",
62		blueprint.RuleParams{
63			Command: `${buildFlagDeclarations} --intermediate ${in} --format textproto --output ${out}`,
64			CommandDeps: []string{
65				"${buildFlagDeclarations}",
66			},
67		})
68)
69
70func init() {
71	RegisterBuildComponents(android.InitRegistrationContext)
72	pctx.Import("android/soong/android")
73	pctx.HostBinToolVariable("buildFlagDeclarations", "build-flag-declarations")
74}
75
76func RegisterBuildComponents(ctx android.RegistrationContext) {
77	ctx.RegisterModuleType("build_flag_declarations", DeclarationsFactory)
78	ctx.RegisterParallelSingletonType("all_build_flag_declarations", AllBuildFlagDeclarationsFactory)
79}
80