Repository: mesonbuild/meson

Issue:
Visual Studio REGEN target fails when using `vs` backend
The version auto-detection does not work when building the `REGEN` target in Visual Studio, if meson was initially invoked with `--backend=vs`.

```
1>------ Build started: Project: REGEN, Configuration: debug Win32 ------
1>Checking whether solution needs to be regenerated.
1>The Meson build system
1>Version: 0.45.0.dev1
1>Source dir: C:\msys64\home\Polarina\mysa
1>Build dir: C:\msys64\home\Polarina\mysa\build
1>Build type: native build
1>Traceback (most recent call last):
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\mesonmain.py", line 365, in run
1>    app.generate()
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\mesonmain.py", line 151, in generate
1>    self._generate(env)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\mesonmain.py", line 174, in _generate
1>    g = vs2010backend.autodetect_vs_version(b)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\backend\vs2010backend.py", line 38, in autodetect_vs_version
1>    if vs_version == '14.0' or 'Visual Studio 14' in vs_install_dir:
1>TypeError: argument of type 'NoneType' is not iterable
1>Traceback (most recent call last):
1>  File "C:\Users\Polarina\AppData\Local\Programs\Python\Python36-32\Scripts\meson.py", line 26, in <module>
1>    sys.exit(main())
1>  File "C:\Users\Polarina\AppData\Local\Programs\Python\Python36-32\Scripts\meson.py", line 23, in main
1>    return mesonmain.run(sys.argv[1:], launcher)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\mesonmain.py", line 322, in run
1>    sys.exit(run_script_command(args[1:]))
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\mesonmain.py", line 278, in run_script_command
1>    return cmdfunc(cmdargs)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\scripts\regen_checker.py", line 55, in run
1>    regen(regeninfo, mesonscript, backend)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\site-packages\mesonbuild\scripts\regen_checker.py", line 41, in regen
1>    subprocess.check_call(cmd)
1>  File "c:\users\polarina\appdata\local\programs\python\python36-32\lib\subprocess.py", line 291, in check_call
1>    raise CalledProcessError(retcode, cmd)
1>subprocess.CalledProcessError: Command '['c:\\users\\polarina\\appdata\\local\\programs\\python\\python36-32\\python.exe', 'C:\\Users\\Polarina\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\meson.py', '--internal', 'regenerate', 'C:\\msys64\\home\\Polarina\\mysa\\build', 'C:\\msys64\\home\\Polarina\\mysa', '--backend=vs']' returned non-zero exit status 1.
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(171,5): error MSB6006: "cmd.exe" exited with code 1.
1>Done building project "REGEN.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
```


Discussion:

Resolving patch:
diff --git a/mesonbuild/backend/vs2010backend.py b/mesonbuild/backend/vs2010backend.py
--- a/mesonbuild/backend/vs2010backend.py
+++ b/mesonbuild/backend/vs2010backend.py
@@ -29,10 +29,9 @@
 def autodetect_vs_version(build):
     vs_version = os.getenv('VisualStudioVersion', None)
     vs_install_dir = os.getenv('VSINSTALLDIR', None)
-    if not vs_version and not vs_install_dir:
-        raise MesonException('Could not detect Visual Studio: VisualStudioVersion and VSINSTALLDIR are unset!\n'
-                             'Are we inside a Visual Studio build environment? '
-                             'You can also try specifying the exact backend to use.')
+    if not vs_install_dir:
+        raise MesonException('Could not detect Visual Studio: Environment variable VSINSTALLDIR is not set!\n'
+                             'Are you running meson from the Visual Studio Developer Command Prompt?')
     # VisualStudioVersion is set since Visual Studio 12.0, but sometimes
     # vcvarsall.bat doesn't set it, so also use VSINSTALLDIR
     if vs_version == '14.0' or 'Visual Studio 14' in vs_install_dir:
diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py
--- a/mesonbuild/mesonmain.py
+++ b/mesonbuild/mesonmain.py
@@ -172,6 +172,7 @@ def _generate(self, env):
         elif self.options.backend == 'vs':
             from .backend import vs2010backend
             g = vs2010backend.autodetect_vs_version(b)
+            env.coredata.set_builtin_option('backend', g.name)
             mlog.log('Auto detected Visual Studio backend:', mlog.bold(g.name))
         elif self.options.backend == 'vs2010':
             from .backend import vs2010backend


Test patch: