CheckCXXPreprocessor.cmake 731 B

12345678910111213141516171819202122232425262728293031323334
  1. macro(CHECK_CXX_PREPROCESSOR VAR DIRECTIVE)
  2. string(RANDOM DEFINED_KEY)
  3. string(RANDOM UNDEFINED_KEY)
  4. set(TMP_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/CxxTmp/src.cpp")
  5. SET(SRC "
  6. #if ${DIRECTIVE}
  7. #error ${DEFINED_KEY}
  8. #else
  9. #error ${UNDEFINED_KEY}
  10. #endif
  11. ")
  12. file(WRITE ${TMP_FILENAME} "${SRC}")
  13. try_compile(RESULT_VAR
  14. ${CMAKE_CURRENT_BINARY_DIR}
  15. ${TMP_FILENAME}
  16. OUTPUT_VARIABLE OUTPUT_VAR
  17. )
  18. if(OUTPUT_VAR MATCHES ${DEFINED_KEY})
  19. set(${VAR} ON)
  20. elseif(OUTPUT_VAR MATCHES ${UNDEFINED_KEY})
  21. set(${VAR} OFF)
  22. else()
  23. message(FATAL_ERROR "Can't determine if \"${DIRECTIVE}\" is true.")
  24. endif()
  25. endmacro()
  26. macro(CHECK_CXX_DEFINE VAR DEFINE)
  27. CHECK_CXX_PREPROCESSOR(${VAR} "defined(${DEFINE})")
  28. endmacro()