Skip to content

Instantly share code, notes, and snippets.

@heuripedes
Created August 7, 2017 19:08
Show Gist options
  • Save heuripedes/614530744e798bfa0d22aae7676958c0 to your computer and use it in GitHub Desktop.
Save heuripedes/614530744e798bfa0d22aae7676958c0 to your computer and use it in GitHub Desktop.
import os
import sys
import re
from os import path
import json
if len(sys.argv) < 2:
print('usage:',sys.argv[0],'project_name')
sys.exit(-1)
project_name = sys.argv[1]
basedir = os.getcwd()
commands = json.load(open('compile_commands.json'))
sources = []
defines = {}
includes = {}
for command in commands:
for param in command['command'].split(' '):
param = param.strip()
matches = re.match('-D([^=]+)(=.+)?', param)
if matches:
value = matches[2][1:] if matches[2] else '1'
defines[matches[1]] = value
matches = re.match('-I(.+)', param)
if matches:
includes[matches[1]] = 1
filename = path.relpath(command['file'], basedir)
name, ext = path.splitext(filename)
sources.append(filename)
if ext == '.cpp' or ext == '.cxx' or ext == '.cc':
for e in ['.hpp', '.hxx', '.hh', '.h']:
if path.exists(name + e):
sources.append(name + e)
elif ext == '.c':
if path.exists(name + '.h'):
sources.append(name + '.h')
with open(project_name + '.config', 'w') as f:
for k, v in defines.items():
f.write('#define ' + k + ' ' + str(v) + "\n")
with open(project_name + '.files', 'w') as f:
for file in sources:
f.write(file + "\n")
with open(project_name + '.includes', 'w') as f:
for file in includes.keys():
f.write(file + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment