| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 |  | 
 | 15 | package cc | 
 | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"fmt" | 
| Logan Chien | 031d2b3 | 2018-07-26 00:19:50 +0800 | [diff] [blame] | 19 | 	"path/filepath" | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 20 | 	"regexp" | 
 | 21 | 	"strings" | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 22 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 23 | 	"android/soong/android" | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 24 | ) | 
 | 25 |  | 
 | 26 | // Efficiently converts a list of include directories to a single string | 
 | 27 | // of cflags with -I prepended to each directory. | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 28 | func includeDirsToFlags(dirs android.Paths) string { | 
 | 29 | 	return android.JoinWithPrefix(dirs.Strings(), "-I") | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 | } | 
 | 31 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 32 | func includeFilesToFlags(files android.Paths) string { | 
 | 33 | 	return android.JoinWithPrefix(files.Strings(), "-include ") | 
| Colin Cross | 39d97f2 | 2015-09-14 12:30:50 -0700 | [diff] [blame] | 34 | } | 
 | 35 |  | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 36 | func ldDirsToFlags(dirs []string) string { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 37 | 	return android.JoinWithPrefix(dirs, "-L") | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | } | 
 | 39 |  | 
 | 40 | func libNamesToFlags(names []string) string { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 41 | 	return android.JoinWithPrefix(names, "-l") | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 42 | } | 
 | 43 |  | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 44 | var indexList = android.IndexList | 
 | 45 | var inList = android.InList | 
 | 46 | var filterList = android.FilterList | 
 | 47 | var removeListFromList = android.RemoveListFromList | 
 | 48 | var removeFromList = android.RemoveFromList | 
| Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 49 |  | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 50 | var libNameRegexp = regexp.MustCompile(`^lib(.*)$`) | 
 | 51 |  | 
 | 52 | func 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 |  | 
| Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 60 | func flagsToBuilderFlags(in Flags) builderFlags { | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 61 | 	return builderFlags{ | 
| Chih-Hung Hsieh | 9e88ba9 | 2018-08-22 14:18:04 -0700 | [diff] [blame] | 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, " "), | 
| Chih-Hung Hsieh | 9e88ba9 | 2018-08-22 14:18:04 -0700 | [diff] [blame] | 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, | 
| Chih-Hung Hsieh | 9e88ba9 | 2018-08-22 14:18:04 -0700 | [diff] [blame] | 79 | 		coverage:        in.Coverage, | 
 | 80 | 		tidy:            in.Tidy, | 
 | 81 | 		sAbiDump:        in.SAbiDump, | 
| Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 82 |  | 
| Colin Cross | c319948 | 2017-03-30 15:03:04 -0700 | [diff] [blame] | 83 | 		systemIncludeFlags: strings.Join(in.SystemIncludeFlags, " "), | 
 | 84 |  | 
| Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 85 | 		groupStaticLibs: in.GroupStaticLibs, | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 86 |  | 
 | 87 | 		protoDeps:        in.protoDeps, | 
 | 88 | 		protoFlags:       strings.Join(in.protoFlags, " "), | 
 | 89 | 		protoOutTypeFlag: in.protoOutTypeFlag, | 
 | 90 | 		protoOutParams:   strings.Join(in.protoOutParams, ","), | 
 | 91 | 		protoC:           in.protoC, | 
 | 92 | 		protoOptionsFile: in.protoOptionsFile, | 
 | 93 | 		protoRoot:        in.ProtoRoot, | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 94 | 	} | 
 | 95 | } | 
| Dan Willemsen | 110a89d | 2016-01-14 15:17:19 -0800 | [diff] [blame] | 96 |  | 
| Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 97 | func addPrefix(list []string, prefix string) []string { | 
 | 98 | 	for i := range list { | 
 | 99 | 		list[i] = prefix + list[i] | 
 | 100 | 	} | 
 | 101 | 	return list | 
 | 102 | } | 
| Jiyong Park | ab0fd5f | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 103 |  | 
 | 104 | func addSuffix(list []string, suffix string) []string { | 
 | 105 | 	for i := range list { | 
 | 106 | 		list[i] = list[i] + suffix | 
 | 107 | 	} | 
 | 108 | 	return list | 
 | 109 | } | 
| Logan Chien | 031d2b3 | 2018-07-26 00:19:50 +0800 | [diff] [blame] | 110 |  | 
| Logan Chien | e4d5a6c | 2018-09-26 10:49:05 +0800 | [diff] [blame] | 111 | var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") | 
| Logan Chien | 031d2b3 | 2018-07-26 00:19:50 +0800 | [diff] [blame] | 112 |  | 
 | 113 | // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without | 
 | 114 | // the file extension and the version number (e.g. "libexample"). suffix stands for the | 
 | 115 | // concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the | 
 | 116 | // file extension after the version numbers are trimmed (e.g. ".so"). | 
 | 117 | func splitFileExt(name string) (string, string, string) { | 
 | 118 | 	// Extract and trim the shared lib version number if the file name ends with dot digits. | 
 | 119 | 	suffix := "" | 
 | 120 | 	matches := shlibVersionPattern.FindAllStringIndex(name, -1) | 
 | 121 | 	if len(matches) > 0 { | 
 | 122 | 		lastMatch := matches[len(matches)-1] | 
 | 123 | 		if lastMatch[1] == len(name) { | 
 | 124 | 			suffix = name[lastMatch[0]:lastMatch[1]] | 
 | 125 | 			name = name[0:lastMatch[0]] | 
 | 126 | 		} | 
 | 127 | 	} | 
 | 128 |  | 
 | 129 | 	// Extract the file name root and the file extension. | 
 | 130 | 	ext := filepath.Ext(name) | 
 | 131 | 	root := strings.TrimSuffix(name, ext) | 
 | 132 | 	suffix = ext + suffix | 
 | 133 |  | 
 | 134 | 	return root, suffix, ext | 
 | 135 | } |