Use PYTORCH_PYTHON to call generate_code.py (#10171)

Summary:
Probably fixes https://github.com/pytorch/pytorch/issues/8373#issuecomment-409994847
Pull Request resolved: https://github.com/pytorch/pytorch/pull/10171

Differential Revision: D9135607

Pulled By: SsnL

fbshipit-source-id: 72f535875658c857621e41fd25c2174052714557
diff --git a/torch/CMakeLists.txt b/torch/CMakeLists.txt
index d64073d..b41b789 100644
--- a/torch/CMakeLists.txt
+++ b/torch/CMakeLists.txt
@@ -46,6 +46,13 @@
   list(APPEND CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
 endif()
 
+# Get the correct Python executable
+if (DEFINED ENV{PYTORCH_PYTHON})
+  message(STATUS "Using python found in $ENV{PYTORCH_PYTHON}")
+  set(PYCMD "$ENV{PYTORCH_PYTHON}")
+else()
+  SET(PYCMD "python")
+endif()
 
 # Generate files
 set(TOOLS_PATH "${TORCH_SRC_DIR}/../tools")
@@ -79,7 +86,7 @@
   "${TORCH_SRC_DIR}/csrc/jit/generated/register_aten_ops.cpp"
   "${TORCH_SRC_DIR}/csrc/jit/generated/aten_interned_strings.h"
   COMMAND
-  python tools/setup_helpers/generate_code.py
+  ${PYCMD} tools/setup_helpers/generate_code.py
     --declarations-path "${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
     --nn-path "aten/src/"
   DEPENDS