forked from microsoft/onnxruntime-genai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
228 lines (194 loc) · 10.3 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# For xcframework support
cmake_minimum_required(VERSION 3.28)
else()
cmake_minimum_required(VERSION 3.26)
endif()
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)
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 11)
message(FATAL_ERROR "GCC version must be greater than or equal to 11")
endif()
# Avoid warning of Calling FetchContent_Populate(Lib) is deprecated temporarily
# TODO: find a better way to handle the header-only 3rd party deps
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.30.0")
cmake_policy(SET CMP0169 OLD)
endif()
if(MSVC)
# DLL initialization errors due to old conda msvcp140.dll dll are a result of the new MSVC compiler
# See https://developercommunity.visualstudio.com/t/Access-violation-with-std::mutex::lock-a/10664660#T-N10668856
# Remove this definition once the conda msvcp140.dll dll is updated.
add_compile_definitions(_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
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)
# Checking if CUDA is supported
include(cmake/check_cuda.cmake)
# Checking if ROCm is supported
include(cmake/check_rocm.cmake)
# Checking if DML is supported
include(cmake/check_dml.cmake)
# Checking if WebGpu is supported
include(cmake/check_webgpu.cmake)
include(cmake/cxx_standard.cmake)
add_compile_definitions(BUILDING_ORT_GENAI_C)
if(MSVC)
# set updated value for __cplusplus macro instead of 199711L
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
add_compile_options(
"$<$<COMPILE_LANGUAGE:C,CXX>:/w15038>"
"$<$<COMPILE_LANGUAGE:C,CXX>:/wd4100>"
"$<$<COMPILE_LANGUAGE:C,CXX>:/W4>"
"$<$<COMPILE_LANGUAGE:C,CXX>:/WX>"
)
endif()
# Suggested by https://gitlab.kitware.com/cmake/cmake/-/issues/20132
# MacCatalyst is not well supported in CMake
# The error that can emerge without this flag can look like:
# "clang : error : overriding '-mmacosx-version-min=11.0' option with '-target x86_64-apple-ios14.0-macabi' [-Werror,-Woverriding-t-option]"
if (PLATFORM_NAME STREQUAL "macabi")
add_compile_options(-Wno-overriding-t-option)
add_link_options(-Wno-overriding-t-option)
endif()
if(ENABLE_TESTS)
# call enable_testing so we can add tests from subdirectories (e.g. test and src/java)
# it applies recursively to all subdirectories
enable_testing()
if (USE_CUDA OR TEST_PHI2)
add_compile_definitions(TEST_PHI2=1)
else()
add_compile_definitions(TEST_PHI2=0)
endif()
endif()
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_SUFFIX=${VERSION_SUFFIX})
target_compile_definitions(onnxruntime-genai PRIVATE FILE_NAME=\"onnxruntime-genai.dll\")
add_library(onnxruntime-genai-static STATIC ${generator_srcs})
else()
add_library(onnxruntime-genai SHARED ${generator_srcs})
add_library(onnxruntime-genai-static STATIC ${generator_srcs})
endif()
target_include_directories(onnxruntime-genai PRIVATE ${ORT_HEADER_DIR})
target_include_directories(onnxruntime-genai-static PRIVATE ${ORT_HEADER_DIR})
target_include_directories(onnxruntime-genai PRIVATE ${onnxruntime_extensions_SOURCE_DIR}/include)
target_include_directories(onnxruntime-genai PRIVATE ${onnxruntime_extensions_SOURCE_DIR}/shared/api/)
target_include_directories(onnxruntime-genai-static PRIVATE ${onnxruntime_extensions_SOURCE_DIR}/include)
target_include_directories(onnxruntime-genai-static PUBLIC ${onnxruntime_extensions_SOURCE_DIR}/shared/api/)
target_link_libraries(onnxruntime-genai PRIVATE onnxruntime_extensions)
target_link_libraries(onnxruntime-genai-static PUBLIC onnxruntime_extensions)
target_link_directories(onnxruntime-genai PRIVATE ${ORT_LIB_DIR})
# 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)))
add_compile_definitions(_ORT_GENAI_USE_DLOPEN)
else()
target_link_libraries(onnxruntime-genai PRIVATE ${ONNXRUNTIME_LIB})
endif()
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 AND CMAKE_CUDA_COMPILER)
add_library(onnxruntime-genai-cuda SHARED ${generator_cudalib_srcs})
target_include_directories(onnxruntime-genai-cuda PRIVATE ${ORT_HEADER_DIR})
target_include_directories(onnxruntime-genai-cuda PRIVATE ${GENERATORS_ROOT})
target_link_libraries(onnxruntime-genai-cuda PRIVATE cublasLt cublas curand cufft cudart)
set_target_properties(onnxruntime-genai-cuda PROPERTIES LINKER_LANGUAGE CUDA)
add_dependencies(onnxruntime-genai onnxruntime-genai-cuda)
source_group(TREE ${GENERATORS_ROOT}/cuda FILES ${generator_cudalib_srcs})
list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai-cuda>")
if(APPLE)
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker -exported_symbols_list ${GENERATORS_ROOT}/cuda/exported_symbols.lst")
elseif(UNIX)
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${GENERATORS_ROOT}/cuda/version_script.lds -Xlinker --gc-sections")
elseif(WIN32)
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${GENERATORS_ROOT}/cuda/symbols.def")
else()
message(FATAL_ERROR "${target} unknown platform, need to specify shared library exports for it")
endif()
endif()
if(CMAKE_GENERATOR_TOOLSET MATCHES "Visual Studio")
target_link_options(onnxruntime-genai PRIVATE "/CETCOMPAT")
target_compile_options(onnxruntime-genai PRIVATE "/sdl")
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(onnxruntime-genai-static PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(onnxruntime-genai-static PRIVATE dl) # For dlopen & co
endif()
if(USE_DML)
list(APPEND ortgenai_embed_libs "${D3D12_LIB_DIR}/D3D12Core.dll")
target_include_directories(onnxruntime-genai PRIVATE $<TARGET_PROPERTY:${WIL_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
target_include_directories(onnxruntime-genai PRIVATE $<TARGET_PROPERTY:${DIRECTX_HEADERS_TARGET},INTERFACE_INCLUDE_DIRECTORIES>/directx)
target_include_directories(onnxruntime-genai PRIVATE $<TARGET_PROPERTY:${DIRECTX_HEADERS_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
target_include_directories(onnxruntime-genai-static PUBLIC $<TARGET_PROPERTY:${WIL_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
target_include_directories(onnxruntime-genai-static PUBLIC $<TARGET_PROPERTY:${DIRECTX_HEADERS_TARGET},INTERFACE_INCLUDE_DIRECTORIES>/directx)
target_include_directories(onnxruntime-genai-static PUBLIC $<TARGET_PROPERTY:${DIRECTX_HEADERS_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
target_include_directories(onnxruntime-genai PRIVATE ${DML_HEADER_DIR})
target_include_directories(onnxruntime-genai-static PRIVATE ${DML_HEADER_DIR})
target_include_directories(onnxruntime-genai PRIVATE ${D3D12_HEADER_DIR})
target_include_directories(onnxruntime-genai-static PRIVATE ${D3D12_HEADER_DIR})
target_link_directories(onnxruntime-genai PRIVATE ${DML_LIB_DIR})
target_link_directories(onnxruntime-genai PRIVATE ${D3D12_LIB_DIR})
target_link_directories(onnxruntime-genai-static PUBLIC ${DML_LIB_DIR})
target_link_directories(onnxruntime-genai-static PUBLIC ${D3D12_LIB_DIR})
target_link_libraries(onnxruntime-genai PRIVATE d3d12.lib dxcore.lib dxguid.lib dxgi.lib)
target_link_libraries(onnxruntime-genai PRIVATE d3d12.lib dxcore.lib dxguid.lib dxgi.lib)
target_link_libraries(onnxruntime-genai-static PUBLIC d3d12.lib dxcore.lib dxguid.lib dxgi.lib)
get_filename_component(PACKAGES_DIR ${CMAKE_CURRENT_BINARY_DIR}/_deps ABSOLUTE)
set(DXC_PACKAGE_DIR ${PACKAGES_DIR}/Microsoft.Direct3D.DXC.1.7.2308.12)
set(NUGET_CONFIG ${PROJECT_SOURCE_DIR}/nuget.config)
set(PACKAGES_CONFIG ${PROJECT_SOURCE_DIR}/packages.config)
add_custom_command(
OUTPUT
${DXC_PACKAGE_DIR}/build/native/bin/x64/dxc.exe
DEPENDS
${PACKAGES_CONFIG}
${NUGET_CONFIG}
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/nuget/src/nuget restore ${PACKAGES_CONFIG} -PackagesDirectory ${PACKAGES_DIR} -ConfigFile ${NUGET_CONFIG}
VERBATIM
)
add_custom_target(
RESTORE_PACKAGES ALL
DEPENDS
${DXC_PACKAGE_DIR}/build/native/bin/x64/dxc.exe
)
add_dependencies(RESTORE_PACKAGES nuget)
add_dependencies(onnxruntime-genai RESTORE_PACKAGES)
add_dependencies(onnxruntime-genai-static RESTORE_PACKAGES)
endif()
if(ANDROID)
# strip the binary if it's not a build with debug info
set_target_properties(onnxruntime-genai PROPERTIES LINK_FLAGS_RELEASE -s)
set_target_properties(onnxruntime-genai PROPERTIES LINK_FLAGS_MINSIZEREL -s)
endif()
if(ENABLE_TESTS)
message("------------------Enabling tests------------------")
add_subdirectory("${REPO_ROOT}/test")
endif()
if(ENABLE_PYTHON)
message("------------------Enabling Python Wheel------------------")
add_subdirectory("${SRC_ROOT}/python")
endif()
if (ENABLE_JAVA)
message("------------------Enabling Java Jar------------------")
add_subdirectory("${SRC_ROOT}/java")
endif()
if(ENABLE_MODEL_BENCHMARK)
message("------------------Enabling model benchmark------------------")
add_subdirectory("${REPO_ROOT}/benchmark/c")
endif()
# Have visual studio put all files into one single folder vs the default split of header files into a separate folder
source_group(TREE ${GENERATORS_ROOT} FILES ${generator_srcs})
include(cmake/package.cmake)