blob: cc89af61a6d8a5b526760ee6fb8a0cb8518cdc90 [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
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
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.
Colin Cross635c3b02016-05-18 15:37:25 -070027func includeDirsToFlags(dirs android.Paths) string {
28 return android.JoinWithPrefix(dirs.Strings(), "-I")
Colin Cross3f40fa42015-01-30 17:27:36 -080029}
30
Colin Cross635c3b02016-05-18 15:37:25 -070031func includeFilesToFlags(files android.Paths) string {
32 return android.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 Cross635c3b02016-05-18 15:37:25 -070036 return android.JoinWithPrefix(dirs, "-L")
Colin Cross3f40fa42015-01-30 17:27:36 -080037}
38
39func libNamesToFlags(names []string) string {
Colin Cross635c3b02016-05-18 15:37:25 -070040 return android.JoinWithPrefix(names, "-l")
Colin Cross3f40fa42015-01-30 17:27:36 -080041}
42
Colin Cross16b23492016-01-06 14:41:07 -080043func indexList(s string, list []string) int {
44 for i, l := range list {
Colin Cross3f40fa42015-01-30 17:27:36 -080045 if l == s {
Colin Cross16b23492016-01-06 14:41:07 -080046 return i
Colin Cross3f40fa42015-01-30 17:27:36 -080047 }
48 }
49
Colin Cross16b23492016-01-06 14:41:07 -080050 return -1
51}
52
53func inList(s string, list []string) bool {
54 return indexList(s, list) != -1
Colin Cross3f40fa42015-01-30 17:27:36 -080055}
56
Colin Crossed4cf0b2015-03-26 14:43:45 -070057func 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
Jiyong Park44cf1a72017-06-16 12:03:55 +090069func removeListFromList(list []string, filter_out []string) (result []string) {
70 result = make([]string, 0, len(list))
71 for _, l := range list {
72 if !inList(l, filter_out) {
73 result = append(result, l)
74 }
75 }
76 return
77}
78
Colin Cross16b23492016-01-06 14:41:07 -080079func removeFromList(s string, list []string) (bool, []string) {
80 i := indexList(s, list)
81 if i != -1 {
82 return true, append(list[:i], list[i+1:]...)
83 } else {
84 return false, list
85 }
86}
87
Colin Cross3f40fa42015-01-30 17:27:36 -080088var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
89
90func moduleToLibName(module string) (string, error) {
91 matches := libNameRegexp.FindStringSubmatch(module)
92 if matches == nil {
93 return "", fmt.Errorf("Library module name %s does not start with lib", module)
94 }
95 return matches[1], nil
96}
97
Colin Crossca860ac2016-01-04 14:34:37 -080098func flagsToBuilderFlags(in Flags) builderFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -080099 return builderFlags{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700100 globalFlags: strings.Join(in.GlobalFlags, " "),
101 arFlags: strings.Join(in.ArFlags, " "),
102 asFlags: strings.Join(in.AsFlags, " "),
103 cFlags: strings.Join(in.CFlags, " "),
104 toolingCFlags: strings.Join(in.ToolingCFlags, " "),
105 conlyFlags: strings.Join(in.ConlyFlags, " "),
106 cppFlags: strings.Join(in.CppFlags, " "),
107 yaccFlags: strings.Join(in.YaccFlags, " "),
108 protoFlags: strings.Join(in.protoFlags, " "),
109 aidlFlags: strings.Join(in.aidlFlags, " "),
110 rsFlags: strings.Join(in.rsFlags, " "),
111 ldFlags: strings.Join(in.LdFlags, " "),
112 libFlags: strings.Join(in.libFlags, " "),
113 tidyFlags: strings.Join(in.TidyFlags, " "),
114 sAbiFlags: strings.Join(in.SAbiFlags, " "),
115 yasmFlags: strings.Join(in.YasmFlags, " "),
116 toolchain: in.Toolchain,
117 clang: in.Clang,
118 coverage: in.Coverage,
119 tidy: in.Tidy,
120 sAbiDump: in.SAbiDump,
Colin Cross18c0c5a2016-12-01 14:45:23 -0800121
Colin Crossc3199482017-03-30 15:03:04 -0700122 systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
123
Colin Cross18c0c5a2016-12-01 14:45:23 -0800124 groupStaticLibs: in.GroupStaticLibs,
Colin Cross3f40fa42015-01-30 17:27:36 -0800125 }
126}
Dan Willemsen110a89d2016-01-14 15:17:19 -0800127
Dan Willemsen20acc5c2016-05-25 14:47:21 -0700128func addPrefix(list []string, prefix string) []string {
129 for i := range list {
130 list[i] = prefix + list[i]
131 }
132 return list
133}
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900134
135func addSuffix(list []string, suffix string) []string {
136 for i := range list {
137 list[i] = list[i] + suffix
138 }
139 return list
140}