xref: /reactos/subsystems/mvdm/asm16.cmake (revision cce399e7)
1## EXPERIMENTAL!!
2
3# We need to use almost the same tricks as the ones used for MSVC 'add_asm_files'
4# support because we are going to compile ASM files for a fixed target (16-bit x86)
5# that is different from the main target.
6
7if(NOT MSVC)
8###
9### For GCC
10###
11function(add_asm16_bin _target _binary_file _base_address)
12    set(_concatenated_asm_file ${CMAKE_CURRENT_BINARY_DIR}/${_target}.asm)
13    set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/${_target}.o)
14
15    # unset(_source_file_list)
16
17    get_defines(_directory_defines)
18    get_includes(_directory_includes)
19    get_directory_property(_defines COMPILE_DEFINITIONS)
20
21    # Build a list of all the defines needed.
22    foreach(_source_file ${ARGN})
23        get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
24        get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
25
26        # unset(_source_file_defines)
27
28        foreach(_define ${_defines_semicolon_list})
29            if(NOT ${_define} STREQUAL "NOTFOUND")
30                list(APPEND _source_file_defines -D${_define})
31            endif()
32        endforeach()
33
34        list(APPEND _source_file_list ${_source_file_full_path})
35    endforeach()
36
37    # We do not support 16-bit ASM linking so the only way to compile
38    # many ASM files is by concatenating them into a single one and
39    # compile the resulting file.
40    concatenate_files(${_concatenated_asm_file} ${_source_file_list})
41    set_source_files_properties(${_concatenated_asm_file} PROPERTIES GENERATED TRUE)
42
43    ##
44    ## All this part is the same as CreateBootSectorTarget
45    ##
46    add_custom_command(
47        OUTPUT ${_object_file}
48        COMMAND ${CMAKE_ASM_COMPILER} -x assembler-with-cpp -o ${_object_file} -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} -D__ASM__ -c ${_concatenated_asm_file}
49        DEPENDS ${_concatenated_asm_file})
50
51    add_custom_command(
52        OUTPUT ${_binary_file}
53        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
54        # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
55        DEPENDS ${_object_file} native-obj2bin)
56
57    add_custom_target(${_target} ALL DEPENDS ${_binary_file})
58    # set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${_target} SUFFIX ".bin")
59    set_target_properties(${_target} PROPERTIES BINARY_PATH ${_binary_file})
60    add_clean_target(${_target})
61endfunction()
62
63else()
64###
65### For MSVC
66###
67function(add_asm16_bin _target _binary_file _base_address)
68    set(_concatenated_asm_file ${CMAKE_CURRENT_BINARY_DIR}/${_target}.asm)
69    set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/${_target}.tmp)
70    set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/${_target}.obj)
71
72    # unset(_source_file_list)
73
74    get_defines(_directory_defines)
75    get_includes(_directory_includes)
76    get_directory_property(_defines COMPILE_DEFINITIONS)
77
78    # Build a list of all the defines needed.
79    foreach(_source_file ${ARGN})
80        get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
81        get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
82
83        # unset(_source_file_defines)
84
85        foreach(_define ${_defines_semicolon_list})
86            if(NOT ${_define} STREQUAL "NOTFOUND")
87                list(APPEND _source_file_defines -D${_define})
88            endif()
89        endforeach()
90
91        list(APPEND _source_file_list ${_source_file_full_path})
92    endforeach()
93
94    # We do not support 16-bit ASM linking so the only way to compile
95    # many ASM files is by concatenating them into a single one and
96    # compile the resulting file.
97    concatenate_files(${_concatenated_asm_file} ${_source_file_list})
98    set_source_files_properties(${_concatenated_asm_file} PROPERTIES GENERATED TRUE)
99
100    ##
101    ## All this part is the same as CreateBootSectorTarget
102    ##
103    if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
104        set(_no_std_includes_flag "-nostdinc")
105    else()
106        set(_no_std_includes_flag "/X")
107    endif()
108
109    add_custom_command(
110        OUTPUT ${_preprocessed_asm_file}
111        #COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_concatenated_asm_file} > ${_preprocessed_asm_file}
112        COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_concatenated_asm_file} > ${_preprocessed_asm_file}
113        DEPENDS ${_concatenated_asm_file})
114
115    set(_pp_asm16_compile_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file})
116
117    add_custom_command(
118        OUTPUT ${_object_file}
119        COMMAND ${_pp_asm16_compile_command}
120        DEPENDS ${_preprocessed_asm_file})
121
122    add_custom_command(
123        OUTPUT ${_binary_file}
124        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
125        DEPENDS ${_object_file} native-obj2bin)
126
127    add_custom_target(${_target} ALL DEPENDS ${_binary_file})
128    # set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${_target} SUFFIX ".bin")
129    set_target_properties(${_target} PROPERTIES BINARY_PATH ${_binary_file})
130    add_clean_target(${_target})
131endfunction()
132
133endif()
134