blob: bd4ab4907e994fb94fcd08ca1a1ac4161cff700e [file] [log] [blame]
LaMont Jonesac796792024-04-11 11:01:18 -07001// Copyright 2024 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 main
16
17import (
18 "flag"
LaMont Jones14e2ac62024-04-17 11:21:37 -070019 "fmt"
LaMont Jonesac796792024-04-11 11:01:18 -070020 "os"
LaMont Jones14e2ac62024-04-17 11:21:37 -070021 "path/filepath"
LaMont Jonesac796792024-04-11 11:01:18 -070022
23 rc_lib "android/soong/cmd/release_config/release_config_lib"
24)
25
26func main() {
27 var top string
LaMont Jones7ac07de2024-04-11 11:50:06 -070028 var quiet bool
LaMont Jonesac796792024-04-11 11:01:18 -070029 var releaseConfigMapPaths rc_lib.StringList
30 var targetRelease string
31 var outputDir string
32 var err error
33 var configs *rc_lib.ReleaseConfigs
LaMont Jonesed22f822024-05-29 13:56:39 -070034 var json, pb, textproto, inheritance bool
LaMont Jones14e2ac62024-04-17 11:21:37 -070035 var product string
LaMont Jones15788822024-04-24 16:01:44 -070036 var allMake bool
LaMont Jonesb4f866c2024-06-04 13:19:01 -070037 var useBuildVar, allowMissing bool
LaMont Jones15902f22024-05-01 10:06:32 -070038 var guard bool
LaMont Jones14e2ac62024-04-17 11:21:37 -070039
40 defaultRelease := os.Getenv("TARGET_RELEASE")
41 if defaultRelease == "" {
42 defaultRelease = "trunk_staging"
43 }
LaMont Jonesac796792024-04-11 11:01:18 -070044
45 flag.StringVar(&top, "top", ".", "path to top of workspace")
LaMont Jones14e2ac62024-04-17 11:21:37 -070046 flag.StringVar(&product, "product", os.Getenv("TARGET_PRODUCT"), "TARGET_PRODUCT for the build")
LaMont Jones7ac07de2024-04-11 11:50:06 -070047 flag.BoolVar(&quiet, "quiet", false, "disable warning messages")
LaMont Jonesac796792024-04-11 11:01:18 -070048 flag.Var(&releaseConfigMapPaths, "map", "path to a release_config_map.textproto. may be repeated")
LaMont Jones14e2ac62024-04-17 11:21:37 -070049 flag.StringVar(&targetRelease, "release", defaultRelease, "TARGET_RELEASE for this build")
LaMont Jonesb4f866c2024-06-04 13:19:01 -070050 flag.BoolVar(&allowMissing, "allow-missing", false, "Use trunk_staging values if release not found")
LaMont Jonesac796792024-04-11 11:01:18 -070051 flag.StringVar(&outputDir, "out_dir", rc_lib.GetDefaultOutDir(), "basepath for the output. Multiple formats are created")
LaMont Jones14e2ac62024-04-17 11:21:37 -070052 flag.BoolVar(&textproto, "textproto", true, "write artifacts as text protobuf")
53 flag.BoolVar(&json, "json", true, "write artifacts as json")
54 flag.BoolVar(&pb, "pb", true, "write artifacts as binary protobuf")
LaMont Jones8e2a6b12024-05-14 09:21:10 -070055 flag.BoolVar(&allMake, "all_make", false, "write makefiles for all release configs")
LaMont Jonesed22f822024-05-29 13:56:39 -070056 flag.BoolVar(&inheritance, "inheritance", true, "write inheritance graph")
LaMont Jonese41ea1e2024-04-29 14:16:19 -070057 flag.BoolVar(&useBuildVar, "use_get_build_var", false, "use get_build_var PRODUCT_RELEASE_CONFIG_MAPS")
LaMont Jones15902f22024-05-01 10:06:32 -070058 flag.BoolVar(&guard, "guard", true, "whether to guard with RELEASE_BUILD_FLAGS_IN_PROTOBUF")
LaMont Jonese41ea1e2024-04-29 14:16:19 -070059
LaMont Jonesac796792024-04-11 11:01:18 -070060 flag.Parse()
61
LaMont Jones7ac07de2024-04-11 11:50:06 -070062 if quiet {
63 rc_lib.DisableWarnings()
64 }
65
LaMont Jonesac796792024-04-11 11:01:18 -070066 if err = os.Chdir(top); err != nil {
67 panic(err)
68 }
LaMont Jonesb4f866c2024-06-04 13:19:01 -070069 configs, err = rc_lib.ReadReleaseConfigMaps(releaseConfigMapPaths, targetRelease, useBuildVar, allowMissing)
LaMont Jonesac796792024-04-11 11:01:18 -070070 if err != nil {
71 panic(err)
72 }
LaMont Jones14e2ac62024-04-17 11:21:37 -070073 config, err := configs.GetReleaseConfig(targetRelease)
74 if err != nil {
75 panic(err)
76 }
LaMont Jonesac796792024-04-11 11:01:18 -070077 err = os.MkdirAll(outputDir, 0775)
78 if err != nil {
79 panic(err)
80 }
LaMont Jonesf4cc08e2024-04-26 09:14:30 -070081
LaMont Jones693c7032024-05-16 10:47:22 -070082 makefilePath := filepath.Join(outputDir, fmt.Sprintf("release_config-%s-%s.varmk", product, targetRelease))
LaMont Jones15902f22024-05-01 10:06:32 -070083 useProto, ok := config.FlagArtifacts["RELEASE_BUILD_FLAGS_IN_PROTOBUF"]
84 if guard && (!ok || rc_lib.MarshalValue(useProto.Value) == "") {
85 // We were told to guard operation and either we have no build flag, or it is False.
86 // Write an empty file so that release_config.mk will use the old process.
87 os.WriteFile(makefilePath, []byte{}, 0644)
LaMont Jones992011e2024-05-08 09:13:13 -070088 return
89 }
90 // Write the makefile where release_config.mk is going to look for it.
91 err = configs.WriteMakefile(makefilePath, targetRelease)
92 if err != nil {
93 panic(err)
94 }
95 if allMake {
LaMont Jones897ab4e2024-05-02 15:19:18 -070096 // Write one makefile per release config, using the canonical release name.
LaMont Jonesce599ef2024-05-15 11:44:26 -070097 for _, c := range configs.GetSortedReleaseConfigs() {
98 if c.Name != targetRelease {
LaMont Jones693c7032024-05-16 10:47:22 -070099 makefilePath = filepath.Join(outputDir, fmt.Sprintf("release_config-%s-%s.varmk", product, c.Name))
LaMont Jonesce599ef2024-05-15 11:44:26 -0700100 err = configs.WriteMakefile(makefilePath, c.Name)
LaMont Jones992011e2024-05-08 09:13:13 -0700101 if err != nil {
102 panic(err)
103 }
LaMont Jones15788822024-04-24 16:01:44 -0700104 }
105 }
LaMont Jonesac796792024-04-11 11:01:18 -0700106 }
LaMont Jonesed22f822024-05-29 13:56:39 -0700107 if inheritance {
108 inheritPath := filepath.Join(outputDir, fmt.Sprintf("inheritance_graph-%s.dot", product))
109 err = configs.WriteInheritanceGraph(inheritPath)
110 if err != nil {
111 panic(err)
112 }
113 }
LaMont Jones14e2ac62024-04-17 11:21:37 -0700114 if json {
115 err = configs.WriteArtifact(outputDir, product, "json")
116 if err != nil {
117 panic(err)
118 }
119 }
120 if pb {
121 err = configs.WriteArtifact(outputDir, product, "pb")
122 if err != nil {
123 panic(err)
124 }
125 }
126 if textproto {
127 err = configs.WriteArtifact(outputDir, product, "textproto")
128 if err != nil {
129 panic(err)
130 }
LaMont Jonesac796792024-04-11 11:01:18 -0700131 }
Justin Yun5f538692024-05-22 20:35:20 +0900132 if err = config.WritePartitionBuildFlags(outputDir); err != nil {
LaMont Jones15902f22024-05-01 10:06:32 -0700133 panic(err)
134 }
135
LaMont Jonesac796792024-04-11 11:01:18 -0700136}