Browse code

cmake: create and link compile_commands.json file

CMake has support to create a json file which contains exact information
how each file in the project is compiled. This file can be consumed by
clangd, which in turn provides precise symbol information to IDEs for
better code navigation and contextual information.

I use it with vscode to be able to quickly switch between native Linux and
mingw builds and have the symbols info change dynamically with it. So
handy that I think it is useful for others as well.

Bump required CMake version for CREATE_LINK.

Change-Id: Ib14c1161b4b0c9df797b9932ad14739e202cea64
Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Message-Id: <20231220123659.55542-1-frank@lichtenheld.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg27840.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Heiko Hund authored on 2023/12/20 21:36:59
Showing 2 changed files
... ...
@@ -40,6 +40,7 @@ m4/ltsugar.m4
40 40
 m4/ltversion.m4
41 41
 m4/lt~obsolete.m4
42 42
 
43
+compile_commands.json
43 44
 doc/openvpn-examples.5
44 45
 doc/openvpn-examples.5.html
45 46
 doc/openvpn.8
... ...
@@ -1,4 +1,4 @@
1
-cmake_minimum_required(VERSION 3.12)
1
+cmake_minimum_required(VERSION 3.14)
2 2
 set(CMAKE_CONFIGURATION_TYPES "Release;Debug;ASAN")
3 3
 project(openvpn)
4 4
 
... ...
@@ -42,6 +42,11 @@ option(USE_WERROR "Treat compiler warnings as errors (-Werror)" ON)
42 42
 
43 43
 set(PLUGIN_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of the plugin directory")
44 44
 
45
+# Create machine readable compile commands
46
+set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
47
+file(CREATE_LINK ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
48
+                 ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
49
+
45 50
 # AddressSanitize - use CXX=clang++ CC=clang cmake -DCMAKE_BUILD_TYPE=asan to build with ASAN
46 51
 set(CMAKE_C_FLAGS_ASAN
47 52
     "-fsanitize=address,undefined -fno-sanitize-recover=all -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer -g -O1"