From 423cfb433fc785314bea23388cac529161746ad5 Mon Sep 17 00:00:00 2001 From: moyo1997 <54333118+moyo1997@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:40:57 -0800 Subject: [PATCH] build onnxruntime as x --- .gitignore | 1 + cmake/CMakeLists.txt | 5 +++++ cmake/arm64x.cmake | 33 +++++++++++++++++++++++++++++++++ tools/ci_build/build.py | 12 +++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 cmake/arm64x.cmake diff --git a/.gitignore b/.gitignore index 6937f338b8a6b..4d0a1205b7c19 100644 --- a/.gitignore +++ b/.gitignore @@ -195,3 +195,4 @@ Package.pins Package.resolved .build/ .swiftpm/ +repros/ diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 5796db03fed7c..d7e78505f662b 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -1774,3 +1774,8 @@ if(TARGET onnxruntime) "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") endif() + +if(DEFINED BUILD_AS_ARM64X) + set(ARM64X_TARGETS onnxruntime) + include("${CMAKE_SOURCE_DIR}/arm64x.cmake") +endif() diff --git a/cmake/arm64x.cmake b/cmake/arm64x.cmake new file mode 100644 index 0000000000000..be476e09625bd --- /dev/null +++ b/cmake/arm64x.cmake @@ -0,0 +1,33 @@ +set(arm64ReproDir "${CMAKE_SOURCE_DIR}/repros") + +if("${BUILD_AS_ARM64X}" STREQUAL "ARM64") + foreach (n ${ARM64X_TARGETS}) + add_custom_target(mkdirs_${n} ALL COMMAND cmd /c (if exist \"${arm64ReproDir}/${n}_temp/\" rmdir /s /q \"${arm64ReproDir}/${n}_temp\") && mkdir \"${arm64ReproDir}/${n}_temp\" ) + add_dependencies(${n} mkdirs_${n}) + target_link_options(${n} PRIVATE "/LINKREPRO:${arm64ReproDir}/${n}_temp") + 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} + WORKING_DIRECTORY ${arm64ReproDir}) + endforeach() + + +elseif("${BUILD_AS_ARM64X}" STREQUAL "ARM64EC") + foreach (n ${ARM64X_TARGETS}) + set(ARM64_LIBS) + set(ARM64_OBJS) + set(ARM64_DEF) + + file(GLOB ARM64_OBJS "${arm64ReproDir}/${n}/*.obj") + file(GLOB ARM64_DEF "${arm64ReproDir}/${n}/*.def") + file(GLOB ARM64_LIBS "${arm64ReproDir}/${n}/*.LIB") + + if(NOT "${ARM64_DEF}" STREQUAL "") + set(ARM64_DEF "/defArm64Native:${ARM64_DEF}") + endif() + target_sources(${n} PRIVATE ${ARM64_OBJS}) + target_link_options(${n} PRIVATE /machine:arm64x "${ARM64_DEF}") + + if(NOT "${ARM64_LIBS}" STREQUAL "") + target_link_libraries(${n} PUBLIC ${ARM64_LIBS}) + endif() + endforeach() +endif() diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 3b1a0317c58f1..e1d1fbe188606 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -346,6 +346,11 @@ def convert_arg_line_to_args(self, arg_line): help="[cross-compiling] Create ARM64EC makefiles. Requires --update and no existing cache " "CMake setup. Delete CMakeCache.txt if needed", ) + parser.add_argument( + "--buildasx", + action="store_true", + help="[cross-compiling] Create ARM64X Binary.", + ) parser.add_argument("--msvc_toolset", help="MSVC toolset to use. e.g. 14.11") parser.add_argument("--windows_sdk_version", help="Windows SDK version to use. e.g. 10.0.19041.0") parser.add_argument("--android", action="store_true", help="Build for Android") @@ -2496,8 +2501,12 @@ def main(): cmake_extra_args = ["-A", "ARM"] elif args.arm64: cmake_extra_args = ["-A", "ARM64"] + if args.buildasx: + cmake_extra_args += ["-D", "BUILD_AS_ARM64X=ARM64"] elif args.arm64ec: - cmake_extra_args = ["-A", "ARM64EC"] + cmake_extra_args = ["-A", "ARM64EC", "-D", "BUILD_AS_ARM64X=ARM64EC"] + if args.buildasx: + cmake_extra_args += ["-D", "BUILD_AS_ARM64X=ARM64EC"] cmake_extra_args += ["-G", args.cmake_generator] # Cannot test on host build machine for cross-compiled # builds (Override any user-defined behaviour for test if any) @@ -2532,6 +2541,7 @@ def main(): cmake_extra_args = ["-A", target_arch, "-T", toolset, "-G", args.cmake_generator] if args.enable_wcos: cmake_extra_defines.append("CMAKE_USER_MAKE_RULES_OVERRIDE=wcos_rules_override.cmake") + elif args.cmake_generator is not None: cmake_extra_args += ["-G", args.cmake_generator]