Repository: conda/conda-build

Issue:
Idea: More robust detection of Visual Studio
I'm looking into the source code of `conda_build.windows.msvc_env_cmd()`:

  https://github.com/conda/conda-build/blob/master/conda_build/windows.py#L56

I've just implemented something similar, not realizing that such functionality already exists in `conda-build` (this is something worth mentioning in the documentation by the way). The only thing that concerns me a bit is `msvc_env_cmd()` assumes that Visual Studio is installed in Program Files rather than getting it from the registry. Perhaps you can refactor that by relying on `setuptools`:

``` python
from distutils.msvc9compiler import VERSION, PLAT_TO_VCVARS, get_platform
from setuptools.msvc9_support import find_vcvarsall

vc = find_vcvarsall(VERSION)
plat = PLAT_TO_VCVARS[get_platform()]

header = 'call "{vc}" {plat}".format(vc=vc, plat=plat)
```

This code gives you the full path to `vcvarsall.bat` matching the version of the current Python interpreter and its `plat` argument. The generated `header` can be inserted in `bld.bat`. It needs to be executed using `%PYTHON%` to produce correct results as it decides what version of Visual Studio to look for based on the version of the current interpreter.



Discussion:
One possible issue I see is that distutils doesn't support the Microsoft Visual C++ Compiler for Python 2.7.

`find_vcvarsall()` from `setuptools` does though:

https://github.com/jaraco/setuptools/blob/master/setuptools/msvc9_support.py#L29

:+1:

+1 we will look into this.  The other alternatives raised internally are to use the VS??COMNTOOLS environment variable, and/or the registry.

This was improved by @patricksnape in #861 

@msarahan Is this part of the latest release? I'm running `conda-build=1.20.1` and it's still looking for vcvarsall in Program Files:

```
C:\ANACONDA\conda-bld\work\zlib-1.2.8>call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
The system cannot find the path specified.
```

My VS 2015 is installed in a different folder. `setuptools.msvc9_support.find_vcvarsall()` returns the correct path.

This will be part of the coming release.  If you'd like to try a beta:

conda install -c conda-team/labels/dev conda-build

This will not fix things that are manually installed elsewhere - it just incorporates more of the different standard install locations (for the VS compiler for Python, for example)

This was actually my main concern, because Visual Studio can be installed virtually anywhere. Mine is at `C:\VS14` for example, so only the registry or the standard VS environment variables can tell us the right location.

Yeah, that's not fixed yet.  Sorry.  @groutr did some work on that.  I'd be happy to review a PR if you want to work on it.

This is something that needs fixing, I agree.  Unfortunately I can't get to it right now.

Here is one way to resolve it:

``` Python
def build_vcvarsall_vs_path(version):
    """
    Given the Visual Studio version, returns the default path to the
    Microsoft Visual Studio vcvarsall.bat file.
    Expected versions are of the form {9, 10, 12, 14}
    """
    return os.path.join(
        os.environ["VS{0}0COMNTOOLS".format(version)],
        '..\\..\\VC\\vcvarsall.bat'
    )
```

This replaces `build_vcvarsall_vs_path` from `1.20.2beta`

Seems fine.  I'll try to work this in.


Resolving patch:
diff --git a/conda_build/windows.py b/conda_build/windows.py
--- a/conda_build/windows.py
+++ b/conda_build/windows.py
@@ -66,7 +66,7 @@ def build_vcvarsall_vs_path(version):
     """
     Given the Visual Studio version, returns the default path to the
     Microsoft Visual Studio vcvarsall.bat file.
-    Expected versions are of the form {9, 10, 12, 14}
+    Expected versions are of the form {9.0, 10.0, 12.0, 14.0}
     """
     # Set up a load of paths that can be imported from the tests
     if 'ProgramFiles(x86)' in os.environ:
@@ -74,7 +74,9 @@ def build_vcvarsall_vs_path(version):
     else:
         PROGRAM_FILES_PATH = os.environ['ProgramFiles']
 
-    vstools = "VS{0}0COMNTOOLS".format(version)
+    flatversion = str(version).replace('.', '')
+    vstools = "VS{0}COMNTOOLS".format(flatversion)
+    
     if vstools in os.environ:
         return os.path.join(os.environ[vstools], '..\\..\\VC\\vcvarsall.bat')
     else:


Test patch: