• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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	"path/filepath"
20	"regexp"
21	"strings"
22
23	"android/soong/android"
24)
25
26// Efficiently converts a list of include directories to a single string
27// of cflags with -I prepended to each directory.
28func includeDirsToFlags(dirs android.Paths) string {
29	return android.JoinWithPrefix(dirs.Strings(), "-I")
30}
31
32func includeFilesToFlags(files android.Paths) string {
33	return android.JoinWithPrefix(files.Strings(), "-include ")
34}
35
36func ldDirsToFlags(dirs []string) string {
37	return android.JoinWithPrefix(dirs, "-L")
38}
39
40func libNamesToFlags(names []string) string {
41	return android.JoinWithPrefix(names, "-l")
42}
43
44var indexList = android.IndexList
45var inList = android.InList
46var filterList = android.FilterList
47var removeListFromList = android.RemoveListFromList
48var removeFromList = android.RemoveFromList
49
50var libNameRegexp = regexp.MustCompile(`^lib(.*)$`)
51
52func moduleToLibName(module string) (string, error) {
53	matches := libNameRegexp.FindStringSubmatch(module)
54	if matches == nil {
55		return "", fmt.Errorf("Library module name %s does not start with lib", module)
56	}
57	return matches[1], nil
58}
59
60func flagsToBuilderFlags(in Flags) builderFlags {
61	return builderFlags{
62		globalFlags:     strings.Join(in.GlobalFlags, " "),
63		arFlags:         strings.Join(in.ArFlags, " "),
64		asFlags:         strings.Join(in.AsFlags, " "),
65		cFlags:          strings.Join(in.CFlags, " "),
66		toolingCFlags:   strings.Join(in.ToolingCFlags, " "),
67		toolingCppFlags: strings.Join(in.ToolingCppFlags, " "),
68		conlyFlags:      strings.Join(in.ConlyFlags, " "),
69		cppFlags:        strings.Join(in.CppFlags, " "),
70		yaccFlags:       strings.Join(in.YaccFlags, " "),
71		aidlFlags:       strings.Join(in.aidlFlags, " "),
72		rsFlags:         strings.Join(in.rsFlags, " "),
73		ldFlags:         strings.Join(in.LdFlags, " "),
74		libFlags:        strings.Join(in.libFlags, " "),
75		tidyFlags:       strings.Join(in.TidyFlags, " "),
76		sAbiFlags:       strings.Join(in.SAbiFlags, " "),
77		yasmFlags:       strings.Join(in.YasmFlags, " "),
78		toolchain:       in.Toolchain,
79		coverage:        in.Coverage,
80		tidy:            in.Tidy,
81		sAbiDump:        in.SAbiDump,
82
83		systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
84
85		groupStaticLibs: in.GroupStaticLibs,
86
87		proto:            in.proto,
88		protoC:           in.protoC,
89		protoOptionsFile: in.protoOptionsFile,
90	}
91}
92
93func addPrefix(list []string, prefix string) []string {
94	for i := range list {
95		list[i] = prefix + list[i]
96	}
97	return list
98}
99
100func addSuffix(list []string, suffix string) []string {
101	for i := range list {
102		list[i] = list[i] + suffix
103	}
104	return list
105}
106
107var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
108
109// splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
110// the file extension and the version number (e.g. "libexample"). suffix stands for the
111// concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the
112// file extension after the version numbers are trimmed (e.g. ".so").
113func splitFileExt(name string) (string, string, string) {
114	// Extract and trim the shared lib version number if the file name ends with dot digits.
115	suffix := ""
116	matches := shlibVersionPattern.FindAllStringIndex(name, -1)
117	if len(matches) > 0 {
118		lastMatch := matches[len(matches)-1]
119		if lastMatch[1] == len(name) {
120			suffix = name[lastMatch[0]:lastMatch[1]]
121			name = name[0:lastMatch[0]]
122		}
123	}
124
125	// Extract the file name root and the file extension.
126	ext := filepath.Ext(name)
127	root := strings.TrimSuffix(name, ext)
128	suffix = ext + suffix
129
130	return root, suffix, ext
131}
132
133// linkDirOnDevice/linkName -> target
134func makeSymlinkCmd(linkDirOnDevice string, linkName string, target string) string {
135	dir := filepath.Join("$(PRODUCT_OUT)", linkDirOnDevice)
136	return "mkdir -p " + dir + " && " +
137		"ln -sf " + target + " " + filepath.Join(dir, linkName)
138}
139