679cc215763f3418cdecd4260350822f91707dda
[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 set(NEW_LINKER_FLAGS "-Wl,--image-base,${IMAGE_BASE}")
58 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
59 if(LINKER_FLAGS)
60 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
61 endif()
62 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
63 endmacro()
64
65 macro(add_importlibs MODULE)
66 foreach(LIB ${ARGN})
67 target_link_libraries(${MODULE} ${LIB}.dll.a)
68 endforeach()
69 endmacro()
70
71 macro(set_module_type MODULE TYPE)
72
73 add_dependencies(${MODULE} psdk buildno_header)
74
75 if(${IS_CPP})
76 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
77 endif()
78
79 if(${TYPE} MATCHES nativecui)
80 set_subsystem(${MODULE} native)
81 set_entrypoint(${MODULE} NtProcessStartup@4)
82 endif()
83 if(${TYPE} MATCHES win32gui)
84 set_subsystem(${MODULE} windows)
85 set_entrypoint(${MODULE} WinMainCRTStartup)
86 if(NOT IS_UNICODE)
87 target_link_libraries(${MODULE} mingw_main)
88 else()
89 target_link_libraries(${MODULE} mingw_wmain)
90 endif(NOT IS_UNICODE)
91 target_link_libraries(${MODULE} mingw_common)
92 endif()
93 if(${TYPE} MATCHES win32cui)
94 set_subsystem(${MODULE} console)
95 set_entrypoint(${MODULE} mainCRTStartup)
96 if(NOT IS_UNICODE)
97 target_link_libraries(${MODULE} mingw_main)
98 else()
99 target_link_libraries(${MODULE} mingw_wmain)
100 endif(NOT IS_UNICODE)
101 target_link_libraries(${MODULE} mingw_common)
102 endif()
103 if(${TYPE} MATCHES win32dll)
104 set_entrypoint(${MODULE} DllMain@12)
105 if(DEFINED baseaddress_${MODULE})
106 set_image_base(${MODULE} ${baseaddress_${MODULE}})
107 else()
108 message(STATUS "${MODULE} has no base address")
109 endif()
110 endif()
111 if(${TYPE} MATCHES win32ocx)
112 set_entrypoint(${MODULE} DllMain@12)
113 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
114 endif()
115 if(${TYPE} MATCHES cpl)
116 set_entrypoint(${MODULE} DllMain@12)
117 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
118 endif()
119 if(${TYPE} MATCHES kernelmodedriver)
120 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols" SUFFIX ".sys")
121 set_entrypoint(${MODULE} DriverEntry@8)
122 set_subsystem(${MODULE} native)
123 set_image_base(${MODULE} 0x00010000)
124 add_dependencies(${MODULE} bugcodes)
125 endif()
126 endmacro()
127
128 macro(set_unicode)
129 add_definitions(-DUNICODE -D_UNICODE)
130 set(IS_UNICODE 1)
131 endmacro()
132
133 # Workaround lack of mingw RC support in cmake
134 macro(set_rc_compiler)
135 get_directory_property(defines COMPILE_DEFINITIONS)
136 get_directory_property(includes INCLUDE_DIRECTORIES)
137
138 foreach(arg ${defines})
139 set(result_defs "${result_defs} -D${arg}")
140 endforeach(arg ${defines})
141
142 foreach(arg ${includes})
143 set(result_incs "-I${arg} ${result_incs}")
144 endforeach(arg ${includes})
145
146 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${result_defs} ${result_incs} -i <SOURCE> -O coff -o <OBJECT>")
147 endmacro()
148
149 #idl files support
150 set(IDL_COMPILER native-widl)
151 set(IDL_FLAGS -m32 --win32)
152 set(IDL_HEADER_ARG -h -H) #.h
153 set(IDL_TYPELIB_ARG -t -T) #.tlb
154 set(IDL_SERVER_ARG -s -S) #.c for server library
155 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
156
157 macro(add_importlib_target _def_file)
158 # empty for now, while import libs are shipped
159 endmacro()
160
161 macro(pdef2def _pdef_file)
162 get_filename_component(_file ${_pdef_file} NAME_WE)
163 add_custom_command(
164 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
165 COMMAND ${MINGW_PREFIX}cpp -o ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -P -E ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file}
166 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_pdef_file})
167 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
168 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
169 add_custom_target(
170 ${_file}_def
171 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def)
172 endmacro(pdef2def _pdef_file)
173
174 macro(set_pdef_file _module _pdef_file)
175 pdef2def(${_pdef_file})
176 get_filename_component(_file ${_pdef_file} NAME_WE)
177 target_link_libraries(${_module} "${CMAKE_CURRENT_BINARY_DIR}/${_file}.def")
178 add_dependencies(${_module} ${_file}_def)
179 endmacro()
180
181 #pseh lib, needed with mingw
182 set(PSEH_LIB "pseh")
183
184 endif()