CMakeLists.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "include(GenerateExportHeader)\n"
  31. "add_compiler_export_flags()\n"
  32. "if(CMAKE_COMPILER_IS_GNUCXX)\n"
  33. " add_definitions(-Werror)\n"
  34. "else()\n"
  35. " if(MSVC)\n"
  36. # Treat deprecation warnings as errors.
  37. " add_definitions(/we4996)\n"
  38. " endif()\n"
  39. "endif()\n"
  40. "if(MSVC)\n"
  41. " add_definitions(-DCOMPILER_IS_MSVC)\n"
  42. "endif()\n"
  43. "add_subdirectory(${LibrarySource})\n"
  44. "include_directories(${LibrarySource} \${CMAKE_CURRENT_BINARY_DIR}/${LibrarySource})\n"
  45. "${CONDITIONAL_STATIC_DEFINE}"
  46. "add_executable(compiletest src.cpp)\n"
  47. "target_link_libraries(compiletest ${Library})\n"
  48. )
  49. try_compile(Result ${CMAKE_CURRENT_BINARY_DIR}/fail${COUNT}
  50. ${CMAKE_CURRENT_BINARY_DIR}/test${COUNT}
  51. compilefail
  52. OUTPUT_VARIABLE Out
  53. )
  54. endmacro()
  55. macro(build_fail Include Library LibrarySource Source Message)
  56. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  57. if((USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY) OR WIN32)
  58. test_fail(Result ${Message})
  59. endif()
  60. test_pass(Result ${Message})
  61. endmacro()
  62. macro(build_pass Include Library LibrarySource Source Message)
  63. _do_build(${Include} ${Library} ${LibrarySource} "${Source}")
  64. test_pass(Result ${Message})
  65. endmacro()
  66. include(GenerateExportHeader)
  67. add_compiler_export_flags()
  68. if (MSVC)
  69. add_definitions(-DCOMPILER_IS_MSVC)
  70. endif()
  71. set(link_libraries)
  72. macro(macro_add_test_library name)
  73. add_subdirectory(${name})
  74. include_directories(${name}
  75. ${${name}_BINARY_DIR} # For the export header.
  76. )
  77. list(APPEND link_libraries ${name})
  78. add_subdirectory(${name}test)
  79. endmacro()
  80. macro_add_test_library(libshared)
  81. macro_add_test_library(libstatic)
  82. add_subdirectory(lib_shared_and_static)
  83. add_subdirectory(lib_shared_and_statictest)
  84. add_subdirectory(override_symbol)
  85. if (CMAKE_COMPILER_IS_GNUCXX)
  86. # We deliberately call deprecated methods, and test for that elsewhere.
  87. # No need to clutter the test output with warnings.
  88. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
  89. endif()
  90. if(MSVC)
  91. add_definitions(/wd4996)
  92. endif()
  93. add_executable(GenerateExportHeader exportheader_test.cpp)
  94. target_link_libraries(GenerateExportHeader ${link_libraries})