Skip to content

Commit 091d306

Browse files
committed
Convert h2inc to a cmake function
1 parent 23de817 commit 091d306

4 files changed

Lines changed: 67 additions & 135 deletions

File tree

eng/native/functions.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,71 @@ function(clr_unknown_arch)
88
endif()
99
endfunction()
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.
1277
function(get_compile_definitions DefinitionName)
1378
# Get the current list of definitions

src/coreclr/vm/h2inc.pl

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/coreclr/vm/h2inc.ps1

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/coreclr/vm/wks/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ if (CLR_CMAKE_TARGET_WIN32)
3434
endif (CLR_CMAKE_HOST_ARCH_I386)
3535

3636
# Convert AsmConstants.h into AsmConstants.inc
37-
find_program(POWERSHELL powershell)
38-
if (POWERSHELL STREQUAL "POWERSHELL-NOTFOUND")
39-
message(FATAL_ERROR "POWERSHELL not found")
40-
endif()
37+
h2inc("${VM_DIR}/${ARCH_SOURCES_DIR}/asmconstants.h" "${CMAKE_CURRENT_BINARY_DIR}/AsmConstants.tmp")
4138

4239
# Get the current list of definitions
4340
get_compile_definitions(DEFINITIONS)
41+
4442
add_custom_command(
4543
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/AsmConstants.inc"
4644
DEPENDS ${VM_DIR}/${ARCH_SOURCES_DIR}/asmconstants.h
47-
COMMAND ${POWERSHELL} -NoProfile -ExecutionPolicy Bypass -NonInteractive \"& \"\"${VM_DIR}/h2inc.ps1\"\"\" \"\"\"${VM_DIR}/${ARCH_SOURCES_DIR}/asmconstants.h\"\"\" >"${CMAKE_CURRENT_BINARY_DIR}/AsmConstants.tmp"
4845
COMMAND ${CMAKE_CXX_COMPILER} ${DEFINITIONS} /EP "${CMAKE_CURRENT_BINARY_DIR}/AsmConstants.tmp" >"${CMAKE_CURRENT_BINARY_DIR}/AsmConstants.inc"
4946
)
5047

0 commit comments

Comments
 (0)