Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 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 cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Logan Chien | 031d2b3 | 2018-07-26 00:19:50 +0800 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 20 | "regexp" |
| 21 | "strings" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 22 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Kiyoung Kim | ae11c23 | 2021-07-19 11:38:04 +0900 | [diff] [blame] | 24 | "android/soong/snapshot" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Efficiently converts a list of include directories to a single string |
| 28 | // of cflags with -I prepended to each directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 29 | func includeDirsToFlags(dirs android.Paths) string { |
| 30 | return android.JoinWithPrefix(dirs.Strings(), "-I") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func ldDirsToFlags(dirs []string) string { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 34 | return android.JoinWithPrefix(dirs, "-L") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func libNamesToFlags(names []string) string { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 38 | return android.JoinWithPrefix(names, "-l") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 41 | var indexList = android.IndexList |
| 42 | var inList = android.InList |
| 43 | var filterList = android.FilterList |
| 44 | var removeListFromList = android.RemoveListFromList |
| 45 | var removeFromList = android.RemoveFromList |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 46 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) |
| 48 | |
| 49 | func moduleToLibName(module string) (string, error) { |
| 50 | matches := libNameRegexp.FindStringSubmatch(module) |
| 51 | if matches == nil { |
| 52 | return "", fmt.Errorf("Library module name %s does not start with lib", module) |
| 53 | } |
| 54 | return matches[1], nil |
| 55 | } |
| 56 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 57 | func flagsToBuilderFlags(in Flags) builderFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 58 | return builderFlags{ |
Colin Cross | 6d88dba | 2019-11-06 07:06:58 -0800 | [diff] [blame] | 59 | globalCommonFlags: strings.Join(in.Global.CommonFlags, " "), |
| 60 | globalAsFlags: strings.Join(in.Global.AsFlags, " "), |
| 61 | globalYasmFlags: strings.Join(in.Global.YasmFlags, " "), |
| 62 | globalCFlags: strings.Join(in.Global.CFlags, " "), |
| 63 | globalToolingCFlags: strings.Join(in.Global.ToolingCFlags, " "), |
| 64 | globalToolingCppFlags: strings.Join(in.Global.ToolingCppFlags, " "), |
| 65 | globalConlyFlags: strings.Join(in.Global.ConlyFlags, " "), |
| 66 | globalCppFlags: strings.Join(in.Global.CppFlags, " "), |
| 67 | globalLdFlags: strings.Join(in.Global.LdFlags, " "), |
| 68 | |
| 69 | localCommonFlags: strings.Join(in.Local.CommonFlags, " "), |
| 70 | localAsFlags: strings.Join(in.Local.AsFlags, " "), |
| 71 | localYasmFlags: strings.Join(in.Local.YasmFlags, " "), |
| 72 | localCFlags: strings.Join(in.Local.CFlags, " "), |
| 73 | localToolingCFlags: strings.Join(in.Local.ToolingCFlags, " "), |
| 74 | localToolingCppFlags: strings.Join(in.Local.ToolingCppFlags, " "), |
| 75 | localConlyFlags: strings.Join(in.Local.ConlyFlags, " "), |
| 76 | localCppFlags: strings.Join(in.Local.CppFlags, " "), |
| 77 | localLdFlags: strings.Join(in.Local.LdFlags, " "), |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 78 | |
| 79 | aidlFlags: strings.Join(in.aidlFlags, " "), |
| 80 | rsFlags: strings.Join(in.rsFlags, " "), |
| 81 | libFlags: strings.Join(in.libFlags, " "), |
| 82 | extraLibFlags: strings.Join(in.extraLibFlags, " "), |
| 83 | tidyFlags: strings.Join(in.TidyFlags, " "), |
| 84 | sAbiFlags: strings.Join(in.SAbiFlags, " "), |
| 85 | toolchain: in.Toolchain, |
Oliver Nguyen | 0452678 | 2020-04-21 12:40:27 -0700 | [diff] [blame] | 86 | gcovCoverage: in.GcovCoverage, |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 87 | tidy: in.Tidy, |
| 88 | sAbiDump: in.SAbiDump, |
| 89 | emitXrefs: in.EmitXrefs, |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 90 | |
Colin Cross | c319948 | 2017-03-30 15:03:04 -0700 | [diff] [blame] | 91 | systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "), |
| 92 | |
Dan Willemsen | 98ab311 | 2019-08-27 21:20:40 -0700 | [diff] [blame] | 93 | assemblerWithCpp: in.AssemblerWithCpp, |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 94 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 95 | proto: in.proto, |
Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 96 | protoC: in.protoC, |
| 97 | protoOptionsFile: in.protoOptionsFile, |
Dan Willemsen | 4e0aa23 | 2019-04-10 22:59:54 -0700 | [diff] [blame] | 98 | |
| 99 | yacc: in.Yacc, |
Matthias Maennich | 22fd4d1 | 2020-07-15 10:58:56 +0200 | [diff] [blame] | 100 | lex: in.Lex, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 103 | |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 104 | func flagsToStripFlags(in Flags) StripFlags { |
| 105 | return StripFlags{Toolchain: in.Toolchain} |
| 106 | } |
| 107 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 108 | func addPrefix(list []string, prefix string) []string { |
| 109 | for i := range list { |
| 110 | list[i] = prefix + list[i] |
| 111 | } |
| 112 | return list |
| 113 | } |
Jiyong Park | ab0fd5f | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 114 | |
| 115 | func addSuffix(list []string, suffix string) []string { |
| 116 | for i := range list { |
| 117 | list[i] = list[i] + suffix |
| 118 | } |
| 119 | return list |
| 120 | } |
Logan Chien | 031d2b3 | 2018-07-26 00:19:50 +0800 | [diff] [blame] | 121 | |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 122 | // linkDirOnDevice/linkName -> target |
| 123 | func makeSymlinkCmd(linkDirOnDevice string, linkName string, target string) string { |
| 124 | dir := filepath.Join("$(PRODUCT_OUT)", linkDirOnDevice) |
| 125 | return "mkdir -p " + dir + " && " + |
| 126 | "ln -sf " + target + " " + filepath.Join(dir, linkName) |
| 127 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 128 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 129 | func combineNoticesRule(ctx android.SingletonContext, paths android.Paths, out string) android.OutputPath { |
| 130 | outPath := android.PathForOutput(ctx, out) |
| 131 | ctx.Build(pctx, android.BuildParams{ |
| 132 | Rule: android.Cat, |
| 133 | Inputs: paths, |
| 134 | Output: outPath, |
| 135 | Description: "combine notices for " + out, |
| 136 | }) |
| 137 | return outPath |
| 138 | } |
| 139 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 140 | // Dump a map to a list file as: |
| 141 | // |
| 142 | // {key1} {value1} |
| 143 | // {key2} {value2} |
| 144 | // ... |
| 145 | func installMapListFileRule(ctx android.SingletonContext, m map[string]string, path string) android.OutputPath { |
| 146 | var txtBuilder strings.Builder |
| 147 | for idx, k := range android.SortedStringKeys(m) { |
| 148 | if idx > 0 { |
| 149 | txtBuilder.WriteString("\n") |
| 150 | } |
| 151 | txtBuilder.WriteString(k) |
| 152 | txtBuilder.WriteString(" ") |
| 153 | txtBuilder.WriteString(m[k]) |
| 154 | } |
Kiyoung Kim | ae11c23 | 2021-07-19 11:38:04 +0900 | [diff] [blame] | 155 | return snapshot.WriteStringToFileRule(ctx, txtBuilder.String(), path) |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 156 | } |