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