GEH-failures.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. set(failure_test_executables
  2. ${CMAKE_CURRENT_BINARY_DIR}/failure_test_targets)
  3. file(WRITE ${failure_test_executables} "")
  4. # Check if we should do anything. If the compiler doesn't support hidden
  5. # visibility, the failure tests won't fail, so just write an empty targets
  6. # list and punt.
  7. if(NOT WIN32 AND NOT CYGWIN AND NOT COMPILER_HAS_HIDDEN_VISIBILITY)
  8. return()
  9. endif()
  10. # Read the input source file
  11. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/exportheader_test.cpp content_post)
  12. set(content_pre "")
  13. # Generate source files for failure test executables
  14. set(counter 0)
  15. while(1)
  16. # Find first occurrence of link error marker in remaining content
  17. string(REGEX MATCH "//([^;\n]+;) LINK ERROR( [(][^)]+[)])?\n(.*)"
  18. match "${content_post}")
  19. if(match STREQUAL "")
  20. # No more matches
  21. break()
  22. endif()
  23. # Shift content buffers and extract failing statement
  24. string(LENGTH "${content_post}" content_post_length)
  25. string(LENGTH "${match}" matched_length)
  26. math(EXPR shift_length "${content_post_length} - ${matched_length}")
  27. string(SUBSTRING "${content_post}" 0 ${shift_length} shift)
  28. set(content_pre "${content_pre}${shift}")
  29. set(content_post "${CMAKE_MATCH_3}")
  30. set(content_active "//${CMAKE_MATCH_1} LINK ERROR${CMAKE_MATCH_2}")
  31. set(statement "${CMAKE_MATCH_1}")
  32. # Check if potential error is conditional, and evaluate condition if so
  33. string(REGEX REPLACE " [(]([^)]+)[)]" "\\1" condition "${CMAKE_MATCH_2}")
  34. if(NOT condition STREQUAL "")
  35. string(REGEX REPLACE " +" ";" condition "${condition}")
  36. if(${condition})
  37. else()
  38. message(STATUS "Not testing '${statement}'; "
  39. "condition (${condition}) is FALSE")
  40. set(content_pre "${content_pre}// link error removed\n")
  41. continue()
  42. endif()
  43. endif()
  44. if(NOT skip)
  45. message(STATUS "Creating failure test for '${statement}'")
  46. math(EXPR counter "${counter} + 1")
  47. # Write new source file
  48. set(out ${CMAKE_CURRENT_BINARY_DIR}/exportheader_failtest-${counter}.cpp)
  49. file(WRITE ${out} "${content_pre}${statement}\n${content_post}")
  50. # Add executable for failure test
  51. add_executable(GEH-fail-${counter} EXCLUDE_FROM_ALL ${out})
  52. target_link_libraries(GEH-fail-${counter} ${link_libraries})
  53. file(APPEND ${failure_test_executables} "GEH-fail-${counter}\n")
  54. endif()
  55. # Add placeholder where failing statement was removed
  56. set(content_pre "${content_pre}${content_active}\n")
  57. endwhile()