Skip to content

Instantly share code, notes, and snippets.

@kammce
Created March 19, 2024 19:58
Show Gist options
  • Save kammce/322b7884cd07276a7c3501c1e1cf17cf to your computer and use it in GitHub Desktop.
Save kammce/322b7884cd07276a7c3501c1e1cf17cf to your computer and use it in GitHub Desktop.
Use this to create a `.clangd` file for workspace folders containing libhal libraries. Should work with any conan project that generates a `compile_commands.json` file
#!/bin/bash
# Start the .clangd file afresh
echo "" > .clangd
# Get the absolute path to the script's directory
root_dir=$(pwd)
# Iterate over all directories in the current directory
for dir in */ ; do
# Trim the trailing slash
trimmed_dir=${dir%/}
# Check if conanfile.py exists in the directory
if [[ -f "${dir}conanfile.py" ]]; then
echo "Building in ${dir}..."
# Change to the directory
cd "$dir"
# Execute conan build with additional arguments for compile_command.json
conan build .
# Assume the build directory is where compile_commands.json is located
# Adjust this path if your build directory is different
json=$(find $(pwd)/build -name "compile_commands.json" | head -n 1)
# Append configuration to the .clangd file
if [ -z "$json" ]; then
echo "Skipping ${dir} as the 'compile_commands.json' does not exist"
else
build_dir=$(dirname $json)
# Generate relative path from project root to the build directory
relative_build_dir=${build_dir#"$root_dir/"}
{
echo "---"
echo "If:"
echo " PathMatch: ${trimmed_dir}/.*"
echo "CompileFlags:"
echo " CompilationDatabase: ${relative_build_dir}"
echo ""
} >> "$root_dir/.clangd"
fi
# Go back to the project root directory
cd "$root_dir"
fi
done
echo "Done generating .clangd with compilation database paths."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment