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" |
| 19 | "regexp" |
| 20 | "strings" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 21 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | // Efficiently converts a list of include directories to a single string |
| 26 | // of cflags with -I prepended to each directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 27 | func includeDirsToFlags(dirs android.Paths) string { |
| 28 | return android.JoinWithPrefix(dirs.Strings(), "-I") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 31 | func includeFilesToFlags(files android.Paths) string { |
| 32 | return android.JoinWithPrefix(files.Strings(), "-include ") |
Colin Cross | 39d97f2 | 2015-09-14 12:30:50 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 35 | func ldDirsToFlags(dirs []string) string { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 36 | return android.JoinWithPrefix(dirs, "-L") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func libNamesToFlags(names []string) string { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 40 | return android.JoinWithPrefix(names, "-l") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 43 | func indexList(s string, list []string) int { |
| 44 | for i, l := range list { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | if l == s { |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 46 | return i |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 50 | return -1 |
| 51 | } |
| 52 | |
| 53 | func inList(s string, list []string) bool { |
| 54 | return indexList(s, list) != -1 |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 57 | func filterList(list []string, filter []string) (remainder []string, filtered []string) { |
| 58 | for _, l := range list { |
| 59 | if inList(l, filter) { |
| 60 | filtered = append(filtered, l) |
| 61 | } else { |
| 62 | remainder = append(remainder, l) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return |
| 67 | } |
| 68 | |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 69 | func removeListFromList(list []string, filter_out []string) (result []string) { |
| 70 | result = make([]string, 0, len(list)) |
| 71 | for _, l := range list { |
| 72 | if !inList(l, filter_out) { |
| 73 | result = append(result, l) |
| 74 | } |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 79 | func removeFromList(s string, list []string) (bool, []string) { |
| 80 | i := indexList(s, list) |
| 81 | if i != -1 { |
| 82 | return true, append(list[:i], list[i+1:]...) |
| 83 | } else { |
| 84 | return false, list |
| 85 | } |
| 86 | } |
| 87 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 88 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) |
| 89 | |
| 90 | func moduleToLibName(module string) (string, error) { |
| 91 | matches := libNameRegexp.FindStringSubmatch(module) |
| 92 | if matches == nil { |
| 93 | return "", fmt.Errorf("Library module name %s does not start with lib", module) |
| 94 | } |
| 95 | return matches[1], nil |
| 96 | } |
| 97 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 98 | func flagsToBuilderFlags(in Flags) builderFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 99 | return builderFlags{ |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 100 | globalFlags: strings.Join(in.GlobalFlags, " "), |
| 101 | arFlags: strings.Join(in.ArFlags, " "), |
| 102 | asFlags: strings.Join(in.AsFlags, " "), |
| 103 | cFlags: strings.Join(in.CFlags, " "), |
| 104 | toolingCFlags: strings.Join(in.ToolingCFlags, " "), |
| 105 | conlyFlags: strings.Join(in.ConlyFlags, " "), |
| 106 | cppFlags: strings.Join(in.CppFlags, " "), |
| 107 | yaccFlags: strings.Join(in.YaccFlags, " "), |
| 108 | protoFlags: strings.Join(in.protoFlags, " "), |
| 109 | aidlFlags: strings.Join(in.aidlFlags, " "), |
| 110 | rsFlags: strings.Join(in.rsFlags, " "), |
| 111 | ldFlags: strings.Join(in.LdFlags, " "), |
| 112 | libFlags: strings.Join(in.libFlags, " "), |
| 113 | tidyFlags: strings.Join(in.TidyFlags, " "), |
| 114 | sAbiFlags: strings.Join(in.SAbiFlags, " "), |
| 115 | yasmFlags: strings.Join(in.YasmFlags, " "), |
| 116 | toolchain: in.Toolchain, |
| 117 | clang: in.Clang, |
| 118 | coverage: in.Coverage, |
| 119 | tidy: in.Tidy, |
| 120 | sAbiDump: in.SAbiDump, |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 121 | |
Colin Cross | c319948 | 2017-03-30 15:03:04 -0700 | [diff] [blame] | 122 | systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "), |
| 123 | |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 124 | groupStaticLibs: in.GroupStaticLibs, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 127 | |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 128 | func addPrefix(list []string, prefix string) []string { |
| 129 | for i := range list { |
| 130 | list[i] = prefix + list[i] |
| 131 | } |
| 132 | return list |
| 133 | } |
Jiyong Park | ab0fd5f | 2017-08-03 21:22:50 +0900 | [diff] [blame^] | 134 | |
| 135 | func addSuffix(list []string, suffix string) []string { |
| 136 | for i := range list { |
| 137 | list[i] = list[i] + suffix |
| 138 | } |
| 139 | return list |
| 140 | } |