Skip to content

Instantly share code, notes, and snippets.

@ichizok
Last active February 18, 2022 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichizok/5ccf1b91430cc25d046c28b926e8c2eb to your computer and use it in GitHub Desktop.
Save ichizok/5ccf1b91430cc25d046c28b926e8c2eb to your computer and use it in GitHub Desktop.
Create compile_commands.json for MacVim
#!/bin/bash
set -e
work_dir=$(mktemp -d)
trap "rm -fr '${work_dir}'" EXIT
if [[ $1 = - ]]; then
build_log=${work_dir}/build.log
tee "${build_log}"
elif [[ -f $1 ]]; then
build_log=$1
fi
if ! [[ -f "$build_log" ]]; then
echo "Usage: $0 build_log" >&2
exit 1
fi
build_c_log=${work_dir}/build.c.log
build_m_log=${work_dir}/build.m.log
sed '/^xcodebuild/,$d' "${build_log}" > "${build_c_log}"
sed -n '/^xcodebuild/,$p' "${build_log}" > "${build_m_log}"
compile_commands_c_json=${work_dir}/compile_commands.c.json
compile_commands_m_json=${work_dir}/compile_commands.m.json
compiledb -f -o "${compile_commands_c_json}" < "${build_c_log}"
xcpretty -r json-compilation-database -o "${compile_commands_m_json}" < "${build_m_log}"
python3 > compile_commands.json <<EOT
import copy
import json
import os
with open('${compile_commands_c_json}') as fp:
r1 = json.load(fp)
for item in r1:
if item['file'] == 'gui.c':
obj = copy.copy(item)
obj['arguments'] = [arg.replace('gui.o', 'gui_macvim.o').replace('gui.c', 'gui_macvim.m') for arg in obj['arguments']]
obj['file'] = 'gui_macvim.m'
r1 += [obj]
elif item['file'] == 'os_mac_conv.c':
obj = copy.copy(item)
obj['arguments'] = [arg.replace('mac_conv.o', 'macos.o').replace('mac_conv.c', 'macosx.m') for arg in obj['arguments']]
obj['file'] = 'os_macosx.m'
r1 += [obj]
with open('${compile_commands_m_json}') as fp:
r2 = []
r = json.load(fp)
for item in r:
obj = {
'directory': os.path.dirname(item['file']),
'arguments': item['command'].split(),
'file': os.path.basename(item['file'])
}
if obj['directory'].endswith('/src/MacVim'):
r2 += [obj]
print(json.dumps(r1 + r2, indent=1))
EOT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment