Repository: pytorch/pytorch

Issue:
The path of *.pyd or *.so (created by cffi) is changed.
(old title) When build C extension, the error: FileNotFoundError was got.

- OS: Windows 10 pro
- PyTorch version: 0.4.0a0+0de5443
- How you installed PyTorch (conda, pip, source): source
- Python version: 3.6.4
- CUDA/cuDNN version: CUDA 9.0
- GCC version (if compiling from source): msvc 14/15 (then compiling with CUDA I use VS2015, but when build extension, the program automatically use vs2017) 

When building c extension on Windows, I got the error:
```
...
正在生成代码
已完成代码的生成
Traceback (most recent call last):
  File "build.py", line 28, in <module>
    ffi.build();
  File "C:\Users\qinka\Anaconda3\envs\torch_src\lib\site-packages\torch\utils\ffi\__init__.py", line 186, in build
    _build_extension(ffi, cffi_wrapper_name, target_dir, verbose)
  File "C:\Users\qinka\Anaconda3\envs\torch_src\lib\site-packages\torch\utils\ffi\__init__.py", line 111, in _build_extension
    os.path.join(target_dir, libname))
  File "C:\Users\qinka\Anaconda3\envs\torch_src\lib\shutil.py", line 241, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\qinka\Anaconda3\envs\torch_src\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\qinka\\AppData\\Local\\Temp\\tmp6mft329i\\dot_power._ext.dow_power._dow_power.pyd'
```
(The Chinese above means compiler success compile the library, and generated *.lib and *.exp)

And the same error was got on a Linux Work Station.

```
...
gcc -pthread -shared -B /home/qinka/anaconda3/envs/qinka_pytorch/compiler_compat -L/home/qinka/anaconda3/envs/qinka_pytorch/lib -Wl,-rpath=/home/qinka/anaconda3/envs/qinka_pytorch/lib -Wl,--no-as-needed -Wl,--sysroot=/ ./my_package/_ext/my_lib/_my_lib.o ./raid/qinka/pytorch-ext-demo/dot_power.pytorch/extension-ffi/package/my_package/src/my_lib.o ./raid/qinka/pytorch-ext-demo/dot_power.pytorch/extension-ffi/package/my_package/src/my_lib_cuda.o -o ./my_package/_ext/my_lib/my_package._ext.my_lib._my_lib.so
Traceback (most recent call last):
  File "build.py", line 32, in <module>
    ffi.build()
  File "/home/qinka/anaconda3/envs/qinka_pytorch/lib/python3.6/site-packages/torch/utils/ffi/__init__.py", line 167, in build
    _build_extension(ffi, cffi_wrapper_name, target_dir, verbose)
  File "/home/qinka/anaconda3/envs/qinka_pytorch/lib/python3.6/site-packages/torch/utils/ffi/__init__.py", line 105, in _build_extension
    os.path.join(target_dir, libname))
  File "/home/qinka/anaconda3/envs/qinka_pytorch/lib/python3.6/shutil.py", line 241, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/home/qinka/anaconda3/envs/qinka_pytorch/lib/python3.6/shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpv1yqw2m6/my_package._ext.my_lib._my_lib.so'
```
(gcc is 5.4.0)


Discussion:
In torch/utils/ffi/__init__.py `_build_extension()`,  `shutil.copy` copy the  linked file from `os.path.join(tmpdir,, libname)`, for example `C:\Users\qinka\AppData\Local\Temp\tmpwp84qchn\dot_power._ext.dow_power._dow_power.pyd`, but
in my Windows, and a Linux work station, the linked one is in:
```python
outprefix = cffi_wrapper_name.split('.')[0:-1] #
shutil.copy(os.path.join(tmpdir, *outprefix, libname), #
                  os.path.join(target_dir, libname))
```
,for example `C:\Users\qinka\AppData\Local\Temp\tmpwp84qchn\dot_power\_ext\dow_power\dot_power._ext.dow_power._dow_power.pyd`.

So, there must be a bug, or error in pytorch's ffi or python's ffi. (pyhton 3.6.4, cffi 1.11.4),


If it broke down because of change in cffi, I think I can create a PR.
I do think this is because of the change of cffi api,


CC @peterjc123 who enabled extension build for Windows on 02317d9336f544cce3a924169acb2ece5b9cb21d
@Qinka Is this change documented in somewhere like Python SDK?

Resolving patch:
diff --git a/torch/utils/ffi/__init__.py b/torch/utils/ffi/__init__.py
--- a/torch/utils/ffi/__init__.py
+++ b/torch/utils/ffi/__init__.py
@@ -106,9 +106,8 @@ def _build_extension(ffi, cffi_wrapper_name, target_dir, verbose):
         tmpdir = tempfile.mkdtemp()
         ext_suf = '.pyd' if os.sys.platform == 'win32' else '.so'
         libname = cffi_wrapper_name + ext_suf
-        ffi.compile(tmpdir=tmpdir, verbose=verbose, target=libname)
-        shutil.copy(os.path.join(tmpdir, libname),
-                    os.path.join(target_dir, libname))
+        outfile = ffi.compile(tmpdir=tmpdir, verbose=verbose, target=libname)
+        shutil.copy(outfile, os.path.join(target_dir, libname))
     finally:
         shutil.rmtree(tmpdir)
 


Test patch: