CMakeLists.txt 3.6 KB

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