Initial Commit
This commit is contained in:
61
extra_script.py
Normal file
61
extra_script.py
Normal file
@@ -0,0 +1,61 @@
|
||||
Import("env")
|
||||
|
||||
def gen_clangd(target, source, env):
|
||||
_ = target
|
||||
_ = source
|
||||
from subprocess import run
|
||||
import shlex
|
||||
from pathlib import Path
|
||||
|
||||
SDCC = run(shlex.split("which sdcc"), capture_output=True).stdout.decode().strip()
|
||||
# SDCC = '/home/fpasqua/.platformio/packages/toolchain-sdcc/bin/sdcc'
|
||||
p = Path(SDCC).parent.parent # toolchain root
|
||||
SDCC_INCLUDE = p / 'share' / 'sdcc' / 'include'
|
||||
# SDCC_INCLUDE = '/home/fpasqua/.platformio/packages/toolchain-sdcc/share/sdcc/include/'
|
||||
GETDEFINEFLAGS = '-E -dM -mstm8 -x c /dev/null'
|
||||
res = run([SDCC] + shlex.split(GETDEFINEFLAGS), capture_output=True)
|
||||
res.check_returncode()
|
||||
defines_raw = res.stdout.decode().strip().split('\n')
|
||||
defines = [d.replace('#define ', '-D').replace(' ', '=') for d in defines_raw]
|
||||
sdcc_defines = ", ".join(defines)
|
||||
|
||||
sdccext_defines = ", ".join(['-D__trap=', '-D__interrupt(x)='])
|
||||
|
||||
SUPPRESSED = [
|
||||
"clang-diagnostic-main-return-type",
|
||||
"clang-diagnostic-empty-body",
|
||||
"pp_including_mainfile_in_preamble",
|
||||
]
|
||||
|
||||
with open('.clangd', 'w') as f:
|
||||
f.writelines([
|
||||
'CompileFlags:\n',
|
||||
f' Add: [-I{SDCC_INCLUDE}, {sdcc_defines}, {sdccext_defines}]\n'
|
||||
' Remove: [--opt-code-size, -mstm8]\n',
|
||||
'Index:\n',
|
||||
' StandardLibrary: No\n',
|
||||
'Diagnostics:\n',
|
||||
' Suppress:\n',
|
||||
*[f' - "{s}"\n' for s in SUPPRESSED],
|
||||
' UnusedIncludes: None\n',
|
||||
])
|
||||
print("Project .clangd created")
|
||||
|
||||
o = run(shlex.split(f'ln -s -f {env["PROJECT_DIR"]}/.clangd .clangd'), cwd=p)
|
||||
o.check_returncode()
|
||||
print(f"Symlink to toolchain {p / '.clangd'} done.")
|
||||
|
||||
# include toolchain paths
|
||||
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)
|
||||
|
||||
# custom .clangd initialization
|
||||
env.AddCustomTarget(
|
||||
name='clangd',
|
||||
dependencies=None,
|
||||
actions=gen_clangd,
|
||||
title='Gen .clangd',
|
||||
description='Generate required files for clangd to work properly.',
|
||||
always_build=True,
|
||||
)
|
||||
|
||||
# TODO: add target to load the .clangd in the sdcc toolchain too
|
||||
Reference in New Issue
Block a user