Skip to contentSkip to conversation

Change in openvpn[master]: cmake: symlink whole build dir not just .json file

Openvpn-devel In review d12fk (Code Review) 2 participants 8 messages Jan 17, 2024 1 message not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1705463396000.Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2@...2715...

#1 lore raw

Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/501?usp=email

to review the following change.

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

cmake: symlink whole build dir not just .json file

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

---
M .gitignore
M CMakeLists.txt
M Makefile.am
M README.cmake.md
D build/Makefile.am
M configure.ac
R ltrc.inc
M src/openvpn/Makefile.am
M src/openvpnmsica/Makefile.am
M src/openvpnserv/Makefile.am
M src/tapctl/Makefile.am

11 files changed, 32 insertions(+), 25 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/01/501/1

.gitignore +1 -1
index 4153a3e..92d65bf 100644
@@ -40,7 +40,7 @@
 m4/ltversion.m4
 m4/lt~obsolete.m4
 
-compile_commands.json
+build
 doc/openvpn-examples.5
 doc/openvpn-examples.5.html
 doc/openvpn.8
CMakeLists.txt +11 -3
index bc46c27..4e5c8fa 100644
@@ -43,9 +43,17 @@
 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
Makefile.am +2 -1
index 2305ab4..792588a 100644
@@ -42,6 +42,7 @@
 EXTRA_DIST = \
 	contrib \
 	debug \
+	ltrc.inc \
 	CMakeLists.txt \
 	CMakePresets.json \
 	config.h.cmake.in
@@ -53,7 +54,7 @@
 	config-version.h
 endif
 
-SUBDIRS = build distro include src sample doc tests
+SUBDIRS = distro include src sample doc tests
 
 dist_doc_DATA = \
 	README \
README.cmake.md +14 -0
index d181b64..4f41c70 100644
@@ -135,3 +135,17 @@
 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.
build/Makefile.am deleted +0 -15
deleted file mode 100644
index e7cc4d8..0000000
@@ -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 <[email protected]...>
-#
-
-MAINTAINERCLEANFILES = \
-	$(srcdir)/Makefile.in
-
-EXTRA_DIST = \
-	ltrc.inc
configure.ac +0 -1
index 2823f04..0710f71 100644
@@ -1515,7 +1515,6 @@
 
 AC_CONFIG_FILES([
 	Makefile
-	build/Makefile
 	distro/Makefile
 	distro/systemd/Makefile
 	doc/Makefile
ltrc.inc renamed from build/ltrc.inc +0 -0
similarity index 100%
rename from build/ltrc.inc
rename to ltrc.inc
src/openvpn/Makefile.am +1 -1
index b953961..1dc1dda 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = \
 	$(srcdir)/Makefile.in
src/openvpnmsica/Makefile.am +1 -1
index dc53f75..b10ce82 100644
@@ -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
 
src/openvpnserv/Makefile.am +1 -1
index d8ba4eb..0c64cce 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
src/tapctl/Makefile.am +1 -1
index 69ea161..4c2958b 100644
@@ -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
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, flichtenheld, plaisthos.

stipa has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/501?usp=email )

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

Patch Set 1: Code-Review+1

(1 comment)

Patchset:

PS1:
I have built and tested Windows part, so someone may want to look at autotools.

Looks good:

- by default, nothing has changed
- when ENABLE_COMPILE_COMMANDS is set and VS is running under Administrator, "build" symlink is created
- when ENABLE_COMPILE_COMMANDS is set and VS is not running under Administrator, build fails as expected because Windows doesn't allow to create symlinks for normal users

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 17 Jan 2024 09:23:58 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, flichtenheld, plaisthos.

cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/501?usp=email )

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

Patch Set 1: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 17 Jan 2024 13:38:15 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: d12fk, flichtenheld, plaisthos.

cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/501?usp=email )

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

Patch Set 1:

(1 comment)

Patchset:

PS1:
Looks reasonable, and the buildbot army confirmed that it does not break autoconf things. Also tested on a local mingw/autoconf build, which also keeps working.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 17 Jan 2024 13:48:33 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#5 lore raw

PATCH 1/1 cmake: symlink whole build dir not just .json file

In reply to a message not in the archive

From: Heiko Hund <[email protected]...>

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

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/501
This mail reflects revision 1 of this Change. Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>

.gitignore +1 -1
index 4153a3e..92d65bf 100644
@@ -40,7 +40,7 @@
 m4/ltversion.m4
 m4/lt~obsolete.m4
 
-compile_commands.json
+build
 doc/openvpn-examples.5
 doc/openvpn-examples.5.html
 doc/openvpn.8
CMakeLists.txt +11 -3
index bc46c27..4e5c8fa 100644
@@ -43,9 +43,17 @@
 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
Makefile.am +2 -1
index 2305ab4..792588a 100644
@@ -42,6 +42,7 @@
 EXTRA_DIST = \
 	contrib \
 	debug \
+	ltrc.inc \
 	CMakeLists.txt \
 	CMakePresets.json \
 	config.h.cmake.in
@@ -53,7 +54,7 @@
 	config-version.h
 endif
 
-SUBDIRS = build distro include src sample doc tests
+SUBDIRS = distro include src sample doc tests
 
 dist_doc_DATA = \
 	README \
README.cmake.md +14 -0
index d181b64..4f41c70 100644
@@ -135,3 +135,17 @@
 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.
build/Makefile.am deleted +0 -15
deleted file mode 100644
index e7cc4d8..0000000
@@ -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 <[email protected]...>
-#
-
-MAINTAINERCLEANFILES = \
-	$(srcdir)/Makefile.in
-
-EXTRA_DIST = \
-	ltrc.inc
build/ltrc.inc deleted +0 -23
deleted file mode 100644
index 701f200..0000000
@@ -1,23 +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) 2008-2012 Alon Bar-Lev <[email protected]...>
-#
-# Required to build Windows resource file
-
-RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
-	$(AM_CPPFLAGS) $(CPPFLAGS)
-LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE)
-
-.rc.lo:
-	$(LTRCCOMPILE) -i "$<" -o "$@"
-
-.rc.o:
-	$(RCCOMPILE) -i "$<" -o "$@"
-
-.mc.rc:
-	$(WINDMC) "$<"
configure.ac +0 -1
index 2823f04..0710f71 100644
@@ -1515,7 +1515,6 @@
 
 AC_CONFIG_FILES([
 	Makefile
-	build/Makefile
 	distro/Makefile
 	distro/systemd/Makefile
 	doc/Makefile
ltrc.inc new file +23 -0
new file mode 100644
index 0000000..701f200
@@ -0,0 +1,23 @@
+#
+#  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) 2008-2012 Alon Bar-Lev <[email protected]...>
+#
+# Required to build Windows resource file
+
+RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
+	$(AM_CPPFLAGS) $(CPPFLAGS)
+LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE)
+
+.rc.lo:
+	$(LTRCCOMPILE) -i "$<" -o "$@"
+
+.rc.o:
+	$(RCCOMPILE) -i "$<" -o "$@"
+
+.mc.rc:
+	$(WINDMC) "$<"
src/openvpn/Makefile.am +1 -1
index b953961..1dc1dda 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = \
 	$(srcdir)/Makefile.in
src/openvpnmsica/Makefile.am +1 -1
index dc53f75..b10ce82 100644
@@ -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
 
src/openvpnserv/Makefile.am +1 -1
index d8ba4eb..0c64cce 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
src/tapctl/Makefile.am +1 -1
index 69ea161..4c2958b 100644
@@ -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
 
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: cmake: symlink whole build dir not just .json file

Your patch has been applied to the master branch.

commit 4bcb38c42ae62a3c3511511eb4ffe0d66b8e32eb

Author: Heiko Hund
Date:   Wed Jan 17 14:49:29 2024 +0100
     cmake: symlink whole build dir not just .json file

     Signed-off-by: Heiko Hund <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg28061.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#7 lore raw
In reply to a message not in the archive

cron2 has uploaded a new patch set (#2) to the change originally created by d12fk. ( http://gerrit.openvpn.net/c/openvpn/+/501?usp=email )

The following approvals got outdated and were removed:
Code-Review+1 by stipa, Code-Review+2 by cron2

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

cmake: symlink whole build dir not just .json file

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

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg28061.html

---
M .gitignore
M CMakeLists.txt
M Makefile.am
M README.cmake.md
D build/Makefile.am
M configure.ac
R ltrc.inc
M src/openvpn/Makefile.am
M src/openvpnmsica/Makefile.am
M src/openvpnserv/Makefile.am
M src/tapctl/Makefile.am

11 files changed, 32 insertions(+), 25 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/01/501/2

.gitignore +1 -1
index 4153a3e..92d65bf 100644
@@ -40,7 +40,7 @@
 m4/ltversion.m4
 m4/lt~obsolete.m4
 
-compile_commands.json
+build
 doc/openvpn-examples.5
 doc/openvpn-examples.5.html
 doc/openvpn.8
CMakeLists.txt +11 -3
index 6a1895a..5e07d50 100644
@@ -43,9 +43,17 @@
 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
Makefile.am +2 -1
index 2305ab4..792588a 100644
@@ -42,6 +42,7 @@
 EXTRA_DIST = \
 	contrib \
 	debug \
+	ltrc.inc \
 	CMakeLists.txt \
 	CMakePresets.json \
 	config.h.cmake.in
@@ -53,7 +54,7 @@
 	config-version.h
 endif
 
-SUBDIRS = build distro include src sample doc tests
+SUBDIRS = distro include src sample doc tests
 
 dist_doc_DATA = \
 	README \
README.cmake.md +14 -0
index d181b64..4f41c70 100644
@@ -135,3 +135,17 @@
 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.
build/Makefile.am deleted +0 -15
deleted file mode 100644
index e7cc4d8..0000000
@@ -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 <[email protected]...>
-#
-
-MAINTAINERCLEANFILES = \
-	$(srcdir)/Makefile.in
-
-EXTRA_DIST = \
-	ltrc.inc
configure.ac +0 -1
index 3ad9f14..14f5be4 100644
@@ -1536,7 +1536,6 @@
 
 AC_CONFIG_FILES([
 	Makefile
-	build/Makefile
 	distro/Makefile
 	distro/systemd/Makefile
 	doc/Makefile
ltrc.inc renamed from build/ltrc.inc +0 -0
similarity index 100%
rename from build/ltrc.inc
rename to ltrc.inc
src/openvpn/Makefile.am +1 -1
index b953961..1dc1dda 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = \
 	$(srcdir)/Makefile.in
src/openvpnmsica/Makefile.am +1 -1
index dc53f75..b10ce82 100644
@@ -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
 
src/openvpnserv/Makefile.am +1 -1
index d8ba4eb..0c64cce 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
src/tapctl/Makefile.am +1 -1
index 69ea161..4c2958b 100644
@@ -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
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset

#8 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/501?usp=email )

Change subject: cmake: symlink whole build dir not just .json file
......................................................................

cmake: symlink whole build dir not just .json file

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

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg28061.html

---
M .gitignore
M CMakeLists.txt
M Makefile.am
M README.cmake.md
D build/Makefile.am
M configure.ac
R ltrc.inc
M src/openvpn/Makefile.am
M src/openvpnmsica/Makefile.am
M src/openvpnserv/Makefile.am
M src/tapctl/Makefile.am

11 files changed, 32 insertions(+), 25 deletions(-)
.gitignore +1 -1
index 4153a3e..92d65bf 100644
@@ -40,7 +40,7 @@
 m4/ltversion.m4
 m4/lt~obsolete.m4
 
-compile_commands.json
+build
 doc/openvpn-examples.5
 doc/openvpn-examples.5.html
 doc/openvpn.8
CMakeLists.txt +11 -3
index 6a1895a..5e07d50 100644
@@ -43,9 +43,17 @@
 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
Makefile.am +2 -1
index 2305ab4..792588a 100644
@@ -42,6 +42,7 @@
 EXTRA_DIST = \
 	contrib \
 	debug \
+	ltrc.inc \
 	CMakeLists.txt \
 	CMakePresets.json \
 	config.h.cmake.in
@@ -53,7 +54,7 @@
 	config-version.h
 endif
 
-SUBDIRS = build distro include src sample doc tests
+SUBDIRS = distro include src sample doc tests
 
 dist_doc_DATA = \
 	README \
README.cmake.md +14 -0
index d181b64..4f41c70 100644
@@ -135,3 +135,17 @@
 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.
build/Makefile.am deleted +0 -15
deleted file mode 100644
index e7cc4d8..0000000
@@ -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 <[email protected]...>
-#
-
-MAINTAINERCLEANFILES = \
-	$(srcdir)/Makefile.in
-
-EXTRA_DIST = \
-	ltrc.inc
configure.ac +0 -1
index 3ad9f14..14f5be4 100644
@@ -1536,7 +1536,6 @@
 
 AC_CONFIG_FILES([
 	Makefile
-	build/Makefile
 	distro/Makefile
 	distro/systemd/Makefile
 	doc/Makefile
ltrc.inc renamed from build/ltrc.inc +0 -0
similarity index 100%
rename from build/ltrc.inc
rename to ltrc.inc
src/openvpn/Makefile.am +1 -1
index b953961..1dc1dda 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = \
 	$(srcdir)/Makefile.in
src/openvpnmsica/Makefile.am +1 -1
index dc53f75..b10ce82 100644
@@ -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
 
src/openvpnserv/Makefile.am +1 -1
index d8ba4eb..0c64cce 100644
@@ -9,7 +9,7 @@
 #  Copyright (C) 2006-2012 Alon Bar-Lev <[email protected]...>
 #
 
-include $(top_srcdir)/build/ltrc.inc
+include $(top_srcdir)/ltrc.inc
 
 MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
 
src/tapctl/Makefile.am +1 -1
index 69ea161..4c2958b 100644
@@ -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
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/501?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib1a5c788269949d8de95d1da2cb0c32a65bf13f2
Gerrit-Change-Number: 501
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged