6469fa4e646151461092f6104ad8622a0a4c80f2
[reactos.git] / msc.cmake
1
2 if(0) # Fixme redefinition warning
3 if(OPTIMIZE STREQUAL "1")
4 add_definitions(/O1)
5 elseif(OPTIMIZE STREQUAL "2")
6 add_definitions(/O2)
7 elseif(OPTIMIZE STREQUAL "3")
8 add_definitions(/Ot /Ox /GS-)
9 elseif(OPTIMIZE STREQUAL "4")
10 add_definitions(/Os /Ox /GS-)
11 elseif(OPTIMIZE STREQUAL "5")
12 add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)
13 endif()
14 endif()
15
16 add_definitions(/X /GS- /Zl /Zi)
17 add_definitions(-Dinline=__inline -D__STDC__=1)
18
19 if(${_MACHINE_ARCH_FLAG} MATCHES X86)
20 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
21 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
22 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")
23 endif()
24
25 if(${ARCH} MATCHES amd64)
26 add_definitions(-D__x86_64)
27 endif()
28
29 link_directories("${REACTOS_BINARY_DIR}/importlibs" ${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)
30
31 set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
32
33 macro(add_pch _target_name _header_filename _src_list)
34 get_filename_component(FILE ${_header_filename} NAME)
35 set(_gch_filename "${_target_name}_${FILE}.gch")
36 add_custom_command(
37 OUTPUT ${_gch_filename}
38 COMMAND echo Ignoring precompiled header
39 DEPENDS ${_header_filename})
40 endmacro()
41
42 macro(add_linkerflag MODULE _flag)
43 set(NEW_LINKER_FLAGS ${_flag})
44 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
45 if(LINKER_FLAGS)
46 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
47 endif()
48 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
49 endmacro()
50
51 macro(set_entrypoint MODULE ENTRYPOINT)
52 if(${ENTRYPOINT} STREQUAL "0")
53 add_linkerflag(${MODULE} "/NOENTRY")
54 else()
55 add_linkerflag(${MODULE} "/ENTRY:${ENTRYPOINT}")
56 endif()
57 endmacro()
58
59 macro(set_subsystem MODULE SUBSYSTEM)
60 add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")
61 endmacro()
62
63 macro(set_image_base MODULE IMAGE_BASE)
64 add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")
65 endmacro()
66
67 macro(set_module_type MODULE TYPE)
68 add_dependencies(${MODULE} psdk buildno_header)
69 if(${TYPE} MATCHES nativecui)
70 set_subsystem(${MODULE} native)
71 set_entrypoint(${MODULE} NtProcessStartup@4)
72 endif()
73 if (${TYPE} MATCHES win32gui)
74 set_subsystem(${MODULE} windows)
75 set_entrypoint(${MODULE} WinMainCRTStartup)
76 if(IS_UNICODE)
77 target_link_libraries(${MODULE} mingw_wmain)
78 else()
79 target_link_libraries(${MODULE} mingw_main)
80 endif()
81 target_link_libraries(${MODULE} mingw_common msvcsup)
82 endif ()
83 if (${TYPE} MATCHES win32cui)
84 set_subsystem(${MODULE} console)
85 set_entrypoint(${MODULE} mainCRTStartup)
86 if(IS_UNICODE)
87 target_link_libraries(${MODULE} mingw_wmain)
88 else()
89 target_link_libraries(${MODULE} mingw_main)
90 endif()
91 target_link_libraries(${MODULE} mingw_common msvcsup)
92 endif ()
93 if(${TYPE} MATCHES win32dll)
94 # Need this only because mingw library is broken
95 set_entrypoint(${MODULE} DllMainCRTStartup@12)
96 if(DEFINED baseaddress_${MODULE})
97 set_image_base(${MODULE} ${baseaddress_${MODULE}})
98 else()
99 message(STATUS "${MODULE} has no base address")
100 endif()
101 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
102 add_linkerflag(${MODULE} "/DLL")
103 endif()
104 if(${TYPE} MATCHES win32ocx)
105 set_entrypoint(${MODULE} DllMainCRTStartup@12)
106 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
107 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
108 add_linkerflag(${MODULE} "/DLL")
109 endif()
110 if(${TYPE} MATCHES cpl)
111 set_entrypoint(${MODULE} DllMainCRTStartup@12)
112 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
113 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
114 add_linkerflag(${MODULE} "/DLL")
115 endif()
116 if(${TYPE} MATCHES kernelmodedriver)
117 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
118 set_entrypoint(${MODULE} DriverEntry@8)
119 set_subsystem(${MODULE} native)
120 set_image_base(${MODULE} 0x00010000)
121 add_linkerflag(${MODULE} "/DRIVER")
122 add_dependencies(${MODULE} bugcodes)
123 target_link_libraries(${MODULE} msvcsup)
124 endif()
125 endmacro()
126
127 macro(set_unicode)
128 add_definitions(-DUNICODE -D_UNICODE)
129 set(IS_UNICODE 1)
130 endmacro()
131
132 set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
133 set(CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
134
135 macro(set_rc_compiler)
136 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake
137 endmacro()
138
139 # Thanks MS for creating a stupid linker
140 macro(add_importlib_target _exports_file)
141 get_filename_component(_name ${_exports_file} NAME_WE)
142 get_target_property(_suffix ${_name} SUFFIX)
143 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
144 get_target_property(_type ${_name} TYPE)
145 if(${_type} MATCHES EXECUTABLE)
146 set(_suffix ".exe")
147 else()
148 set(_suffix ".dll")
149 endif()
150 endif()
151
152 # Generate the asm stub file and the export def file
153 add_custom_command(
154 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
155 COMMAND native-spec2def --ms --kill-at -r -n=${_name}${_suffix} -d=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def -l=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
156 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
157
158 # Assemble the stub file
159 add_custom_command(
160 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
161 COMMAND ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj /c /Ta ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
162 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")
163
164 # Add neccessary importlibs for redirections
165 set(_libraries "")
166 foreach(_lib ${ARGN})
167 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
168 list(APPEND _dependencies ${_lib})
169 endforeach()
170
171 # Build the importlib
172 add_custom_command(
173 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
174 COMMAND LINK /LIB /NOLOGO /MACHINE:X86 /DEF:${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def /OUT:${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${_libraries}
175 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)
176
177 # Add the importlib target
178 add_custom_target(
179 lib${_name}
180 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)
181
182 add_dependencies(lib${_name} asm ${_dependencies})
183 endmacro()
184
185 macro(add_importlibs MODULE)
186 foreach(LIB ${ARGN})
187 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.lib)
188 add_dependencies(${MODULE} lib${LIB})
189 endforeach()
190 endmacro()
191
192 macro(spec2def _dllname _spec_file)
193 get_filename_component(_file ${_spec_file} NAME_WE)
194 add_custom_command(
195 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
196 COMMAND native-spec2def --ms --kill-at -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
197 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
198 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
199 PROPERTIES GENERATED TRUE)
200 list(APPEND SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c)
201 endmacro()
202
203 macro(macro_mc FILE)
204 set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
205 endmacro()
206
207 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
208
209 #pseh workaround
210 set(PSEH_LIB "pseh")
211