[CMAKE]
[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(add_pch _target_name _header_filename _src_list)
37 get_filename_component(FILE ${_header_filename} NAME)
38 set(_gch_filename "${_target_name}_${FILE}.gch")
39 list(APPEND ${_src_list} ${_gch_filename})
40 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
41 file(REMOVE ${_gch_filename})
42 add_custom_command(
43 OUTPUT ${_gch_filename}
44 COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${_args}
45 DEPENDS ${_header_filename})
46 ENDMACRO(add_pch _target_name _header_filename _src_list)
47
48 macro(add_linkerflag MODULE _flag)
49 set(NEW_LINKER_FLAGS ${_flag})
50 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
51 if(LINKER_FLAGS)
52 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
53 endif()
54 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
55 endmacro()
56
57 macro(set_entrypoint MODULE ENTRYPOINT)
58 if(${ENTRYPOINT} STREQUAL "0")
59 add_linkerflag(${MODULE} "-Wl,-entry,0")
60 else()
61 add_linkerflag(${MODULE} "-Wl,-entry,_${ENTRYPOINT}")
62 endif()
63 endmacro()
64
65 macro(set_subsystem MODULE SUBSYSTEM)
66 add_linkerflag(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
67 endmacro()
68
69 macro(set_image_base MODULE IMAGE_BASE)
70 add_linkerflag(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
71 endmacro()
72
73 macro(add_importlibs MODULE)
74 foreach(LIB ${ARGN})
75 target_link_libraries(${MODULE} ${LIB}.dll.a)
76 endforeach()
77 endmacro()
78
79 macro(set_module_type MODULE TYPE)
80
81 add_dependencies(${MODULE} psdk buildno_header)
82
83 if(${IS_CPP})
84 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
85 endif()
86
87 if(${TYPE} MATCHES nativecui)
88 set_subsystem(${MODULE} native)
89 set_entrypoint(${MODULE} NtProcessStartup@4)
90 endif()
91 if(${TYPE} MATCHES win32gui)
92 set_subsystem(${MODULE} windows)
93 set_entrypoint(${MODULE} WinMainCRTStartup)
94 if(NOT IS_UNICODE)
95 target_link_libraries(${MODULE} mingw_main)
96 else()
97 target_link_libraries(${MODULE} mingw_wmain)
98 endif(NOT IS_UNICODE)
99 target_link_libraries(${MODULE} mingw_common)
100 endif()
101 if(${TYPE} MATCHES win32cui)
102 set_subsystem(${MODULE} console)
103 set_entrypoint(${MODULE} mainCRTStartup)
104 if(NOT IS_UNICODE)
105 target_link_libraries(${MODULE} mingw_main)
106 else()
107 target_link_libraries(${MODULE} mingw_wmain)
108 endif(NOT IS_UNICODE)
109 target_link_libraries(${MODULE} mingw_common)
110 endif()
111 if(${TYPE} MATCHES win32dll)
112 set_entrypoint(${MODULE} DllMain@12)
113 if(DEFINED baseaddress_${MODULE})
114 set_image_base(${MODULE} ${baseaddress_${MODULE}})
115 else()
116 message(STATUS "${MODULE} has no base address")
117 endif()
118 endif()
119 if(${TYPE} MATCHES win32ocx)
120 set_entrypoint(${MODULE} DllMain@12)
121 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
122 endif()
123 if(${TYPE} MATCHES cpl)
124 set_entrypoint(${MODULE} DllMain@12)
125 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
126 endif()
127 if(${TYPE} MATCHES kernelmodedriver)
128 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols -Wl,-file-alignment=0x1000 -Wl,-section-alignment=0x1000" SUFFIX ".sys")
129 set_entrypoint(${MODULE} DriverEntry@8)
130 set_subsystem(${MODULE} native)
131 set_image_base(${MODULE} 0x00010000)
132 add_dependencies(${MODULE} bugcodes)
133 endif()
134 endmacro()
135
136 macro(set_unicode)
137 add_definitions(-DUNICODE -D_UNICODE)
138 set(IS_UNICODE 1)
139 endmacro()
140
141 # Workaround lack of mingw RC support in cmake
142 macro(set_rc_compiler)
143 get_directory_property(defines COMPILE_DEFINITIONS)
144 get_directory_property(includes INCLUDE_DIRECTORIES)
145
146 foreach(arg ${defines})
147 set(result_defs "${result_defs} -D${arg}")
148 endforeach(arg ${defines})
149
150 foreach(arg ${includes})
151 set(result_incs "-I${arg} ${result_incs}")
152 endforeach(arg ${includes})
153
154 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${result_defs} ${result_incs} -i <SOURCE> -O coff -o <OBJECT>")
155 endmacro()
156
157 #idl files support
158 set(IDL_COMPILER native-widl)
159 set(IDL_FLAGS -m32 --win32)
160 set(IDL_HEADER_ARG -h -H) #.h
161 set(IDL_TYPELIB_ARG -t -T) #.tlb
162 set(IDL_SERVER_ARG -s -S) #.c for server library
163 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
164
165 macro(add_importlib_target _def_file)
166 # empty for now, while import libs are shipped
167 endmacro()
168
169 macro(spec2def _dllname _spec_file)
170 get_filename_component(_file ${_spec_file} NAME_WE)
171 add_custom_command(
172 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
173 COMMAND native-spec2def --dll ${_dllname} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
174 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
175 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
176 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
177 endmacro()
178
179 # Optional 3rd parameter: dllname
180 macro(set_export_spec _module _spec_file)
181 get_filename_component(_file ${_spec_file} NAME_WE)
182 if (${ARGC} GREATER 2)
183 set(_dllname ${ARGV2})
184 else()
185 set(_dllname ${_file}.dll)
186 endif()
187 spec2def(${_dllname} ${_spec_file})
188 endmacro()
189
190 #pseh lib, needed with mingw
191 set(PSEH_LIB "pseh")
192
193 endif()