PropertiesGenerateCommand-check.cmake 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. if(NOT EXISTS "${RunCMake_TEST_BINARY_DIR}/compile_commands.json")
  2. set(RunCMake_TEST_FAILED "compile_commands.json not generated")
  3. return()
  4. endif()
  5. file(READ "${RunCMake_TEST_BINARY_DIR}/compile_commands.json" compile_commands)
  6. macro(check_error)
  7. if(error)
  8. message(SEND_ERROR "Unexpected error \"${error}\"\nFor: ${compile_commands}")
  9. endif()
  10. endmacro()
  11. string(JSON num_commands ERROR_VARIABLE error LENGTH "${compile_commands}")
  12. check_error()
  13. # Only one of the targets has the EXPORT_COMPILE_COMMANDS property enabled.
  14. if(NOT num_commands STREQUAL 1)
  15. message(SEND_ERROR "Expected 1 compile command, got ${num_commands} for ${compile_commands}")
  16. endif()
  17. # Get the compile command generated.
  18. string(JSON result ERROR_VARIABLE error GET "${compile_commands}" 0)
  19. check_error()
  20. string(JSON result ERROR_VARIABLE error GET "${result}" file)
  21. check_error()
  22. # And ensure the correct target is in that compile command.
  23. cmake_path(COMPARE "${result}" EQUAL "${RunCMake_TEST_SOURCE_DIR}/expected_file.c" good)
  24. if(NOT good)
  25. message(SEND_ERROR "Expected \"${result}\" in \"${RunCMake_TEST_SOURCE_DIR}/expected_file.c\"")
  26. endif()