Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 18 | "fmt" |
| 19 | "regexp" |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 20 | "sort" |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 21 | "strings" |
| 22 | ) |
| 23 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 24 | const ( |
| 25 | bazelIndent = 4 |
| 26 | ) |
| 27 | |
| 28 | type bazelVarExporter interface { |
| 29 | asBazel(exportedStringVariables, exportedStringListVariables) []bazelConstant |
| 30 | } |
| 31 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 32 | // Helpers for exporting cc configuration information to Bazel. |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 33 | var ( |
| 34 | // Map containing toolchain variables that are independent of the |
| 35 | // environment variables of the build. |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 36 | exportedStringListVars = exportedStringListVariables{} |
| 37 | exportedStringVars = exportedStringVariables{} |
| 38 | exportedStringListDictVars = exportedStringListDictVariables{} |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 39 | ) |
| 40 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 41 | // Ensure that string s has no invalid characters to be generated into the bzl file. |
| 42 | func validateCharacters(s string) string { |
| 43 | for _, c := range []string{`\n`, `"`, `\`} { |
| 44 | if strings.Contains(s, c) { |
| 45 | panic(fmt.Errorf("%s contains illegal character %s", s, c)) |
| 46 | } |
| 47 | } |
| 48 | return s |
| 49 | } |
| 50 | |
| 51 | type bazelConstant struct { |
| 52 | variableName string |
| 53 | internalDefinition string |
| 54 | } |
| 55 | |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 56 | type exportedStringVariables map[string]string |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 57 | |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 58 | func (m exportedStringVariables) Set(k string, v string) { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 59 | m[k] = v |
| 60 | } |
| 61 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 62 | func bazelIndention(level int) string { |
| 63 | return strings.Repeat(" ", level*bazelIndent) |
| 64 | } |
| 65 | |
| 66 | func printBazelList(items []string, indentLevel int) string { |
| 67 | list := make([]string, 0, len(items)+2) |
| 68 | list = append(list, "[") |
| 69 | innerIndent := bazelIndention(indentLevel + 1) |
| 70 | for _, item := range items { |
| 71 | list = append(list, fmt.Sprintf(`%s"%s",`, innerIndent, item)) |
| 72 | } |
| 73 | list = append(list, bazelIndention(indentLevel)+"]") |
| 74 | return strings.Join(list, "\n") |
| 75 | } |
| 76 | |
| 77 | func (m exportedStringVariables) asBazel(stringScope exportedStringVariables, stringListScope exportedStringListVariables) []bazelConstant { |
| 78 | ret := make([]bazelConstant, 0, len(m)) |
| 79 | for k, variableValue := range m { |
| 80 | expandedVar := expandVar(variableValue, exportedStringVars, exportedStringListVars) |
| 81 | if len(expandedVar) > 1 { |
| 82 | panic(fmt.Errorf("%s expands to more than one string value: %s", variableValue, expandedVar)) |
| 83 | } |
| 84 | ret = append(ret, bazelConstant{ |
| 85 | variableName: k, |
| 86 | internalDefinition: fmt.Sprintf(`"%s"`, validateCharacters(expandedVar[0])), |
| 87 | }) |
| 88 | } |
| 89 | return ret |
| 90 | } |
| 91 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 92 | // Convenience function to declare a static variable and export it to Bazel's cc_toolchain. |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 93 | func exportStringStaticVariable(name string, value string) { |
| 94 | pctx.StaticVariable(name, value) |
| 95 | exportedStringVars.Set(name, value) |
| 96 | } |
| 97 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 98 | type exportedStringListVariables map[string][]string |
| 99 | |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 100 | func (m exportedStringListVariables) Set(k string, v []string) { |
| 101 | m[k] = v |
| 102 | } |
| 103 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 104 | func (m exportedStringListVariables) asBazel(stringScope exportedStringVariables, stringListScope exportedStringListVariables) []bazelConstant { |
| 105 | ret := make([]bazelConstant, 0, len(m)) |
| 106 | // For each exported variable, recursively expand elements in the variableValue |
| 107 | // list to ensure that interpolated variables are expanded according to their values |
| 108 | // in the variable scope. |
| 109 | for k, variableValue := range m { |
| 110 | var expandedVars []string |
| 111 | for _, v := range variableValue { |
| 112 | expandedVars = append(expandedVars, expandVar(v, stringScope, stringListScope)...) |
| 113 | } |
| 114 | // Assign the list as a bzl-private variable; this variable will be exported |
| 115 | // out through a constants struct later. |
| 116 | ret = append(ret, bazelConstant{ |
| 117 | variableName: k, |
| 118 | internalDefinition: printBazelList(expandedVars, 0), |
| 119 | }) |
| 120 | } |
| 121 | return ret |
| 122 | } |
| 123 | |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 124 | // Convenience function to declare a static variable and export it to Bazel's cc_toolchain. |
| 125 | func exportStringListStaticVariable(name string, value []string) { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 126 | pctx.StaticVariable(name, strings.Join(value, " ")) |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 127 | exportedStringListVars.Set(name, value) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 130 | type exportedStringListDictVariables map[string]map[string][]string |
| 131 | |
| 132 | func (m exportedStringListDictVariables) Set(k string, v map[string][]string) { |
| 133 | m[k] = v |
| 134 | } |
| 135 | |
| 136 | func printBazelStringListDict(dict map[string][]string) string { |
| 137 | bazelDict := make([]string, 0, len(dict)+2) |
| 138 | bazelDict = append(bazelDict, "{") |
| 139 | for k, v := range dict { |
| 140 | bazelDict = append(bazelDict, |
| 141 | fmt.Sprintf(`%s"%s": %s,`, bazelIndention(1), k, printBazelList(v, 1))) |
| 142 | } |
| 143 | bazelDict = append(bazelDict, "}") |
| 144 | return strings.Join(bazelDict, "\n") |
| 145 | } |
| 146 | |
| 147 | // Since dictionaries are not supported in Ninja, we do not expand variables for dictionaries |
| 148 | func (m exportedStringListDictVariables) asBazel(_ exportedStringVariables, _ exportedStringListVariables) []bazelConstant { |
| 149 | ret := make([]bazelConstant, 0, len(m)) |
| 150 | for k, dict := range m { |
| 151 | ret = append(ret, bazelConstant{ |
| 152 | variableName: k, |
| 153 | internalDefinition: printBazelStringListDict(dict), |
| 154 | }) |
| 155 | } |
| 156 | return ret |
| 157 | } |
| 158 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 159 | // BazelCcToolchainVars generates bzl file content containing variables for |
| 160 | // Bazel's cc_toolchain configuration. |
| 161 | func BazelCcToolchainVars() string { |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 162 | return bazelToolchainVars( |
| 163 | exportedStringListDictVars, |
| 164 | exportedStringListVars, |
| 165 | exportedStringVars) |
| 166 | } |
| 167 | |
| 168 | func bazelToolchainVars(vars ...bazelVarExporter) string { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 169 | ret := "# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.\n\n" |
| 170 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 171 | results := []bazelConstant{} |
| 172 | for _, v := range vars { |
| 173 | results = append(results, v.asBazel(exportedStringVars, exportedStringListVars)...) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 176 | sort.Slice(results, func(i, j int) bool { return results[i].variableName < results[j].variableName }) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 177 | |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 178 | definitions := make([]string, 0, len(results)) |
| 179 | constants := make([]string, 0, len(results)) |
| 180 | for _, b := range results { |
| 181 | definitions = append(definitions, |
| 182 | fmt.Sprintf("_%s = %s", b.variableName, b.internalDefinition)) |
| 183 | constants = append(constants, |
| 184 | fmt.Sprintf("%[1]s%[2]s = _%[2]s,", bazelIndention(1), b.variableName)) |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 187 | // Build the exported constants struct. |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 188 | ret += strings.Join(definitions, "\n\n") |
| 189 | ret += "\n\n" |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 190 | ret += "constants = struct(\n" |
Liz Kammer | 82ad8cc | 2021-08-02 10:41:48 -0400 | [diff] [blame^] | 191 | ret += strings.Join(constants, "\n") |
| 192 | ret += "\n)" |
| 193 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 194 | return ret |
| 195 | } |
| 196 | |
| 197 | // expandVar recursively expand interpolated variables in the exportedVars scope. |
| 198 | // |
| 199 | // We're using a string slice to track the seen variables to avoid |
| 200 | // stackoverflow errors with infinite recursion. it's simpler to use a |
| 201 | // string slice than to handle a pass-by-referenced map, which would make it |
| 202 | // quite complex to track depth-first interpolations. It's also unlikely the |
| 203 | // interpolation stacks are deep (n > 1). |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 204 | func expandVar(toExpand string, stringScope exportedStringVariables, stringListScope exportedStringListVariables) []string { |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 205 | // e.g. "${ExternalCflags}" |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 206 | r := regexp.MustCompile(`\${([a-zA-Z0-9_]+)}`) |
| 207 | |
| 208 | // Internal recursive function. |
| 209 | var expandVarInternal func(string, map[string]bool) []string |
| 210 | expandVarInternal = func(toExpand string, seenVars map[string]bool) []string { |
| 211 | var ret []string |
| 212 | for _, v := range strings.Split(toExpand, " ") { |
| 213 | matches := r.FindStringSubmatch(v) |
| 214 | if len(matches) == 0 { |
| 215 | return []string{v} |
| 216 | } |
| 217 | |
| 218 | if len(matches) != 2 { |
| 219 | panic(fmt.Errorf( |
| 220 | "Expected to only match 1 subexpression in %s, got %d", |
| 221 | v, |
| 222 | len(matches)-1)) |
| 223 | } |
| 224 | |
| 225 | // Index 1 of FindStringSubmatch contains the subexpression match |
| 226 | // (variable name) of the capture group. |
| 227 | variable := matches[1] |
| 228 | // toExpand contains a variable. |
| 229 | if _, ok := seenVars[variable]; ok { |
| 230 | panic(fmt.Errorf( |
| 231 | "Unbounded recursive interpolation of variable: %s", variable)) |
| 232 | } |
| 233 | // A map is passed-by-reference. Create a new map for |
| 234 | // this scope to prevent variables seen in one depth-first expansion |
| 235 | // to be also treated as "seen" in other depth-first traversals. |
| 236 | newSeenVars := map[string]bool{} |
| 237 | for k := range seenVars { |
| 238 | newSeenVars[k] = true |
| 239 | } |
| 240 | newSeenVars[variable] = true |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 241 | if unexpandedVars, ok := stringListScope[variable]; ok { |
| 242 | for _, unexpandedVar := range unexpandedVars { |
| 243 | ret = append(ret, expandVarInternal(unexpandedVar, newSeenVars)...) |
| 244 | } |
| 245 | } else if unexpandedVar, ok := stringScope[variable]; ok { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 246 | ret = append(ret, expandVarInternal(unexpandedVar, newSeenVars)...) |
| 247 | } |
| 248 | } |
| 249 | return ret |
| 250 | } |
| 251 | |
| 252 | return expandVarInternal(toExpand, map[string]bool{}) |
| 253 | } |