summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJaremy Creechley <creechley@gmail.com>2022-11-29 07:27:48 -0700
committerGitHub <noreply@github.com>2022-11-29 15:27:48 +0100
commit5658e8e5c5e3852f877eb0501b12e664114de567 (patch)
tree5ee1980e746106f7c28386d4e20dcc3155d08327
parent77a337a39abcf85b2ba60b90273eebadecedfc18 (diff)
downloadNim-5658e8e5c5e3852f877eb0501b12e664114de567.tar.gz
Add `--genCDeps` for better integration with CMake (#20950)
* add gencdeps option

* add case statement

* Update compiler/main.nim

* Update compiler/main.nim

* Apply suggestions from code review

Fixes

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
-rw-r--r--compiler/commands.nim3
-rw-r--r--compiler/main.nim22
-rw-r--r--compiler/options.nim1
3 files changed, 26 insertions, 0 deletions
diff --git a/compiler/commands.nim b/compiler/commands.nim
index c2ed01891..a252f505f 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -326,6 +326,7 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool
   of "run", "r": result = contains(conf.globalOptions, optRun)
   of "symbolfiles": result = conf.symbolFiles != disabledSf
   of "genscript": result = contains(conf.globalOptions, optGenScript)
+  of "gencdeps": result = contains(conf.globalOptions, optGenCDeps)
   of "threads": result = contains(conf.globalOptions, optThreads)
   of "tlsemulation": result = contains(conf.globalOptions, optTlsEmulation)
   of "implicitstatic": result = contains(conf.options, optImplicitStatic)
@@ -914,6 +915,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
     if switch.normalize == "gendeps": deprecatedAlias(switch, "genscript")
     processOnOffSwitchG(conf, {optGenScript}, arg, pass, info)
     processOnOffSwitchG(conf, {optCompileOnly}, arg, pass, info)
+  of "gencdeps":
+    processOnOffSwitchG(conf, {optGenCDeps}, arg, pass, info)
   of "colors": processOnOffSwitchG(conf, {optUseColors}, arg, pass, info)
   of "lib":
     expectArg(conf, switch, arg, pass, info)
diff --git a/compiler/main.nim b/compiler/main.nim
index 71e913d6c..b8e0e2f12 100644
--- a/compiler/main.nim
+++ b/compiler/main.nim
@@ -47,6 +47,26 @@ proc writeDepsFile(g: ModuleGraph) =
       f.writeLine(toFullPath(g.config, k))
   f.close()
 
+proc writeCMakeDepsFile(conf: ConfigRef) =
+  ## write a list of C files for build systems like CMake.
+  ## only updated when the C file list changes.
+  let fname = getNimcacheDir(conf) / conf.outFile.changeFileExt("cdeps")
+  # generate output files list
+  var cfiles: seq[string] = @[]
+  for it in conf.toCompile: cfiles.add(it.cname.string)
+  let fileset = cfiles.toCountTable()
+  # read old cfiles list
+  var fl: File
+  var prevset = initCountTable[string]()
+  if open(fl, fname.string, fmRead):
+    for line in fl.lines: prevset.inc(line)
+    fl.close()
+  # write cfiles out
+  if fileset != prevset:
+    fl = open(fname.string, fmWrite)
+    for line in cfiles: fl.writeLine(line)
+    fl.close()
+
 proc commandGenDepend(graph: ModuleGraph) =
   semanticPasses(graph)
   registerPass(graph, gendependPass)
@@ -126,6 +146,8 @@ proc commandCompileToC(graph: ModuleGraph) =
       extccomp.writeJsonBuildInstructions(conf)
     if optGenScript in graph.config.globalOptions:
       writeDepsFile(graph)
+    if optGenCDeps in graph.config.globalOptions:
+      writeCMakeDepsFile(conf)
 
 proc commandJsonScript(graph: ModuleGraph) =
   extccomp.runJsonBuildInstructions(graph.config, graph.config.jsonBuildInstructionsFile)
diff --git a/compiler/options.nim b/compiler/options.nim
index fc6f4c308..44275bb8c 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -60,6 +60,7 @@ type                          # please make sure we have under 32 options
     optGenStaticLib,          # generate a static library
     optGenGuiApp,             # generate a GUI application
     optGenScript,             # generate a script file to compile the *.c files
+    optGenCDeps,              # generate a list of *.c files to be read by CMake
     optGenMapping,            # generate a mapping file
     optRun,                   # run the compiled project
     optUseNimcache,           # save artifacts (including binary) in $nimcache