[FREELDR] Re-integrate the ASM files (and corresponding C code) in MSVC builds, that...
[reactos.git] / boot / freeldr / freeldr / CMakeLists.txt
index c4ca5b4..14723b6 100644 (file)
@@ -5,9 +5,80 @@ if(SEPARATE_DBG)
     set(CMAKE_LDR_PE_HELPER_STANDARD_LIBRARIES "-lgcc" CACHE STRING "Standard C Libraries")
 endif()
 
+if(NOT MSVC)
+###
+### For GCC
+###
+function(add_linker_script _target _linker_script_file)
+    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
+    add_target_link_flags(${_target} "-Wl,-T,${_file_full_path}")
+
+    # Unfortunately LINK_DEPENDS is ignored in non-Makefile generators (for now...)
+    # See also http://www.cmake.org/pipermail/cmake/2010-May/037206.html
+    add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
+endfunction()
+
+else()
+###
+### For MSVC
+###
+function(add_linker_script _target _linker_script_file)
+    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
+    get_filename_component(_file_name ${_linker_script_file} NAME)
+    set(_generated_file_path_prefix "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_target}.dir/${_file_name}")
+
+    # Generate the ASM module containing sections specifications and layout.
+    set(_generated_file "${_generated_file_path_prefix}.S")
+    add_custom_command(
+        OUTPUT ${_generated_file}
+        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
+        DEPENDS ${_file_full_path})
+    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "ASM" GENERATED TRUE)
+    add_asm_files(freeldr_linker_file ${_generated_file})
+
+    # Generate the C module containing extra sections specifications and layout,
+    # as well as comment-type linker #pragma directives.
+    set(_generated_file "${_generated_file_path_prefix}.c")
+    add_custom_command(
+        OUTPUT ${_generated_file}
+        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
+        DEPENDS ${_file_full_path})
+    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "C" GENERATED TRUE)
+    list(APPEND freeldr_linker_file ${_generated_file})
+
+    # Add both files to the sources of the target.
+    target_sources(${_target} PRIVATE ${freeldr_linker_file})
+
+    # Create the additional linker response file.
+    set(_generated_file "${_generated_file_path_prefix}.rsp")
+    if(USE_CLANG_CL)
+        set(_no_std_includes_flag "-nostdinc")
+    else()
+        set(_no_std_includes_flag "/X")
+    endif()
+    add_custom_command(
+        #OUTPUT ${_generated_file}
+        TARGET ${_target} PRE_LINK
+        COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}" > "${_generated_file}"
+        DEPENDS ${_file_full_path}
+        VERBATIM)
+    set_source_files_properties(${_generated_file} PROPERTIES GENERATED TRUE)
+    add_target_link_flags(${_target} "@${_generated_file}")
+
+    # Unfortunately LINK_DEPENDS is ignored in non-Makefile generators (for now...)
+    # See also http://www.cmake.org/pipermail/cmake/2010-May/037206.html
+    add_target_property(${_target} LINK_DEPENDS ${_generated_file})
+endfunction()
+
+endif()
+
+
 if(MSVC)
-    # We don't need it here
+    # We don't need hotpatching
     replace_compile_flags("/hotpatch" " ")
+
+    # Explicitly use string pooling
+    add_compile_flags("/GF")
 endif()
 
 spec2def(freeldr_pe.exe freeldr.spec)
@@ -32,13 +103,12 @@ include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/elf)
 
 add_definitions(-D_NTHAL_ -D_BLDR_ -D_NTSYSTEM_)
 
-
-list(APPEND FREELDR_BOOTLIB_COMMON_SOURCE
+list(APPEND FREELDR_BOOTLIB_SOURCE
     lib/debug.c
     lib/peloader.c
-
     lib/comm/rs232.c
     ## add KD support
+    lib/fs/btrfs.c
     lib/fs/ext2.c
     lib/fs/fat.c
     lib/fs/fs.c
@@ -49,18 +119,16 @@ list(APPEND FREELDR_BOOTLIB_COMMON_SOURCE
     lib/inifile/parse.c
     lib/mm/meminit.c
     lib/mm/mm.c
-    lib/mm/heap.c
-    )
+    lib/mm/heap.c)
 
-list(APPEND FREELDR_NTLDR_COMMON_SOURCE
+list(APPEND FREELDR_NTLDR_SOURCE
     ntldr/conversion.c
     ntldr/registry.c
     ntldr/winldr.c
     ntldr/wlmemory.c
-    ntldr/wlregistry.c
-    )
+    ntldr/wlregistry.c)
 
-list(APPEND FREELDR_ARC_COMMON_SOURCE
+list(APPEND FREELDR_ARC_SOURCE
     arcname.c
     machine.c
     arch/archwsup.c
@@ -68,11 +136,10 @@ list(APPEND FREELDR_ARC_COMMON_SOURCE
     cache/cache.c
     disk/disk.c
     disk/partition.c
-    disk/ramdisk.c
+    disk/ramdisk.c)
     #disk/scsiport.c
-    )
 
-list(APPEND FREELDR_COMMON_SOURCE
+list(APPEND FREELDR_BOOTMGR_SOURCE
     include/freeldr.h
     cmdline.c
     custom.c
@@ -90,31 +157,30 @@ list(APPEND FREELDR_COMMON_SOURCE
     ui/ui.c
     video/fade.c
     video/palette.c
-    video/video.c
-    )
+    video/video.c)
+
+list(APPEND FREELDR_BASE_ASM_SOURCE)
 
 if(ARCH STREQUAL "i386")
+    list(APPEND FREELDR_BASE_ASM_SOURCE
+        arch/i386/multiboot.S)
+
     list(APPEND FREELDR_COMMON_ASM_SOURCE
+        arch/i386/drvmap.S
         arch/i386/entry.S
         arch/i386/int386.S
         arch/i386/pnpbios.S
         arch/i386/i386trap.S
-        arch/i386/linux.S
-        arch/i386/mb.S
-        )
+        arch/i386/linux.S)
 
-    list(APPEND FREELDR_NTLDR_COMMON_SOURCE
+    list(APPEND FREELDR_NTLDR_SOURCE
         ntldr/arch/i386/winldr.c
-        ntldr/headless.c
-        )
+        ntldr/headless.c)
 
-    ## list(APPEND FREELDR_COMMON_SOURCE
-    list(APPEND FREELDR_ARC_COMMON_SOURCE
+    list(APPEND FREELDR_ARC_SOURCE
         lib/fs/pxe.c
-
         arch/i386/halstub.c
         arch/i386/ntoskrnl.c
-
         arch/i386/archmach.c
         arch/i386/drivemap.c
         arch/i386/hardware.c
@@ -141,25 +207,19 @@ if(ARCH STREQUAL "i386")
         arch/i386/xboxrtc.c
         arch/i386/xboxvideo.c
         disk/scsiport.c)
-    if(NOT MSVC)
-        list(APPEND FREELDR_COMMON_ASM_SOURCE arch/i386/drvmap.S)
-    endif()
+
 elseif(ARCH STREQUAL "amd64")
     list(APPEND FREELDR_COMMON_ASM_SOURCE
         arch/amd64/entry.S
         arch/amd64/int386.S
         arch/amd64/pnpbios.S)
 
-    list(APPEND FREELDR_NTLDR_COMMON_SOURCE
-        ntldr/arch/amd64/winldr.c
-        )
+    list(APPEND FREELDR_NTLDR_SOURCE
+        ntldr/arch/amd64/winldr.c)
 
-    ## list(APPEND FREELDR_COMMON_SOURCE
-    list(APPEND FREELDR_ARC_COMMON_SOURCE
+    list(APPEND FREELDR_ARC_SOURCE
         lib/fs/pxe.c
-
         arch/i386/ntoskrnl.c
-
         arch/i386/drivemap.c
         arch/i386/hardware.c
         arch/i386/hwacpi.c
@@ -175,16 +235,15 @@ elseif(ARCH STREQUAL "amd64")
         arch/i386/pcmem.c
         arch/i386/pcrtc.c
         arch/i386/pcvideo.c)
+
 elseif(ARCH STREQUAL "arm")
     list(APPEND FREELDR_COMMON_ASM_SOURCE
         arch/arm/boot.S)
 
-    list(APPEND FREELDR_NTLDR_COMMON_SOURCE
-        ntldr/arch/arm/winldr.c
-        )
+    list(APPEND FREELDR_NTLDR_SOURCE
+        ntldr/arch/arm/winldr.c)
 
-    ## list(APPEND FREELDR_COMMON_SOURCE
-    list(APPEND FREELDR_ARC_COMMON_SOURCE
+    list(APPEND FREELDR_ARC_SOURCE
         arch/arm/entry.c
         arch/arm/macharm.c)
 else()
@@ -192,22 +251,41 @@ else()
 endif()
 
 add_asm_files(freeldr_common_asm ${FREELDR_COMMON_ASM_SOURCE})
-add_library(freeldr_common ${FREELDR_BOOTLIB_COMMON_SOURCE} ${FREELDR_ARC_COMMON_SOURCE}
-            ${FREELDR_NTLDR_COMMON_SOURCE}
-            ${FREELDR_COMMON_SOURCE} ${freeldr_common_asm})
-add_pch(freeldr_common include/freeldr.h FREELDR_COMMON_SOURCE)
+
+add_library(freeldr_common
+    ${freeldr_common_asm}
+    ${FREELDR_BOOTLIB_SOURCE}
+    ${FREELDR_ARC_SOURCE}
+    ${FREELDR_NTLDR_SOURCE}
+    ${FREELDR_BOOTMGR_SOURCE})
+
+if(USE_CLANG_CL)
+    # We need to reduce the binary size
+    add_target_compile_flags(freeldr_common "/Os")
+endif()
+
+set(PCH_SOURCE
+    ${FREELDR_BOOTLIB_SOURCE}
+    ${FREELDR_ARC_SOURCE}
+    ${FREELDR_NTLDR_SOURCE}
+    ${FREELDR_BOOTMGR_SOURCE})
+
+add_pch(freeldr_common include/freeldr.h PCH_SOURCE)
 add_dependencies(freeldr_common bugcodes asm xdk)
 
+## GCC builds need this extra thing for some reason...
 if(ARCH STREQUAL "i386" AND NOT MSVC)
-    list(APPEND FREELDR_BASE_SOURCE arch/i386/multiboot.S)
     target_link_libraries(freeldr_common mini_hal)
 endif()
 
+add_asm_files(freeldr_base_asm ${FREELDR_BASE_ASM_SOURCE})
+
 list(APPEND FREELDR_BASE_SOURCE
+    ${freeldr_base_asm}
     bootmgr.c # This file is compiled with custom definitions
     freeldr.c
     ntldr/setupldr.c ## Strangely enough this file is needed in GCC builds
-                     ## even if ${FREELDR_NTLDR_COMMON_SOURCE} is not added,
+                     ## even if ${FREELDR_NTLDR_SOURCE} is not added,
                      ## otherwise we get linking errors with Rtl**Bitmap** APIs.
                      ## Do not happen on MSVC builds however...
     lib/inffile/inffile.c
@@ -232,12 +310,16 @@ if(MSVC)
         add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER")
         add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER")
     else()
-        add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER /FIXED /ALIGN:0x400 /SECTION:.text,ERW /SECTION:.data,RW /MERGE:.text16=.text /MERGE:.data=.text /MERGE:.rdata=.text /MERGE:.bss=.text")
-        add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER /FIXED /ALIGN:0x400 /SECTION:.text,ERW /SECTION:.data,RW /MERGE:.text16=.text /MERGE:.data=.text /MERGE:.rdata=.text /MERGE:.bss=.text")
+        add_target_link_flags(freeldr_pe "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
+        add_linker_script(freeldr_pe freeldr_i386.msvc.lds)
+        add_target_link_flags(freeldr_pe_dbg "/ignore:4078 /ignore:4254 /DRIVER /FIXED /FILEALIGN:0x200 /ALIGN:0x200")
+        add_linker_script(freeldr_pe_dbg freeldr_i386.msvc.lds)
     endif()
 else()
-    add_target_link_flags(freeldr_pe "-Wl,--strip-all,--exclude-all-symbols,--file-alignment,0x1000,-T,${CMAKE_CURRENT_SOURCE_DIR}/freeldr_i386.lds")
-    add_target_link_flags(freeldr_pe_dbg "-Wl,--exclude-all-symbols,--file-alignment,0x1000,-T,${CMAKE_CURRENT_SOURCE_DIR}/freeldr_i386.lds")
+    add_target_link_flags(freeldr_pe "-Wl,--strip-all,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
+    add_linker_script(freeldr_pe freeldr_i386.lds)
+    add_target_link_flags(freeldr_pe_dbg "-Wl,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200")
+    add_linker_script(freeldr_pe_dbg freeldr_i386.lds)
 endif()
 
 set_image_base(freeldr_pe 0x10000)
@@ -268,20 +350,17 @@ endif()
 add_dependencies(freeldr_pe asm)
 add_dependencies(freeldr_pe_dbg asm)
 
-# Retrieve the full path to the generated file of the 'freeldr_pe' target
-get_target_property(_freeldr_pe_output_file freeldr_pe LOCATION)
-
 if(NOT ARCH STREQUAL "arm")
     concatenate_files(
         ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys
         ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
-        ${_freeldr_pe_output_file})
+        ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
     add_custom_target(freeldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys)
 else()
-    add_custom_target(freeldr ALL DEPENDS ${_freeldr_pe_output_file})
+    add_custom_target(freeldr ALL DEPENDS freeldr_pe)
 endif()
 
-# rename freeldr on livecd to setupldr.sys because isoboot.bin looks for setupldr.sys
+# Rename freeldr on livecd to setupldr.sys because isoboot.bin looks for setupldr.sys
 add_cd_file(TARGET freeldr FILE ${CMAKE_CURRENT_BINARY_DIR}/freeldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)
 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)
 
@@ -289,10 +368,10 @@ if(NOT ARCH STREQUAL "arm")
     concatenate_files(
         ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys
         ${CMAKE_CURRENT_BINARY_DIR}/frldr16.bin
-        ${_freeldr_pe_output_file})
+        ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:freeldr_pe>)
     add_custom_target(setupldr ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys)
 else()
-    add_custom_target(setupldr ALL DEPENDS ${_freeldr_pe_output_file})
+    add_custom_target(setupldr ALL DEPENDS freeldr_pe)
 endif()
 
 add_cd_file(TARGET setupldr FILE ${CMAKE_CURRENT_BINARY_DIR}/setupldr.sys DESTINATION loader NO_CAB FOR bootcd regtest)