unleashed-firmware/scripts/fbt_tools/crosscc.py

87 lines
2.3 KiB
Python
Raw Normal View History

import subprocess
import gdb
import objdump
Improve vscode clangd experience (#2431) * Improve vscode clangd experience - Resolve and use absolute path for the toolchain. This allows clangd to use compile_commands.json file without running under fbtenv, simplifies setup for vscode clangd extension. As a side effect, a rebuild is needed to update compile_commands.json after moving the source tree. - Add the recommended default settings of the extension to settings.json. * Use build/latest for compile-commands-dir This makes it behave closer to c-cpp-properties. * Reformat crosscc.py This is a PEP-8 violation but black seems to enforce it * Bypass --query-driver This has some security implications as it grants clangd the ability to execute any executables anywhere while trying to probe a compiler based on CDB. However it's very hard to do this the safe and intended way without resorting to config generation due to reason listed in #2431. Besides that we already have workspace trust so what could go wrong? ;) * Add an option for vscode_dist to switch between clangd and cpptools This will install different extensions.json tuned for either clangd or cpptools based on user selection. It will also install c_cpp_properties.json when using cpptools since clangd doesn't use this file. The root .gitignore now also doesn't accidentally ignore everything under the .vscode directory. * Use absolute path for .vscode gitignore Turns out the previously used "relative" paths aren't even valid gitignore patterns and to actually do what it means one needs to use the absolute paths instead. * Handle variable parsing in commandline.scons commandline.scons is the place where all other command line parsing happens. Move LANG_SERVER variable parsing there and add a constraint to make the code more consistent. --------- Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: hedger <hedger@nanode.su>
2023-08-16 04:23:09 +03:00
import shutil
import strip
from SCons.Action import _subproc
from SCons.Errors import StopError
from SCons.Tool import ar, asm, gcc, gnulink, gxx
def prefix_commands(env, command_prefix, cmd_list):
for command in cmd_list:
if command in env:
Improve vscode clangd experience (#2431) * Improve vscode clangd experience - Resolve and use absolute path for the toolchain. This allows clangd to use compile_commands.json file without running under fbtenv, simplifies setup for vscode clangd extension. As a side effect, a rebuild is needed to update compile_commands.json after moving the source tree. - Add the recommended default settings of the extension to settings.json. * Use build/latest for compile-commands-dir This makes it behave closer to c-cpp-properties. * Reformat crosscc.py This is a PEP-8 violation but black seems to enforce it * Bypass --query-driver This has some security implications as it grants clangd the ability to execute any executables anywhere while trying to probe a compiler based on CDB. However it's very hard to do this the safe and intended way without resorting to config generation due to reason listed in #2431. Besides that we already have workspace trust so what could go wrong? ;) * Add an option for vscode_dist to switch between clangd and cpptools This will install different extensions.json tuned for either clangd or cpptools based on user selection. It will also install c_cpp_properties.json when using cpptools since clangd doesn't use this file. The root .gitignore now also doesn't accidentally ignore everything under the .vscode directory. * Use absolute path for .vscode gitignore Turns out the previously used "relative" paths aren't even valid gitignore patterns and to actually do what it means one needs to use the absolute paths instead. * Handle variable parsing in commandline.scons commandline.scons is the place where all other command line parsing happens. Move LANG_SERVER variable parsing there and add a constraint to make the code more consistent. --------- Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: hedger <hedger@nanode.su>
2023-08-16 04:23:09 +03:00
env[command] = shutil.which(command_prefix + env[command])
def _get_tool_version(env, tool):
verstr = "version unknown"
proc = _subproc(
env,
env.subst("${%s} --version" % tool),
stdout=subprocess.PIPE,
stderr="devnull",
stdin="devnull",
universal_newlines=True,
error="raise",
shell=True,
)
if proc:
verstr = proc.stdout.readline()
proc.communicate()
return verstr
def generate(env, **kw):
if not env.get("VERBOSE", False):
env.SetDefault(
CCCOMSTR="\tCC\t${SOURCE}",
CXXCOMSTR="\tCPP\t${SOURCE}",
ASCOMSTR="\tASM\t${SOURCE}",
ARCOMSTR="\tAR\t${TARGET}",
RANLIBCOMSTR="\tRANLIB\t${TARGET}",
LINKCOMSTR="\tLINK\t${TARGET}",
INSTALLSTR="\tINSTALL\t${TARGET}",
APPSCOMSTR="\tAPPS\t${TARGET}",
VERSIONCOMSTR="\tVERSION\t${TARGET}",
STRIPCOMSTR="\tSTRIP\t${TARGET}",
OBJDUMPCOMSTR="\tOBJDUMP\t${TARGET}",
)
for orig_tool in (asm, gcc, gxx, ar, gnulink, strip, gdb, objdump):
orig_tool.generate(env)
env.SetDefault(
TOOLCHAIN_PREFIX=kw.get("toolchain_prefix"),
)
prefix_commands(
env,
env.subst("$TOOLCHAIN_PREFIX"),
[
"AR",
"AS",
"CC",
"CXX",
"OBJCOPY",
"RANLIB",
"STRIP",
"GDB",
"GDBPY",
"OBJDUMP",
],
)
# Call CC to check version
if whitelisted_versions := kw.get("versions", ()):
cc_version = _get_tool_version(env, "CC")
# print("CC version =", cc_version)
# print(list(filter(lambda v: v in cc_version, whitelisted_versions)))
if not any(filter(lambda v: v in cc_version, whitelisted_versions)):
raise StopError(
f"Toolchain version is not supported. Allowed: {whitelisted_versions}, toolchain: {cc_version} "
)
def exists(env):
return True