Skip to content

Commit

Permalink
cmake: symlink whole build dir not just .json file
Browse files Browse the repository at this point in the history
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 <[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]>
  • Loading branch information
d12fk authored and cron2 committed Jan 17, 2024
1 parent 6e010d4 commit 4bcb38c
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CLEANFILES = \
EXTRA_DIST = \
contrib \
debug \
ltrc.inc \
CMakeLists.txt \
CMakePresets.json \
config.h.cmake.in
Expand All @@ -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 \
Expand Down
14 changes: 14 additions & 0 deletions README.cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 0 additions & 15 deletions build/Makefile.am

This file was deleted.

1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,6 @@ AC_SUBST([TEST_CFLAGS])

AC_CONFIG_FILES([
Makefile
build/Makefile
distro/Makefile
distro/systemd/Makefile
doc/Makefile
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/openvpn/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/openvpnmsica/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/openvpnserv/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/tapctl/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4bcb38c

Please sign in to comment.