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	"path/filepath"
19	"strings"
20
21	"android/soong/android"
22)
23
24// Efficiently converts a list of include directories to a single string
25// of cflags with -I prepended to each directory.
26func includeDirsToFlags(dirs android.Paths) string {
27	return android.JoinWithPrefix(dirs.Strings(), "-I")
28}
29
30var indexList = android.IndexList[string]
31var inList = android.InList[string]
32var filterList = android.FilterList
33var removeListFromList = android.RemoveListFromList
34var removeFromList = android.RemoveFromList
35
36func flagsToBuilderFlags(in Flags) builderFlags {
37	return builderFlags{
38		globalCommonFlags:     strings.Join(in.Global.CommonFlags, " "),
39		globalAsFlags:         strings.Join(in.Global.AsFlags, " "),
40		globalYasmFlags:       strings.Join(in.Global.YasmFlags, " "),
41		globalCFlags:          strings.Join(in.Global.CFlags, " "),
42		globalToolingCFlags:   strings.Join(in.Global.ToolingCFlags, " "),
43		globalToolingCppFlags: strings.Join(in.Global.ToolingCppFlags, " "),
44		globalConlyFlags:      strings.Join(in.Global.ConlyFlags, " "),
45		globalCppFlags:        strings.Join(in.Global.CppFlags, " "),
46		globalLdFlags:         strings.Join(in.Global.LdFlags, " "),
47
48		localCommonFlags:     strings.Join(in.Local.CommonFlags, " "),
49		localAsFlags:         strings.Join(in.Local.AsFlags, " "),
50		localYasmFlags:       strings.Join(in.Local.YasmFlags, " "),
51		localCFlags:          strings.Join(in.Local.CFlags, " "),
52		localToolingCFlags:   strings.Join(in.Local.ToolingCFlags, " "),
53		localToolingCppFlags: strings.Join(in.Local.ToolingCppFlags, " "),
54		localConlyFlags:      strings.Join(in.Local.ConlyFlags, " "),
55		localCppFlags:        strings.Join(in.Local.CppFlags, " "),
56		localLdFlags:         strings.Join(in.Local.LdFlags, " "),
57
58		noOverrideFlags: strings.Join(in.NoOverrideFlags, " "),
59		aidlFlags:       strings.Join(in.aidlFlags, " "),
60		rsFlags:         strings.Join(in.rsFlags, " "),
61		libFlags:        strings.Join(in.libFlags, " "),
62		extraLibFlags:   strings.Join(in.extraLibFlags, " "),
63		tidyFlags:       strings.Join(in.TidyFlags, " "),
64		sAbiFlags:       strings.Join(in.SAbiFlags, " "),
65		toolchain:       in.Toolchain,
66		gcovCoverage:    in.GcovCoverage,
67		tidy:            in.Tidy,
68		needTidyFiles:   in.NeedTidyFiles,
69		sAbiDump:        in.SAbiDump,
70		emitXrefs:       in.EmitXrefs,
71		clangVerify:     in.ClangVerify,
72
73		systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "),
74
75		assemblerWithCpp: in.AssemblerWithCpp,
76
77		proto:            in.proto,
78		protoC:           in.protoC,
79		protoOptionsFile: in.protoOptionsFile,
80
81		yacc: in.Yacc,
82		lex:  in.Lex,
83	}
84}
85
86func flagsToStripFlags(in Flags) StripFlags {
87	return StripFlags{Toolchain: in.Toolchain}
88}
89
90func addPrefix(list []string, prefix string) []string {
91	for i := range list {
92		list[i] = prefix + list[i]
93	}
94	return list
95}
96
97// linkDirOnDevice/linkName -> target
98func makeSymlinkCmd(linkDirOnDevice string, linkName string, target string) string {
99	dir := filepath.Join("$(PRODUCT_OUT)", linkDirOnDevice)
100	return "mkdir -p " + dir + " && " +
101		"ln -sf " + target + " " + filepath.Join(dir, linkName)
102}
103
104func WriteStringToFileRule(ctx android.SingletonContext, content, out string) android.OutputPath {
105	outPath := android.PathForOutput(ctx, out)
106	android.WriteFileRule(ctx, outPath, content)
107	return outPath
108}
109
110// Dump a map to a list file as:
111//
112// {key1} {value1}
113// {key2} {value2}
114// ...
115func installMapListFileRule(ctx android.SingletonContext, m map[string]string, path string) android.OutputPath {
116	var txtBuilder strings.Builder
117	for idx, k := range android.SortedKeys(m) {
118		if idx > 0 {
119			txtBuilder.WriteString("\n")
120		}
121		txtBuilder.WriteString(k)
122		txtBuilder.WriteString(" ")
123		txtBuilder.WriteString(m[k])
124	}
125	return WriteStringToFileRule(ctx, txtBuilder.String(), path)
126}
127