#!/usr/bin/env bash code_dirs[0]="$HOME/dev/code/cpp" code_dirs[1]="$HOME/dev/code" build_dir="$HOME/dev/build" json_file="compile_commands.json" project_name=$(pwd | sed -r "s_$build_dir/([^/]+).*\$_\1_") project_path="" for z in "${code_dirs[@]}" do if [ -d "$z/$project_name" ]; then project_path="$z/$project_name" break; fi done if [ -z "$project_path" ]; then >&2 echo "Project \"$project_name\" not found in any of the search paths" exit 1 fi config_name=$(pwd | sed -r "s@$build_dir/$project_name/([^/_]+_)?([^/]+).*\$@\2@") compiler_name=$(pwd | sed -r "s@$build_dir/$project_name/([^/_]+_)?([^/]+).*\$@\1@") compiler_option="" if [ -n "$compiler_name" ]; then compiler_name="${compiler_name%?}" case "$compiler_name" in "clang") compiler_option="CXX=clang++ CC=clang" ;; "gcc") compiler_option="CXX=g++ CC=gcc" ;; esac fi config_option="" if [ -n "$config_name" ]; then config_name="$(tr '[:lower:]' '[:upper:]' <<< ${config_name:0:1})${config_name:1}" config_option="-DCMAKE_BUILD_TYPE=$config_name" fi eval $compiler_option cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON "$config_option" $@ "$project_path" if [ -h "$project_path/$json_file" ]; then unlink "$project_path/$json_file" fi work_dir="$PWD" (cd "$project_path" && ln -s "$work_dir/$json_file")