[FREELDR][SDK:CMAKE] Move the `MSVC "linker script" file' functionality out of FreeLd...
[reactos.git] / boot / freeldr / freeldr / CMakeLists.txt
1
2 if(SEPARATE_DBG)
3 # FIXME: http://sourceware.org/bugzilla/show_bug.cgi?id=11822
4 set(CMAKE_LDR_PE_HELPER_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
5 set(CMAKE_LDR_PE_HELPER_STANDARD_LIBRARIES "-lgcc" CACHE STRING "Standard C Libraries")
6 endif()
7
8 if(MSVC)
9 # We don't need hotpatching
10 replace_compile_flags("/hotpatch" " ")
11
12 # Explicitly use string pooling
13 add_compile_flags("/GF")
14 endif()
15
16 spec2def(freeldr_pe.exe freeldr.spec)
17
18 if(ARCH STREQUAL "i386")
19 CreateBootSectorTarget(frldr16
20 ${CMAKE_CURRENT_SOURCE_DIR}/arch/realmode/i386.S
21 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
22 F800)
23 elseif(ARCH STREQUAL "amd64")
24 CreateBootSectorTarget(frldr16
25 ${CMAKE_CURRENT_SOURCE_DIR}/arch/realmode/amd64.S
26 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
27 F800)
28 endif()
29
30 include_directories(BEFORE include)
31 include_directories(${REACTOS_SOURCE_DIR}/ntoskrnl/include)
32 include_directories(${REACTOS_SOURCE_DIR}/sdk/lib/cmlib)
33 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/libs)
34 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/elf)
35
36 add_definitions(-D_NTHAL_ -D_BLDR_ -D_NTSYSTEM_)
37
38 list(APPEND FREELDR_BOOTLIB_SOURCE
39 lib/debug.c
40 lib/peloader.c
41 lib/comm/rs232.c
42 ## add KD support
43 lib/fs/btrfs.c
44 lib/fs/ext2.c
45 lib/fs/fat.c
46 lib/fs/fs.c
47 lib/fs/iso.c
48 lib/fs/ntfs.c
49 lib/inifile/ini_init.c
50 lib/inifile/inifile.c
51 lib/inifile/parse.c
52 lib/mm/meminit.c
53 lib/mm/mm.c
54 lib/mm/heap.c)
55
56 list(APPEND FREELDR_NTLDR_SOURCE
57 ntldr/conversion.c
58 ntldr/registry.c
59 ntldr/winldr.c
60 ntldr/wlmemory.c
61 ntldr/wlregistry.c)
62
63 list(APPEND FREELDR_ARC_SOURCE
64 arcname.c
65 machine.c
66 arch/archwsup.c
67 cache/blocklist.c
68 cache/cache.c
69 disk/disk.c
70 disk/partition.c
71 disk/ramdisk.c)
72 #disk/scsiport.c
73
74 list(APPEND FREELDR_BOOTMGR_SOURCE
75 include/freeldr.h
76 cmdline.c
77 custom.c
78 linuxboot.c
79 miscboot.c
80 options.c
81 oslist.c
82 version.c
83 ui/directui.c
84 ui/gui.c
85 ui/minitui.c
86 ui/noui.c
87 ui/tui.c
88 ui/tuimenu.c
89 ui/ui.c
90 video/fade.c
91 video/palette.c
92 video/video.c)
93
94 list(APPEND FREELDR_BASE_ASM_SOURCE)
95
96 if(ARCH STREQUAL "i386")
97 list(APPEND FREELDR_BASE_ASM_SOURCE
98 arch/i386/multiboot.S)
99
100 list(APPEND FREELDR_COMMON_ASM_SOURCE
101 arch/i386/drvmap.S
102 arch/i386/entry.S
103 arch/i386/int386.S
104 arch/i386/pnpbios.S
105 arch/i386/i386trap.S
106 arch/i386/linux.S)
107
108 list(APPEND FREELDR_NTLDR_SOURCE
109 ntldr/arch/i386/winldr.c
110 ntldr/headless.c)
111
112 list(APPEND FREELDR_ARC_SOURCE
113 lib/fs/pxe.c
114 arch/i386/halstub.c
115 arch/i386/ntoskrnl.c
116 arch/i386/archmach.c
117 arch/i386/drivemap.c
118 arch/i386/hardware.c
119 arch/i386/hwacpi.c
120 arch/i386/hwapm.c
121 arch/i386/hwdisk.c
122 arch/i386/hwpci.c
123 arch/i386/i386bug.c
124 arch/i386/i386idt.c
125 arch/i386/i386rtl.c
126 arch/i386/i386vid.c
127 arch/i386/machpc.c
128 arch/i386/pccons.c
129 arch/i386/pcdisk.c
130 arch/i386/pcmem.c
131 arch/i386/pcrtc.c
132 arch/i386/pcvideo.c
133 arch/i386/machxbox.c
134 arch/i386/xboxcons.c
135 arch/i386/xboxdisk.c
136 arch/i386/xboxfont.c
137 arch/i386/xboxi2c.c
138 arch/i386/xboxmem.c
139 arch/i386/xboxrtc.c
140 arch/i386/xboxvideo.c
141 disk/scsiport.c)
142
143 elseif(ARCH STREQUAL "amd64")
144 list(APPEND FREELDR_COMMON_ASM_SOURCE
145 arch/amd64/entry.S
146 arch/amd64/int386.S
147 arch/amd64/pnpbios.S)
148
149 list(APPEND FREELDR_NTLDR_SOURCE
150 ntldr/arch/amd64/winldr.c)
151
152 list(APPEND FREELDR_ARC_SOURCE
153 lib/fs/pxe.c
154 arch/i386/ntoskrnl.c
155 arch/i386/drivemap.c
156 arch/i386/hardware.c
157 arch/i386/hwacpi.c
158 arch/i386/hwapm.c
159 arch/i386/hwdisk.c
160 arch/i386/hwpci.c
161 arch/i386/i386bug.c
162 arch/i386/i386rtl.c
163 arch/i386/i386vid.c
164 arch/i386/machpc.c
165 arch/i386/pccons.c
166 arch/i386/pcdisk.c
167 arch/i386/pcmem.c
168 arch/i386/pcrtc.c
169 arch/i386/pcvideo.c)
170
171 elseif(ARCH STREQUAL "arm")
172 list(APPEND FREELDR_COMMON_ASM_SOURCE
173 arch/arm/boot.S)
174
175 list(APPEND FREELDR_NTLDR_SOURCE
176 ntldr/arch/arm/winldr.c)
177
178 list(APPEND FREELDR_ARC_SOURCE
179 arch/arm/entry.c
180 arch/arm/macharm.c)
181 else()
182 #TBD
183 endif()
184
185 add_asm_files(freeldr_common_asm ${FREELDR_COMMON_ASM_SOURCE})
186
187 add_library(freeldr_common
188 ${freeldr_common_asm}
189 ${FREELDR_BOOTLIB_SOURCE}
190 ${FREELDR_ARC_SOURCE}
191 ${FREELDR_NTLDR_SOURCE}
192 ${FREELDR_BOOTMGR_SOURCE})
193
194 if(USE_CLANG_CL)
195 # We need to reduce the binary size
196 add_target_compile_flags(freeldr_common "/Os")
197 endif()
198
199 set(PCH_SOURCE
200 ${FREELDR_BOOTLIB_SOURCE}
201 ${FREELDR_ARC_SOURCE}
202 ${FREELDR_NTLDR_SOURCE}
203 ${FREELDR_BOOTMGR_SOURCE})
204
205 add_pch(freeldr_common include/freeldr.h PCH_SOURCE)
206 add_dependencies(freeldr_common bugcodes asm xdk)
207
208 ## GCC builds need this extra thing for some reason...
209 if(ARCH STREQUAL "i386" AND NOT MSVC)
210 target_link_libraries(freeldr_common mini_hal)
211 endif()
212
213 add_asm_files(freeldr_base_asm ${FREELDR_BASE_ASM_SOURCE})
214
215 list(APPEND FREELDR_BASE_SOURCE
216 ${freeldr_base_asm}
217 bootmgr.c # This file is compiled with custom definitions
218 freeldr.c
219 ntldr/setupldr.c ## Strangely enough this file is needed in GCC builds
220 ## even if ${FREELDR_NTLDR_SOURCE} is not added,
221 ## otherwise we get linking errors with Rtl**Bitmap** APIs.
222 ## Do not happen on MSVC builds however...
223 lib/inffile/inffile.c
224 lib/rtl/libsupp.c)
225
226 if(ARCH STREQUAL "i386")
227 # Must be included together with disk/scsiport.c
228 list(APPEND FREELDR_BASE_SOURCE
229 ${CMAKE_CURRENT_BINARY_DIR}/freeldr_pe.def)
230 endif()
231
232 add_executable(freeldr_pe ${FREELDR_BASE_SOURCE})
233 add_executable(freeldr_pe_dbg EXCLUDE_FROM_ALL ${FREELDR_BASE_SOURCE})
234
235 set_property(TARGET freeldr_pe PROPERTY ENABLE_EXPORTS TRUE)
236 set_property(TARGET freeldr_pe_dbg PROPERTY ENABLE_EXPORTS TRUE)
237
238 if(NOT MSVC AND SEPARATE_DBG)
239 set_target_properties(freeldr_pe PROPERTIES LINKER_LANGUAGE LDR_PE_HELPER)
240 set_target_properties(freeldr_pe_dbg PROPERTIES LINKER_LANGUAGE LDR_PE_HELPER)
241 endif()
242
243 if(MSVC)
244 if(ARCH STREQUAL "arm")
245 add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER")
246 add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER")
247 else()
248 add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
249 add_linker_script(freeldr_pe freeldr_i386.msvc.lds)
250 add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
251 add_linker_script(freeldr_pe_dbg freeldr_i386.msvc.lds)
252 endif()
253 else()
254 add_target_link_flags(freeldr_pe "-Wl,--strip-all,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
255 add_linker_script(freeldr_pe freeldr_i386.lds)
256 add_target_link_flags(freeldr_pe_dbg "-Wl,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
257 add_linker_script(freeldr_pe_dbg freeldr_i386.lds)
258 endif()
259
260 set_image_base(freeldr_pe 0x10000)
261 set_subsystem(freeldr_pe native)
262 set_entrypoint(freeldr_pe RealEntryPoint)
263
264 set_image_base(freeldr_pe_dbg 0x10000)
265 set_subsystem(freeldr_pe_dbg native)
266 set_entrypoint(freeldr_pe_dbg RealEntryPoint)
267
268 if(ARCH STREQUAL "i386")
269 target_link_libraries(freeldr_pe mini_hal)
270 target_link_libraries(freeldr_pe_dbg mini_hal)
271 endif()
272
273 target_link_libraries(freeldr_pe freeldr_common cportlib cmlib rtl libcntpr)
274 target_link_libraries(freeldr_pe_dbg freeldr_common cportlib cmlib rtl libcntpr)
275
276 if(STACK_PROTECTOR)
277 target_link_libraries(freeldr_pe gcc_ssp)
278 target_link_libraries(freeldr_pe_dbg gcc_ssp)
279 elseif(RUNTIME_CHECKS)
280 target_link_libraries(freeldr_pe runtmchk)
281 target_link_libraries(freeldr_pe_dbg runtmchk)
282 add_target_link_flags(freeldr_pe "/MERGE:.rtc=.text")
283 endif()
284
285 add_dependencies(freeldr_pe asm)
286 add_dependencies(freeldr_pe_dbg asm)
287
288 if(NOT ARCH STREQUAL "arm")
289 concatenate_files(
290 ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys
291 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
292 ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
293 add_custom_target(freeldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys)
294 else()
295 add_custom_target(freeldr ALL DEPENDS freeldr_pe)
296 endif()
297
298 # Rename freeldr on livecd to setupldr.sys because isoboot.bin looks for setupldr.sys
299 add_cd_file(TARGET freeldr FILE ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)
300 add_cd_file(TARGET freeldr FILE ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys DESTINATION loader NO_CAB NOT_IN_HYBRIDCD FOR livecd hybridcd NAME_ON_CD setupldr.sys)
301
302 if(NOT ARCH STREQUAL "arm")
303 concatenate_files(
304 ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys
305 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
306 ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
307 add_custom_target(setupldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys)
308 else()
309 add_custom_target(setupldr ALL DEPENDS freeldr_pe)
310 endif()
311
312 add_cd_file(TARGET setupldr FILE ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)