Skip to content

Commit 9479ba5

Browse files
authored
Build onnxruntime.dll as arm64x (microsoft#18633)
Build onnxruntime.dll as arm64x Added a .cmake file to generate a link repro of the onnxruntime.dll during arm64 build. This provides us a directory containing all the arm64 objs, def file and libs to link to when it is time to building arm64x onnxruntime.dll during the arm64ec build by passing the /machine:arm64x flag to the linker along with the arm64 artifacts. If other dlls wanted to be built as x, setting the ARM64X_TARGETS variable in the toplevel cmakelists.txt to include these other targets is all that will be needed. Added build_arm64x.bat as a wrapper for the multiple (rm64, then arm64ec) cmake calls needed to build as arm64x. AB#22533
1 parent 7762f3f commit 9479ba5

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,4 @@ Package.pins
195195
Package.resolved
196196
.build/
197197
.swiftpm/
198+
repros/

build_arm64x.bat

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:: Copyright (c) Microsoft Corporation. All rights reserved.
2+
:: Licensed under the MIT License.
3+
4+
@echo off
5+
6+
setlocal
7+
set PATH=C:\Program Files\Git\usr\bin;%PATH%
8+
set LINK_REPRO_NAME=/mylink.rsp
9+
10+
rem Requires a Python install to be available in your PATH
11+
python "%~dp0\tools\ci_build\build.py" --arm64 --buildasx --build_dir "%~dp0\build\arm64-x" %*
12+
python "%~dp0\tools\ci_build\build.py" --arm64ec --buildasx --build_dir "%~dp0\build\arm64ec-x" %*

cmake/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1776,3 +1776,8 @@ if(TARGET onnxruntime)
17761776
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
17771777
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
17781778
endif()
1779+
1780+
if(DEFINED BUILD_AS_ARM64X)
1781+
set(ARM64X_TARGETS onnxruntime)
1782+
include("${CMAKE_SOURCE_DIR}/arm64x.cmake")
1783+
endif()

cmake/arm64x.cmake

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
set(arm64ReproDir "${CMAKE_SOURCE_DIR}/repros")
2+
3+
if("${BUILD_AS_ARM64X}" STREQUAL "ARM64")
4+
foreach (n ${ARM64X_TARGETS})
5+
add_custom_target(mkdirs_${n} ALL COMMAND cmd /c (if exist \"${arm64ReproDir}/${n}_temp/\" rmdir /s /q \"${arm64ReproDir}/${n}_temp\") && mkdir \"${arm64ReproDir}/${n}_temp\" )
6+
add_dependencies(${n} mkdirs_${n})
7+
target_link_options(${n} PRIVATE "/LINKREPRO:${arm64ReproDir}/${n}_temp")
8+
add_custom_target(${n}_checkRepro ALL COMMAND cmd /c if exist \"${n}_temp/*.obj\" if exist \"${n}\" rmdir /s /q \"${n}\" 2>nul && if not exist \"${n}\" ren \"${n}_temp\" \"${n}\" DEPENDS ${n}
9+
WORKING_DIRECTORY ${arm64ReproDir})
10+
endforeach()
11+
12+
13+
elseif("${BUILD_AS_ARM64X}" STREQUAL "ARM64EC")
14+
foreach (n ${ARM64X_TARGETS})
15+
set(ARM64_LIBS)
16+
set(ARM64_OBJS)
17+
set(ARM64_DEF)
18+
19+
file(GLOB ARM64_OBJS "${arm64ReproDir}/${n}/*.obj")
20+
file(GLOB ARM64_DEF "${arm64ReproDir}/${n}/*.def")
21+
file(GLOB ARM64_LIBS "${arm64ReproDir}/${n}/*.LIB")
22+
23+
if(NOT "${ARM64_DEF}" STREQUAL "")
24+
set(ARM64_DEF "/defArm64Native:${ARM64_DEF}")
25+
endif()
26+
target_sources(${n} PRIVATE ${ARM64_OBJS})
27+
target_link_options(${n} PRIVATE /machine:arm64x "${ARM64_DEF}")
28+
29+
if(NOT "${ARM64_LIBS}" STREQUAL "")
30+
target_link_libraries(${n} PUBLIC ${ARM64_LIBS})
31+
endif()
32+
endforeach()
33+
endif()

tools/ci_build/build.py

+10
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ def convert_arg_line_to_args(self, arg_line):
346346
help="[cross-compiling] Create ARM64EC makefiles. Requires --update and no existing cache "
347347
"CMake setup. Delete CMakeCache.txt if needed",
348348
)
349+
parser.add_argument(
350+
"--buildasx",
351+
action="store_true",
352+
help="[cross-compiling] Create ARM64X Binary.",
353+
)
349354
parser.add_argument("--msvc_toolset", help="MSVC toolset to use. e.g. 14.11")
350355
parser.add_argument("--windows_sdk_version", help="Windows SDK version to use. e.g. 10.0.19041.0")
351356
parser.add_argument("--android", action="store_true", help="Build for Android")
@@ -2517,8 +2522,12 @@ def main():
25172522
cmake_extra_args = ["-A", "ARM"]
25182523
elif args.arm64:
25192524
cmake_extra_args = ["-A", "ARM64"]
2525+
if args.buildasx:
2526+
cmake_extra_args += ["-D", "BUILD_AS_ARM64X=ARM64"]
25202527
elif args.arm64ec:
25212528
cmake_extra_args = ["-A", "ARM64EC"]
2529+
if args.buildasx:
2530+
cmake_extra_args += ["-D", "BUILD_AS_ARM64X=ARM64EC"]
25222531
cmake_extra_args += ["-G", args.cmake_generator]
25232532
# Cannot test on host build machine for cross-compiled
25242533
# builds (Override any user-defined behaviour for test if any)
@@ -2553,6 +2562,7 @@ def main():
25532562
cmake_extra_args = ["-A", target_arch, "-T", toolset, "-G", args.cmake_generator]
25542563
if args.enable_wcos:
25552564
cmake_extra_defines.append("CMAKE_USER_MAKE_RULES_OVERRIDE=wcos_rules_override.cmake")
2565+
25562566
elif args.cmake_generator is not None:
25572567
cmake_extra_args += ["-G", args.cmake_generator]
25582568

0 commit comments

Comments
 (0)