[FREELDR] Fix VSSolution configuring and build
[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(NOT MSVC)
9 ###
10 ### For GCC
11 ###
12 function(add_linker_script _target _linker_script_file)
13 get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
14 add_target_link_flags(${_target} "-Wl,-T,${_file_full_path}")
15
16 # Unfortunately LINK_DEPENDS is ignored in non-Makefile generators (for now...)
17 # See also http://www.cmake.org/pipermail/cmake/2010-May/037206.html
18 add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
19 endfunction()
20
21 else()
22 ###
23 ### For MSVC
24 ###
25 function(add_linker_script _target _linker_script_file)
26 get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
27 get_filename_component(_file_name ${_linker_script_file} NAME)
28 set(_generated_file_path_prefix "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_target}.dir/${_file_name}")
29
30 # Generate the ASM module containing sections specifications and layout.
31 set(_generated_file "${_generated_file_path_prefix}.S")
32 add_custom_command(
33 OUTPUT ${_generated_file}
34 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
35 DEPENDS ${_file_full_path})
36 set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "ASM" GENERATED TRUE)
37 add_asm_files(${_target}_freeldr_linker_file ${_generated_file})
38
39 # Generate the C module containing extra sections specifications and layout,
40 # as well as comment-type linker #pragma directives.
41 set(_generated_file "${_generated_file_path_prefix}.c")
42 add_custom_command(
43 OUTPUT ${_generated_file}
44 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
45 DEPENDS ${_file_full_path})
46 set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "C" GENERATED TRUE)
47 list(APPEND ${_target}_freeldr_linker_file ${_generated_file})
48
49 # Add both files to the sources of the target.
50 target_sources(${_target} PRIVATE "${${_target}_freeldr_linker_file}")
51
52 # Create the additional linker response file.
53 set(_generated_file "${_generated_file_path_prefix}.rsp")
54 if(USE_CLANG_CL)
55 set(_no_std_includes_flag "-nostdinc")
56 else()
57 set(_no_std_includes_flag "/X")
58 endif()
59 add_custom_command(
60 #OUTPUT ${_generated_file}
61 TARGET ${_target} PRE_LINK
62 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}" > "${_generated_file}"
63 DEPENDS ${_file_full_path}
64 VERBATIM)
65 set_source_files_properties(${_generated_file} PROPERTIES GENERATED TRUE)
66 add_target_link_flags(${_target} "@${_generated_file}")
67
68 # Unfortunately LINK_DEPENDS is ignored in non-Makefile generators (for now...)
69 # See also http://www.cmake.org/pipermail/cmake/2010-May/037206.html
70 add_target_property(${_target} LINK_DEPENDS ${_generated_file})
71 endfunction()
72
73 endif()
74
75
76 if(MSVC)
77 # We don't need hotpatching
78 replace_compile_flags("/hotpatch" " ")
79
80 # Explicitly use string pooling
81 add_compile_flags("/GF")
82 endif()
83
84 spec2def(freeldr_pe.exe freeldr.spec)
85
86 if(ARCH STREQUAL "i386")
87 CreateBootSectorTarget(frldr16
88 ${CMAKE_CURRENT_SOURCE_DIR}/arch/realmode/i386.S
89 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
90 F800)
91 elseif(ARCH STREQUAL "amd64")
92 CreateBootSectorTarget(frldr16
93 ${CMAKE_CURRENT_SOURCE_DIR}/arch/realmode/amd64.S
94 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
95 F800)
96 endif()
97
98 include_directories(BEFORE include)
99 include_directories(${REACTOS_SOURCE_DIR}/ntoskrnl/include)
100 include_directories(${REACTOS_SOURCE_DIR}/sdk/lib/cmlib)
101 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/libs)
102 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/elf)
103
104 add_definitions(-D_NTHAL_ -D_BLDR_ -D_NTSYSTEM_)
105
106 list(APPEND FREELDR_BOOTLIB_SOURCE
107 lib/debug.c
108 lib/peloader.c
109 lib/comm/rs232.c
110 ## add KD support
111 lib/fs/btrfs.c
112 lib/fs/ext2.c
113 lib/fs/fat.c
114 lib/fs/fs.c
115 lib/fs/iso.c
116 lib/fs/ntfs.c
117 lib/inifile/ini_init.c
118 lib/inifile/inifile.c
119 lib/inifile/parse.c
120 lib/mm/meminit.c
121 lib/mm/mm.c
122 lib/mm/heap.c)
123
124 list(APPEND FREELDR_NTLDR_SOURCE
125 ntldr/conversion.c
126 ntldr/registry.c
127 ntldr/winldr.c
128 ntldr/wlmemory.c
129 ntldr/wlregistry.c)
130
131 list(APPEND FREELDR_ARC_SOURCE
132 arcname.c
133 machine.c
134 arch/archwsup.c
135 cache/blocklist.c
136 cache/cache.c
137 disk/disk.c
138 disk/partition.c
139 disk/ramdisk.c)
140 #disk/scsiport.c
141
142 list(APPEND FREELDR_BOOTMGR_SOURCE
143 include/freeldr.h
144 cmdline.c
145 custom.c
146 linuxboot.c
147 miscboot.c
148 options.c
149 oslist.c
150 version.c
151 ui/directui.c
152 ui/gui.c
153 ui/minitui.c
154 ui/noui.c
155 ui/tui.c
156 ui/tuimenu.c
157 ui/ui.c
158 video/fade.c
159 video/palette.c
160 video/video.c)
161
162 list(APPEND FREELDR_BASE_ASM_SOURCE)
163
164 if(ARCH STREQUAL "i386")
165 list(APPEND FREELDR_BASE_ASM_SOURCE
166 arch/i386/multiboot.S)
167
168 list(APPEND FREELDR_COMMON_ASM_SOURCE
169 arch/i386/drvmap.S
170 arch/i386/entry.S
171 arch/i386/int386.S
172 arch/i386/pnpbios.S
173 arch/i386/i386trap.S
174 arch/i386/linux.S)
175
176 list(APPEND FREELDR_NTLDR_SOURCE
177 ntldr/arch/i386/winldr.c
178 ntldr/headless.c)
179
180 list(APPEND FREELDR_ARC_SOURCE
181 lib/fs/pxe.c
182 arch/i386/halstub.c
183 arch/i386/ntoskrnl.c
184 arch/i386/archmach.c
185 arch/i386/drivemap.c
186 arch/i386/hardware.c
187 arch/i386/hwacpi.c
188 arch/i386/hwapm.c
189 arch/i386/hwdisk.c
190 arch/i386/hwpci.c
191 arch/i386/i386bug.c
192 arch/i386/i386idt.c
193 arch/i386/i386rtl.c
194 arch/i386/i386vid.c
195 arch/i386/machpc.c
196 arch/i386/pccons.c
197 arch/i386/pcdisk.c
198 arch/i386/pcmem.c
199 arch/i386/pcrtc.c
200 arch/i386/pcvideo.c
201 arch/i386/machxbox.c
202 arch/i386/xboxcons.c
203 arch/i386/xboxdisk.c
204 arch/i386/xboxfont.c
205 arch/i386/xboxi2c.c
206 arch/i386/xboxmem.c
207 arch/i386/xboxrtc.c
208 arch/i386/xboxvideo.c
209 disk/scsiport.c)
210
211 elseif(ARCH STREQUAL "amd64")
212 list(APPEND FREELDR_COMMON_ASM_SOURCE
213 arch/amd64/entry.S
214 arch/amd64/int386.S
215 arch/amd64/pnpbios.S)
216
217 list(APPEND FREELDR_NTLDR_SOURCE
218 ntldr/arch/amd64/winldr.c)
219
220 list(APPEND FREELDR_ARC_SOURCE
221 lib/fs/pxe.c
222 arch/i386/ntoskrnl.c
223 arch/i386/drivemap.c
224 arch/i386/hardware.c
225 arch/i386/hwacpi.c
226 arch/i386/hwapm.c
227 arch/i386/hwdisk.c
228 arch/i386/hwpci.c
229 arch/i386/i386bug.c
230 arch/i386/i386rtl.c
231 arch/i386/i386vid.c
232 arch/i386/machpc.c
233 arch/i386/pccons.c
234 arch/i386/pcdisk.c
235 arch/i386/pcmem.c
236 arch/i386/pcrtc.c
237 arch/i386/pcvideo.c)
238
239 elseif(ARCH STREQUAL "arm")
240 list(APPEND FREELDR_COMMON_ASM_SOURCE
241 arch/arm/boot.S)
242
243 list(APPEND FREELDR_NTLDR_SOURCE
244 ntldr/arch/arm/winldr.c)
245
246 list(APPEND FREELDR_ARC_SOURCE
247 arch/arm/entry.c
248 arch/arm/macharm.c)
249 else()
250 #TBD
251 endif()
252
253 add_asm_files(freeldr_common_asm ${FREELDR_COMMON_ASM_SOURCE})
254
255 add_library(freeldr_common
256 ${freeldr_common_asm}
257 ${FREELDR_BOOTLIB_SOURCE}
258 ${FREELDR_ARC_SOURCE}
259 ${FREELDR_NTLDR_SOURCE}
260 ${FREELDR_BOOTMGR_SOURCE})
261
262 if(USE_CLANG_CL)
263 # We need to reduce the binary size
264 add_target_compile_flags(freeldr_common "/Os")
265 endif()
266
267 set(PCH_SOURCE
268 ${FREELDR_BOOTLIB_SOURCE}
269 ${FREELDR_ARC_SOURCE}
270 ${FREELDR_NTLDR_SOURCE}
271 ${FREELDR_BOOTMGR_SOURCE})
272
273 add_pch(freeldr_common include/freeldr.h PCH_SOURCE)
274 add_dependencies(freeldr_common bugcodes asm xdk)
275
276 ## GCC builds need this extra thing for some reason...
277 if(ARCH STREQUAL "i386" AND NOT MSVC)
278 target_link_libraries(freeldr_common mini_hal)
279 endif()
280
281 add_asm_files(freeldr_base_asm ${FREELDR_BASE_ASM_SOURCE})
282
283 list(APPEND FREELDR_BASE_SOURCE
284 ${freeldr_base_asm}
285 bootmgr.c # This file is compiled with custom definitions
286 freeldr.c
287 ntldr/setupldr.c ## Strangely enough this file is needed in GCC builds
288 ## even if ${FREELDR_NTLDR_SOURCE} is not added,
289 ## otherwise we get linking errors with Rtl**Bitmap** APIs.
290 ## Do not happen on MSVC builds however...
291 lib/inffile/inffile.c
292 lib/rtl/libsupp.c)
293
294 if(ARCH STREQUAL "i386")
295 # Must be included together with disk/scsiport.c
296 list(APPEND FREELDR_BASE_SOURCE
297 ${CMAKE_CURRENT_BINARY_DIR}/freeldr_pe.def)
298 endif()
299
300 add_executable(freeldr_pe ${FREELDR_BASE_SOURCE})
301 add_executable(freeldr_pe_dbg EXCLUDE_FROM_ALL ${FREELDR_BASE_SOURCE})
302
303 set_property(TARGET freeldr_pe PROPERTY ENABLE_EXPORTS TRUE)
304 set_property(TARGET freeldr_pe_dbg PROPERTY ENABLE_EXPORTS TRUE)
305
306 if(NOT MSVC AND SEPARATE_DBG)
307 set_target_properties(freeldr_pe PROPERTIES LINKER_LANGUAGE LDR_PE_HELPER)
308 set_target_properties(freeldr_pe_dbg PROPERTIES LINKER_LANGUAGE LDR_PE_HELPER)
309 endif()
310
311 if(MSVC)
312 if(ARCH STREQUAL "arm")
313 add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER")
314 add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER")
315 else()
316 add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
317 add_linker_script(freeldr_pe freeldr_i386.msvc.lds)
318 add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
319 add_linker_script(freeldr_pe_dbg freeldr_i386.msvc.lds)
320 endif()
321 else()
322 add_target_link_flags(freeldr_pe "-Wl,--strip-all,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
323 add_linker_script(freeldr_pe freeldr_i386.lds)
324 add_target_link_flags(freeldr_pe_dbg "-Wl,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
325 add_linker_script(freeldr_pe_dbg freeldr_i386.lds)
326 endif()
327
328 set_image_base(freeldr_pe 0x10000)
329 set_subsystem(freeldr_pe native)
330 set_entrypoint(freeldr_pe RealEntryPoint)
331
332 set_image_base(freeldr_pe_dbg 0x10000)
333 set_subsystem(freeldr_pe_dbg native)
334 set_entrypoint(freeldr_pe_dbg RealEntryPoint)
335
336 if(ARCH STREQUAL "i386")
337 target_link_libraries(freeldr_pe mini_hal)
338 target_link_libraries(freeldr_pe_dbg mini_hal)
339 endif()
340
341 target_link_libraries(freeldr_pe freeldr_common cportlib cmlib rtl libcntpr)
342 target_link_libraries(freeldr_pe_dbg freeldr_common cportlib cmlib rtl libcntpr)
343
344 if(STACK_PROTECTOR)
345 target_link_libraries(freeldr_pe gcc_ssp)
346 target_link_libraries(freeldr_pe_dbg gcc_ssp)
347 elseif(RUNTIME_CHECKS)
348 target_link_libraries(freeldr_pe runtmchk)
349 target_link_libraries(freeldr_pe_dbg runtmchk)
350 add_target_link_flags(freeldr_pe "/MERGE:.rtc=.text")
351 endif()
352
353 add_dependencies(freeldr_pe asm)
354 add_dependencies(freeldr_pe_dbg asm)
355
356 if(NOT ARCH STREQUAL "arm")
357 concatenate_files(
358 ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys
359 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
360 ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
361 add_custom_target(freeldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys)
362 else()
363 add_custom_target(freeldr ALL DEPENDS freeldr_pe)
364 endif()
365
366 # Rename freeldr on livecd to setupldr.sys because isoboot.bin looks for setupldr.sys
367 add_cd_file(TARGET freeldr FILE ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)
368 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)
369
370 if(NOT ARCH STREQUAL "arm")
371 concatenate_files(
372 ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys
373 ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
374 ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
375 add_custom_target(setupldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys)
376 else()
377 add_custom_target(setupldr ALL DEPENDS freeldr_pe)
378 endif()
379
380 add_cd_file(TARGET setupldr FILE ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)