Skip to content
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
47a5965
enable GENAI with FetchContent and add find_package() configs
apwojcik Nov 9, 2025
05f5749
use private scope for llguidance in target_include_directories
apwojcik Nov 10, 2025
0691161
fix Python codecov and model benchmark
apwojcik Nov 11, 2025
68a545d
copy soversion library for Python wheel if available
apwojcik Nov 11, 2025
4e628e3
fix onnxruntime-genai Java library name
apwojcik Nov 12, 2025
5f7aeea
add Android and IOS to the exclusion
apwojcik Nov 13, 2025
5305384
Merge branch 'main' into fetch_content
apwojcik Dec 10, 2025
a7b6ff2
fix copy command for Java package on Windows/Android
apwojcik Dec 10, 2025
9285680
Merge branch 'main' into fetch_content
apwojcik Feb 26, 2026
fdbaffc
update example CMakeLists.txt
apwojcik Feb 26, 2026
006bf1a
Merge branch 'main' into fetch_content
apwojcik Mar 23, 2026
0d7b08e
update to recent Java changes
apwojcik Mar 23, 2026
4ac8295
fix building examples
apwojcik Mar 23, 2026
893560a
Merge branch 'main' into fetch_content
apwojcik Apr 10, 2026
f24dbf1
resolve merge conflicts
apwojcik Apr 10, 2026
38a4346
Update examples/c/CMakeLists.txt
apwojcik Apr 10, 2026
b84ab59
Update cmake/package.cmake
apwojcik Apr 10, 2026
e9b479a
Update src/csharp/Microsoft.ML.OnnxRuntimeGenAI.sln
apwojcik Apr 10, 2026
3401949
revert examples
apwojcik Apr 10, 2026
4e623ea
Update cmake/version.cmake
apwojcik Apr 11, 2026
a7d4a51
incorporate Copilot feedback
apwojcik Apr 11, 2026
83c18bd
incorporate Copilot's feedback
apwojcik Apr 11, 2026
6f9e3b8
use CMAKE_SYSTEM_PROCESSOR for macOS to autodetect architecture
apwojcik Apr 11, 2026
1fd3a6d
Merge branch 'main' into fetch_content
apwojcik Apr 11, 2026
a6a3a85
rewrite examples CMakeLists.txt
apwojcik Apr 12, 2026
6009a52
update .gitignore
apwojcik Apr 12, 2026
e8311ee
Merge branch 'main' into fetch_content
apwojcik Apr 12, 2026
c1241ff
include ONNXRuntime installed localy in ort directory
apwojcik Apr 12, 2026
b77750e
fix Windows build
apwojcik Apr 13, 2026
0d461a4
incorporate review feedback
apwojcik Apr 16, 2026
37e63ed
copy runtime dlls
apwojcik Apr 16, 2026
7f04a01
Merge branch 'main' into fetch_content
apwojcik May 15, 2026
11311dd
remove extra space
apwojcik May 15, 2026
9546c37
Merge branch 'main' into fetch_content
apwojcik May 19, 2026
4e23a0e
incorporate review feedback
apwojcik May 19, 2026
1fcce6e
incorporate review feedback
apwojcik May 19, 2026
beee6b5
incorporate review feedback
apwojcik May 19, 2026
c82b6d7
Merge branch 'main' into fetch_content
apwojcik May 21, 2026
3c768e6
Merge branch 'main' into fetch_content
apwojcik Jun 11, 2026
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
*obj/
src/csharp/AssemblyInfo.cs
/ort
/build
build*/
/build_*
build*/

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate?

/test/test_models/*
/cache_models
/onnxruntime-linux-x64-*
*.csv
.idea
.venv
cache_dir
example-models
*.onnx
Expand Down Expand Up @@ -44,4 +46,4 @@ examples/csharp/ModelChat/models
/src/java/local.properties
/src/java/build
/src/java/CMakeFiles
/src/java/CMakeCache.txt
/src/java/CMakeCache.txt
32 changes: 23 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ else()
cmake_minimum_required(VERSION 3.26)
endif()

include(cmake/version.cmake)

project(Generators VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES C CXX)

include(GNUInstallDirs)
include(FetchContent)
include(CMakeDependentOption)
project(Generators LANGUAGES C CXX)

# All Options should be defined in cmake/options.cmake This must be included before any other cmake file is included
include(cmake/options.cmake)
Expand Down Expand Up @@ -98,7 +102,6 @@ endif()

include(cmake/ortlib.cmake)


include(cmake/external/onnxruntime_external_deps.cmake)
# All Global variables, including GLOB, for the top level CMakeLists.txt should be defined here
include(cmake/global_variables.cmake)
Expand Down Expand Up @@ -158,23 +161,36 @@ find_package(Threads REQUIRED)
if(WIN32)
add_library(onnxruntime-genai SHARED ${generator_srcs} "${GENERATORS_ROOT}/dll/onnxruntime-genai.rc")
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_INFO=\"${VERSION_INFO}\")
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MAJOR=${VERSION_MAJOR})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MINOR=${VERSION_MINOR})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_PATCH=${VERSION_PATCH})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MAJOR=${PROJECT_VERSION_MAJOR})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MINOR=${PROJECT_VERSION_MINOR})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_PATCH=${PROJECT_VERSION_PATCH})
target_compile_definitions(onnxruntime-genai PRIVATE VERSION_SUFFIX=${VERSION_SUFFIX})
target_compile_definitions(onnxruntime-genai PRIVATE FILE_NAME=\"onnxruntime-genai.dll\")
else()
add_library(onnxruntime-genai SHARED ${generator_srcs})
set_target_properties(onnxruntime-genai PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
endif()

target_include_directories(onnxruntime-genai INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> $<INSTALL_INTERFACE:include>)
Comment on lines +174 to +175

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this leak all internal headers to consumers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only the headers marked public and interface.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are all the files installed on Windows with the cmake --install ... command. I anonymized the actual installation path with .... The command installs similar files on other operating systems, except that the .so files are located in the lib directory rather than bin.

❯ cmake --install .\build\Release\ --prefix C:\...\onnxruntime-genai\Release --config Release
-- Installing: C:/.../onnxruntime-genai/Release/lib/onnxruntime-genai.lib
-- Installing: C:/.../onnxruntime-genai/Release/bin/onnxruntime-genai.dll
-- Installing: C:/.../onnxruntime-genai/Release/include/ort_genai_c.h
-- Installing: C:/.../onnxruntime-genai/Release/include/ort_genai.h
-- Installing: C:/.../onnxruntime-genai/Release/lib/cmake/onnxruntime-genai/onnxruntime-genai-targets.cmake
-- Installing: C:/.../onnxruntime-genai/Release/lib/cmake/onnxruntime-genai/onnxruntime-genai-targets-release.cmake
-- Installing: C:/.../onnxruntime-genai/Release/./README.md
-- Installing: C:/.../onnxruntime-genai/Release/./ThirdPartyNotices.txt
-- Installing: C:/.../onnxruntime-genai/Release/./SECURITY.md
-- Installing: C:/.../onnxruntime-genai/Release/./LICENSE
-- Installing: C:/.../onnxruntime-genai/Release/lib/cmake/onnxruntime-genai/onnxruntime-genai-config.cmake
-- Installing: C:/.../onnxruntime-genai/Release/lib/cmake/onnxruntime-genai/onnxruntime-genai-config-version.cmake


target_include_directories(onnxruntime-genai PRIVATE ${ORT_HEADER_DIR})
target_include_directories(onnxruntime-genai PRIVATE ${onnxruntime_extensions_SOURCE_DIR}/shared/api)
target_link_libraries(onnxruntime-genai PRIVATE onnxruntime_extensions)
target_link_directories(onnxruntime-genai PRIVATE ${ORT_LIB_DIR})
target_link_libraries(onnxruntime-genai PRIVATE Threads::Threads)

# The genai library itself is always embedded in the shared library
list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai>")
if(WIN32 OR ANDROID OR IOS)
list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai>")
else()
if (ENABLE_PYTHON)
list(APPEND python_embed_libs "$<TARGET_SONAME_FILE:onnxruntime-genai>")
endif()
if (ENABLE_JAVA)
list(APPEND java_embed_libs "$<TARGET_LINKER_FILE:onnxruntime-genai>")
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.
Expand All @@ -191,7 +207,6 @@ if(APPLE)
target_link_libraries(onnxruntime-genai PRIVATE "-framework Foundation" "-framework CoreML")
endif()


# Build all source files using CUDA as a separate shared library we dynamically load at runtime
if((USE_CUDA OR USE_TRT_RTX) AND CMAKE_CUDA_COMPILER)
# Suppress nvcc warnings:
Expand Down Expand Up @@ -234,9 +249,8 @@ if((USE_CUDA OR USE_TRT_RTX) AND CMAKE_CUDA_COMPILER)
endif()
endif()


if(USE_GUIDANCE)
target_include_directories(onnxruntime-genai PUBLIC ${llguidance_SOURCE_DIR}/parser/)
target_include_directories(onnxruntime-genai PRIVATE ${llguidance_SOURCE_DIR}/parser/)
target_link_libraries(onnxruntime-genai PRIVATE llguidance)
if (WIN32)
# bcrypt is needed for the rust std lib
Expand Down
10 changes: 2 additions & 8 deletions benchmark/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ endif()

add_executable(model_benchmark ${model_benchmark_srcs})

target_include_directories(model_benchmark PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src # directory containing the ort_genai headers
)

target_link_libraries(model_benchmark PRIVATE onnxruntime-genai ${ONNXRUNTIME_LIB})

target_link_directories(model_benchmark PRIVATE ${ORT_LIB_DIR})
target_link_libraries(model_benchmark PRIVATE onnxruntime-genai)
target_include_directories(model_benchmark PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${model_benchmark_srcs})
77 changes: 54 additions & 23 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescript

# Default to not building the language bindings
parser.add_argument("--build_csharp", action="store_true", help="Build the C# API.")
parser.add_argument("--msbuild_extra_options", nargs="+", action="extend", default=[], help="Extra MSBuild properties (/p:key=value). Provide as key=value.")
parser.add_argument("--build_java", action="store_true", help="Build Java bindings.")
parser.add_argument(
"--publish_java_maven_local",
Expand Down Expand Up @@ -402,9 +403,11 @@ def _get_csharp_properties(args: argparse.Namespace, ort_lib_dir: Path):
)
ort_lib_path = f"/p:OrtLibDir={ort_lib_dir}"

props = [configuration, platform, native_lib_path, ort_lib_path]
msbuild_extra_options = list(args.msbuild_extra_options)
if args.config == "Release" and not [opt for opt in msbuild_extra_options if 'IsReleaseBuild' in opt]:
msbuild_extra_options.append("IsReleaseBuild=True")

return props
return [configuration, platform, native_lib_path, ort_lib_path] + ["/p:" + option for option in msbuild_extra_options]


def _run_android_tests(args: argparse.Namespace):
Expand Down Expand Up @@ -651,6 +654,9 @@ def _get_opencv_toolchain_file():
)
args.test = False

if util.is_windows() and 'Ninja' in args.cmake_generator:
command += ["--compile-no-warning-as-error"]

if args.cmake_extra_defines != []:
command += args.cmake_extra_defines

Expand Down Expand Up @@ -761,13 +767,18 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]):
Build the examples.
"""
examples_dir = REPO_ROOT / "examples" / "c"
build_dir = examples_dir / "build"

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

build_dir.mkdir()
build_dir = examples_dir / "build" / args.config

# Removing the build directory will no longer work because of
# the FetchContent used in the examples. It creates the _deps/
# directory, which contains .git/ subdirectories. Removing those
# requires elevated privileges.
# if build_dir.exists():
# log.info(f"Removing existing build directory: {build_dir}")
# shutil.rmtree(build_dir)
#
# Do not need to create build dir manually. CMake will do that.
# build_dir.mkdir(parents=True)

samples_to_build = [
"-DMODEL_QA=ON",
Expand All @@ -777,13 +788,37 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]):
"-DNEMOTRON_SPEECH=ON"
]

ort_include_dir = REPO_ROOT / "ort" / "include"
ort_lib_dir = REPO_ROOT / "ort" / "lib"
oga_include_dir = REPO_ROOT / "src"
oga_lib_dir = args.build_dir
if util.is_windows():
# On Windows, the library files are in a subdirectory named after the configuration (e.g. Debug, Release, etc.)
oga_lib_dir = oga_lib_dir / args.config
cmake_prefix_path = str(args.build_dir)
if args.ort_home:
cmake_prefix_path += ";" + str(args.ort_home / args.config)
else:
ortlib_src_dir = args.build_dir / "_deps" / "ortlib-src"
ort_include_dir = REPO_ROOT / "ort" / "include"
if not ort_include_dir.exists():
ort_include_dir = ortlib_src_dir / "build" / "native" / "include"
ortlib_runtimes_dir = ortlib_src_dir / "runtimes"
ort_lib_dir = REPO_ROOT / "ort" / "lib"
if not ort_lib_dir.exists():
if util.is_windows():
ort_lib_dir = ortlib_runtimes_dir / "win-x64"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implicit assumption that is_windows() is always win-x64? Haven't looked at is_windows(), does it only return true if it is win-x64?

@apwojcik Artur Wojcik (apwojcik) May 19, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is needed for CI to pass when building examples, and the ONNX Runtime is extracted from a .nuget package. There's no Windows 32bit variant anymore. In the .nuget package, there are only win-x64 and win-arm64 subdirectories.

But you're right, the if will consume both win-x64 and win-arm64 and hardcode the path for win-x64. I will make win-arm64 the first to be checked.

elif util.is_windows_arm() or (util.is_windows() and (args.arm64 or args.arm64ec)):
ort_lib_dir = ortlib_runtimes_dir / "win-arm64"
elif args.android:
ort_lib_dir = ortlib_runtimes_dir / "android"
elif util.is_linux():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@apwojcik Artur Wojcik (apwojcik) May 19, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is to make CI pass with the ONNX Runtime extracted from the .nuget package. When used with find_package(), CMake will automatically configure all directories for building examples.

ort_lib_dir = ortlib_runtimes_dir / "linux-x64"
elif util.is_linux_arm() or (util.is_linux() and (args.arm64 or args.arm64ec)):
ort_lib_dir = ortlib_runtimes_dir / "linux-arm64"
elif util.is_mac() or args.macos:
ort_lib_dir = ortlib_runtimes_dir / "osx-arm64"
elif args.ios:
ort_lib_dir = ortlib_runtimes_dir / "ios"
elif not util.is_aix():
raise RuntimeError("Unsupported operating system to build examples for")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this error be cleaner (explicitly mention the build platform)? Any reason to not build here?

@apwojcik Artur Wojcik (apwojcik) May 19, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is only when ONNX Runtime has been extracted from the .nuget package. There are no ONNXRuntime libraries for AIX in the .nuget package. The script is not blocking on building AIX here. It only blocks on unknown operating systems. The find_package() branch above will build examples for any operating system.

samples_to_build += [
"-DORT_INCLUDE_DIR=" + str(ort_include_dir),
"-DORT_LIB_DIR=" + str(ort_lib_dir / "native")
]

cmake_command = (
[
Expand All @@ -794,14 +829,10 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]):
str(build_dir),
"-G",
args.cmake_generator,
"-DCMAKE_PREFIX_PATH=" + cmake_prefix_path

]
+ samples_to_build
+ [
"-DORT_INCLUDE_DIR=" + str(ort_include_dir),
"-DORT_LIB_DIR=" + str(ort_lib_dir),
"-DOGA_INCLUDE_DIR=" + str(oga_include_dir),
"-DOGA_LIB_DIR=" + str(oga_lib_dir),
]
)

if args.cmake_generator.startswith("Visual Studio"):
Expand All @@ -810,7 +841,7 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]):
elif args.arm64ec:
cmake_command += ["-A", "ARM64EC"]

if args.cmake_extra_defines != []:
if args.cmake_extra_defines:
cmake_command += args.cmake_extra_defines

util.run(cmake_command, env=env)
Expand Down
30 changes: 3 additions & 27 deletions cmake/global_variables.cmake
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# All Global variables for the top level CMakeLists.txt should be defined here

# Define the Version Info
file(READ "VERSION_INFO" ver)
set(VERSION_INFO ${ver})

# Example:
# VERSION_INFO: 0.4.0-dev
# VERSION_STR: 0.4.0
# VERSION_SUFFIX: dev
# VERSION_MAJOR: 0
# VERSION_MINOR: 4
# VERSION_PATCH: 0
string(REPLACE "-" ";" VERSION_LIST ${VERSION_INFO})
list(GET VERSION_LIST 0 VERSION_STR)
# Check if it is a stable or dev version
list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
if(VERSION_LIST_LENGTH GREATER 1)
list(GET VERSION_LIST 1 VERSION_SUFFIX)
else()
set(VERSION_SUFFIX "") # Set VERSION_SUFFIX to empty if stable version
endif()
string(REPLACE "." ";" VERSION_LIST ${VERSION_STR})
list(GET VERSION_LIST 0 VERSION_MAJOR)
list(GET VERSION_LIST 1 VERSION_MINOR)
list(GET VERSION_LIST 2 VERSION_PATCH)

include_guard()

# Define the project directories
set(REPO_ROOT ${PROJECT_SOURCE_DIR})
Expand Down Expand Up @@ -102,8 +78,8 @@ if (IOS OR MAC_CATALYST)
elseif (USE_WINML)
message(STATUS "Using WinML, does NOT include ONNX Runtime library, is provied by Windows.")
else()
if(NOT EXISTS "${ORT_LIB_DIR}/${ONNXRUNTIME_LIB}")
message(FATAL_ERROR "Expected the ONNX Runtime library to be found at ${ORT_LIB_DIR}/${ONNXRUNTIME_LIB}. Actual: Not found.")
if(NOT EXISTS "${ORT_BIN_DIR}/${ONNXRUNTIME_LIB}")
message(FATAL_ERROR "Expected the ONNX Runtime library to be found at ${ORT_BIN_DIR}/${ONNXRUNTIME_LIB}. Actual: Not found.")
Comment thread
apwojcik marked this conversation as resolved.
endif()
endif()

Expand Down
5 changes: 5 additions & 0 deletions cmake/onnxruntime-genai-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@
if (NOT TARGET onnxruntime::onnxruntime-genai)
include(${CMAKE_CURRENT_LIST_DIR}/onnxruntime-genai-targets.cmake)
endif()
check_required_components(onnxruntime-genai)
Loading
Loading