Fix generate_compile_commands.sh for incremental builds 71/311471/8
authorKrzysztof Malysa <k.malysa@samsung.com>
Tue, 21 May 2024 14:42:58 +0000 (16:42 +0200)
committerKrzysztof Malysa <k.malysa@samsung.com>
Tue, 4 Jun 2024 15:15:08 +0000 (17:15 +0200)
Change-Id: I5dc6a4adac8474e23924c633ef13d6f48f065f97

generate_compile_commands.sh

index aa210af7f4a1287bbee7a546d9628e2939e216c1..f154e948919e7f08459c4f86d2c225c4db2edf78 100755 (executable)
@@ -1,33 +1,73 @@
 #!/bin/bash
 set -euo pipefail
 
+PROJECT=webauthn-ble
+
 # Change working directory to the directory containing the currently run script
 cd -P -- "$(dirname -- "$0")"
 
-ORIG_COMPILE_COMMANDS_JSON=$(ls -t "$HOME"/GBS-ROOT/local/BUILD-ROOTS/*/home/abuild/rpmbuild/BUILD/webauthn-ble-*/compile_commands.json 2> /dev/null | head -n 1 || true)
+GBS_BUILD_ROOTFS="$(ls -t --directory "$HOME/GBS-ROOT/local/BUILD-ROOTS/"* | head -n 1 || true)"
+if [ -z "$GBS_BUILD_ROOTFS" ]; then
+    echo "Could not find most recent GBS build root, please build the project with GBS first"
+    exit 1
+fi
+
+ARCH=$(echo "${GBS_BUILD_ROOTFS}" | sed 's%.*/scratch.\([^/]*\).0%\1%')
+if [ "${ARCH}" == "armv7l" ]; then
+    target=armv7a-arm-linux
+    function extra_filters {
+        cat
+    }
+elif [ "${ARCH}" == "i586" ]; then
+    target=i586-pc-linux
+    function extra_filters {
+        sed 's% -msse4.2 % %' |
+        sed 's% -mfpmath=sse % %'
+    }
+else
+    echo "Unexpected architecture: ${ARCH}"
+    exit 1
+fi
 
-if [ -z "${ORIG_COMPILE_COMMANDS_JSON}" ]; then
-    echo "Could not find compile_commands.json generated by cmake, please build the project with GBS first"
+ORIG_COMPILE_COMMANDS_JSON=$(echo "${GBS_BUILD_ROOTFS}"/home/abuild/rpmbuild/BUILD/${PROJECT}-*/compile_commands.json)
+
+# During incremental builds the compile_commands.json is modified directly, use it if this is the
+# case and it is not already rewrtitten
+if grep --quiet -- '"file": "/home/abuild/' compile_commands.json 2> /dev/null; then
+    if [ ! -f "${ORIG_COMPILE_COMMANDS_JSON}" ]; then
+        ORIG_COMPILE_COMMANDS_JSON=compile_commands.json
+    else
+        # Use the newer file
+        ORIG_COMPILE_COMMANDS_JSON="$(ls -t "compile_commands.json" "${ORIG_COMPILE_COMMANDS_JSON}" | head -n 1)"
+    fi
+elif [ ! -f "${ORIG_COMPILE_COMMANDS_JSON}" ]; then
+    echo "Could not find compile_commands.json generated by CMake, please build the project with GBS first"
     exit 1
+elif [ "compile_commands.json" -nt "${ORIG_COMPILE_COMMANDS_JSON}" ] && [ "compile_commands.json" -nt "$(basename "$0")" ]; then # this check fails if compile_commands.json does not exist
+    exit 0 # compile_commands.json seem not to need regeneration
 fi
 
-GBS_BUILD_ROOTFS=$(sed "s%/home/abuild/rpmbuild/BUILD/webauthn-ble-.*%%" <<< "${ORIG_COMPILE_COMMANDS_JSON}")
 CURR_DIR=$(pwd -P)
 
-# Fix paths so that they are valid outside GBS build
-sed "s%\\([\"I ]\\)/%\1${GBS_BUILD_ROOTFS}/%g" "${ORIG_COMPILE_COMMANDS_JSON}" |
+# This way so that it works if ${ORIG_COMPILE_COMMANDS_JSON} and compile_commands.json are the same
+# file (e.g. during GBS incremental build)
+ORIG_COMPILE_COMMANDS_CONTENTS=$(cat "${ORIG_COMPILE_COMMANDS_JSON}")
+
+# Fix paths so that they are valid outside the GBS build
+sed "s%\\([\"I ]\\)/%\1${GBS_BUILD_ROOTFS}/%g" <<< "${ORIG_COMPILE_COMMANDS_CONTENTS}" |
     # Replace path to copied sources with paths to local sources
-    sed "s%\\([\"I ]\\)${GBS_BUILD_ROOTFS}/home/abuild/rpmbuild/BUILD/webauthn-ble-[^/]*/%\1${CURR_DIR}/%g" |
-    # Remove architecture specific flags
-    sed 's%-march=[^ ]* \|-mtune=[^ ]* \|-mfpmath=sse \|-msse4.2 %%g' |
-    # Remove flags unrecognized by Clang
-    sed 's%-Wno-stringop-overflow\|-Wl[^ ]*\|-mfloat-abi=softfp\|-mfpu=neon\|-fmax-errors=1\|-mthumb%%g' |
+    sed "s%\\([\"I ]\\)${GBS_BUILD_ROOTFS}/home/abuild/rpmbuild/BUILD/${PROJECT}-[^/]*/%\1${CURR_DIR}/%g" |
+    # Specify the target architecture
+    sed "s% -std=% -target ${target} -std=%" |
+    # Remove flags Clang gives error about
+    sed 's% -Wl[^ ]*%%g' |
+    sed 's% -fmax-errors=[^ ]*%%g' |
+    sed 's% -Wno-stringop-overflow % %g' |
     # Add system includes from the GBS rootfs
-    sed "s%-isystem%-isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include-fixed/) -isystem%" |
-    sed "s%-isystem%-isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/) -isystem%" |
-    sed "s%-isystem%-isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/c++/) -isystem%" |
-    sed "s%-isystem%-isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/c++/*-tizen-linux-gnu*/) -isystem%" |
-    sed "s%-isystem%-isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/include/) -isystem%" |
-    # Force 32-bit compilation
-    sed "s%-isystem%-m32 -isystem%" > compile_commands.json
+    sed "s% -std=% -isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include-fixed/) -std=%" |
+    sed "s% -std=% -isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/) -std=%" |
+    sed "s% -std=% -isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/c++/) -std=%" |
+    sed "s% -std=% -isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/lib/gcc/*-tizen-linux-gnu*/*/include/c++/*-tizen-linux-gnu*/) -std=%" |
+    sed "s% -std=% -isystem $(echo "${GBS_BUILD_ROOTFS}"/usr/include/) -std=%" |
+    extra_filters > compile_commands.json