PchReuseFromObjLib.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. set(CMAKE_INTERMEDIATE_DIR_STRATEGY FULL CACHE STRING "" FORCE)
  2. enable_language(C)
  3. enable_language(CXX)
  4. set(CMAKE_PCH_WARN_INVALID OFF)
  5. if(CMAKE_CXX_COMPILE_OPTIONS_USE_PCH)
  6. add_definitions(-DHAVE_PCH_SUPPORT)
  7. endif()
  8. ######################################################################
  9. file(WRITE ${CMAKE_BINARY_DIR}/CONFIG/config.hxx "/*empty*/\n")
  10. file(WRITE ${CMAKE_BINARY_DIR}/pch.cxx [=[
  11. void nothing()
  12. {
  13. }
  14. ]=])
  15. file(WRITE ${CMAKE_BINARY_DIR}/string.hxx [=[
  16. #include <string.h>
  17. namespace std {
  18. struct string
  19. {
  20. char storage[20];
  21. string(const char* s) {
  22. strcpy(storage, s);
  23. }
  24. const char* c_str() const {
  25. return storage;
  26. }
  27. };
  28. }
  29. ]=])
  30. add_library(pch-generator OBJECT ${CMAKE_BINARY_DIR}/pch.cxx)
  31. set_property(TARGET pch-generator PROPERTY POSITION_INDEPENDENT_CODE ON)
  32. target_precompile_headers(pch-generator PRIVATE ${CMAKE_BINARY_DIR}/string.hxx)
  33. target_include_directories(pch-generator PRIVATE ${CMAKE_BINARY_DIR}/CONFIG)
  34. ######################################################################
  35. file(WRITE ${CMAKE_BINARY_DIR}/message.cxx [=[
  36. #include "message.hxx"
  37. #ifndef HAVE_PCH_SUPPORT
  38. #include "string.hxx"
  39. #endif
  40. const char* message()
  41. {
  42. static std::string greeting("hi there");
  43. return greeting.c_str();
  44. }
  45. ]=])
  46. file(WRITE ${CMAKE_BINARY_DIR}/message.hxx [=[
  47. #include "config.hxx"
  48. #ifdef WIN32_BUILD_SHARED
  49. #ifdef BUILD_LIBRARY
  50. #define MESSAGE_EXPORT __declspec(dllexport)
  51. #else
  52. #define MESSAGE_EXPORT __declspec(dllimport)
  53. #endif
  54. #else
  55. #define MESSAGE_EXPORT
  56. #endif
  57. MESSAGE_EXPORT const char* message();
  58. ]=])
  59. ######################################################################
  60. file(WRITE ${CMAKE_BINARY_DIR}/main.cxx [=[
  61. #include "message.hxx"
  62. #include <string.h>
  63. int main()
  64. {
  65. return strcmp(message(), "hi there");
  66. }
  67. ]=])
  68. ######################################################################
  69. enable_testing()
  70. function(add_library_and_executable type)
  71. add_library(message_${type} ${type} ${CMAKE_BINARY_DIR}/message.cxx)
  72. target_precompile_headers(message_${type} REUSE_FROM pch-generator)
  73. set_property(TARGET message_${type} PROPERTY POSITION_INDEPENDENT_CODE ON)
  74. set_property(TARGET message_${type} PROPERTY DEFINE_SYMBOL "")
  75. if (WIN32 AND type STREQUAL "SHARED")
  76. file(WRITE ${CMAKE_BINARY_DIR}/SHARED/config.hxx [=[
  77. #define BUILD_LIBRARY
  78. #define WIN32_BUILD_SHARED
  79. ]=])
  80. target_include_directories(message_${type} PRIVATE ${CMAKE_BINARY_DIR}/SHARED)
  81. # Workaround for VS2008, the compiler fails with
  82. # c1xx : fatal error C1083: Cannot open source file: '_WINDLL': No such file or directory
  83. file(WRITE ${CMAKE_BINARY_DIR}/_WINDLL "/*empty*/\n")
  84. else()
  85. target_include_directories(message_${type} PRIVATE ${CMAKE_BINARY_DIR}/CONFIG)
  86. endif()
  87. add_executable(main_${type} ${CMAKE_BINARY_DIR}/main.cxx)
  88. target_include_directories(main_${type} PRIVATE ${CMAKE_BINARY_DIR})
  89. if (WIN32 AND type STREQUAL "SHARED")
  90. file(WRITE ${CMAKE_BINARY_DIR}/main_SHARED/config.hxx "#define WIN32_BUILD_SHARED\n")
  91. target_include_directories(main_${type} PRIVATE ${CMAKE_BINARY_DIR}/main_SHARED)
  92. else()
  93. target_include_directories(main_${type} PRIVATE ${CMAKE_BINARY_DIR}/CONFIG)
  94. endif()
  95. target_link_libraries(main_${type} PRIVATE message_${type})
  96. add_test(NAME main_${type} COMMAND main_${type})
  97. endfunction()
  98. foreach(type OBJECT STATIC SHARED)
  99. add_library_and_executable(${type})
  100. endforeach()