@@ -12,7 +12,7 @@ macro(set_project_options_src_dir)
1212endmacro ()
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} "\n compile_commands.json" )
124+ if (NOT HAS_IGNORED)
125+ message (TRACE "Adding compile_commands.json to .gitignore" )
126+ file (APPEND ${GITIGNORE_FILE} "\n compile_commands.json" )
127+ endif ()
122128 endif ()
123129 endif ()
124130 endif ()
0 commit comments