blob: db14c748bbde8eee60b7056000ccb233bdf407a2 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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
15package cc
16
17import (
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.
25func includeDirsToFlags(dirs []string) string {
26 return joinWithPrefix(dirs, "-I")
27}
28
29func ldDirsToFlags(dirs []string) string {
30 return joinWithPrefix(dirs, "-L")
31}
32
33func libNamesToFlags(names []string) string {
34 return joinWithPrefix(names, "-l")
35}
36
37func 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
62func 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
Colin Crossed4cf0b2015-03-26 14:43:45 -070072func filterList(list []string, filter []string) (remainder []string, filtered []string) {
73 for _, l := range list {
74 if inList(l, filter) {
75 filtered = append(filtered, l)
76 } else {
77 remainder = append(remainder, l)
78 }
79 }
80
81 return
82}
83
Colin Cross3f40fa42015-01-30 17:27:36 -080084var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
85
86func moduleToLibName(module string) (string, error) {
87 matches := libNameRegexp.FindStringSubmatch(module)
88 if matches == nil {
89 return "", fmt.Errorf("Library module name %s does not start with lib", module)
90 }
91 return matches[1], nil
92}
93
Colin Cross97ba0732015-03-23 17:50:24 -070094func ccFlagsToBuilderFlags(in CCFlags) builderFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -080095 return builderFlags{
Colin Cross97ba0732015-03-23 17:50:24 -070096 globalFlags: strings.Join(in.GlobalFlags, " "),
97 asFlags: strings.Join(in.AsFlags, " "),
98 cFlags: strings.Join(in.CFlags, " "),
99 conlyFlags: strings.Join(in.ConlyFlags, " "),
100 cppFlags: strings.Join(in.CppFlags, " "),
101 ldFlags: strings.Join(in.LdFlags, " "),
102 ldLibs: strings.Join(in.LdLibs, " "),
103 incFlags: includeDirsToFlags(in.IncludeDirs),
104 nocrt: in.Nocrt,
105 toolchain: in.Toolchain,
106 clang: in.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 }
108}