|
| 1 | +package cfig.lazybox.staging |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 4 | +import java.io.File |
| 5 | +import java.io.FileInputStream |
| 6 | +import java.util.regex.Pattern |
| 7 | +import java.util.zip.GZIPInputStream |
| 8 | + |
| 9 | +class AospCompiledb { |
| 10 | + data class CompileCommand( |
| 11 | + val directory: String, |
| 12 | + val command: String, |
| 13 | + val file: String |
| 14 | + ) |
| 15 | + |
| 16 | + fun findAndroidRoot(logFile: File): String { |
| 17 | + // Get absolute path of the log file |
| 18 | + val logAbsPath = logFile.absoluteFile |
| 19 | + |
| 20 | + // The log is in out/verbose.log.gz, so Android root is the parent of the "out" directory |
| 21 | + var currentDir = logAbsPath.parentFile // This should be "out" directory |
| 22 | + while (currentDir != null && currentDir.name != "out") { |
| 23 | + currentDir = currentDir.parentFile |
| 24 | + } |
| 25 | + |
| 26 | + return if (currentDir != null) { |
| 27 | + // Go up one more level to get the Android root (parent of "out") |
| 28 | + currentDir.parentFile?.absolutePath ?: System.getProperty("user.dir") |
| 29 | + } else { |
| 30 | + // Fallback: try to find Android root by looking for typical Android files |
| 31 | + var dir = logAbsPath.parentFile |
| 32 | + while (dir != null) { |
| 33 | + if (File(dir, "build/make").exists() || File(dir, "Makefile").exists() || File(dir, "build.gradle").exists()) { |
| 34 | + return dir.absolutePath |
| 35 | + } |
| 36 | + dir = dir.parentFile |
| 37 | + } |
| 38 | + System.getProperty("user.dir") |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fun parseVerboseLog(gzFile: File, androidRoot: String): List<CompileCommand> { |
| 43 | + val compileCommands = mutableListOf<CompileCommand>() |
| 44 | + val ninjaCommandPattern = Pattern.compile("""^\[(\d+)/(\d+)\]\s+(.+)$""") |
| 45 | + |
| 46 | + GZIPInputStream(FileInputStream(gzFile)).bufferedReader().use { reader -> |
| 47 | + reader.lineSequence().forEach { line -> |
| 48 | + val matcher = ninjaCommandPattern.matcher(line) |
| 49 | + if (matcher.matches()) { |
| 50 | + val command = matcher.group(3) |
| 51 | + |
| 52 | + // Only process compilation commands (with -c flag), not linking commands |
| 53 | + if (command.contains(" -c ") && command.contains("clang")) { |
| 54 | + val compileCommand = parseCompilationCommand(command, androidRoot) |
| 55 | + if (compileCommand != null) { |
| 56 | + compileCommands.add(compileCommand) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return compileCommands |
| 64 | + } |
| 65 | + |
| 66 | + fun parseCompilationCommand(commandLine: String, androidRoot: String): CompileCommand? { |
| 67 | + try { |
| 68 | + // Parse the command to extract compiler path, flags, and source file |
| 69 | + val parts = splitCommandLine(commandLine) |
| 70 | + if (parts.isEmpty()) return null |
| 71 | + |
| 72 | + // Find the compiler executable |
| 73 | + val compilerIndex = parts.indexOfFirst { it.contains("clang") } |
| 74 | + if (compilerIndex == -1) return null |
| 75 | + |
| 76 | + // Find source files (typically .c, .cpp, .cc files that are not output files) |
| 77 | + val sourceFiles = findSourceFiles(parts) |
| 78 | + if (sourceFiles.isEmpty()) return null |
| 79 | + |
| 80 | + // Build the clean command (without PWD prefix) |
| 81 | + val cleanCommand = buildCleanCommand(parts, compilerIndex) |
| 82 | + |
| 83 | + // Create compile command for each source file |
| 84 | + val sourceFile = sourceFiles.first() // Take the first source file |
| 85 | + |
| 86 | + return CompileCommand( |
| 87 | + directory = androidRoot, |
| 88 | + command = cleanCommand, |
| 89 | + file = sourceFile |
| 90 | + ) |
| 91 | + } catch (e: Exception) { |
| 92 | + println("Warning: Failed to parse command: ${e.message}") |
| 93 | + return null |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + fun splitCommandLine(commandLine: String): List<String> { |
| 99 | + // Remove PWD= prefix if present |
| 100 | + val cleanCommand = commandLine.replace(Regex("""PWD=[^\s]+\s*"""), "") |
| 101 | + |
| 102 | + // Simple command line splitting (handles basic quoting) |
| 103 | + val parts = mutableListOf<String>() |
| 104 | + var current = StringBuilder() |
| 105 | + var inQuotes = false |
| 106 | + var escapeNext = false |
| 107 | + |
| 108 | + for (char in cleanCommand) { |
| 109 | + when { |
| 110 | + escapeNext -> { |
| 111 | + current.append(char) |
| 112 | + escapeNext = false |
| 113 | + } |
| 114 | + char == '\\' -> { |
| 115 | + escapeNext = true |
| 116 | + } |
| 117 | + char == '"' -> { |
| 118 | + inQuotes = !inQuotes |
| 119 | + } |
| 120 | + char == ' ' && !inQuotes -> { |
| 121 | + if (current.isNotEmpty()) { |
| 122 | + parts.add(current.toString()) |
| 123 | + current = StringBuilder() |
| 124 | + } |
| 125 | + } |
| 126 | + else -> { |
| 127 | + current.append(char) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (current.isNotEmpty()) { |
| 133 | + parts.add(current.toString()) |
| 134 | + } |
| 135 | + |
| 136 | + return parts |
| 137 | + } |
| 138 | + |
| 139 | + fun findSourceFiles(parts: List<String>): List<String> { |
| 140 | + val sourceExtensions = setOf(".c", ".cpp", ".cc", ".cxx", ".c++") |
| 141 | + return parts.filter { part -> |
| 142 | + sourceExtensions.any { ext -> part.endsWith(ext) } && |
| 143 | + !part.startsWith("-") && // Not a flag |
| 144 | + !part.contains("crtbegin") && // Not a crt file |
| 145 | + !part.contains("crtend") && |
| 146 | + File(part).extension in sourceExtensions.map { it.substring(1) } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + fun buildCleanCommand(parts: List<String>, compilerIndex: Int): String { |
| 151 | + // Join all parts starting from the compiler |
| 152 | + return parts.drop(compilerIndex).joinToString(" ") |
| 153 | + } |
| 154 | + |
| 155 | + fun run() { |
| 156 | + val logFile = File("out/verbose.log.gz") |
| 157 | + val outputFile = File("compile_commands.json") |
| 158 | + |
| 159 | + if (!logFile.exists()) { |
| 160 | + println("Error: verbose.log.gz not found in out/ directory") |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + // Find Android root directory from verbose log location |
| 165 | + val androidRoot = findAndroidRoot(logFile) |
| 166 | + println("Android root directory: $androidRoot") |
| 167 | + |
| 168 | + println("Parsing verbose build log...") |
| 169 | + val compileCommands = parseVerboseLog(logFile, androidRoot) |
| 170 | + |
| 171 | + println("Found ${compileCommands.size} compilation commands") |
| 172 | + |
| 173 | + // Generate JSON |
| 174 | + val json = ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(compileCommands) |
| 175 | + |
| 176 | + outputFile.writeText(json) |
| 177 | + println("Generated compile_commands.json with ${compileCommands.size} entries") |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments