1212import importlib
1313import importlib .util
1414import inspect
15+ import json
1516import os
1617import re
1718import string
2728materialisingStack = []
2829defaultGlobals = {}
2930outputTargets = set ()
31+ commandsDb = []
3032
3133RE_FORMAT_SPEC = re .compile (
3234 r"(?:(?P<fill>[\s\S])?(?P<align>[<>=^]))?"
@@ -540,6 +542,15 @@ def shell(*args):
540542 shellFp .write (s )
541543
542544
545+ def add_commanddb_entry (commands , file ):
546+ global commandsDb
547+ commandsDb += [{
548+ "directory" : os .getcwd (),
549+ "command" : (" && " .join (commands )),
550+ "file" : file
551+ }
552+ ]
553+
543554def emit_rule (self , ins , outs , cmds = [], label = None ):
544555 name = self .name
545556 fins = [self .templateexpand (f ) for f in set (filenamesof (ins ))]
@@ -558,19 +569,26 @@ def emit_rule(self, ins, outs, cmds=[], label=None):
558569 os .makedirs (self .dir , exist_ok = True )
559570 rule = []
560571
561- sandbox = join (self .dir , "sandbox" )
562- emit (f"rm -rf { sandbox } " , into = rule )
563- emit (
564- f"{ G .PYTHON } build/_sandbox.py --link -s" , sandbox , * fins , into = rule
565- )
566- for c in cmds :
567- emit (f"(cd { sandbox } &&" , c , ")" , into = rule )
568- emit (
569- f"{ G .PYTHON } build/_sandbox.py --export -s" ,
570- sandbox ,
571- * fouts ,
572- into = rule ,
573- )
572+ if G .AB_SANDBOX == "yes" :
573+ sandbox = join (self .dir , "sandbox" )
574+ emit (f"rm -rf { sandbox } " , into = rule )
575+ emit (
576+ f"{ G .PYTHON } build/_sandbox.py --link -s" ,
577+ sandbox ,
578+ * fins ,
579+ into = rule ,
580+ )
581+ for c in cmds :
582+ emit (f"(cd { sandbox } &&" , c , ")" , into = rule )
583+ emit (
584+ f"{ G .PYTHON } build/_sandbox.py --export -s" ,
585+ sandbox ,
586+ * fouts ,
587+ into = rule ,
588+ )
589+ else :
590+ for c in cmds :
591+ emit (c , into = rule )
574592
575593 ruletext = "" .join (rule )
576594 if len (ruletext ) > 7000 :
@@ -581,7 +599,7 @@ def emit_rule(self, ins, outs, cmds=[], label=None):
581599 fp .write ("set -e\n " )
582600 fp .write (ruletext )
583601
584- emit ("build" , * fouts , ":rule" , * fins , rulef )
602+ emit ("build" , * fouts , ":rule" , * fins )
585603 emit (" command=sh" , rulef )
586604 else :
587605 emit ("build" , * fouts , ":rule" , * fins )
@@ -592,7 +610,6 @@ def emit_rule(self, ins, outs, cmds=[], label=None):
592610 if label :
593611 emit (" description=" , label )
594612 emit ("build" , name , ":phony" , * fouts )
595-
596613 else :
597614 assert len (cmds ) == 0 , "rules with no outputs cannot have commands"
598615 emit ("build" , name , ":phony" , * fins )
@@ -608,6 +625,7 @@ def simplerule(
608625 outs : Targets = [],
609626 deps : Targets = [],
610627 commands = [],
628+ add_to_commanddb = False ,
611629 label = "RULE" ,
612630):
613631 self .ins = ins
@@ -623,8 +641,22 @@ def simplerule(
623641
624642 cs = [("mkdir -p %s" % dir ) for dir in dirs ]
625643
644+ coreCommands = []
626645 for c in commands :
627- cs += [self .templateexpand (c )]
646+ coreCommands += [self .templateexpand (c )]
647+ cs += coreCommands
648+
649+ if add_to_commanddb :
650+ infiles = filenamesof (ins )
651+ if len (infiles ) > 0 :
652+ global commandsDb
653+ commandsDb += [
654+ {
655+ "directory" : os .getcwd (),
656+ "command" : (" && " .join (coreCommands )),
657+ "file" : infiles [0 ],
658+ }
659+ ]
628660
629661 emit_rule (
630662 self = self ,
@@ -696,8 +728,9 @@ def main():
696728 if "=" in line :
697729 name , value = line .split ("=" , 1 )
698730 G .setdefault (name .strip (), value .strip ())
731+ G .setdefault ("AB_SANDBOX" , "yes" )
699732
700- global ninjaFp , shellFp , outputdir
733+ global ninjaFp , shellFp , jsonFp , outputdir
701734 outputdir = args .outputdir
702735 G .setdefault ("OBJ" , outputdir )
703736 ninjaFp = open (outputdir + "/build.ninja" , "wt" )
@@ -721,5 +754,8 @@ def main():
721754 fp .write ("ninja-targets =" )
722755 fp .write (substituteGlobalVariables (" " .join (outputTargets )))
723756
757+ with open (outputdir + "/compile_commands.json" , "wt" ) as fp :
758+ json .dump (commandsDb , fp )
759+
724760
725761main ()
0 commit comments