aboutsummaryrefslogtreecommitdiff
path: root/scripts/cmake/patch-compile-commands.cmake
blob: d855ce3c39625f270e49a87e18ebcd6d72fdfd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function (patch_compile_commands input output_var)
	set(ENTRY_FILE ${CMAKE_BINARY_DIR}/compile_commands_entry)
	set(RESULT "[]")
	string(JSON COUNT LENGTH "${input}")
	message("Total commands need to patch: " ${COUNT})
	math(EXPR COUNT "${COUNT} - 1")
	foreach(INDEX RANGE ${COUNT})
		message("Patching: " ${INDEX} "/" ${COUNT})
		string(JSON ORIGINAL_COMMAND GET "${input}" ${INDEX} command)
		string(JSON WORKING_DIR GET "${input}" ${INDEX} directory)
		string(FIND "${ORIGINAL_COMMAND}" " " COMMAND_FIRST_SPACE)
		string(SUBSTRING "${ORIGINAL_COMMAND}" 0 ${COMMAND_FIRST_SPACE} COMMAND_COMPILER)
		string(SUBSTRING "${ORIGINAL_COMMAND}" ${COMMAND_FIRST_SPACE} -1 ORIGINAL_COMMAND_ARGS)
		string(CONCAT COMMAND_GENERATE "${COMMAND_COMPILER}" " -MJ ${ENTRY_FILE}" "${ORIGINAL_COMMAND_ARGS}")
		#message("Run: " "${COMMAND_GENERATE}")
		execute_process(COMMAND sh -c "${COMMAND_GENERATE}" WORKING_DIRECTORY "${WORKING_DIR}")
		file(READ ${ENTRY_FILE} ENTRY)
		string(JSON RESULT SET "${RESULT}" ${INDEX} "${ENTRY}")
	endforeach()
	set(${output_var} "${RESULT}" PARENT_SCOPE)
endfunction()

function (patch_compile_commands_file)
	file(READ ${CMAKE_BINARY_DIR}/compile_commands.json COMPILE_COMMANDS)
	patch_compile_commands("${COMPILE_COMMANDS}" COMPILE_COMMANDS_PATCHED)
	file(WRITE ${CMAKE_BINARY_DIR}/compile_commands-patched.json "${COMPILE_COMMANDS_PATCHED}")
endfunction()