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" |
| 21 | ) |
| 22 | |
| 23 | // Efficiently converts a list of include directories to a single string |
| 24 | // of cflags with -I prepended to each directory. |
| 25 | func includeDirsToFlags(dirs []string) string { |
| 26 | return joinWithPrefix(dirs, "-I") |
| 27 | } |
| 28 | |
| 29 | func ldDirsToFlags(dirs []string) string { |
| 30 | return joinWithPrefix(dirs, "-L") |
| 31 | } |
| 32 | |
| 33 | func libNamesToFlags(names []string) string { |
| 34 | return joinWithPrefix(names, "-l") |
| 35 | } |
| 36 | |
| 37 | func joinWithPrefix(strs []string, prefix string) string { |
| 38 | if len(strs) == 0 { |
| 39 | return "" |
| 40 | } |
| 41 | |
| 42 | if len(strs) == 1 { |
| 43 | return prefix + strs[0] |
| 44 | } |
| 45 | |
| 46 | n := len(" ") * (len(strs) - 1) |
| 47 | for _, s := range strs { |
| 48 | n += len(prefix) + len(s) |
| 49 | } |
| 50 | |
| 51 | ret := make([]byte, 0, n) |
| 52 | for i, s := range strs { |
| 53 | if i != 0 { |
| 54 | ret = append(ret, ' ') |
| 55 | } |
| 56 | ret = append(ret, prefix...) |
| 57 | ret = append(ret, s...) |
| 58 | } |
| 59 | return string(ret) |
| 60 | } |
| 61 | |
| 62 | func inList(s string, list []string) bool { |
| 63 | for _, l := range list { |
| 64 | if l == s { |
| 65 | return true |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) |
| 73 | |
| 74 | func moduleToLibName(module string) (string, error) { |
| 75 | matches := libNameRegexp.FindStringSubmatch(module) |
| 76 | if matches == nil { |
| 77 | return "", fmt.Errorf("Library module name %s does not start with lib", module) |
| 78 | } |
| 79 | return matches[1], nil |
| 80 | } |
| 81 | |
| 82 | func ccFlagsToBuilderFlags(in ccFlags) builderFlags { |
| 83 | return builderFlags{ |
| 84 | globalFlags: strings.Join(in.globalFlags, " "), |
| 85 | asFlags: strings.Join(in.asFlags, " "), |
| 86 | cFlags: strings.Join(in.cFlags, " "), |
| 87 | conlyFlags: strings.Join(in.conlyFlags, " "), |
| 88 | cppFlags: strings.Join(in.cppFlags, " "), |
| 89 | ldFlags: strings.Join(in.ldFlags, " "), |
| 90 | ldLibs: strings.Join(in.ldLibs, " "), |
| 91 | incFlags: includeDirsToFlags(in.includeDirs), |
| 92 | nocrt: in.nocrt, |
| 93 | toolchain: in.toolchain, |
| 94 | clang: in.clang, |
| 95 | } |
| 96 | } |