[][openwrt][common][sdk][Add support to generate compile_commands.json for clangd]
[Description]
Add support to generate compile_commands.json of kernel, kernel modules
and packages for clangd.
[Release-log]
N/A
Change-Id: I458a19adf23c68dd45f9fbff83c7c51fdb6ab45e
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/8202712
diff --git a/openwrt_patches-21.02/170-add-tool-for-generate-clangd-compile_commands.patch b/openwrt_patches-21.02/170-add-tool-for-generate-clangd-compile_commands.patch
new file mode 100644
index 0000000..544b039
--- /dev/null
+++ b/openwrt_patches-21.02/170-add-tool-for-generate-clangd-compile_commands.patch
@@ -0,0 +1,12 @@
+--- a/tools/Makefile
++++ b/tools/Makefile
+@@ -27,7 +27,7 @@ tools-y += e2fsprogs fakeroot findutils
+ tools-y += libressl libtool lzma m4 make-ext4fs missing-macros mkimage
+ tools-y += mklibs mm-macros mtd-utils mtools padjffs2 patch-image
+ tools-y += patchelf pkgconf quilt squashfskit4 sstrip zip zlib zstd
+-tools-y += crc32sum fdt-patch-dm-verify
++tools-y += crc32sum fdt-patch-dm-verify codechecker
+ tools-$(BUILD_B43_TOOLS) += b43-tools
+ tools-$(BUILD_ISL) += isl
+ tools-$(BUILD_TOOLCHAIN) += expat gmp libelf mpc mpfr
+
diff --git a/openwrt_patches-21.02/171-generate-clangd-compile_commands-for-kernel-and-packages.patch b/openwrt_patches-21.02/171-generate-clangd-compile_commands-for-kernel-and-packages.patch
new file mode 100644
index 0000000..0057ef8
--- /dev/null
+++ b/openwrt_patches-21.02/171-generate-clangd-compile_commands-for-kernel-and-packages.patch
@@ -0,0 +1,32 @@
+--- a/include/kernel.mk
++++ b/include/kernel.mk
+@@ -98,7 +98,13 @@ else
+ LINUX_KARCH := $(ARCH)
+ endif
+
+-KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
++KERNEL_VARS = \
++ LD_PRELOAD="$(STAGING_DIR_HOST)/lib/ldlogger.so" \
++ CC_LOGGER_GCC_LIKE="gcc:g++:clang:clang++:cc:c++" \
++ CC_LOGGER_FILE="$(LINUX_DIR)/compile_commands.json" \
++ CC_LOGGER_KEEP_LINK=true
++
++KERNEL_MAKE = $(KERNEL_VARS) $(MAKE) $(KERNEL_MAKEOPTS)
+
+ KERNEL_MAKE_FLAGS = \
+ KCFLAGS="$(call iremap,$(BUILD_DIR),$(notdir $(BUILD_DIR))) $(filter-out -fno-plt,$(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))) $(call qstrip,$(CONFIG_KERNEL_CFLAGS))" \
+--- a/include/package-defaults.mk
++++ b/include/package-defaults.mk
+@@ -126,7 +126,11 @@ endef
+ MAKE_VARS = \
+ CFLAGS="$(TARGET_CFLAGS) $(EXTRA_CFLAGS) $(TARGET_CPPFLAGS) $(EXTRA_CPPFLAGS)" \
+ CXXFLAGS="$(TARGET_CXXFLAGS) $(EXTRA_CXXFLAGS) $(TARGET_CPPFLAGS) $(EXTRA_CPPFLAGS)" \
+- LDFLAGS="$(TARGET_LDFLAGS) $(EXTRA_LDFLAGS)"
++ LDFLAGS="$(TARGET_LDFLAGS) $(EXTRA_LDFLAGS)" \
++ LD_PRELOAD="$(STAGING_DIR_HOST)/lib/ldlogger.so" \
++ CC_LOGGER_GCC_LIKE="gcc:g++:clang:clang++:cc:c++" \
++ CC_LOGGER_FILE="$(PKG_BUILD_DIR)/compile_commands.json" \
++ CC_LOGGER_KEEP_LINK=true
+
+ MAKE_FLAGS = \
+ $(TARGET_CONFIGURE_OPTS) \
diff --git a/tools/codechecker/Makefile b/tools/codechecker/Makefile
new file mode 100644
index 0000000..e0d67eb
--- /dev/null
+++ b/tools/codechecker/Makefile
@@ -0,0 +1,30 @@
+#
+# Copyright (C) 2023 Weijie Gao <weijie.gao@mediatek.com>
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=codechecker
+
+PKG_SOURCE_URL=https://github.com/Ericsson/codechecker.git
+PKG_SOURCE_PROTO:=git
+PKG_SOURCE_VERSION:=v6.22.2
+PKG_MIRROR_HASH:=6dddd1c7455e1c6d2115ccffcad831a51826f0f1dce18928d651d2665aa41f60
+
+include $(INCLUDE_DIR)/host-build.mk
+
+define Host/Compile
+ $(MAKE) -C $(HOST_BUILD_DIR)/analyzer/tools/build-logger all
+endef
+
+define Host/Install
+ $(INSTALL_DATA) $(HOST_BUILD_DIR)/analyzer/tools/build-logger/build/lib/`uname -m`/ldlogger.so $(STAGING_DIR_HOST)/lib/
+endef
+
+define Host/Clean
+ rm -f $(STAGING_DIR_HOST)/lib/ldlogger.so
+endef
+
+$(eval $(call HostBuild))