@@ -8,6 +8,71 @@ function(clr_unknown_arch)
88 endif ()
99endfunction ()
1010
11+ # C to MASM include file translator
12+ # This is replacement for the deprecated h2inc tool that used to be part of VS.
13+ function (h2inc filename output )
14+ file (STRINGS ${filename} lines )
15+ get_filename_component (path "${filename} " DIRECTORY )
16+ file (RELATIVE_PATH relative_filename "${CLR_REPO_ROOT_DIR} " "${filename} " )
17+
18+ file (APPEND "${output} " "// File start: ${relative_filename} \n " )
19+
20+ # Use of NEWLINE_CONSUME is needed for lines with trailing backslash
21+ file (STRINGS ${filename} contents NEWLINE_CONSUME )
22+ string (REGEX REPLACE "\\\\\n " "\\\\\\\\ \n " contents "${contents} " )
23+ string (REGEX REPLACE "\n " ";" lines "${contents} " )
24+
25+ foreach (line IN LISTS lines)
26+ string (REGEX REPLACE "\\\\\\\\ " "\\\\ " line "${line} " )
27+
28+ if (line MATCHES "^ *# pragma" )
29+ # Ignore pragmas
30+ continue ()
31+ endif ()
32+
33+ if (line MATCHES "^ *# *include *\" (.*)\" " )
34+ # Expand includes.
35+ h2inc ("${path} /${CMAKE_MATCH_1} " "${output} " )
36+ continue ()
37+ endif ()
38+
39+ if (line MATCHES "^ *#define +([0-9A-Za-z_()]+) *(.*)" )
40+ # Augment #defines with their MASM equivalent
41+ set (name "${CMAKE_MATCH_1} " )
42+ set (value "${CMAKE_MATCH_2} " )
43+
44+ # Note that we do not handle multiline constants
45+
46+ # Strip comments from value
47+ string (REGEX REPLACE "//.*" "" value "${value} " )
48+ string (REGEX REPLACE "/\\ *.*\\ */" "" value "${value} " )
49+
50+ # Strip whitespaces from value
51+ string (REPLACE " +$" "" value "${value} " )
52+
53+ # ignore #defines with arguments
54+ if (NOT "${name} " MATCHES "\\ (" )
55+ set (HEX_NUMBER_PATTERN "0x([0-9A-Fa-f]+)" )
56+ set (DECIMAL_NUMBER_PATTERN "(-?[0-9]+)" )
57+
58+ if ("${value} " MATCHES "${HEX_NUMBER_PATTERN} " )
59+ string (REGEX REPLACE "${HEX_NUMBER_PATTERN} " "0\\ 1h" value "${value} " ) # Convert hex constants
60+ file (APPEND "${output} " "${name} EQU ${value} \n " )
61+ elseif ("${value} " MATCHES "${DECIMAL_NUMBER_PATTERN} " AND (NOT "${value} " MATCHES "[G-Zg-z]+" OR "${value} " MATCHES "\\ (" ))
62+ string (REGEX REPLACE "${DECIMAL_NUMBER_PATTERN} " "\\ 1t" value "${value} " ) # Convert dec constants
63+ file (APPEND "${output} " "${name} EQU ${value} \n " )
64+ else ()
65+ file (APPEND "${output} " "${name} TEXTEQU <${value} >\n " )
66+ endif ()
67+ endif ()
68+ endif ()
69+
70+ file (APPEND "${output} " "${line} \n " )
71+ endforeach ()
72+
73+ file (APPEND "${output} " "// File end: ${relative_filename} \n " )
74+ endfunction ()
75+
1176# Build a list of compiler definitions by putting -D in front of each define.
1277function (get_compile_definitions DefinitionName )
1378 # Get the current list of definitions
0 commit comments