[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-spec2def -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-spec2def -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} /NOLOGO /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 set(_libraries "")
126 foreach(_lib ${ARGN})
127 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
128 endforeach()
129
130 # Build the importlib
131 add_custom_command(
132 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
133 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}
134 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def ${_libraries}
135 )
136
137 # Add the importlib target
138 add_custom_target(
139 lib${_name}
140 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
141 )
142 endmacro()
143
144 macro(add_importlibs MODULE)
145 foreach(LIB ${ARGN})
146 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.lib)
147 add_dependencies(${MODULE} lib${LIB})
148 endforeach()
149 endmacro()
150
151 MACRO(spec2def _dllname _spec_file)
152 get_filename_component(_file ${_spec_file} NAME_WE)
153 add_custom_command(
154 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
155 COMMAND native-spec2def -n --dll ${_dllname} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
156 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
157 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
158 PROPERTIES GENERATED TRUE)
159 add_custom_target(
160 ${_dllname}.def
161 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def)
162 ENDMACRO(spec2def _dllname _spec_file)
163
164 # Optional 3rd parameter: dllname
165 macro(set_export_spec _module _spec_file)
166 get_filename_component(_file ${_spec_file} NAME_WE)
167 if (${ARGC} GREATER 2)
168 set(_dllname ${ARGV2})
169 else()
170 set(_dllname ${_file}.dll)
171 endif()
172 spec2def(${_dllname} ${_spec_file})
173 add_linkerflag(${_module} "/DEF:${CMAKE_CURRENT_BINARY_DIR}/${_file}.def")
174 add_dependencies(${_module} ${_dllname}.def)
175 endmacro()
176
177 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
178
179 #pseh workaround
180 set(PSEH_LIB "")
181
182 endif()