1
0

CMakeLists.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
  2. project(GenerateExportHeader)
  3. # Prevent timeout on Watcom by not running the tests.
  4. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  5. file(WRITE
  6. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  7. "int main() { return 0; }
  8. "
  9. )
  10. add_executable(
  11. GenerateExportHeader
  12. "${CMAKE_CURRENT_BINARY_DIR}/main.cxx"
  13. )
  14. return()
  15. endif()
  16. include(CheckCXXCompilerFlag)
  17. set( CMAKE_INCLUDE_CURRENT_DIR ON )
  18. macro(TEST_FAIL value msg)
  19. if (${value})
  20. message (SEND_ERROR "Test fail:" ${msg} ${Out} )
  21. endif ()
  22. endmacro()
  23. macro(TEST_PASS value msg)
  24. if (NOT ${value})
  25. message (SEND_ERROR "Test fail:" ${msg} ${Out} )
  26. endif ()
  27. endmacro()
  28. check_cxx_compiler_flag(-Werror HAS_WERROR_FLAG)
  29. if(HAS_WERROR_FLAG)
  30. set(ERROR_FLAG "-Werror")
  31. else()
  32. # MSVC
  33. # And intel on windows?
  34. # http://software.intel.com/en-us/articles/how-to-handle-warnings-message-in-compiler/?wapkw=%28compiler+warning+message%29
  35. check_cxx_compiler_flag("/WX" HAS_WX_FLAG)
  36. if(HAS_WX_FLAG)
  37. set(ERROR_FLAG "/WX")
  38. else()
  39. # Sun CC
  40. # http://www.acsu.buffalo.edu/~charngda/sunstudio.html
  41. check_cxx_compiler_flag("-errwarn=%all" HAS_ERRWARN_ALL)
  42. if (HAS_ERRWARN_ALL)
  43. set(ERROR_FLAG "-errwarn=%all")
  44. else()
  45. endif()
  46. endif()
  47. endif()
  48. # We seem to get race conditions is writing this stuff to the same file at least on MinGW
  49. # So to write to separate source and build directories, we use a count to differentiate.
  50. set (COUNT 0)
  51. macro(_do_build Include Library LibrarySource Source)
  52. math(EXPR COUNT "${COUNT} + 1" )
  53. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}/src.cpp" "#include \"${Include}\"\n"
  54. "int main() { ${Source}; }\n"
  55. )
  56. file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/../${LibrarySource}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}")
  57. if ("${Library}" STREQUAL "static_variant")
  58. set(CONDITIONAL_STATIC_DEFINE "add_definitions(-DLIBSHARED_AND_STATIC_STATIC_DEFINE)\n")
  59. endif()
  60. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}/CMakeLists.txt"
  61. "cmake_minimum_required(VERSION 2.8)\n"
  62. "project(compiletest)\n"
  63. "set(CMAKE_INCLUDE_CURRENT_DIR ON)\n"
  64. "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY \"\${CMAKE_CURRENT_BINARY_DIR}\")\n"
  65. "include(GenerateExportHeader)\n"
  66. "add_compiler_export_flags()\n"
  67. "if(NOT \"${ERROR_FLAG}\" STREQUAL \"\")\n"
  68. " add_definitions(${ERROR_FLAG})\n"
  69. "endif()\n"
  70. "if(MSVC)\n"
  71. " add_definitions(-DCOMPILER_IS_MSVC)\n"
  72. "endif()\n"
  73. "add_subdirectory(\"${LibrarySource}\")\n"
  74. "include_directories(\"${LibrarySource}\" \"\${CMAKE_CURRENT_BINARY_DIR}/${LibrarySource}\")\n"
  75. "${CONDITIONAL_STATIC_DEFINE}"
  76. "add_executable(compiletest src.cpp)\n"
  77. "target_link_libraries(compiletest ${Library})\n"
  78. )
  79. try_compile(Result ${CMAKE_CURRENT_BINARY_DIR}/fail${COUNT}
  80. ${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}
  81. compiletest
  82. OUTPUT_VARIABLE Out
  83. )
  84. endmacro()
  85. macro(build_fail Include Library LibrarySource Source Message)
  86. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  87. test_fail(Result ${Message})
  88. endmacro()
  89. macro(build_pass Include Library LibrarySource Source Message)
  90. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  91. test_pass(Result ${Message})
  92. endmacro()
  93. include(GenerateExportHeader)
  94. add_compiler_export_flags()
  95. if (MSVC)
  96. add_definitions(-DCOMPILER_IS_MSVC)
  97. endif()
  98. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  99. message("#### COMPILER_HAS_DEPRECATED: " ${COMPILER_HAS_DEPRECATED})
  100. message("#### COMPILER_HAS_HIDDEN_VISIBILITY: " ${COMPILER_HAS_HIDDEN_VISIBILITY})
  101. message("#### WIN32: " ${WIN32})
  102. message("#### HAS_WERROR_FLAG: " ${HAS_WERROR_FLAG})
  103. set(link_libraries)
  104. macro(macro_add_test_library name)
  105. add_subdirectory(${name})
  106. include_directories(${name}
  107. ${${name}_BINARY_DIR} # For the export header.
  108. )
  109. list(APPEND link_libraries ${name})
  110. add_subdirectory(${name}test)
  111. endmacro()
  112. macro_add_test_library(libshared)
  113. macro_add_test_library(libstatic)
  114. add_subdirectory(lib_shared_and_static)
  115. add_subdirectory(lib_shared_and_statictest)
  116. add_subdirectory(override_symbol)
  117. add_subdirectory(nodeprecated)
  118. add_subdirectory(prefix)
  119. if (CMAKE_COMPILER_IS_GNUCXX OR (${CMAKE_CXX_COMPILER_ID} MATCHES Clang))
  120. # We deliberately call deprecated methods, and test for that elsewhere.
  121. # No need to clutter the test output with warnings.
  122. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
  123. endif()
  124. if(MSVC AND COMPILER_HAS_DEPRECATED)
  125. add_definitions(/wd4996)
  126. endif()
  127. add_executable(GenerateExportHeader exportheader_test.cpp)
  128. target_link_libraries(GenerateExportHeader ${link_libraries})