]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
cmake: symlink whole build dir not just .json file
authorHeiko Hund <heiko@ist.eigentlich.net>
Wed, 17 Jan 2024 13:49:29 +0000 (14:49 +0100)
committerGert Doering <gert@greenie.muc.de>
Wed, 17 Jan 2024 14:01:54 +0000 (15:01 +0100)
It turned out that symlinking compile_commands.json from the top level
source dir has some issues:
  * file is not created on Windows and symlinking may cause an error
  * some IDEs create their own json and error out b/c a file exists

Since clangd also looks for the json in build/ directories by default,
we now symlink the whole build directory instead, not just the json file.

This approach requires for the existing build/ dir in the repo to
vanish. Luckily it only contains one automake include file, which is
moved to the top level source dir.

Lastly, make this an opt-in feature, so that the default configuration
of the buildsystem never causes a build failure because of this.

Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Signed-off-by: Heiko Hund <heiko@ist.eigentlich.net>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20240117134929.5317-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28061.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
.gitignore
CMakeLists.txt
Makefile.am
README.cmake.md
build/Makefile.am [deleted file]
configure.ac
ltrc.inc [moved from build/ltrc.inc with 100% similarity]
src/openvpn/Makefile.am
src/openvpnmsica/Makefile.am
src/openvpnserv/Makefile.am
src/tapctl/Makefile.am

index 4153a3ed7d2441e16c314e825affe1744db70b30..92d65bf767c48dcb3479528d63518bb3f4213bc4 100644 (file)
@@ -40,7 +40,7 @@ m4/ltsugar.m4
 m4/ltversion.m4
 m4/lt~obsolete.m4
 
-compile_commands.json
+build
 doc/openvpn-examples.5
 doc/openvpn-examples.5.html
 doc/openvpn.8
index 6a1895a44169501abaa720b91a6c318749c79431..5e07d50d3f7b415591137c445f81baaded245b3f 100644 (file)
@@ -43,9 +43,17 @@ option(USE_WERROR "Treat compiler warnings as errors (-Werror)" ON)
 set(PLUGIN_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of the plugin directory")
 
 # Create machine readable compile commands
-set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
-file(CREATE_LINK ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
-                 ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json SYMBOLIC)
+option(ENABLE_COMPILE_COMMANDS "Generate compile_commands.json and a symlink for clangd to find it" OFF)
+if (ENABLE_COMPILE_COMMANDS)
+    if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build AND NOT IS_SYMLINK ${CMAKE_CURRENT_SOURCE_DIR}/build)
+        message(FATAL_ERROR "The top level source directory contains a 'build' file or directory. Please remove or rename it. CMake creates a symlink with that name during build.")
+    endif()
+    set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
+    add_custom_target(
+        symlink-build-dir ALL
+        ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/build
+        )
+endif ()
 
 # AddressSanitize - use CXX=clang++ CC=clang cmake -DCMAKE_BUILD_TYPE=asan to build with ASAN
 set(CMAKE_C_FLAGS_ASAN
index 2305ab428369b8ef09243f3d41095f1a695d6a72..792588a3c1d18f3077ac4a25cdb695f89b3f37f3 100644 (file)
@@ -42,6 +42,7 @@ CLEANFILES = \
 EXTRA_DIST = \
        contrib \
        debug \
+       ltrc.inc \
        CMakeLists.txt \
        CMakePresets.json \
        config.h.cmake.in
@@ -53,7 +54,7 @@ BUILT_SOURCES = \
        config-version.h
 endif
 
-SUBDIRS = build distro include src sample doc tests
+SUBDIRS = distro include src sample doc tests
 
 dist_doc_DATA = \
        README \
index d181b64cd9d462f9deb093cce8dac8e037b15dcc..4f41c701e38c256125ce69dc2b64c180bffe986f 100644 (file)
@@ -135,3 +135,17 @@ to cmake to be able to use these builds.
 The `unix-native` CMake preset is available for these builds. This preset does
 not require VCPKG and instead assumes all build-dependencies are provided by
 the system natively.
+
+Generating compile_commands.json
+--------------------------------
+
+To have the CMake buildsystem generate compile_commands.json you can specify
+`-DENABLE_COMPILE_COMMANDS=ON` on the command line or enable the CMake option
+another way you like. For supported generators the file will then be created.
+Additionally, the buildsystem will create a symlink `build/` to the --preset
+build directory that contains the generated JSON file. This is done so that
+clangd is able to find it.
+
+Enabling this option may cause an error on Windows, since creating a symlink
+is a privileged operation there. If you enable Developer Mode for the system,
+symlinks can be created by regular users.
diff --git a/build/Makefile.am b/build/Makefile.am
deleted file mode 100644 (file)
index e7cc4d8..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#
-#  OpenVPN -- An application to securely tunnel IP networks
-#             over a single UDP port, with support for SSL/TLS-based
-#             session authentication and key exchange,
-#             packet encryption, packet authentication, and
-#             packet compression.
-#
-#  Copyright (C) 2002-2023 OpenVPN Inc <sales@openvpn.net>
-#
-
-MAINTAINERCLEANFILES = \
-       $(srcdir)/Makefile.in
-
-EXTRA_DIST = \
-       ltrc.inc
index 3ad9f14be3958e78c439919d32d1e83be83c130c..14f5be40aa1ec6335ba22f2069af7cf9f63718d2 100644 (file)
@@ -1536,7 +1536,6 @@ AC_SUBST([TEST_CFLAGS])
 
 AC_CONFIG_FILES([
        Makefile
-       build/Makefile
        distro/Makefile
        distro/systemd/Makefile
        doc/Makefile
similarity index 100%
rename from build/ltrc.inc
rename to ltrc.inc
index b953961e4d1c7e94bd5b9b2cea1c420470ac6bd6..1dc1dda2fdbc234cba93c95b39daa556def1121d 100644 (file)
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <alon.barlev@gmail.com>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = \
        $(srcdir)/Makefile.in
index dc53f758b78c8fed48ef01030ffeceeccf5e0ce4..b10ce8235d7fa57b9996cc7417db940298784ad3 100644 (file)
@@ -18,7 +18,7 @@
 #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
index d8ba4eb08d3425155fb37a964b2ee7b3c5c7e1ac..0c64cce8d69494c03fdc377bc4ddc3ef4a2d1066 100644 (file)
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <alon.barlev@gmail.com>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
index 69ea1611ba3beb90110ef77d02291d36d65cf242..4c2958b740a773757851cec1070474aba45801d0 100644 (file)
@@ -18,7 +18,7 @@
 #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in