[CMAKE]
[reactos.git] / msc.cmake
1
2 if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86")
3 add_definitions(-D__i386__)
4 endif()
5
6 add_definitions(-Dinline=__inline)
7
8 if(NOT CMAKE_CROSSCOMPILING)
9
10
11 else()
12
13 add_definitions(/GS- /Zl /Zi)
14 add_definitions(-Dinline=__inline -D__STDC__=1)
15
16 IF(${_MACHINE_ARCH_FLAG} MATCHES X86)
17 SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
18 SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
19 SET (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")
20 ENDIF()
21
22 link_directories("${REACTOS_BINARY_DIR}/importlibs" ${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)
23
24 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>")
25
26 macro(add_linkerflag MODULE _flag)
27 set(NEW_LINKER_FLAGS ${_flag})
28 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
29 if(LINKER_FLAGS)
30 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
31 endif()
32 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
33 endmacro()
34
35 macro(set_entrypoint MODULE ENTRYPOINT)
36 if(${ENTRYPOINT} STREQUAL "0")
37 add_linkerflag(${MODULE} "/ENTRY:0")
38 else()
39 add_linkerflag(${MODULE} "/ENTRY:${ENTRYPOINT}")
40 endif()
41 endmacro()
42
43 macro(set_subsystem MODULE SUBSYSTEM)
44 add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")
45 endmacro()
46
47 macro(set_image_base MODULE IMAGE_BASE)
48 add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")
49 endmacro()
50
51 macro(set_module_type MODULE TYPE)
52 add_dependencies(${MODULE} psdk buildno_header)
53 if(${TYPE} MATCHES nativecui)
54 set_subsystem(${MODULE} native)
55 add_importlibs(${MODULE} ntdll)
56 endif()
57 if (${TYPE} MATCHES win32gui)
58 set_subsystem(${MODULE} windows)
59 set_entrypoint(${MODULE} WinMainCRTStartup)
60 target_link_libraries(${MODULE} mingw_common mingw_wmain)
61 endif ()
62 if (${TYPE} MATCHES win32cui)
63 set_subsystem(${MODULE} console)
64 set_entrypoint(${MODULE} mainCRTStartup)
65 target_link_libraries(${MODULE} mingw_common mingw_wmain)
66 endif ()
67 if(${TYPE} MATCHES win32dll)
68 # Need this only because mingw library is broken
69 set_entrypoint(${MODULE} DllMainCRTStartup@12)
70 if(DEFINED baseaddress_${MODULE})
71 set_image_base(${MODULE} ${baseaddress_${MODULE}})
72 else()
73 message(STATUS "${MODULE} has no base address")
74 endif()
75 target_link_libraries(${MODULE} mingw_common mingw_dllmain)
76 add_importlibs(${MODULE} msvcrt kernel32)
77 add_linkerflag(${MODULE} "/DLL")
78 endif()
79
80 endmacro()
81
82 macro(set_unicode)
83 add_definitions(-DUNICODE -D_UNICODE)
84 endmacro()
85
86 set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
87 set(CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
88
89 macro(set_rc_compiler)
90 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake
91 endmacro()
92
93 #idl files support
94 set(IDL_COMPILER midl)
95 set(IDL_FLAGS /win32 /Dstrict_context_handle=)
96 set(IDL_HEADER_ARG /h) #.h
97 set(IDL_TYPELIB_ARG /tlb) #.tlb
98 set(IDL_SERVER_ARG /sstub) #.c for stub server library
99 set(IDL_CLIENT_ARG /cstub) #.c for stub client library
100
101 # Thanks MS for creating a stupid linker
102 macro(add_importlib_target _spec_file)
103 get_filename_component(_name ${_spec_file} NAME_WE)
104
105 # Generate the asm stub file
106 add_custom_command(
107 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
108 COMMAND native-spec2pdef -s ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
109 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
110
111 # Generate a the export def file
112 add_custom_command(
113 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
114 COMMAND native-spec2pdef -n -r ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
115 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
116
117 # Assemble the file
118 add_custom_command(
119 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
120 COMMAND ${CMAKE_ASM_COMPILER} /Fo${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj /c /Ta ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
121 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm"
122 )
123
124 # Add neccessary importlibs for redirections
125 foreach(_lib ${ARGN})
126 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
127 endforeach()
128
129 # Build the importlib
130 add_custom_command(
131 OUTPUT {CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
132 COMMAND LINK /LIB /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}
133 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj" "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def" ${_libraries}
134 )
135
136 # Add the importlib target
137 add_custom_target(
138 lib${_name}
139 DEPENDS {CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
140 )
141 endmacro()
142
143 macro(add_importlibs MODULE)
144 foreach(LIB ${ARGN})
145 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.lib)
146 add_dependencies(${MODULE} lib${LIB})
147 endforeach()
148 endmacro()
149
150 MACRO(spec2def _dllname _spec_file)
151 get_filename_component(_file ${_spec_file} NAME_WE)
152 add_custom_command(
153 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
154 COMMAND native-spec2pdef -n --dll ${_dllname} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
155 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
156 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
157 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
158 ENDMACRO(spec2def _dllname _spec_file)
159
160 macro(pdef2def _pdef_file)
161 get_filename_component(_file ${_pdef_file} NAME_WE)
162 add_custom_command(
163 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
164 COMMAND ${CMAKE_C_COMPILER} /EP /c ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file} > ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
165 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file})
166 add_custom_target(
167 ${_file}_def
168 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def)
169 endmacro(pdef2def _pdef_file)
170
171 macro(set_pdef_file _module _pdef_file)
172 pdef2def(${_pdef_file})
173 get_filename_component(_file ${_pdef_file} NAME_WE)
174 add_linkerflag(${_module} "/DEF:${CMAKE_CURRENT_BINARY_DIR}/${_file}.def")
175 add_dependencies(${_module} ${_file}_def)
176 endmacro()
177
178 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
179
180 #pseh workaround
181 set(PSEH_LIB "")
182
183 endif()