blob: d9cd6f77ef93d6dd684ea85badabedb8ffcbd1ea [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
Colin Cross16b23492016-01-06 14:41:07 -080069func removeFromList(s string, list []string) (bool, []string) {
70 i := indexList(s, list)
71 if i != -1 {
72 return true, append(list[:i], list[i+1:]...)
73 } else {
74 return false, list
75 }
76}
77
Colin Cross3f40fa42015-01-30 17:27:36 -080078var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
79
80func moduleToLibName(module string) (string, error) {
81 matches := libNameRegexp.FindStringSubmatch(module)
82 if matches == nil {
83 return "", fmt.Errorf("Library module name %s does not start with lib", module)
84 }
85 return matches[1], nil
86}
87
Colin Crossca860ac2016-01-04 14:34:37 -080088func flagsToBuilderFlags(in Flags) builderFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -080089 return builderFlags{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070090 globalFlags: strings.Join(in.GlobalFlags, " "),
91 arFlags: strings.Join(in.ArFlags, " "),
92 asFlags: strings.Join(in.AsFlags, " "),
93 cFlags: strings.Join(in.CFlags, " "),
94 toolingCFlags: strings.Join(in.ToolingCFlags, " "),
95 conlyFlags: strings.Join(in.ConlyFlags, " "),
96 cppFlags: strings.Join(in.CppFlags, " "),
97 yaccFlags: strings.Join(in.YaccFlags, " "),
98 protoFlags: strings.Join(in.protoFlags, " "),
99 aidlFlags: strings.Join(in.aidlFlags, " "),
100 rsFlags: strings.Join(in.rsFlags, " "),
101 ldFlags: strings.Join(in.LdFlags, " "),
102 libFlags: strings.Join(in.libFlags, " "),
103 tidyFlags: strings.Join(in.TidyFlags, " "),
104 sAbiFlags: strings.Join(in.SAbiFlags, " "),
105 yasmFlags: strings.Join(in.YasmFlags, " "),
106 toolchain: in.Toolchain,
107 clang: in.Clang,
108 coverage: in.Coverage,
109 tidy: in.Tidy,
110 sAbiDump: in.SAbiDump,
Colin Cross18c0c5a2016-12-01 14:45:23 -0800111
Colin Crossc3199482017-03-30 15:03:04 -0700112 systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
113
Colin Cross18c0c5a2016-12-01 14:45:23 -0800114 groupStaticLibs: in.GroupStaticLibs,
Colin Cross3f40fa42015-01-30 17:27:36 -0800115 }
116}
Dan Willemsen110a89d2016-01-14 15:17:19 -0800117
Dan Willemsen20acc5c2016-05-25 14:47:21 -0700118func addPrefix(list []string, prefix string) []string {
119 for i := range list {
120 list[i] = prefix + list[i]
121 }
122 return list
123}