CMakeLists.txt 4.2 KB

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