CMakeLists.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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}\n" ${Out} )
  21. endif ()
  22. endmacro()
  23. macro(TEST_PASS value msg)
  24. if (NOT ${value})
  25. message (SEND_ERROR "Test fail:" "${msg}\n" ${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. set(DEPS
  49. libshared
  50. libstatic
  51. lib_shared_and_static
  52. )
  53. foreach(DEP ${DEPS})
  54. try_compile(Result ${CMAKE_CURRENT_BINARY_DIR}/${DEP}_build
  55. ${CMAKE_CURRENT_SOURCE_DIR}/${DEP}
  56. ${DEP}
  57. OUTPUT_VARIABLE Out
  58. )
  59. if (NOT Result)
  60. message("OUTPUT: ${Out}")
  61. endif()
  62. endforeach()
  63. # The _do_build macro is called from a child scope, where
  64. # the current source and binary dir are different. Save them here
  65. # for use in the macro.
  66. set(TEST_TOP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  67. set(TEST_TOP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
  68. # We seem to get race conditions is writing this stuff to the same file at least on MinGW
  69. # So to write to separate source and build directories, we use a count to differentiate.
  70. set (COUNT 0)
  71. macro(_do_build Include Library LibrarySource Source)
  72. math(EXPR COUNT "${COUNT} + 1" )
  73. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}/src.cpp" "#include \"${Include}\"\n"
  74. "int main() { ${Source}; }\n"
  75. )
  76. if ("${Library}" STREQUAL "static_variant")
  77. set(CONDITIONAL_STATIC_DEFINE "add_definitions(-DLIBSHARED_AND_STATIC_STATIC_DEFINE)\n")
  78. endif()
  79. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}/CMakeLists.txt"
  80. "cmake_minimum_required(VERSION 2.8)\n"
  81. "project(compiletest)\n"
  82. "set(CMAKE_INCLUDE_CURRENT_DIR ON)\n"
  83. "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY \"\${CMAKE_CURRENT_BINARY_DIR}\")\n"
  84. "include(GenerateExportHeader)\n"
  85. "add_compiler_export_flags()\n"
  86. "if(NOT \"${ERROR_FLAG}\" STREQUAL \"\")\n"
  87. " add_definitions(${ERROR_FLAG})\n"
  88. "endif()\n"
  89. "include(\"${TEST_TOP_BINARY_DIR}/${LibrarySource}_build/Targets.cmake\")\n"
  90. "include_directories(\"${TEST_TOP_SOURCE_DIR}/${LibrarySource}\"\n"
  91. " \"${TEST_TOP_BINARY_DIR}/${LibrarySource}_build\")\n"
  92. "${CONDITIONAL_STATIC_DEFINE}"
  93. "add_executable(compiletest src.cpp)\n"
  94. "target_link_libraries(compiletest ${Library})\n"
  95. )
  96. try_compile(Result ${CMAKE_CURRENT_BINARY_DIR}/fail${COUNT}
  97. ${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}
  98. compiletest
  99. OUTPUT_VARIABLE Out
  100. )
  101. endmacro()
  102. macro(build_fail Include Library LibrarySource Source Message)
  103. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  104. test_fail(Result ${Message})
  105. endmacro()
  106. macro(build_pass Include Library LibrarySource Source Message)
  107. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  108. test_pass(Result ${Message})
  109. endmacro()
  110. include(GenerateExportHeader)
  111. add_subdirectory(visibility_preset)
  112. add_compiler_export_flags()
  113. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  114. message("#### COMPILER_HAS_DEPRECATED: " ${COMPILER_HAS_DEPRECATED})
  115. message("#### COMPILER_HAS_HIDDEN_VISIBILITY: " ${COMPILER_HAS_HIDDEN_VISIBILITY})
  116. message("#### WIN32: " ${WIN32})
  117. message("#### HAS_WERROR_FLAG: " ${HAS_WERROR_FLAG})
  118. set(link_libraries)
  119. macro(macro_add_test_library name)
  120. add_subdirectory(${name})
  121. include_directories(${name}
  122. ${${name}_BINARY_DIR} # For the export header.
  123. )
  124. list(APPEND link_libraries ${name})
  125. add_subdirectory(${name}test)
  126. endmacro()
  127. macro_add_test_library(libshared)
  128. macro_add_test_library(libstatic)
  129. add_subdirectory(lib_shared_and_static)
  130. add_subdirectory(lib_shared_and_statictest)
  131. add_subdirectory(override_symbol)
  132. add_subdirectory(nodeprecated)
  133. add_subdirectory(prefix)
  134. if(NOT BORLAND)
  135. add_subdirectory(c_identifier)
  136. endif()
  137. if (CMAKE_COMPILER_IS_GNUCXX OR (${CMAKE_CXX_COMPILER_ID} MATCHES Clang))
  138. # We deliberately call deprecated methods, and test for that elsewhere.
  139. # No need to clutter the test output with warnings.
  140. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
  141. endif()
  142. if(MSVC AND COMPILER_HAS_DEPRECATED)
  143. add_definitions(/wd4996)
  144. endif()
  145. add_executable(GenerateExportHeader exportheader_test.cpp)
  146. target_link_libraries(GenerateExportHeader ${link_libraries})