[PSDK]
[reactos.git] / gcc.cmake
1
2 if(NOT CMAKE_CROSSCOMPILING)
3
4 add_definitions(-fshort-wchar)
5
6 else()
7
8 # Linking
9 link_directories("${REACTOS_SOURCE_DIR}/importlibs" ${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)
10 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
11 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
12 set(CMAKE_EXE_LINKER_FLAGS "-nodefaultlibs -nostdlib -Wl,--enable-auto-image-base -Wl,--kill-at -Wl,--disable-auto-import")
13 # -Wl,-T,${REACTOS_SOURCE_DIR}/global.lds
14 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
15
16 # Compiler Core
17 add_definitions(-pipe -fms-extensions)
18
19 set(CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
20 set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
21 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>")
22
23 # Debugging (Note: DWARF-4 on 4.5.1 when we ship)
24 #add_definitions(-gdwarf-2 -g2 -femit-struct-debug-detailed=none -feliminate-unused-debug-types)
25
26 # Tuning
27 add_definitions(-march=pentium -mtune=i686)
28
29 # Warnings
30 add_definitions(-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-error=uninitialized -Wno-unused-value -Winvalid-pch)
31
32 # Optimizations
33 add_definitions(-Os -fno-strict-aliasing -ftracer -momit-leaf-frame-pointer -mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls)
34
35 # Macros
36 MACRO(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
37 # Add the precompiled header to the build
38 get_filename_component(FILE ${_header_filename} NAME)
39 set(_gch_filename "${_target_name}_${FILE}.gch")
40 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
41
42 # This gets us our includes
43 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
44 foreach(item ${DIRINC})
45 list(APPEND ${_out_compile_flags} -I${item})
46 endforeach(item)
47
48 # This is a particular bit of undocumented/hacky magic I'm quite proud of
49 get_directory_property(_compiler_flags DEFINITIONS)
50 string(REPLACE "\ " "\t" _compiler_flags ${_compiler_flags})
51 list(APPEND ${_out_compile_flags} ${_compiler_flags})
52
53 # This gets any specific definitions that were added with set-target-property
54 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
55 if (_target_defs)
56 foreach(item ${_target_defs})
57 list(APPEND ${_out_compile_flags} -D${item})
58 endforeach(item)
59 endif()
60 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
61
62 MACRO(add_pch _target_name _header_filename _src_list)
63 get_filename_component(FILE ${_header_filename} NAME)
64 set(_gch_filename "${_target_name}_${FILE}.gch")
65 list(APPEND ${_src_list} ${_gch_filename})
66 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
67 file(REMOVE ${_gch_filename})
68 add_custom_command(
69 OUTPUT ${_gch_filename}
70 COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${_args}
71 DEPENDS ${_header_filename})
72 ENDMACRO(add_pch _target_name _header_filename _src_list)
73
74 macro(add_linkerflag MODULE _flag)
75 set(NEW_LINKER_FLAGS ${_flag})
76 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
77 if(LINKER_FLAGS)
78 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
79 endif()
80 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
81 endmacro()
82
83 macro(set_entrypoint MODULE ENTRYPOINT)
84 if(${ENTRYPOINT} STREQUAL "0")
85 add_linkerflag(${MODULE} "-Wl,-entry,0")
86 else()
87 add_linkerflag(${MODULE} "-Wl,-entry,_${ENTRYPOINT}")
88 endif()
89 endmacro()
90
91 macro(set_subsystem MODULE SUBSYSTEM)
92 add_linkerflag(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
93 endmacro()
94
95 macro(set_image_base MODULE IMAGE_BASE)
96 add_linkerflag(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
97 endmacro()
98
99 macro(add_importlibs MODULE)
100 foreach(LIB ${ARGN})
101 target_link_libraries(${MODULE} ${LIB}.dll.a)
102 endforeach()
103 endmacro()
104
105 macro(set_module_type MODULE TYPE)
106
107 add_dependencies(${MODULE} psdk buildno_header)
108
109 if(${IS_CPP})
110 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
111 endif()
112
113 if(${TYPE} MATCHES nativecui)
114 set_subsystem(${MODULE} native)
115 set_entrypoint(${MODULE} NtProcessStartup@4)
116 endif()
117 if(${TYPE} MATCHES win32gui)
118 set_subsystem(${MODULE} windows)
119 set_entrypoint(${MODULE} WinMainCRTStartup)
120 if(NOT IS_UNICODE)
121 target_link_libraries(${MODULE} mingw_main)
122 else()
123 target_link_libraries(${MODULE} mingw_wmain)
124 endif(NOT IS_UNICODE)
125 target_link_libraries(${MODULE} mingw_common)
126 endif()
127 if(${TYPE} MATCHES win32cui)
128 set_subsystem(${MODULE} console)
129 set_entrypoint(${MODULE} mainCRTStartup)
130 if(NOT IS_UNICODE)
131 target_link_libraries(${MODULE} mingw_main)
132 else()
133 target_link_libraries(${MODULE} mingw_wmain)
134 endif(NOT IS_UNICODE)
135 target_link_libraries(${MODULE} mingw_common)
136 endif()
137 if(${TYPE} MATCHES win32dll)
138 set_entrypoint(${MODULE} DllMainCRTStartup@12)
139 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
140 if(DEFINED baseaddress_${MODULE})
141 set_image_base(${MODULE} ${baseaddress_${MODULE}})
142 else()
143 message(STATUS "${MODULE} has no base address")
144 endif()
145 endif()
146 if(${TYPE} MATCHES win32ocx)
147 set_entrypoint(${MODULE} DllMainCRTStartup@12)
148 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
149 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
150 endif()
151 if(${TYPE} MATCHES cpl)
152 set_entrypoint(${MODULE} DllMainCRTStartup@12)
153 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
154 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
155 endif()
156 if(${TYPE} MATCHES kernelmodedriver)
157 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols -Wl,-file-alignment=0x1000 -Wl,-section-alignment=0x1000" SUFFIX ".sys")
158 set_entrypoint(${MODULE} DriverEntry@8)
159 set_subsystem(${MODULE} native)
160 set_image_base(${MODULE} 0x00010000)
161 add_dependencies(${MODULE} bugcodes)
162 endif()
163 endmacro()
164
165 macro(set_unicode)
166 add_definitions(-DUNICODE -D_UNICODE)
167 set(IS_UNICODE 1)
168 endmacro()
169
170 # Workaround lack of mingw RC support in cmake
171 macro(set_rc_compiler)
172 get_directory_property(defines COMPILE_DEFINITIONS)
173 get_directory_property(includes INCLUDE_DIRECTORIES)
174
175 foreach(arg ${defines})
176 set(result_defs "${result_defs} -D${arg}")
177 endforeach(arg ${defines})
178
179 foreach(arg ${includes})
180 set(result_incs "-I${arg} ${result_incs}")
181 endforeach(arg ${includes})
182
183 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${result_defs} ${result_incs} -i <SOURCE> -O coff -o <OBJECT>")
184 endmacro()
185
186 #idl files support
187 set(IDL_COMPILER native-widl)
188 set(IDL_FLAGS -m32 --win32)
189 set(IDL_HEADER_ARG -h -H) #.h
190 set(IDL_TYPELIB_ARG -t -T) #.tlb
191 set(IDL_SERVER_ARG -s -S) #.c for server library
192 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
193
194 macro(add_importlib_target _def_file)
195 # empty for now, while import libs are shipped
196 endmacro()
197
198 macro(spec2def _dllname _spec_file)
199 get_filename_component(_file ${_spec_file} NAME_WE)
200 add_custom_command(
201 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
202 COMMAND native-spec2def --dll ${_dllname} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
203 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
204 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
205 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
206 endmacro()
207
208 # Optional 3rd parameter: dllname
209 macro(set_export_spec _module _spec_file)
210 get_filename_component(_file ${_spec_file} NAME_WE)
211 if (${ARGC} GREATER 2)
212 set(_dllname ${ARGV2})
213 else()
214 set(_dllname ${_file}.dll)
215 endif()
216 spec2def(${_dllname} ${_spec_file})
217 endmacro()
218
219 #pseh lib, needed with mingw
220 set(PSEH_LIB "pseh")
221
222 endif()