blob: fb6338ac2620047a401c201d13a5765c28381b9a [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"
Logan Chien031d2b32018-07-26 00:19:50 +080019 "path/filepath"
Colin Cross3f40fa42015-01-30 17:27:36 -080020 "regexp"
21 "strings"
Colin Crossc0b06f12015-04-08 13:03:43 -070022
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26// Efficiently converts a list of include directories to a single string
27// of cflags with -I prepended to each directory.
Colin Cross635c3b02016-05-18 15:37:25 -070028func includeDirsToFlags(dirs android.Paths) string {
29 return android.JoinWithPrefix(dirs.Strings(), "-I")
Colin Cross3f40fa42015-01-30 17:27:36 -080030}
31
32func ldDirsToFlags(dirs []string) string {
Colin Cross635c3b02016-05-18 15:37:25 -070033 return android.JoinWithPrefix(dirs, "-L")
Colin Cross3f40fa42015-01-30 17:27:36 -080034}
35
36func libNamesToFlags(names []string) string {
Colin Cross635c3b02016-05-18 15:37:25 -070037 return android.JoinWithPrefix(names, "-l")
Colin Cross3f40fa42015-01-30 17:27:36 -080038}
39
Colin Crossb4330e22017-12-22 15:47:09 -080040var indexList = android.IndexList
41var inList = android.InList
42var filterList = android.FilterList
43var removeListFromList = android.RemoveListFromList
44var removeFromList = android.RemoveFromList
Colin Cross16b23492016-01-06 14:41:07 -080045
Colin Cross3f40fa42015-01-30 17:27:36 -080046var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
47
48func moduleToLibName(module string) (string, error) {
49 matches := libNameRegexp.FindStringSubmatch(module)
50 if matches == nil {
51 return "", fmt.Errorf("Library module name %s does not start with lib", module)
52 }
53 return matches[1], nil
54}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056func flagsToBuilderFlags(in Flags) builderFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -080057 return builderFlags{
Chih-Hung Hsieh9e88ba92018-08-22 14:18:04 -070058 globalFlags: strings.Join(in.GlobalFlags, " "),
59 arFlags: strings.Join(in.ArFlags, " "),
60 asFlags: strings.Join(in.AsFlags, " "),
61 cFlags: strings.Join(in.CFlags, " "),
62 toolingCFlags: strings.Join(in.ToolingCFlags, " "),
63 toolingCppFlags: strings.Join(in.ToolingCppFlags, " "),
64 conlyFlags: strings.Join(in.ConlyFlags, " "),
65 cppFlags: strings.Join(in.CppFlags, " "),
Chih-Hung Hsieh9e88ba92018-08-22 14:18:04 -070066 aidlFlags: strings.Join(in.aidlFlags, " "),
67 rsFlags: strings.Join(in.rsFlags, " "),
68 ldFlags: strings.Join(in.LdFlags, " "),
69 libFlags: strings.Join(in.libFlags, " "),
Pete Bentley99f2fc22019-08-02 14:02:20 +010070 extraLibFlags: strings.Join(in.extraLibFlags, " "),
Chih-Hung Hsieh9e88ba92018-08-22 14:18:04 -070071 tidyFlags: strings.Join(in.TidyFlags, " "),
72 sAbiFlags: strings.Join(in.SAbiFlags, " "),
73 yasmFlags: strings.Join(in.YasmFlags, " "),
74 toolchain: in.Toolchain,
Chih-Hung Hsieh9e88ba92018-08-22 14:18:04 -070075 coverage: in.Coverage,
76 tidy: in.Tidy,
77 sAbiDump: in.SAbiDump,
Sasha Smundak2a4549e2018-11-05 16:49:08 -080078 emitXrefs: in.EmitXrefs,
Colin Cross18c0c5a2016-12-01 14:45:23 -080079
Colin Crossc3199482017-03-30 15:03:04 -070080 systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
81
Colin Cross18c0c5a2016-12-01 14:45:23 -080082 groupStaticLibs: in.GroupStaticLibs,
Dan Willemsen60e62f02018-11-16 21:05:32 -080083
Colin Cross19878da2019-03-28 14:45:07 -070084 proto: in.proto,
Dan Willemsen60e62f02018-11-16 21:05:32 -080085 protoC: in.protoC,
86 protoOptionsFile: in.protoOptionsFile,
Dan Willemsen4e0aa232019-04-10 22:59:54 -070087
88 yacc: in.Yacc,
Colin Cross3f40fa42015-01-30 17:27:36 -080089 }
90}
Dan Willemsen110a89d2016-01-14 15:17:19 -080091
Dan Willemsen20acc5c2016-05-25 14:47:21 -070092func addPrefix(list []string, prefix string) []string {
93 for i := range list {
94 list[i] = prefix + list[i]
95 }
96 return list
97}
Jiyong Parkab0fd5f2017-08-03 21:22:50 +090098
99func addSuffix(list []string, suffix string) []string {
100 for i := range list {
101 list[i] = list[i] + suffix
102 }
103 return list
104}
Logan Chien031d2b32018-07-26 00:19:50 +0800105
Logan Chiene4d5a6c2018-09-26 10:49:05 +0800106var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
Logan Chien031d2b32018-07-26 00:19:50 +0800107
108// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
109// the file extension and the version number (e.g. "libexample"). suffix stands for the
110// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
111// file extension after the version numbers are trimmed (e.g. ".so").
112func splitFileExt(name string) (string, string, string) {
113 // Extract and trim the shared lib version number if the file name ends with dot digits.
114 suffix := ""
115 matches := shlibVersionPattern.FindAllStringIndex(name, -1)
116 if len(matches) > 0 {
117 lastMatch := matches[len(matches)-1]
118 if lastMatch[1] == len(name) {
119 suffix = name[lastMatch[0]:lastMatch[1]]
120 name = name[0:lastMatch[0]]
121 }
122 }
123
124 // Extract the file name root and the file extension.
125 ext := filepath.Ext(name)
126 root := strings.TrimSuffix(name, ext)
127 suffix = ext + suffix
128
129 return root, suffix, ext
130}
Jiyong Parkf1194352019-02-25 11:05:47 +0900131
132// linkDirOnDevice/linkName -> target
133func makeSymlinkCmd(linkDirOnDevice string, linkName string, target string) string {
134 dir := filepath.Join("$(PRODUCT_OUT)", linkDirOnDevice)
135 return "mkdir -p " + dir + " && " +
136 "ln -sf " + target + " " + filepath.Join(dir, linkName)
137}