Skip to content

Commit d8857cc

Browse files
authored
Merge pull request #217 from Project579/compile_commands
2 parents 4215588 + c2fa2a5 commit d8857cc

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ project_options(
134134
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
135135
# ENABLE_SANITIZER_THREAD
136136
# ENABLE_SANITIZER_MEMORY
137+
# ENABLE_COMPILE_COMMANDS_SYMLINK
137138
# ENABLE_PCH
138139
# PCH_HEADERS
139140
# WARNINGS_AS_ERRORS

docs/src/project_options_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `ENABLE_SANITIZER_UNDEFINED_BEHAVIOR`: Enable undefined behavior sanitizer
1818
- `ENABLE_SANITIZER_THREAD`: Enable thread sanitizer
1919
- `ENABLE_SANITIZER_MEMORY`: Enable memory sanitizer
20+
- `ENABLE_COMPILE_COMMANDS_SYMLINK`: Enable compile_commands.json symlink creation
2021
- `ENABLE_PCH`: Enable Precompiled Headers
2122
- `ENABLE_INCLUDE_WHAT_YOU_USE`: Enable static analysis with include-what-you-use
2223
- `ENABLE_GCC_ANALYZER`: Enable static analysis with GCC (10+) analyzer

docs/src/project_options_example.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ project_options(
9292
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
9393
# ENABLE_SANITIZER_THREAD
9494
# ENABLE_SANITIZER_MEMORY
95+
# ENABLE_COMPILE_COMMANDS_SYMLINK
9596
# ENABLE_PCH
9697
# PCH_HEADERS
9798
# WARNINGS_AS_ERRORS

src/Common.cmake

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro(set_project_options_src_dir)
1212
endmacro()
1313

1414
# Common project settings run by default for all the projects that call `project_options()`
15-
macro(common_project_options)
15+
macro(common_project_options ENABLE_COMPILE_COMMANDS_SYMLINK)
1616
set_project_options_src_dir()
1717
message(DEBUG "${ProjectOptions_SRC_DIR}")
1818

@@ -74,51 +74,57 @@ macro(common_project_options)
7474
# Enable generate compile_commands.json
7575
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
7676

77-
# Make a symbol link of compile_commands.json on the source dir to help clang based tools find it
78-
if(WIN32)
79-
# Detect whether cmake is run as administrator (only administrator can read the LOCAL SERVICE account reg key)
80-
execute_process(
81-
COMMAND reg query "HKU\\S-1-5-19"
82-
ERROR_VARIABLE IS_NONADMINISTRATOR
83-
OUTPUT_QUIET)
84-
else()
85-
set(IS_NONADMINISTRATOR "")
86-
endif()
77+
if(${ENABLE_COMPILE_COMMANDS_SYMLINK})
78+
# Make a symbol link of compile_commands.json on the source dir to help clang based tools find it
79+
if(WIN32)
80+
# Detect whether cmake is run as administrator (only administrator can read the LOCAL SERVICE account reg key)
81+
execute_process(
82+
COMMAND reg query "HKU\\S-1-5-19"
83+
ERROR_VARIABLE IS_NONADMINISTRATOR
84+
OUTPUT_QUIET)
85+
else()
86+
set(IS_NONADMINISTRATOR "")
87+
endif()
8788

88-
if(IS_NONADMINISTRATOR)
89-
# For non-administrator, create an auxiliary target and ask user to run it
90-
add_custom_command(
91-
OUTPUT ${CMAKE_SOURCE_DIR}/compile_commands.json
92-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/compile_commands.json
93-
${CMAKE_SOURCE_DIR}/compile_commands.json
94-
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
95-
VERBATIM)
96-
add_custom_target(
97-
_copy_compile_commands
98-
DEPENDS ${CMAKE_SOURCE_DIR}/compile_commands.json
99-
VERBATIM)
100-
message(
101-
STATUS
102-
"compile_commands.json was not symlinked to the root. Run `cmake --build <build_dir> -t _copy_compile_commands` if needed."
103-
)
104-
else()
105-
file(
106-
CREATE_LINK
107-
${CMAKE_BINARY_DIR}/compile_commands.json
108-
${CMAKE_SOURCE_DIR}/compile_commands.json
109-
SYMBOLIC)
110-
message(TRACE "compile_commands.json was symlinked to the root.")
111-
endif()
89+
if(IS_NONADMINISTRATOR)
90+
# For non-administrator, create an auxiliary target and ask user to run it
91+
add_custom_command(
92+
OUTPUT ${CMAKE_SOURCE_DIR}/compile_commands.json
93+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/compile_commands.json
94+
${CMAKE_SOURCE_DIR}/compile_commands.json
95+
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json
96+
VERBATIM)
97+
add_custom_target(
98+
_copy_compile_commands
99+
DEPENDS ${CMAKE_SOURCE_DIR}/compile_commands.json
100+
VERBATIM)
101+
message(
102+
STATUS
103+
"compile_commands.json was not symlinked to the root. Run `cmake --build <build_dir> -t _copy_compile_commands` if needed."
104+
)
105+
else()
106+
file(
107+
CREATE_LINK
108+
${CMAKE_BINARY_DIR}/compile_commands.json
109+
${CMAKE_SOURCE_DIR}/compile_commands.json
110+
SYMBOLIC RESULT _compile_commands_symlink_result)
111+
if(_compile_commands_symlink_result EQUAL 0)
112+
message(TRACE "compile_commands.json was symlinked to the root.")
113+
else()
114+
message(WARNING "failed to create compile_commands.json symlink to the root.")
115+
endif()
116+
endif()
112117

113-
# Add compile_commans.json to .gitignore if .gitignore exists
114-
set(GITIGNORE_FILE "${CMAKE_SOURCE_DIR}/.gitignore")
118+
# Add compile_commans.json to .gitignore if .gitignore exists
119+
set(GITIGNORE_FILE "${CMAKE_SOURCE_DIR}/.gitignore")
115120

116-
if(EXISTS ${GITIGNORE_FILE})
117-
file(STRINGS ${GITIGNORE_FILE} HAS_IGNORED REGEX "^compile_commands.json")
121+
if(EXISTS ${GITIGNORE_FILE})
122+
file(STRINGS ${GITIGNORE_FILE} HAS_IGNORED REGEX "^compile_commands.json")
118123

119-
if(NOT HAS_IGNORED)
120-
message(TRACE "Adding compile_commands.json to .gitignore")
121-
file(APPEND ${GITIGNORE_FILE} "\ncompile_commands.json")
124+
if(NOT HAS_IGNORED)
125+
message(TRACE "Adding compile_commands.json to .gitignore")
126+
file(APPEND ${GITIGNORE_FILE} "\ncompile_commands.json")
127+
endif()
122128
endif()
123129
endif()
124130
endif()

src/DynamicProjectOptions.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ macro(dynamic_project_options)
7575
"0\;ENABLE_SANITIZER_UNDEFINED_BEHAVIOR\;OFF\;${SUPPORTS_UBSAN}\;Make certain types (numeric mostly) of undefined behavior into runtime errors"
7676
"0\;ENABLE_SANITIZER_THREAD\;OFF\;OFF\;Make thread race conditions into hard runtime errors"
7777
"0\;ENABLE_SANITIZER_MEMORY\;OFF\;OFF\;Make other memory errors into runtime errors"
78+
"0\;ENABLE_COMPILE_COMMANDS_SYMLINK\;OFF\;OFF\;Don't create a symlink for compile_commands.json"
7879
"1\;LINKER\;\;\;Choose a specific linker"
7980
"1\;VS_ANALYSIS_RULESET\;\;\;Override the defaults for the code analysis rule set in Visual Studio"
8081
"1\;CONAN_PROFILE\;\;\;Use specific Conan profile"
@@ -204,6 +205,7 @@ macro(dynamic_project_options)
204205
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_VALUE}
205206
${ENABLE_SANITIZER_THREAD_VALUE}
206207
${ENABLE_SANITIZER_MEMORY_VALUE}
208+
${ENABLE_COMPILE_COMMANDS_SYMLINK_VALUE}
207209
${LINKER_VALUE}
208210
${VS_ANALYSIS_RULESET_VALUE}
209211
${CONAN_PROFILE_VALUE}

src/Index.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ macro(project_options)
6666
ENABLE_SANITIZER_LEAK
6767
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
6868
ENABLE_SANITIZER_THREAD
69-
ENABLE_SANITIZER_MEMORY)
69+
ENABLE_SANITIZER_MEMORY
70+
ENABLE_COMPILE_COMMANDS_SYMLINK)
7071
set(oneValueArgs
7172
PREFIX
7273
LINKER
@@ -100,7 +101,7 @@ macro(project_options)
100101
set(WARNING_MESSAGE WARNING)
101102
endif()
102103

103-
common_project_options()
104+
common_project_options(${ProjectOptions_ENABLE_COMPILE_COMMANDS_SYMLINK})
104105

105106
# Add an interface library for the options
106107
set(_options_target project_options)

0 commit comments

Comments
 (0)