Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ examples/csharp/ModelChat/models

!/test/models/engine/
/test/models/engine/*
!/test/models/engine/synthetic-fixed-components/
!/test/models/engine/synthetic-fixed-components/decoder.onnx
!/test/models/engine/synthetic-fixed-components/genai_config.json
!/test/models/engine/synthetic-paged-scales/
!/test/models/engine/synthetic-paged-scales/decoder.onnx
!/test/models/engine/synthetic-paged-scales/genai_config.json
Expand Down
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ endif()
# The genai library itself is always embedded in the shared library
list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai>")

if(WIN32 AND NOT USE_WINML AND ENABLE_TESTS)
file(GLOB ort_runtime_dlls "${ORT_LIB_DIR}/*.dll")
if(ort_runtime_dlls)
add_custom_target(onnxruntime-genai-ort-runtime
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:onnxruntime-genai>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ort_runtime_dlls}
"$<TARGET_FILE_DIR:onnxruntime-genai>"
DEPENDS ${ort_runtime_dlls}
COMMAND_EXPAND_LISTS
)
add_dependencies(onnxruntime-genai onnxruntime-genai-ort-runtime)
endif()
endif()

# we keep the shared libraries disconnected on Android as they will come from separate AARs and we don't want to force
# the ORT version to match in both.
if(CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "Linux" OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND (NOT BUILD_APPLE_FRAMEWORK) AND (NOT MAC_CATALYST)))
Expand Down
40 changes: 34 additions & 6 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import platform
import shlex
import shutil
import stat
import subprocess
import sys
import textwrap
Expand Down Expand Up @@ -557,12 +558,31 @@ def _run_android_tests(args: argparse.Namespace):


def _get_windows_build_args(args: argparse.Namespace):
spectre_enabled = True
for definition in args.cmake_extra_defines:
name, separator, value = definition.removeprefix("-D").partition("=")
if name.split(":", 1)[0] == "ENABLE_MSVC_SPECTRE" and separator:
value = value.strip().upper()
spectre_enabled = value not in {
"",
"0",
"OFF",
"NO",
"FALSE",
"N",
"IGNORE",
"NOTFOUND",
} and not value.endswith("-NOTFOUND")
spectre_flag = " /Qspectre" if spectre_enabled else ""
win_args = [
"-DCMAKE_EXE_LINKER_FLAGS_INIT=/profile /DYNAMICBASE",
"-DCMAKE_MODULE_LINKER_FLAGS_INIT=/profile /DYNAMICBASE",
"-DCMAKE_SHARED_LINKER_FLAGS_INIT=/profile /DYNAMICBASE",
]
cmake_c_flags = "/EHsc /Qspectre /MP /guard:cf /DWIN32 /D_WINDOWS /DWINAPI_FAMILY=100 /DWINVER=0x0A00 /D_WIN32_WINNT=0x0A00 /DNTDDI_VERSION=0x0A000000"
cmake_c_flags = (
"/EHsc /MP /guard:cf /DWIN32 /D_WINDOWS /DWINAPI_FAMILY=100 /DWINVER=0x0A00 /D_WIN32_WINNT=0x0A00 /DNTDDI_VERSION=0x0A000000"
+ spectre_flag
)
if args.config == "Release":
cmake_c_flags += " /O2 /Ob2 /DNDEBUG"
elif args.config == "RelWithDebInfo":
Expand All @@ -575,7 +595,9 @@ def _get_windows_build_args(args: argparse.Namespace):
]
if args.use_cuda:
win_args += [
'-DCMAKE_CUDA_FLAGS_INIT=/DWIN32 /D_WINDOWS /DWINAPI_FAMILY=100 /DWINVER=0x0A00 /D_WIN32_WINNT=0x0A00 /DNTDDI_VERSION=0x0A000000 -Xcompiler=" /MP /guard:cf /Qspectre " -allow-unsupported-compiler',
'-DCMAKE_CUDA_FLAGS_INIT=/DWIN32 /D_WINDOWS /DWINAPI_FAMILY=100 /DWINVER=0x0A00 /D_WIN32_WINNT=0x0A00 /DNTDDI_VERSION=0x0A000000 -Xcompiler=" /MP /guard:cf'
+ spectre_flag
+ ' " -allow-unsupported-compiler',
]
return win_args

Expand Down Expand Up @@ -910,7 +932,7 @@ def _build_sdk_cmake(args: argparse.Namespace, env: dict[str, str]):
util.run(build_command, env=env)

if args.sdk == "python" and not args.skip_wheel:
util.run(build_command + ["--target", "PyPackageBuild"], env=env)
util.run([*build_command, "--target", "PyPackageBuild"], env=env)


def _build_sdk_csharp(args: argparse.Namespace, env: dict[str, str]):
Expand Down Expand Up @@ -1003,14 +1025,20 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]):

if build_dir.exists():
log.info(f"Removing existing build directory: {build_dir}")
shutil.rmtree(build_dir)

def remove_readonly(function, path, _):
os.chmod(path, stat.S_IWRITE)
function(path)

shutil.rmtree(build_dir, onerror=remove_readonly)

build_dir.mkdir()

samples_to_build = ["-DMODEL_QA=ON", "-DMODEL_CHAT=ON", "-DMODEL_MM=ON", "-DWHISPER=ON", "-DMODEL_ASR=ON"]

ort_include_dir = REPO_ROOT / "ort" / "include"
ort_lib_dir = REPO_ROOT / "ort" / "lib"
ort_root = args.ort_home if args.ort_home else REPO_ROOT / "ort"
ort_include_dir = ort_root / "include"
ort_lib_dir = ort_root / "lib"
oga_include_dir = REPO_ROOT / "src"
oga_lib_dir = args.build_dir
if util.is_windows():
Expand Down
14 changes: 10 additions & 4 deletions cmake/msvc_security.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ unset(_msec_plat_lc)
# --- /Qspectre Spectre variant 1 mitigations ---
# Generator expressions restrict this to C/C++ only — CUDA nvcc does not
# accept /Qspectre and will error if it is passed to it.
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:/Qspectre>"
"$<$<COMPILE_LANGUAGE:CXX>:/Qspectre>"
)
if(NOT DEFINED ENABLE_MSVC_SPECTRE)
# Standalone SDK builds do not include options.cmake. Keep mitigation on there too.
set(ENABLE_MSVC_SPECTRE ON)
endif()
if(ENABLE_MSVC_SPECTRE)
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:/Qspectre>"
"$<$<COMPILE_LANGUAGE:CXX>:/Qspectre>"
)
endif()
1 change: 1 addition & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option(USE_TRT_RTX "Build with TensorRT-RTX support" OFF)
option(USE_DML "Build with DML support" OFF)
option(USE_WINML "Build with WinML support" OFF)
option(USE_GUIDANCE "Build with guidance support" OFF)
option(ENABLE_MSVC_SPECTRE "Enable MSVC Spectre variant 1 mitigations" ON)
set(NUGET_PACKAGE_SOURCE "" CACHE STRING "Override the NuGet package source used by the build")
set(NUGET_CONFIG_FILE "" CACHE FILEPATH "Use a custom NuGet configuration file")

Expand Down
Loading