blob: 762f5614bae0f57b9b304a7b697b5f8c8edb2df2 [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 Jones14e2ac62024-04-17 11:21:37 -070034 var json, pb, textproto bool
35 var product string
LaMont Jones15788822024-04-24 16:01:44 -070036 var allMake bool
LaMont Jonese41ea1e2024-04-29 14:16:19 -070037 var useBuildVar 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 Jonesac796792024-04-11 11:01:18 -070050 flag.StringVar(&outputDir, "out_dir", rc_lib.GetDefaultOutDir(), "basepath for the output. Multiple formats are created")
LaMont Jones14e2ac62024-04-17 11:21:37 -070051 flag.BoolVar(&textproto, "textproto", true, "write artifacts as text protobuf")
52 flag.BoolVar(&json, "json", true, "write artifacts as json")
53 flag.BoolVar(&pb, "pb", true, "write artifacts as binary protobuf")
LaMont Jones8e2a6b12024-05-14 09:21:10 -070054 flag.BoolVar(&allMake, "all_make", false, "write makefiles for all release configs")
LaMont Jonese41ea1e2024-04-29 14:16:19 -070055 flag.BoolVar(&useBuildVar, "use_get_build_var", false, "use get_build_var PRODUCT_RELEASE_CONFIG_MAPS")
LaMont Jones15902f22024-05-01 10:06:32 -070056 flag.BoolVar(&guard, "guard", true, "whether to guard with RELEASE_BUILD_FLAGS_IN_PROTOBUF")
LaMont Jonese41ea1e2024-04-29 14:16:19 -070057
LaMont Jonesac796792024-04-11 11:01:18 -070058 flag.Parse()
59
LaMont Jones7ac07de2024-04-11 11:50:06 -070060 if quiet {
61 rc_lib.DisableWarnings()
62 }
63
LaMont Jonesac796792024-04-11 11:01:18 -070064 if err = os.Chdir(top); err != nil {
65 panic(err)
66 }
LaMont Jonese41ea1e2024-04-29 14:16:19 -070067 configs, err = rc_lib.ReadReleaseConfigMaps(releaseConfigMapPaths, targetRelease, useBuildVar)
LaMont Jonesac796792024-04-11 11:01:18 -070068 if err != nil {
69 panic(err)
70 }
LaMont Jones14e2ac62024-04-17 11:21:37 -070071 config, err := configs.GetReleaseConfig(targetRelease)
72 if err != nil {
73 panic(err)
74 }
LaMont Jonesac796792024-04-11 11:01:18 -070075 err = os.MkdirAll(outputDir, 0775)
76 if err != nil {
77 panic(err)
78 }
LaMont Jonesf4cc08e2024-04-26 09:14:30 -070079
LaMont Jones693c7032024-05-16 10:47:22 -070080 makefilePath := filepath.Join(outputDir, fmt.Sprintf("release_config-%s-%s.varmk", product, targetRelease))
LaMont Jones15902f22024-05-01 10:06:32 -070081 useProto, ok := config.FlagArtifacts["RELEASE_BUILD_FLAGS_IN_PROTOBUF"]
82 if guard && (!ok || rc_lib.MarshalValue(useProto.Value) == "") {
83 // We were told to guard operation and either we have no build flag, or it is False.
84 // Write an empty file so that release_config.mk will use the old process.
85 os.WriteFile(makefilePath, []byte{}, 0644)
LaMont Jones992011e2024-05-08 09:13:13 -070086 return
87 }
88 // Write the makefile where release_config.mk is going to look for it.
89 err = configs.WriteMakefile(makefilePath, targetRelease)
90 if err != nil {
91 panic(err)
92 }
93 if allMake {
LaMont Jones897ab4e2024-05-02 15:19:18 -070094 // Write one makefile per release config, using the canonical release name.
LaMont Jonesce599ef2024-05-15 11:44:26 -070095 for _, c := range configs.GetSortedReleaseConfigs() {
96 if c.Name != targetRelease {
LaMont Jones693c7032024-05-16 10:47:22 -070097 makefilePath = filepath.Join(outputDir, fmt.Sprintf("release_config-%s-%s.varmk", product, c.Name))
LaMont Jonesce599ef2024-05-15 11:44:26 -070098 err = configs.WriteMakefile(makefilePath, c.Name)
LaMont Jones992011e2024-05-08 09:13:13 -070099 if err != nil {
100 panic(err)
101 }
LaMont Jones15788822024-04-24 16:01:44 -0700102 }
103 }
LaMont Jonesac796792024-04-11 11:01:18 -0700104 }
LaMont Jones14e2ac62024-04-17 11:21:37 -0700105 if json {
106 err = configs.WriteArtifact(outputDir, product, "json")
107 if err != nil {
108 panic(err)
109 }
110 }
111 if pb {
112 err = configs.WriteArtifact(outputDir, product, "pb")
113 if err != nil {
114 panic(err)
115 }
116 }
117 if textproto {
118 err = configs.WriteArtifact(outputDir, product, "textproto")
119 if err != nil {
120 panic(err)
121 }
LaMont Jonesac796792024-04-11 11:01:18 -0700122 }
Justin Yun5f538692024-05-22 20:35:20 +0900123 if err = config.WritePartitionBuildFlags(outputDir); err != nil {
LaMont Jones15902f22024-05-01 10:06:32 -0700124 panic(err)
125 }
126
LaMont Jonesac796792024-04-11 11:01:18 -0700127}