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