@@ -1003,6 +1003,22 @@ proc callCCompiler*(conf: ConfigRef) =
10031003
10041004template hashNimExe (): string = $ secureHashFile (os.getAppFilename ())
10051005
1006+ iterator compileCommandsJsonFiles * (conf: ConfigRef ): AbsoluteFile =
1007+ # `outFile` is better than `projectName`, as it allows having different json
1008+ # files for a given source file compiled with different options; it also
1009+ # works out of the box with `hashMainCompilationParams`.
1010+ var rf = if conf.projectCompileCommandJsons:
1011+ (" compile_commands_" & conf.outFile.changeFileExt (" json" ).string ).RelativeFile
1012+ else :
1013+ " compile_commands" .RelativeFile
1014+
1015+ yield getNimcacheDir (conf) / rf
1016+
1017+ if conf.copyCompileCommandsJsonToCurDir:
1018+ # TODO
1019+ yield getNimcacheDir (conf) / rf
1020+
1021+
10061022proc jsonBuildInstructionsFile * (conf: ConfigRef ): AbsoluteFile =
10071023 # `outFile` is better than `projectName`, as it allows having different json
10081024 # files for a given source file compiled with different options; it also
@@ -1027,6 +1043,16 @@ type BuildCache = object
10271043 depfiles: seq [(string , string )]
10281044 nimexe: string
10291045
1046+ proc jsonCompileCommands * (conf: ConfigRef ): JsonNode =
1047+ # ref: https://clang.llvm.org/docs/JSONCompilationDatabase.html
1048+ result = %* []
1049+ for it in conf.toCompile:
1050+ result .add %* {
1051+ " file" : it.cname.string ,
1052+ " command" : getCompileCFileCmd (conf, it),
1053+ " directory" : conf.outDir.string ,
1054+ }
1055+
10301056proc writeJsonBuildInstructions * (conf: ConfigRef ; deps: StringTableRef ) =
10311057 var linkFiles = collect (for it in conf.externalToLink:
10321058 var it = it
@@ -1060,8 +1086,15 @@ proc writeJsonBuildInstructions*(conf: ConfigRef; deps: StringTableRef) =
10601086 if fileExists (bcache.outputFile):
10611087 bcache.outputLastModificationTime = $ getLastModificationTime (bcache.outputFile)
10621088 conf.jsonBuildFile = conf.jsonBuildInstructionsFile
1089+
10631090 conf.jsonBuildFile.string .writeFile (bcache.toJson.pretty)
10641091
1092+ # NOTE: compile_commands.json uses indent of 2 by convention
1093+ if conf.compileCommandsJson:
1094+ let compileCmds = conf.jsonCompileCommands.pretty (2 )
1095+ for p in conf.compileCommandsJsonFiles:
1096+ p.writeFile (compileCmds)
1097+
10651098proc changeDetectedViaJsonBuildInstructions * (conf: ConfigRef ; jsonFile: AbsoluteFile ): bool =
10661099 result = false
10671100 if not fileExists (jsonFile) or not fileExists (conf.absOutFile): return true
0 commit comments