diff --git a/CMakeLists.txt b/CMakeLists.txt index b325c2763..6a91fc17a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ project(Generators LANGUAGES C CXX) # All Options should be defined in cmake/options.cmake include(cmake/options.cmake) include(cmake/external/onnxruntime_external_deps.cmake) +include(cmake/util_func.cmake) file(READ "VERSION_INFO" ver) set(VERSION_INFO ${ver} CACHE STRING "Set the onnxruntime-genai version info.") @@ -106,10 +107,7 @@ add_library(onnxruntime-genai-static STATIC ${generator_srcs}) target_include_directories(onnxruntime-genai PRIVATE ${ORT_HEADER_DIR}) target_include_directories(onnxruntime-genai-static PRIVATE ${ORT_HEADER_DIR}) -if(CMAKE_GENERATOR_TOOLSET MATCHES "Visual Studio") - target_link_options(onnxruntime-genai PRIVATE "/CETCOMPAT") - target_compile_options(onnxruntime-genai PRIVATE "/sdl") -endif() +genai_set_target_compile_options(onnxruntime-genai) if(CMAKE_SYSTEM_NAME STREQUAL "Linux") set_target_properties(onnxruntime-genai-static PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/cmake/util_func.cmake b/cmake/util_func.cmake new file mode 100644 index 000000000..80847da7f --- /dev/null +++ b/cmake/util_func.cmake @@ -0,0 +1,15 @@ +function(genai_set_target_compile_options target) + if (MSVC) + check_cxx_compiler_flag(-sdl HAS_SDL) + check_cxx_compiler_flag(-Qspectre HAS_QSPECTRE) + if (HAS_SDL) + target_compile_options(${target} PRIVATE "/sdl") + endif () + if (HAS_QSPECTRE) + target_compile_options(${target} PRIVATE "/Qspectre") + endif () + if (NOT CMAKE_SYSTEM_PROCESSOR MATCHES "arm" AND NOT USE_CUDA) + target_compile_options(${target} PRIVATE "/CETCOMPAT") + endif () + endif () +endfunction() \ No newline at end of file diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index e2175039a..fdbe33023 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -1,5 +1,5 @@ include(${CMAKE_SOURCE_DIR}/cmake/cxx_standard.cmake) - +include(${CMAKE_SOURCE_DIR}/cmake/util_func.cmake) file(GLOB python_srcs CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" @@ -14,10 +14,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux") endif() set_target_properties(python PROPERTIES OUTPUT_NAME "onnxruntime_genai") -if(CMAKE_GENERATOR_TOOLSET MATCHES "Visual Studio") - target_link_options(python PRIVATE "/CETCOMPAT") - target_compile_options(python PRIVATE "/sdl" PRIVATE "/Qspectre") -endif() +genai_set_target_compile_options(python) if(USE_CUDA AND CMAKE_CUDA_COMPILER) cmake_policy(SET CMP0104 OLD)