blob: 1d27f7354096fe5198055fc2a73a090bcc599202 [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"
Colin Crossc0b06f12015-04-08 13:03:43 -070021
22 "android/soong/common"
Colin Cross3f40fa42015-01-30 17:27:36 -080023)
24
25// Efficiently converts a list of include directories to a single string
26// of cflags with -I prepended to each directory.
Dan Willemsen34cc69e2015-09-23 15:26:20 -070027func includeDirsToFlags(dirs common.Paths) string {
28 return common.JoinWithPrefix(dirs.Strings(), "-I")
Colin Cross3f40fa42015-01-30 17:27:36 -080029}
30
Dan Willemsen34cc69e2015-09-23 15:26:20 -070031func includeFilesToFlags(files common.Paths) string {
32 return common.JoinWithPrefix(files.Strings(), "-include ")
Colin Cross39d97f22015-09-14 12:30:50 -070033}
34
Colin Cross3f40fa42015-01-30 17:27:36 -080035func ldDirsToFlags(dirs []string) string {
Colin Crossc0b06f12015-04-08 13:03:43 -070036 return common.JoinWithPrefix(dirs, "-L")
Colin Cross3f40fa42015-01-30 17:27:36 -080037}
38
39func libNamesToFlags(names []string) string {
Colin Crossc0b06f12015-04-08 13:03:43 -070040 return common.JoinWithPrefix(names, "-l")
Colin Cross3f40fa42015-01-30 17:27:36 -080041}
42
43func inList(s string, list []string) bool {
44 for _, l := range list {
45 if l == s {
46 return true
47 }
48 }
49
50 return false
51}
52
Colin Crossed4cf0b2015-03-26 14:43:45 -070053func filterList(list []string, filter []string) (remainder []string, filtered []string) {
54 for _, l := range list {
55 if inList(l, filter) {
56 filtered = append(filtered, l)
57 } else {
58 remainder = append(remainder, l)
59 }
60 }
61
62 return
63}
64
Colin Cross3f40fa42015-01-30 17:27:36 -080065var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
66
67func moduleToLibName(module string) (string, error) {
68 matches := libNameRegexp.FindStringSubmatch(module)
69 if matches == nil {
70 return "", fmt.Errorf("Library module name %s does not start with lib", module)
71 }
72 return matches[1], nil
73}
74
Colin Crossca860ac2016-01-04 14:34:37 -080075func flagsToBuilderFlags(in Flags) builderFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -080076 return builderFlags{
Colin Cross97ba0732015-03-23 17:50:24 -070077 globalFlags: strings.Join(in.GlobalFlags, " "),
78 asFlags: strings.Join(in.AsFlags, " "),
79 cFlags: strings.Join(in.CFlags, " "),
80 conlyFlags: strings.Join(in.ConlyFlags, " "),
81 cppFlags: strings.Join(in.CppFlags, " "),
Colin Cross581c1892015-04-07 16:50:10 -070082 yaccFlags: strings.Join(in.YaccFlags, " "),
Colin Cross97ba0732015-03-23 17:50:24 -070083 ldFlags: strings.Join(in.LdFlags, " "),
Colin Cross97ba0732015-03-23 17:50:24 -070084 nocrt: in.Nocrt,
85 toolchain: in.Toolchain,
86 clang: in.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -080087 }
88}
Dan Willemsen110a89d2016-01-14 15:17:19 -080089
90func copyVariantFlags(m map[string][]string) map[string][]string {
91 ret := make(map[string][]string, len(m))
92 for k, v := range m {
93 l := make([]string, len(m[k]))
94 for i := range m[k] {
95 l[i] = v[i]
96 }
97 ret[k] = l
98 }
99 return ret
100}
101
102func variantOrDefault(variants map[string]string, choice string) string {
103 if ret, ok := variants[choice]; ok {
104 return ret
105 }
106 return variants[""]
107}