From a6f62055835c5c3f9546818fbbcd43c73baef57f Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 6 Aug 2021 15:49:26 +0100 Subject: Implement compile_commands.json generation --- .gitignore | 4 +++- .licignore | 2 +- .vars.rc.do | 6 +++--- README.md | 4 ++++ all.do | 7 +++++-- compile_commands.json.do | 5 +++++ compile_flags.txt.do | 5 ----- configure | 4 +++- default.cmd.do | 12 ++++++++++++ default.o.do | 3 ++- default.tool.do | 9 +++++---- libpack.do | 1 + test.do | 1 + test_gen.do | 1 + 14 files changed, 46 insertions(+), 18 deletions(-) create mode 100755 compile_commands.json.do delete mode 100755 compile_flags.txt.do create mode 100755 default.cmd.do diff --git a/.gitignore b/.gitignore index 63763aa..b2f50d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,13 @@ +*.cmd *.d *.inc *.o *.tool +.cache/ .redo/ .vars.rc all -compile_flags.txt +compile_commands.json config.rc test test_gen diff --git a/.licignore b/.licignore index bd06040..d772771 100644 --- a/.licignore +++ b/.licignore @@ -7,4 +7,4 @@ LICENSE README.md clean configure -scripts/ +do-link diff --git a/.vars.rc.do b/.vars.rc.do index 8a31fe7..7c4d417 100755 --- a/.vars.rc.do +++ b/.vars.rc.do @@ -4,7 +4,7 @@ set -e # Ensure variables from the config aren't set before the config is read to # avoid capturing transient state unset {,T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} \ - colour debug optimise verbose warn werror + colour debug optimise verbose warn werror extra_targets CFLAGS=(-std=c11) TCFLAGS=(-fPIC) @@ -15,7 +15,7 @@ warnings=( -Wstrict-prototypes ) -declare -a {,T,H}{CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} +declare -a {,T,H}{CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} extra_targets if [ -e config.rc ]; then redo-ifchange config.rc @@ -72,7 +72,7 @@ done { echo "# generated by $0" - declare -p {T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} + declare -p {T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} extra_targets } >"$3" if [[ $verbose ]]; then diff --git a/README.md b/README.md index 678abc3..25038b3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Pack has no runtime dependencies. The following is a list of build dependencies: - POSIX cat, find, rm, sed, sh and tr - xargs with support for the `-r` gnu extension - bash 4.4 or newer (parameter expansion transformations) +- (optional) jq (for generating `compile_commands.json`) Compilation ----------- @@ -76,6 +77,9 @@ Development Please configure with `-w` and ensure code compiles cleanly. Be aware that different compiler versions can enable different warnings. If in doubt, ask. +If you use a tool such as `clangd` which makes use of `compile_commands.json` +then make sure you have `jq` installed and run `./configure` with `-f`. + Contributing ------------ diff --git a/all.do b/all.do index e8dccf3..3994db3 100755 --- a/all.do +++ b/all.do @@ -1,2 +1,5 @@ -#!/bin/sh -redo-ifchange libpack test compile_flags.txt +#!/usr/bin/env bash +set -e +redo-ifchange .vars.rc +. ./.vars.rc +redo-ifchange libpack test "${extra_targets[@]}" diff --git a/compile_commands.json.do b/compile_commands.json.do new file mode 100755 index 0000000..f01e0fa --- /dev/null +++ b/compile_commands.json.do @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +cmds=(common.cmd ieee754b.cmd pack.cmd test.cmd test_gen.cmd trace.cmd unpack.cmd) +redo-ifchange "${cmds[@]}" +cat "${cmds[@]}" | jq -s >"$3" diff --git a/compile_flags.txt.do b/compile_flags.txt.do deleted file mode 100755 index e43c8e4..0000000 --- a/compile_flags.txt.do +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash -set -e -redo-ifchange .vars.rc -. ./.vars.rc -printf '%s\n' "${TCFLAGS[@]}" "${TCPPFLAGS[@]}" >"$3" diff --git a/configure b/configure index b656155..9ed3fb4 100755 --- a/configure +++ b/configure @@ -13,6 +13,7 @@ Options: -c when Enable compiler colours (always|auto|off) [default: auto] -d Enable debugging flags -e Enable -Werror + -f Generate compile_commands.json database (needs jq) -h Show this help -o Enable optimisation flags -v Print results of configuration @@ -26,7 +27,7 @@ exec 3>config.rc conf() { echo "$1" >&3; } colour=auto -while getopts B:C:L:P:W:c:dehovw opt; do +while getopts B:C:L:P:W:c:defhovw opt; do qopt=${OPTARG@Q} case $opt in B) conf "LDLIBS+=($qopt)";; @@ -37,6 +38,7 @@ while getopts B:C:L:P:W:c:dehovw opt; do c) colour="$OPTARG";; d) conf "debug=1" >&3;; e) conf "werror=1" >&3;; + f) conf "extra_targets+=(compile_commands.json)" ;; h) usage; help; exit;; o) conf "optimise=1" >&3;; v) conf "verbose=1" >&3;; diff --git a/default.cmd.do b/default.cmd.do new file mode 100755 index 0000000..a726a0c --- /dev/null +++ b/default.cmd.do @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -e +src=${1%.cmd}.c +if grep -q '@BUILD_CC host' "$src"; then + tool=compile-host.tool +else + tool=compile.tool +fi +redo-ifchange "$tool" "$src" +. ./"$tool" "${1%.cmd}.o" "$2" "$3" +jq -n --arg d "$PWD" --arg c "${cmd[*]@Q}" --arg f "$src" \ + '{directory:$d, command:$c, file:$f}' >"$3" diff --git a/default.o.do b/default.o.do index 4ab2ffa..0d129c4 100755 --- a/default.o.do +++ b/default.o.do @@ -9,4 +9,5 @@ else fi redo-ifchange "$compiler" .parse-deps.sed "$src" sed -n 's|.*@BUILD_DEP \(.*\)|\1|p' "$src" | redo-ifchg-pipe -. "./$compiler" | sed -f .parse-deps.sed | redo-ifchg-pipe +. "./$compiler" +"${cmd[@]}" | sed -f .parse-deps.sed | redo-ifchg-pipe diff --git a/default.tool.do b/default.tool.do index a4e18e7..d9845fa 100755 --- a/default.tool.do +++ b/default.tool.do @@ -1,8 +1,8 @@ #!/usr/bin/env bash set -e -redo-ifchange .vars.rc -if [[ $1 = *-host.tool ]]; then prefix=H; else prefix=T; fi -case "${1##*/}" in +tool=${1##*/} +if [[ $tool = *-host.tool ]]; then prefix=H; else prefix=T; fi +case "$tool" in compile*.tool) vars=(CC CFLAGS CPPFLAGS) cmd='"$CC" -MMD -MF - "${CFLAGS[@]}" "${CPPFLAGS[@]}" -c -o "$3" "${1%.o}.c"' @@ -17,9 +17,10 @@ link-*.tool) exit 1 ;; esac for v in "${vars[@]}"; do cmd=${cmd//$v/$prefix$v}; done +redo-ifchange .vars.rc . ./.vars.rc exec >"$3" echo "#generated by $0 $1" declare -p "${vars[@]/#/$prefix}" -echo "$cmd" +echo "cmd=($cmd)" if command -v redo-stamp &>/dev/null; then redo-stamp <"$3"; fi diff --git a/libpack.do b/libpack.do index 3ecb302..ae5f746 100755 --- a/libpack.do +++ b/libpack.do @@ -3,3 +3,4 @@ set -e objects=(common.o pack.o trace.o unpack.o ieee754b.o) redo-ifchange link-library.tool "${objects[@]}" . ./link-library.tool +"${cmd[@]}" diff --git a/test.do b/test.do index 82cbd07..483411f 100755 --- a/test.do +++ b/test.do @@ -3,3 +3,4 @@ set -e objects=(common.o pack.o test.o trace.o unpack.o ieee754b.o) redo-ifchange link-executable.tool "${objects[@]}" . ./link-executable.tool +"${cmd[@]}" diff --git a/test_gen.do b/test_gen.do index 1eab838..e879f8f 100755 --- a/test_gen.do +++ b/test_gen.do @@ -3,3 +3,4 @@ set -e objects=(test_gen.o) redo-ifchange link-executable-host.tool "${objects[@]}" . ./link-executable-host.tool +"${cmd[@]}" -- cgit v1.2.3-54-g00ecf