Skip to content

Instantly share code, notes, and snippets.

@mkroman
Last active May 21, 2020 20:05
Show Gist options
  • Select an option

  • Save mkroman/8838869dfeb7d148fdaba91ff5f884e1 to your computer and use it in GitHub Desktop.

Select an option

Save mkroman/8838869dfeb7d148fdaba91ff5f884e1 to your computer and use it in GitHub Desktop.

Revisions

  1. mkroman revised this gist May 21, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generate_compile_commands.py
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@
    try:
    command.remove('-fstrict-volatile-bitfields')
    except ValueError as e:
    print('...')
    pass

    # Replace `-mlongcalls` with `-mlong-calls` as the naming is different
    # for clangd
  2. mkroman created this gist May 21, 2020.
    56 changes: 56 additions & 0 deletions generate_compile_commands.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    #!/usr/bin/env python
    # This scripts generates and makes changes to `compile_commands.json` in order
    # to make it work with coc-clangd without throwing a fit

    import os
    import json
    import shlex
    import subprocess

    project_root = os.path.join(os.path.dirname(__file__), '..')
    build_root = os.path.join(project_root, 'build')

    # Generate the compile_commands.json using CMake with the Ninja generator
    initial_dir = os.getcwd()
    os.chdir(build_root)
    subprocess.run(["cmake", "-G", "Ninja", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", ".."])
    os.chdir(initial_dir)

    # Load the generated commands
    file_path = os.path.join(build_root, 'compile_commands.json')
    with open(file_path, 'r') as file:
    lines = json.load(file)

    # Enumerate through each command and apply fixes
    for line in lines:
    command = shlex.split(line['command'])
    basename = os.path.basename(command[0])

    if basename in ['xtensa-esp32-elf-gcc', 'xtensa-esp32-elf-g++']:
    # Inject the NOOP definition for __GLIBC_USE(...) since clangd seems to
    # to read the later included limits.h from the system headers which
    # expects it to be available
    command.insert(1, '-D__GLIBC_USE(...)=0')

    # Remove `-fstrict-volatile-bitfields` as it's not supported by clangd
    try:
    command.remove('-fstrict-volatile-bitfields')
    except ValueError as e:
    print('...')

    # Replace `-mlongcalls` with `-mlong-calls` as the naming is different
    # for clangd
    # The definition can be present multiple times so we'll use a do while
    # loop
    while True:
    try:
    idx = command.index('-mlongcalls')
    command[idx] = '-mlong-calls'
    except ValueError as e:
    break

    line['command'] = shlex.join(command)

    # Save the fixed `compile_commands.json` in the project root
    with open(os.path.join(project_root, 'compile_commands.json'), 'w') as f:
    json.dump(lines, f, indent=2)