From 3803d1c901b6e2632682b02589b2d5aa888b0a88 Mon Sep 17 00:00:00 2001 From: peter Date: Sat, 27 Apr 2019 23:02:35 -0700 Subject: [PATCH] Fix conda build for Windows (#19824) Summary: Let's test it before merging. Pull Request resolved: https://github.com/pytorch/pytorch/pull/19824 Differential Revision: D15116111 Pulled By: soumith fbshipit-source-id: 0a73de3f045ee1349061674f5f8e2aaba382493c --- cmake/Dependencies.cmake | 31 +++++++++++++++++++++++++++++++ cmake/GoogleTestPatch.cmake | 24 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 cmake/GoogleTestPatch.cmake diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index ebfc2db7e9c9..51dbbc068ec0 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -338,6 +338,22 @@ if(BUILD_TEST) if (NOT CAFFE2_USE_MSVC_STATIC_RUNTIME) set(gtest_force_shared_crt ON CACHE BOOL "force shared crt on gtest" FORCE) endif() + # We need to replace googletest cmake scripts too. + # Otherwise, it will sometimes break the build. + # To make the git clean after the build, we make a backup first. + if (MSVC AND MSVC_Z7_OVERRIDE) + execute_process( + COMMAND ${CMAKE_COMMAND} + "-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake" + "-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak" + "-DREVERT=0" + "-P" + "${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake" + RESULT_VARIABLE _exitcode) + if(NOT ${_exitcode} EQUAL 0) + message(WARNING "Patching failed for Google Test. The build may fail.") + endif() + endif() # Add googletest subdirectory but make sure our INCLUDE_DIRECTORIES # don't bleed into it. This is because libraries installed into the root conda @@ -363,6 +379,21 @@ if(BUILD_TEST) # Recover build options. set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE) + + # To make the git clean after the build, we revert the changes here. + if (MSVC AND MSVC_Z7_OVERRIDE) + execute_process( + COMMAND ${CMAKE_COMMAND} + "-DFILENAME=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake" + "-DBACKUP=${CMAKE_CURRENT_LIST_DIR}/../third_party/googletest/googletest/cmake/internal_utils.cmake.bak" + "-DREVERT=1" + "-P" + "${CMAKE_CURRENT_LIST_DIR}/GoogleTestPatch.cmake" + RESULT_VARIABLE _exitcode) + if(NOT ${_exitcode} EQUAL 0) + message(WARNING "Reverting changes failed for Google Test. The build may fail.") + endif() + endif() endif() # ---[ FBGEMM diff --git a/cmake/GoogleTestPatch.cmake b/cmake/GoogleTestPatch.cmake new file mode 100644 index 000000000000..d03ed7c923e8 --- /dev/null +++ b/cmake/GoogleTestPatch.cmake @@ -0,0 +1,24 @@ +# CMake file to replace the string contents in Google Test and Google Mock +# Usage example: +# Patch the cmake file +# cmake -DFILENAME=internal_utils.cmake +# -DBACKUP=internal_utils.cmake.bak +# -DREVERT=0 +# -P GoogleTestPatch.cmake +# Revert the changes +# cmake -DFILENAME=internal_utils.cmake +# -DBACKUP=internal_utils.cmake.bak +# -DREVERT=1 +# -P GoogleTestPatch.cmake + + +if(REVERT) + file(READ ${BACKUP} content) + file(WRITE ${FILENAME} "${content}") + file(REMOVE ${BACKUP}) +else(REVERT) + file(READ ${FILENAME} content) + file(WRITE ${BACKUP} "${content}") + string(REGEX REPLACE "[-/]Z[iI]" "/Z7" content "${content}") + file(WRITE ${FILENAME} "${content}") +endif(REVERT)