From 04b70059b89869000520acf4402bf3399e4a8d4e Mon Sep 17 00:00:00 2001 From: Allison Piper Date: Fri, 5 Apr 2024 15:08:04 +0000 Subject: [PATCH] Setup clangd compile commands output. --- .gitignore | 2 ++ CMakeLists.txt | 4 ++- cmake/NVBenchClangdCompileInfo.cmake | 28 +++++++++++++++++ cmake/NVBenchUtilities.cmake | 45 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 cmake/NVBenchClangdCompileInfo.cmake diff --git a/.gitignore b/.gitignore index d41aa02..50fac98 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ build*/ .idea cmake-build-* *~ +compile_commands.json +CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b4518f..b052350 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,13 +41,15 @@ option(NVBench_ENABLE_DEVICE_TESTING option(NVBench_ENABLE_EXAMPLES "Build NVBench examples." OFF) option(NVBench_ENABLE_INSTALL_RULES "Install NVBench." ${NVBench_TOPLEVEL_PROJECT}) +include(cmake/NVBenchUtilities.cmake) # Must be first +include(cmake/NVBenchClangdCompileInfo.cmake) # Must be before any targets are created + include(cmake/NVBenchConfigTarget.cmake) include(cmake/NVBenchDependentDlls.cmake) include(cmake/NVBenchExports.cmake) include(cmake/NVBenchWriteConfigHeader.cmake) include(cmake/NVBenchDependencies.cmake) include(cmake/NVBenchInstallRules.cmake) -include(cmake/NVBenchUtilities.cmake) message(STATUS "NVBench CUDA architectures: ${CMAKE_CUDA_ARCHITECTURES}") diff --git a/cmake/NVBenchClangdCompileInfo.cmake b/cmake/NVBenchClangdCompileInfo.cmake new file mode 100644 index 0000000..a4b9c5e --- /dev/null +++ b/cmake/NVBenchClangdCompileInfo.cmake @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Tell cmake to generate a json file of compile commands for clangd: +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Symlink the compile command output to the source dir, where clangd will find it. +set(compile_commands_file "${CMAKE_BINARY_DIR}/compile_commands.json") +set(compile_commands_link "${CMAKE_SOURCE_DIR}/compile_commands.json") +message(STATUS "Creating symlink from ${compile_commands_link} to ${compile_commands_file}...") +nvbench_execute_non_fatal_process(COMMAND + "${CMAKE_COMMAND}" -E rm -f "${compile_commands_link}") +nvbench_execute_non_fatal_process(COMMAND + "${CMAKE_COMMAND}" -E touch "${compile_commands_file}") +nvbench_execute_non_fatal_process(COMMAND + "${CMAKE_COMMAND}" -E create_symlink "${compile_commands_file}" "${compile_commands_link}") diff --git a/cmake/NVBenchUtilities.cmake b/cmake/NVBenchUtilities.cmake index 3668420..caa79b8 100644 --- a/cmake/NVBenchUtilities.cmake +++ b/cmake/NVBenchUtilities.cmake @@ -1,3 +1,48 @@ +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Passes all args directly to execute_process while setting up the following +# results variables and propogating them to the caller's scope: +# +# - nvbench_process_exit_code +# - nvbench_process_stdout +# - nvbench_process_stderr +# +# If the command is not successful (e.g. the last command does not return zero), +# a non-fatal warning is printed. +function(nvbench_execute_non_fatal_process) + execute_process(${ARGN} + RESULT_VARIABLE nvbench_process_exit_code + OUTPUT_VARIABLE nvbench_process_stdout + ERROR_VARIABLE nvbench_process_stderr + ) + + if (NOT nvbench_process_exit_code EQUAL 0) + message(WARNING + "execute_process failed with non-zero exit code: ${nvbench_process_exit_code}\n" + "${ARGN}\n" + "stdout:\n${nvbench_process_stdout}\n" + "stderr:\n${nvbench_process_stderr}\n" + ) + endif() + + set(nvbench_process_exit_code "${nvbench_process_exit_code}" PARENT_SCOPE) + set(nvbench_process_stdout "${nvbench_process_stdout}" PARENT_SCOPE) + set(nvbench_process_stderr "${nvbench_process_stderr}" PARENT_SCOPE) +endfunction() + # Writes CMAKE_CUDA_ARCHITECTURES to out_var, but using escaped semicolons # as delimiters function(nvbench_escaped_cuda_arches out_var)