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. |
| 27 | func includeDirsToFlags(dirs []string) string { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame^] | 28 | return common.JoinWithPrefix(dirs, "-I") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | func ldDirsToFlags(dirs []string) string { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame^] | 32 | return common.JoinWithPrefix(dirs, "-L") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func libNamesToFlags(names []string) string { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame^] | 36 | return common.JoinWithPrefix(names, "-l") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func inList(s string, list []string) bool { |
| 40 | for _, l := range list { |
| 41 | if l == s { |
| 42 | return true |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return false |
| 47 | } |
| 48 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 49 | func filterList(list []string, filter []string) (remainder []string, filtered []string) { |
| 50 | for _, l := range list { |
| 51 | if inList(l, filter) { |
| 52 | filtered = append(filtered, l) |
| 53 | } else { |
| 54 | remainder = append(remainder, l) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return |
| 59 | } |
| 60 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 61 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) |
| 62 | |
| 63 | func moduleToLibName(module string) (string, error) { |
| 64 | matches := libNameRegexp.FindStringSubmatch(module) |
| 65 | if matches == nil { |
| 66 | return "", fmt.Errorf("Library module name %s does not start with lib", module) |
| 67 | } |
| 68 | return matches[1], nil |
| 69 | } |
| 70 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 71 | func ccFlagsToBuilderFlags(in CCFlags) builderFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 72 | return builderFlags{ |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 73 | globalFlags: strings.Join(in.GlobalFlags, " "), |
| 74 | asFlags: strings.Join(in.AsFlags, " "), |
| 75 | cFlags: strings.Join(in.CFlags, " "), |
| 76 | conlyFlags: strings.Join(in.ConlyFlags, " "), |
| 77 | cppFlags: strings.Join(in.CppFlags, " "), |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 78 | yaccFlags: strings.Join(in.YaccFlags, " "), |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 79 | ldFlags: strings.Join(in.LdFlags, " "), |
| 80 | ldLibs: strings.Join(in.LdLibs, " "), |
| 81 | incFlags: includeDirsToFlags(in.IncludeDirs), |
| 82 | nocrt: in.Nocrt, |
| 83 | toolchain: in.Toolchain, |
| 84 | clang: in.Clang, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 85 | } |
| 86 | } |