From 7a275f11572a59a10d1a6ea9bbd96c50eae889e1 Mon Sep 17 00:00:00 2001 From: vlado Date: Sun, 25 Oct 2020 20:08:58 -0600 Subject: [PATCH] Add option to build with shared c runtime on windows (#19409) Co-authored-by: vlado --- CMakeLists.txt | 143 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 100 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e93e0b3876c..ea49798c0923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ option(USE_INT64_TENSOR_SIZE "Use int64_t to represent the total number of eleme option(BUILD_CYTHON_MODULES "Build cython modules." OFF) cmake_dependent_option(USE_SPLIT_ARCH_DLL "Build a separate DLL for each Cuda arch (Windows only)." ON "MSVC" OFF) cmake_dependent_option(USE_CCACHE "Attempt using CCache to wrap the compilation" ON "UNIX" OFF) - +cmake_dependent_option(MXNET_FORCE_SHARED_CRT "Build with dynamic CRT on Windows (/MD)" ON "MXNET_BUILD_SHARED_LIBS" OFF) message(STATUS "CMAKE_CROSSCOMPILING ${CMAKE_CROSSCOMPILING}") message(STATUS "CMAKE_HOST_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR}") @@ -93,6 +93,11 @@ if(USE_TVM_OP) add_definitions(-DMXNET_USE_TVM_OP=1) endif() +if(MXNET_FORCE_SHARED_CRT) + set(DMLC_FORCE_SHARED_CRT ON) + set(gtest_force_shared_crt ON) +endif() + message(STATUS "CMake version '${CMAKE_VERSION}' using generator '${CMAKE_GENERATOR}'") project(mxnet C CXX) if(USE_CUDA) @@ -280,8 +285,16 @@ endif() if(USE_MKLDNN) # CPU architecture (e.g., C5) can't run on another architecture (e.g., g3). if(MSVC) - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /EHsc /MT") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /EHsc /Gy /MT") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /EHsc") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /EHsc /Gy") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /EHsc /Gy") + set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /EHsc /Gy") + if(NOT MXNET_FORCE_SHARED_CRT) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT") + set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT") + endif() endif() function(load_mkldnn) @@ -601,13 +614,15 @@ if (NOT (EXTRA_OPERATORS STREQUAL "")) list(APPEND SOURCE ${EXTRA_SRC} ${EXTRA_CUSRC}) endif() -if(MSVC) +if(MSVC AND NOT MXNET_FORCE_SHARED_CRT) foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) if(${flag_var} MATCHES "/MD") string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") - endif(${flag_var} MATCHES "/MD") + elseif(${flag_var} MATCHES "/MDd") + string(REGEX REPLACE "/MDd" "/MTd" ${flag_var} "${${flag_var}}") + endif() endforeach(flag_var) endif() @@ -713,54 +728,90 @@ if(UNIX) set_target_properties(mxnet_static PROPERTIES OUTPUT_NAME mxnet) elseif(MSVC) if(USE_CUDA) - if(MSVC) - if(USE_SPLIT_ARCH_DLL) - add_executable(gen_warp tools/windowsbuild/gen_warp.cpp) - add_library(mxnet SHARED tools/windowsbuild/warp_dll.cpp ${CMAKE_BINARY_DIR}/warp_gen_cpp.cpp - ${CMAKE_BINARY_DIR}/warp_gen.asm) - target_link_libraries(mxnet PRIVATE cudart Shlwapi) - list(GET arch_code_list 0 mxnet_first_arch) - foreach(arch ${arch_code_list}) - add_library(mxnet_${arch} SHARED ${SOURCE}) - target_link_libraries(mxnet_${arch} PUBLIC mshadow) + if(USE_SPLIT_ARCH_DLL) + add_executable(gen_warp tools/windowsbuild/gen_warp.cpp) + add_library(mxnet SHARED tools/windowsbuild/warp_dll.cpp ${CMAKE_BINARY_DIR}/warp_gen_cpp.cpp + ${CMAKE_BINARY_DIR}/warp_gen.asm) + target_link_libraries(mxnet PRIVATE cudart Shlwapi) + list(GET arch_code_list 0 mxnet_first_arch) + foreach(arch ${arch_code_list}) + add_library(mxnet_${arch} SHARED ${SOURCE}) + target_link_libraries(mxnet_${arch} PUBLIC mshadow) + target_compile_options( + mxnet_${arch} + PRIVATE + "$<$:--gpu-architecture=compute_${arch}>" + ) + target_compile_options( + mxnet_${arch} + PRIVATE + "$<$:--gpu-code=sm_${arch},compute_${arch}>" + ) + if(MXNET_FORCE_SHARED_CRT) + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MDd -Gy /bigobj>") target_compile_options( mxnet_${arch} - PRIVATE - "$<$:--gpu-architecture=compute_${arch}>" - ) + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") target_compile_options( mxnet_${arch} - PRIVATE - "$<$:--gpu-code=sm_${arch},compute_${arch}>" - ) + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") target_compile_options( - mxnet_${arch} + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") + else() + target_compile_options( + mxnet_${arch} PRIVATE "$<$,$>:-Xcompiler=-MTd -Gy /bigobj>") target_compile_options( mxnet_${arch} PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") - endforeach() - - add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/warp_gen_cpp.cpp ${CMAKE_BINARY_DIR}/warp_gen.asm - COMMAND gen_warp $ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/ DEPENDS $) - else(USE_SPLIT_ARCH_DLL) - string(REPLACE ";" " " NVCC_FLAGS_ARCH "${NVCC_FLAGS_ARCH}") - string(APPEND CMAKE_CUDA_FLAGS " ${CUDA_ARCH_FLAGS_SPACES}") - add_library(mxnet SHARED ${SOURCE}) - target_link_libraries(mxnet PUBLIC mshadow) + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") + endif() + endforeach() + + add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/warp_gen_cpp.cpp ${CMAKE_BINARY_DIR}/warp_gen.asm + COMMAND gen_warp $ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/ DEPENDS $) + else(USE_SPLIT_ARCH_DLL) + string(REPLACE ";" " " NVCC_FLAGS_ARCH "${NVCC_FLAGS_ARCH}") + string(APPEND CMAKE_CUDA_FLAGS " ${CUDA_ARCH_FLAGS_SPACES}") + add_library(mxnet SHARED ${SOURCE}) + target_link_libraries(mxnet PUBLIC mshadow) + if(MXNET_FORCE_SHARED_CRT) target_compile_options( mxnet - PRIVATE "$<$,$>:-Xcompiler=-MTd -Gy /bigobj>") + PRIVATE "$<$,$>:-Xcompiler=-MDd -Gy /bigobj>") target_compile_options( mxnet - PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") - - endif(USE_SPLIT_ARCH_DLL) - else() - add_library(mxnet SHARED ${SOURCE}) - target_link_libraries(mxnet PUBLIC mshadow) - endif() + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") + target_compile_options( + mxnet + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") + target_compile_options( + mxnet + PRIVATE "$<$,$>:-Xcompiler=-MD -Gy /bigobj>") + else() + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MTd -Gy /bigobj>") + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") + target_compile_options( + mxnet_${arch} + PRIVATE "$<$,$>:-Xcompiler=-MT -Gy /bigobj>") + endif() + endif(USE_SPLIT_ARCH_DLL) else() add_library(mxnet SHARED ${SOURCE}) target_link_libraries(mxnet PUBLIC mshadow) @@ -799,9 +850,15 @@ elseif(MSVC) set_target_properties(subgraph_lib PROPERTIES PREFIX "lib") set_target_properties(pass_lib PROPERTIES PREFIX "lib") if(USE_CUDA) - target_compile_options(customop_gpu_lib PRIVATE "$<$:-Xcompiler=-LD -MT>") - target_compile_options(customop_gpu_lib PRIVATE "$<$:/LD>") - target_compile_options(customop_gpu_lib PRIVATE "$<$:/MT>") + if(MXNET_FORCE_SHARED_CRT) + target_compile_options(customop_gpu_lib PRIVATE "$<$:-Xcompiler=-LD -MD>") + target_compile_options(customop_gpu_lib PRIVATE "$<$:/LD>") + target_compile_options(customop_gpu_lib PRIVATE "$<$:/MD>") + else() + target_compile_options(customop_gpu_lib PRIVATE "$<$:-Xcompiler=-LD -MT>") + target_compile_options(customop_gpu_lib PRIVATE "$<$:/LD>") + target_compile_options(customop_gpu_lib PRIVATE "$<$:/MT>") + endif() set_target_properties(customop_gpu_lib PROPERTIES PREFIX "lib") endif() endif()