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 | |
| 22 | "android/soong/common" |
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. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 27 | func includeDirsToFlags(dirs common.Paths) string { |
| 28 | return common.JoinWithPrefix(dirs.Strings(), "-I") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 31 | func includeFilesToFlags(files common.Paths) string { |
| 32 | return common.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 | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 36 | return common.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 | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 40 | return common.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 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame^] | 69 | func removeFromList(s string, list []string) (bool, []string) { |
| 70 | i := indexList(s, list) |
| 71 | if i != -1 { |
| 72 | return true, append(list[:i], list[i+1:]...) |
| 73 | } else { |
| 74 | return false, list |
| 75 | } |
| 76 | } |
| 77 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 78 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) |
| 79 | |
| 80 | func moduleToLibName(module string) (string, error) { |
| 81 | matches := libNameRegexp.FindStringSubmatch(module) |
| 82 | if matches == nil { |
| 83 | return "", fmt.Errorf("Library module name %s does not start with lib", module) |
| 84 | } |
| 85 | return matches[1], nil |
| 86 | } |
| 87 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 88 | func flagsToBuilderFlags(in Flags) builderFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 89 | return builderFlags{ |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 90 | globalFlags: strings.Join(in.GlobalFlags, " "), |
| 91 | asFlags: strings.Join(in.AsFlags, " "), |
| 92 | cFlags: strings.Join(in.CFlags, " "), |
| 93 | conlyFlags: strings.Join(in.ConlyFlags, " "), |
| 94 | cppFlags: strings.Join(in.CppFlags, " "), |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 95 | yaccFlags: strings.Join(in.YaccFlags, " "), |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 96 | ldFlags: strings.Join(in.LdFlags, " "), |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame^] | 97 | libFlags: strings.Join(in.libFlags, " "), |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 98 | nocrt: in.Nocrt, |
| 99 | toolchain: in.Toolchain, |
| 100 | clang: in.Clang, |
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 | |
| 104 | func copyVariantFlags(m map[string][]string) map[string][]string { |
| 105 | ret := make(map[string][]string, len(m)) |
| 106 | for k, v := range m { |
| 107 | l := make([]string, len(m[k])) |
| 108 | for i := range m[k] { |
| 109 | l[i] = v[i] |
| 110 | } |
| 111 | ret[k] = l |
| 112 | } |
| 113 | return ret |
| 114 | } |
| 115 | |
| 116 | func variantOrDefault(variants map[string]string, choice string) string { |
| 117 | if ret, ok := variants[choice]; ok { |
| 118 | return ret |
| 119 | } |
| 120 | return variants[""] |
| 121 | } |