| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | 	"encoding/json" | 
 | 19 | 	"log" | 
 | 20 | 	"os" | 
 | 21 | 	"path/filepath" | 
 | 22 | 	"strings" | 
 | 23 |  | 
 | 24 | 	"android/soong/android" | 
 | 25 | ) | 
 | 26 |  | 
 | 27 | // This singleton generates a compile_commands.json file. It does so for each | 
 | 28 | // blueprint Android.bp resulting in a cc.Module when either make, mm, mma, mmm | 
 | 29 | // or mmma is called. It will only create a single compile_commands.json file | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 30 | // at ${OUT_DIR}/soong/development/ide/compdb/compile_commands.json. It will also symlink it | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 31 | // to ${SOONG_LINK_COMPDB_TO} if set. In general this should be created by running | 
 | 32 | // make SOONG_GEN_COMPDB=1 nothing to get all targets. | 
 | 33 |  | 
 | 34 | func init() { | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 35 | 	android.RegisterParallelSingletonType("compdb_generator", compDBGeneratorSingleton) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 36 | } | 
 | 37 |  | 
 | 38 | func compDBGeneratorSingleton() android.Singleton { | 
 | 39 | 	return &compdbGeneratorSingleton{} | 
 | 40 | } | 
 | 41 |  | 
 | 42 | type compdbGeneratorSingleton struct{} | 
 | 43 |  | 
 | 44 | const ( | 
 | 45 | 	compdbFilename                = "compile_commands.json" | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 46 | 	compdbOutputProjectsDirectory = "development/ide/compdb" | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 47 |  | 
 | 48 | 	// Environment variables used to modify behavior of this singleton. | 
 | 49 | 	envVariableGenerateCompdb          = "SOONG_GEN_COMPDB" | 
 | 50 | 	envVariableGenerateCompdbDebugInfo = "SOONG_GEN_COMPDB_DEBUG" | 
 | 51 | 	envVariableCompdbLink              = "SOONG_LINK_COMPDB_TO" | 
 | 52 | ) | 
 | 53 |  | 
 | 54 | // A compdb entry. The compile_commands.json file is a list of these. | 
 | 55 | type compDbEntry struct { | 
 | 56 | 	Directory string   `json:"directory"` | 
 | 57 | 	Arguments []string `json:"arguments"` | 
 | 58 | 	File      string   `json:"file"` | 
 | 59 | 	Output    string   `json:"output,omitempty"` | 
 | 60 | } | 
 | 61 |  | 
 | 62 | func (c *compdbGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
 | 63 | 	if !ctx.Config().IsEnvTrue(envVariableGenerateCompdb) { | 
 | 64 | 		return | 
 | 65 | 	} | 
 | 66 |  | 
 | 67 | 	// Instruct the generator to indent the json file for easier debugging. | 
 | 68 | 	outputCompdbDebugInfo := ctx.Config().IsEnvTrue(envVariableGenerateCompdbDebugInfo) | 
 | 69 |  | 
 | 70 | 	// We only want one entry per file. We don't care what module/isa it's from | 
 | 71 | 	m := make(map[string]compDbEntry) | 
 | 72 | 	ctx.VisitAllModules(func(module android.Module) { | 
 | 73 | 		if ccModule, ok := module.(*Module); ok { | 
 | 74 | 			if compiledModule, ok := ccModule.compiler.(CompiledInterface); ok { | 
 | 75 | 				generateCompdbProject(compiledModule, ctx, ccModule, m) | 
 | 76 | 			} | 
 | 77 | 		} | 
 | 78 | 	}) | 
 | 79 |  | 
 | 80 | 	// Create the output file. | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 81 | 	dir := android.PathForOutput(ctx, compdbOutputProjectsDirectory) | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 82 | 	os.MkdirAll(filepath.Join(android.AbsSrcDirForExistingUseCases(), dir.String()), 0777) | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 83 | 	compDBFile := dir.Join(ctx, compdbFilename) | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 84 | 	f, err := os.Create(filepath.Join(android.AbsSrcDirForExistingUseCases(), compDBFile.String())) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 85 | 	if err != nil { | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 86 | 		log.Fatalf("Could not create file %s: %s", compDBFile, err) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 87 | 	} | 
| Tamir Duberstein | cc5ce65 | 2024-08-09 08:50:57 -0700 | [diff] [blame] | 88 | 	defer func() { | 
 | 89 | 		if err := f.Close(); err != nil { | 
 | 90 | 			log.Fatalf("Could not close file %s: %s", compDBFile, err) | 
 | 91 | 		} | 
 | 92 | 	}() | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 93 |  | 
 | 94 | 	v := make([]compDbEntry, 0, len(m)) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 95 | 	for _, value := range m { | 
 | 96 | 		v = append(v, value) | 
 | 97 | 	} | 
| Tamir Duberstein | cc5ce65 | 2024-08-09 08:50:57 -0700 | [diff] [blame] | 98 |  | 
 | 99 | 	w := json.NewEncoder(f) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 100 | 	if outputCompdbDebugInfo { | 
| Tamir Duberstein | cc5ce65 | 2024-08-09 08:50:57 -0700 | [diff] [blame] | 101 | 		w.SetIndent("", " ") | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 102 | 	} | 
| Tamir Duberstein | cc5ce65 | 2024-08-09 08:50:57 -0700 | [diff] [blame] | 103 | 	if err := w.Encode(v); err != nil { | 
 | 104 | 		log.Fatalf("Failed to encode: %s", err) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 105 | 	} | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 106 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 107 | 	if finalLinkDir := ctx.Config().Getenv(envVariableCompdbLink); finalLinkDir != "" { | 
 | 108 | 		finalLinkPath := filepath.Join(finalLinkDir, compdbFilename) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 109 | 		os.Remove(finalLinkPath) | 
| Changyeon Jo | 9627925 | 2019-12-19 23:26:16 +0000 | [diff] [blame] | 110 | 		if err := os.Symlink(compDBFile.String(), finalLinkPath); err != nil { | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 111 | 			log.Fatalf("Unable to symlink %s to %s: %s", compDBFile, finalLinkPath, err) | 
 | 112 | 		} | 
 | 113 | 	} | 
 | 114 | } | 
 | 115 |  | 
 | 116 | func expandAllVars(ctx android.SingletonContext, args []string) []string { | 
 | 117 | 	var out []string | 
 | 118 | 	for _, arg := range args { | 
 | 119 | 		if arg != "" { | 
 | 120 | 			if val, err := evalAndSplitVariable(ctx, arg); err == nil { | 
 | 121 | 				out = append(out, val...) | 
 | 122 | 			} else { | 
 | 123 | 				out = append(out, arg) | 
 | 124 | 			} | 
 | 125 | 		} | 
 | 126 | 	} | 
 | 127 | 	return out | 
 | 128 | } | 
 | 129 |  | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 130 | func getArguments(src android.Path, ctx android.SingletonContext, ccModule *Module, ccPath string, cxxPath string) []string { | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 131 | 	var args []string | 
 | 132 | 	isCpp := false | 
 | 133 | 	isAsm := false | 
 | 134 | 	// TODO It would be better to ask soong for the types here. | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 135 | 	var clangPath string | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 136 | 	switch src.Ext() { | 
 | 137 | 	case ".S", ".s", ".asm": | 
 | 138 | 		isAsm = true | 
 | 139 | 		isCpp = false | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 140 | 		clangPath = ccPath | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 141 | 	case ".c": | 
 | 142 | 		isAsm = false | 
 | 143 | 		isCpp = false | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 144 | 		clangPath = ccPath | 
| Colin Cross | d34ab7c | 2019-06-27 14:46:10 -0700 | [diff] [blame] | 145 | 	case ".cpp", ".cc", ".cxx", ".mm": | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 146 | 		isAsm = false | 
 | 147 | 		isCpp = true | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 148 | 		clangPath = cxxPath | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 149 | 	default: | 
 | 150 | 		log.Print("Unknown file extension " + src.Ext() + " on file " + src.String()) | 
 | 151 | 		isAsm = true | 
 | 152 | 		isCpp = false | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 153 | 		clangPath = ccPath | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 154 | 	} | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 155 | 	args = append(args, clangPath) | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 156 | 	args = append(args, expandAllVars(ctx, ccModule.flags.Global.CommonFlags)...) | 
 | 157 | 	args = append(args, expandAllVars(ctx, ccModule.flags.Local.CommonFlags)...) | 
 | 158 | 	args = append(args, expandAllVars(ctx, ccModule.flags.Global.CFlags)...) | 
 | 159 | 	args = append(args, expandAllVars(ctx, ccModule.flags.Local.CFlags)...) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 160 | 	if isCpp { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 161 | 		args = append(args, expandAllVars(ctx, ccModule.flags.Global.CppFlags)...) | 
 | 162 | 		args = append(args, expandAllVars(ctx, ccModule.flags.Local.CppFlags)...) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 163 | 	} else if !isAsm { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 164 | 		args = append(args, expandAllVars(ctx, ccModule.flags.Global.ConlyFlags)...) | 
 | 165 | 		args = append(args, expandAllVars(ctx, ccModule.flags.Local.ConlyFlags)...) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 166 | 	} | 
 | 167 | 	args = append(args, expandAllVars(ctx, ccModule.flags.SystemIncludeFlags)...) | 
| Luis Useche | 342fa6b | 2024-04-01 19:33:18 -0700 | [diff] [blame] | 168 | 	args = append(args, expandAllVars(ctx, ccModule.flags.NoOverrideFlags)...) | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 169 | 	args = append(args, src.String()) | 
 | 170 | 	return args | 
 | 171 | } | 
 | 172 |  | 
 | 173 | func generateCompdbProject(compiledModule CompiledInterface, ctx android.SingletonContext, ccModule *Module, builds map[string]compDbEntry) { | 
 | 174 | 	srcs := compiledModule.Srcs() | 
 | 175 | 	if len(srcs) == 0 { | 
 | 176 | 		return | 
 | 177 | 	} | 
 | 178 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 179 | 	pathToCC, err := ctx.Eval(pctx, "${config.ClangBin}") | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 180 | 	ccPath := "/bin/false" | 
 | 181 | 	cxxPath := "/bin/false" | 
 | 182 | 	if err == nil { | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 183 | 		ccPath = filepath.Join(pathToCC, "clang") | 
 | 184 | 		cxxPath = filepath.Join(pathToCC, "clang++") | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 185 | 	} | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 186 | 	for _, src := range srcs { | 
 | 187 | 		if _, ok := builds[src.String()]; !ok { | 
 | 188 | 			builds[src.String()] = compDbEntry{ | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 189 | 				Directory: android.AbsSrcDirForExistingUseCases(), | 
| Alex Light | be96aea | 2018-11-07 11:35:47 -0800 | [diff] [blame] | 190 | 				Arguments: getArguments(src, ctx, ccModule, ccPath, cxxPath), | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 191 | 				File:      src.String(), | 
 | 192 | 			} | 
 | 193 | 		} | 
 | 194 | 	} | 
 | 195 | } | 
 | 196 |  | 
 | 197 | func evalAndSplitVariable(ctx android.SingletonContext, str string) ([]string, error) { | 
 | 198 | 	evaluated, err := ctx.Eval(pctx, str) | 
 | 199 | 	if err == nil { | 
| Alex Light | 0c7cc1c | 2018-10-02 11:19:46 -0700 | [diff] [blame] | 200 | 		return strings.Fields(evaluated), nil | 
| Alex Light | ec868fc | 2018-04-17 16:50:48 -0700 | [diff] [blame] | 201 | 	} | 
 | 202 | 	return []string{""}, err | 
 | 203 | } |