Skip to content

Commit

Permalink
Setup clangd compile commands output.
Browse files Browse the repository at this point in the history
  • Loading branch information
alliepiper committed Apr 5, 2024
1 parent eb5940c commit 04b7005
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ build*/
.idea
cmake-build-*
*~
compile_commands.json
CMakeUserPresets.json
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
28 changes: 28 additions & 0 deletions cmake/NVBenchClangdCompileInfo.cmake
Original file line number Diff line number Diff line change
@@ -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}")
45 changes: 45 additions & 0 deletions cmake/NVBenchUtilities.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 04b7005

Please sign in to comment.