Skip to content

Commit c3f667a

Browse files
authored
Merge pull request #321 from firesim/compile_cmds
add: option to build compile_commands.json for lsp to ingest
2 parents 514fab6 + 5b4efac commit c3f667a

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

marshal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def main():
3434
build_parser.add_argument('config_files', nargs='+', help="Configuration file(s) to use.")
3535
build_parser.add_argument('-B', '--binOnly', action='store_true', help="Only build the binary")
3636
build_parser.add_argument('-I', '--imgOnly', action='store_true', help="Only build the image (may require an image if you have guest-init scripts)")
37+
build_parser.add_argument('--lspOnly', action='store_true', help="Generate compile_commands.json for LSP support instead of building kernel")
3738
build_parser.add_argument('-s', '--spike', action='store_true', help=argparse.SUPPRESS)
3839

3940
# Launch command
@@ -148,7 +149,9 @@ def main():
148149
j['nodisk'] = True
149150

150151
if args.command == "build":
151-
if args.binOnly or args.imgOnly:
152+
if args.lspOnly:
153+
ret = wlutil.buildWorkload(cfgName, cfgs, lspOnly=True)
154+
elif args.binOnly or args.imgOnly:
152155
# It's fine if they pass -IB, it just builds both
153156
ret = wlutil.buildWorkload(cfgName, cfgs, buildBin=args.binOnly, buildImg=args.imgOnly)
154157
else:

wlutil/build.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,35 @@ def buildDepGraph(cfgs):
342342
return loader
343343

344344

345-
def buildWorkload(cfgName, cfgs, buildBin=True, buildImg=True):
345+
def buildWorkload(cfgName, cfgs, buildBin=True, buildImg=True, lspOnly=False):
346346
# This should only be built once (multiple builds will mess up doit)
347347
global taskLoader
348348
if taskLoader is None:
349349
taskLoader = buildDepGraph(cfgs)
350350

351351
config = cfgs[cfgName]
352352

353+
if lspOnly:
354+
# For LSP mode, we only need to generate compile_commands.json
355+
if 'linux' not in config:
356+
print("Warning: No Linux configuration found for LSP generation")
357+
return 0
358+
359+
# Create a simple task for LSP generation using the existing build system
360+
lspTask = {
361+
'name': f"lsp-{cfgName}",
362+
'actions': [(makeBin, [config], {'lspOnly': True})],
363+
'targets': [str(config['out-dir'] / 'compile_commands.json')],
364+
'file_dep': config['linux']['config'] if isinstance(config['linux']['config'], list) else [config['linux']['config']],
365+
'task_dep': []
366+
}
367+
368+
# Add the LSP task to the loader
369+
taskLoader.addTask(lspTask)
370+
371+
doitHandle = doit.doit_cmd.DoitMain(taskLoader)
372+
return doitHandle.run([str(config['out-dir'] / 'compile_commands.json')])
373+
353374
imgList = []
354375
binList = []
355376

@@ -520,7 +541,7 @@ def makeOpenSBI(config, nodisk=False):
520541
return config['firmware']['source'] / 'build' / 'platform' / 'generic' / 'firmware' / 'fw_payload.elf'
521542

522543

523-
def makeBin(config, nodisk=False):
544+
def makeBin(config, nodisk=False, lspOnly=False):
524545
"""Build the binary specified in 'config'.
525546
526547
This is called as a doit task (see buildDepGraph() and addDep())
@@ -561,23 +582,36 @@ def makeBin(config, nodisk=False):
561582
initramfsIncludes += [wlutil.getOpt('initramfs-dir') / "disk"]
562583
initramfsPath = makeInitramfs(initramfsIncludes, cpioDir, includeDevNodes=True)
563584

564-
makeInitramfsKfrag(initramfsPath, cpioDir / "initramfs.kfrag")
565-
generateKConfig(config['linux']['config'] + [cpioDir / "initramfs.kfrag"], config['linux']['source'])
566-
wlutil.run(['make'] + wlutil.getOpt('linux-make-args') + ['vmlinux', 'Image', '-j' + str(wlutil.getOpt('jlevel'))], cwd=config['linux']['source'])
567-
# copy files needed to build linux (busybox copying is put here so that it is shown per linux build)
568-
shutil.copy(config['linux']['source'] / '.config', config['out-dir'] / 'linux_config')
569-
shutil.copy(wlutil.getOpt('busybox-dir') / '.config', config['out-dir'] / 'busybox_config')
585+
if lspOnly:
586+
# For LSP mode, just generate compile_commands.json
587+
generateKConfig(config['linux']['config'], config['linux']['source'])
588+
wlutil.run(['make'] + wlutil.getOpt('linux-make-args') + ['compile_commands.json'],
589+
cwd=config['linux']['source'])
590+
591+
# Copy the generated compile_commands.json to the output directory
592+
shutil.copy(config['linux']['source'] / 'compile_commands.json',
593+
config['out-dir'] / 'compile_commands.json')
594+
shutil.copy(config['linux']['source'] / '.config',
595+
config['out-dir'] / 'linux_config')
596+
else:
597+
# Normal build process
598+
makeInitramfsKfrag(initramfsPath, cpioDir / "initramfs.kfrag")
599+
generateKConfig(config['linux']['config'] + [cpioDir / "initramfs.kfrag"], config['linux']['source'])
600+
wlutil.run(['make'] + wlutil.getOpt('linux-make-args') + ['vmlinux', 'Image', '-j' + str(wlutil.getOpt('jlevel'))], cwd=config['linux']['source'])
601+
# copy files needed to build linux (busybox copying is put here so that it is shown per linux build)
602+
shutil.copy(config['linux']['source'] / '.config', config['out-dir'] / 'linux_config')
603+
shutil.copy(wlutil.getOpt('busybox-dir') / '.config', config['out-dir'] / 'busybox_config')
570604

571-
fw = makeOpenSBI(config, nodisk)
605+
fw = makeOpenSBI(config, nodisk)
572606

573-
config['bin'].parent.mkdir(parents=True, exist_ok=True)
574-
config['dwarf'].parent.mkdir(parents=True, exist_ok=True)
575-
if nodisk:
576-
shutil.copy(fw, wlutil.noDiskPath(config['bin']))
577-
shutil.copy(config['linux']['source'] / 'vmlinux', wlutil.noDiskPath(config['dwarf']))
578-
else:
579-
shutil.copy(fw, config['bin'])
580-
shutil.copy(config['linux']['source'] / 'vmlinux', config['dwarf'])
607+
config['bin'].parent.mkdir(parents=True, exist_ok=True)
608+
config['dwarf'].parent.mkdir(parents=True, exist_ok=True)
609+
if nodisk:
610+
shutil.copy(fw, wlutil.noDiskPath(config['bin']))
611+
shutil.copy(config['linux']['source'] / 'vmlinux', wlutil.noDiskPath(config['dwarf']))
612+
else:
613+
shutil.copy(fw, config['bin'])
614+
shutil.copy(config['linux']['source'] / 'vmlinux', config['dwarf'])
581615

582616
return True
583617

0 commit comments

Comments
 (0)