Skip to content

Instantly share code, notes, and snippets.

@MartyLake
Created September 27, 2019 12:56
Show Gist options
  • Save MartyLake/05c16f69f37e250b67d6aaaf05c50348 to your computer and use it in GitHub Desktop.
Save MartyLake/05c16f69f37e250b67d6aaaf05c50348 to your computer and use it in GitHub Desktop.
Generate compile_commands.json from a cmakelists.txt file
""" a big WIP, as I discovered that VS2019 supports the export """
import os
import subprocess
import shutil
import contextlib
import tempfile
SRC_DIR=(os.path.dirname(os.path.realpath(__file__)))
@contextlib.contextmanager
def make_temp_directory():
temp_dir = tempfile.mkdtemp(suffix="generate_compile_commands_json")
try:
yield temp_dir
finally:
os.chdir(last_cwd)
with make_temp_directory() as TMP_DIR:
cmd = "cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja \"{}\"".format(SRC_DIR)
process = subprocess.check_call(cmd, stdout=None, stderr=subprocess.STDOUT, cwd=TMP_DIR)
shutil.copyfile(
"{}/{}".format(TMP_DIR, "compile_commands.json"),
"{}/{}".format(SRC_DIR, "compile_commands.json"),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment