aaa6d34e02dc0ee768394be2cc90ce4a34cfd6e5
[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
15 # Compiler Core
16 add_definitions(-pipe -fms-extensions)
17
18 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>")
19 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>")
20 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>")
21
22 # Debugging (Note: DWARF-4 on 4.5.1 when we ship)
23 #add_definitions(-gdwarf-2 -g2 -femit-struct-debug-detailed=none -feliminate-unused-debug-types)
24
25 # Tuning
26 add_definitions(-march=pentium -mtune=i686)
27
28 # Warnings
29 add_definitions(-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-error=uninitialized -Wno-unused-value -Winvalid-pch)
30
31 # Optimizations
32 add_definitions(-Os -fno-strict-aliasing -ftracer -momit-leaf-frame-pointer -mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls)
33
34 # Macros
35 macro(add_linkerflag MODULE _flag)
36 set(NEW_LINKER_FLAGS ${_flag})
37 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
38 if(LINKER_FLAGS)
39 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
40 endif()
41 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
42 endmacro()
43
44 macro(set_entrypoint MODULE ENTRYPOINT)
45 if(${ENTRYPOINT} STREQUAL "0")
46 add_linkerflag(${MODULE} "-Wl,-entry,0")
47 else()
48 add_linkerflag(${MODULE} "-Wl,-entry,_${ENTRYPOINT}")
49 endif()
50 endmacro()
51
52 macro(set_subsystem MODULE SUBSYSTEM)
53 add_linkerflag(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
54 endmacro()
55
56 macro(set_image_base MODULE IMAGE_BASE)
57 add_linkerflag(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
58 endmacro()
59
60 macro(add_importlibs MODULE)
61 foreach(LIB ${ARGN})
62 target_link_libraries(${MODULE} ${LIB}.dll.a)
63 endforeach()
64 endmacro()
65
66 macro(set_module_type MODULE TYPE)
67
68 add_dependencies(${MODULE} psdk buildno_header)
69
70 if(${IS_CPP})
71 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
72 endif()
73
74 if(${TYPE} MATCHES nativecui)
75 set_subsystem(${MODULE} native)
76 set_entrypoint(${MODULE} NtProcessStartup@4)
77 endif()
78 if(${TYPE} MATCHES win32gui)
79 set_subsystem(${MODULE} windows)
80 set_entrypoint(${MODULE} WinMainCRTStartup)
81 if(NOT IS_UNICODE)
82 target_link_libraries(${MODULE} mingw_main)
83 else()
84 target_link_libraries(${MODULE} mingw_wmain)
85 endif(NOT IS_UNICODE)
86 target_link_libraries(${MODULE} mingw_common)
87 endif()
88 if(${TYPE} MATCHES win32cui)
89 set_subsystem(${MODULE} console)
90 set_entrypoint(${MODULE} mainCRTStartup)
91 if(NOT IS_UNICODE)
92 target_link_libraries(${MODULE} mingw_main)
93 else()
94 target_link_libraries(${MODULE} mingw_wmain)
95 endif(NOT IS_UNICODE)
96 target_link_libraries(${MODULE} mingw_common)
97 endif()
98 if(${TYPE} MATCHES win32dll)
99 set_entrypoint(${MODULE} DllMain@12)
100 if(DEFINED baseaddress_${MODULE})
101 set_image_base(${MODULE} ${baseaddress_${MODULE}})
102 else()
103 message(STATUS "${MODULE} has no base address")
104 endif()
105 endif()
106 if(${TYPE} MATCHES win32ocx)
107 set_entrypoint(${MODULE} DllMain@12)
108 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
109 endif()
110 if(${TYPE} MATCHES cpl)
111 set_entrypoint(${MODULE} DllMain@12)
112 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
113 endif()
114 if(${TYPE} MATCHES kernelmodedriver)
115 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols" SUFFIX ".sys")
116 set_entrypoint(${MODULE} DriverEntry@8)
117 set_subsystem(${MODULE} native)
118 set_image_base(${MODULE} 0x00010000)
119 add_dependencies(${MODULE} bugcodes)
120 endif()
121 endmacro()
122
123 macro(set_unicode)
124 add_definitions(-DUNICODE -D_UNICODE)
125 set(IS_UNICODE 1)
126 endmacro()
127
128 # Workaround lack of mingw RC support in cmake
129 macro(set_rc_compiler)
130 get_directory_property(defines COMPILE_DEFINITIONS)
131 get_directory_property(includes INCLUDE_DIRECTORIES)
132
133 foreach(arg ${defines})
134 set(result_defs "${result_defs} -D${arg}")
135 endforeach(arg ${defines})
136
137 foreach(arg ${includes})
138 set(result_incs "-I${arg} ${result_incs}")
139 endforeach(arg ${includes})
140
141 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${result_defs} ${result_incs} -i <SOURCE> -O coff -o <OBJECT>")
142 endmacro()
143
144 #idl files support
145 set(IDL_COMPILER native-widl)
146 set(IDL_FLAGS -m32 --win32)
147 set(IDL_HEADER_ARG -h -H) #.h
148 set(IDL_TYPELIB_ARG -t -T) #.tlb
149 set(IDL_SERVER_ARG -s -S) #.c for server library
150 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
151
152 macro(add_importlib_target _def_file)
153 # empty for now, while import libs are shipped
154 endmacro()
155
156 MACRO(spec2def _dllname _spec_file)
157 get_filename_component(_file ${_spec_file} NAME_WE)
158 add_custom_command(
159 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
160 COMMAND native-winebuild -o ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def --def -E ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} --filename ${_dllname}
161 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
162 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
163 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
164 ENDMACRO(spec2def _dllname _spec_file)
165
166 macro(pdef2def _pdef_file)
167 get_filename_component(_file ${_pdef_file} NAME_WE)
168 add_custom_command(
169 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
170 COMMAND ${MINGW_PREFIX}cpp -o ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -P -E ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file}
171 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file})
172 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
173 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
174 add_custom_target(
175 ${_file}_def
176 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def)
177 endmacro(pdef2def _pdef_file)
178
179 macro(set_pdef_file _module _pdef_file)
180 pdef2def(${_pdef_file})
181 get_filename_component(_file ${_pdef_file} NAME_WE)
182 target_link_libraries(${_module} "${CMAKE_CURRENT_BINARY_DIR}/${_file}.def")
183 add_dependencies(${_module} ${_file}_def)
184 endmacro()
185
186 #pseh lib, needed with mingw
187 set(PSEH_LIB "pseh")
188
189 endif()