Skip to content

Instantly share code, notes, and snippets.

@iauns
Last active March 25, 2020 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iauns/6276327 to your computer and use it in GitHub Desktop.
Save iauns/6276327 to your computer and use it in GitHub Desktop.
Simple bash function which finds a number of compilation databases and passes them into my cc_merge script.
#!/bin/bash
MergeCompileCommands()
{
BIN_DIR=$1
OUT_FILE=$BIN_DIR/compile_commands.json
command -v node >/dev/null 2>&1 || { echo >&2 "Node.js (node) is required but not installed. Aborting."; exit 1; }
command -v npm >/dev/null 2>&1 || { echo >&2 "Node.js' package manager (npm) is required not installed. Aborting."; exit 1; }
# Ensure node app is setup correctly (look for node_modules in target dir).
# ccMerge = compile commands merge.
NODE_MERGER_DIR=${HOME}/prosp/common/cc_merge
if [ ! -d $NODE_MERGER_DIR/node_modules ]; then
pushd $NODE_MERGER_DIR &> /dev/null
echo "Running npm install in NODE_MERGER_DIR"
npm install
popd &> /dev/null
fi
# Gather all of the compile_commands.json files.
COMPILE_COMMANDS_FILES=`find $BIN_DIR -name compile_commands.json`
# Loop through in an attempt to find $OUT_FILE.
# Once found, remove it from the list.
# See: http://superuser.com/questions/196572/check-if-two-paths-are-pointing-to-the-same-file
FILTERED_COMMANDS_FILES=''
PRIOR_COMMAND=false
for file in $COMPILE_COMMANDS_FILES ; do
# Only use files that do NOT match the output file.
if ! [ $OUT_FILE -ef $file ]; then
if $PRIOR_COMMAND ; then
FILTERED_COMMANDS_FILES="$FILTERED_COMMANDS_FILES
$file"
else
FILTERED_COMMANDS_FILES=$file
PRIOR_COMMAND=true
fi
fi
done
# Run node now that all of the dependencies are correctly handled.
# This will merge all of the compile_commands.json files into one, with
# already existing files being overwritten in OUT_FILE.
pushd $NODE_MERGER_DIR &> /dev/null
echo "Running compile_commands.json merging utility."
node cc_merge.js -o $OUT_FILE $FILTERED_COMMANDS_FILES
popd &> /dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment