欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Visual Studio 2017中的编译器工具布局

程序员文章站 2022-03-21 21:22:55
...

写在前面

  • 在工作中,偶尔会用到VC编译器。目前,最新版的VS已发布到了2017。其中,微软对于VS的目录结构进行了调整。
  • 该文翻译自微软官方博文 Compiler Tools Layout in Visual Studio 2017。水平有限,仅供参考!后面也附录了原文。
  • 微软官网也有个翻译版本,但是是机器翻译的,错误有点多!
  • 部分注释为译者添加。
  • 需要注意的是,这篇文章是早期针对VS 2017 Preview 4的。目前,正式版的VS 2017可能有变化。

  这篇文章由 Andrew Pardoe、Mark Levine 和 Iyyappa Murugandi 撰写。
  感谢您的反馈意见! 根据您的反馈,我们已对布局进行了一些更改。
  2016年12月8日更新查找VC安装的位置:一些人问如何在开发人员的机器上找到Visual C的实例。现在,已经没有可以供查询的注册表值。如果可以启动VS命令提示符,则会发现%VCInstallDir%环境变量指向VC安装的根目录。如果您需要以编程方式查询安装,则需要查询COM以查找Visual Studio和VC的实例。希思·斯图尔特用很多语言的示例代码写了一篇关于这个主题的优秀博客文章
  只要一加载第一个项目,你就会在Visual Studio 2017中看到许多针对C开发人员的改进。但有些产品中的变化并不是那么明显。其中一个特殊的变化可能让你感到惊讶:我们已经改变了MSVC 编译器工具集在磁盘上的位置。

为什么我们要移动编译器工具集

  VS2015中编译工具在磁盘上的布局反映出了我们近几年来得一些决定。在2002年,我们发布 Visual Studio .NET 时,我们发布了仅支持x86架构的编译器。x64以及itanium架构在近期的关于Visual studio .Net的开发中才被提及。 编译器在磁盘上的布局仅仅适用于只会使用x86编译的情况。

注意:
  1. itanium架构目前英特尔好像已经放弃!

  当我们引入对Itanium和x64体系结构的支持时,我们在编译器的合理位置添加了它:bin目录中。由于在bin目录中,已经存在一个x86编译器,所以我们相应的添加了子目录x86_ia64x86_amd64。 这些子目录的编译器可以运行在x86平台并生成64位平台代码。 还有amd64目录下的编译器,运行在x64平台下,用来生成x64平台的代码。
  不过,事情远不止于此。后续,我们又增加了针对ARM平台的编译器。接着,有添加了在X64平台上运行的针对,x86和ARM平台的编译器。最终,形成了一个负责的树形目录结构。每当开发人员想要编写与我们的编译器有关的自动化时,他们都必须在其x86平台的脚本中对编写特殊情况。

编译器工具集转移到VS 2017后的位置

  首先,我们先讨论编译器工具集的主要部分。.它们分布在最上层目录,例如:%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\VC. (注意这个改变的目录(也就是%VCINSTALLDIR%)之前在VS2015中的目录是 %ProgramFiles(x86)%\Microsoft Visual Studio 14\VC.)

  • Tool: 这个目录就是一般认为的MSVC编译器的工具集目录。包含早期的bincrtincludelib这几个目录及这些目录之中的一些二进制文件。这些二进制元文件包括cl.exelink.exe,还有相应的头文件以及VC++运行时需要的库文件。
  • Redist: 可再发行组件目录。可由最终开发人员使用构建的应用程序重新分发的文件。CRT redist就位于此目录下,其他运行时例如AMPOpenMP也包含在里面。
  • Auxiliary: 这个目录包含了辅助编译工作但不是必须的一些工具和脚本。比如配置开发者环境的vcvars*.bat脚本、CppCoreCheckersUnit Test libraries

  请注意,目前VS2017预览版5中的编译器工具集布局就是个预览版。 根据来自内部和外部开发人员的反馈和要求,我们最终可能会更改布局甚至*%VCINSTALLDIR%目录。

注意:
  目录如下图:
Visual Studio 2017中的编译器工具布局

Tool目录

  让我们进一步看一下Tools目录——该目录包含编译器工具集。 和VS2015安装目录不同, 微软Visual C++编译器所在的目录命名为MSVC。如果在安装时你选择了Clang with Microsoft codegen选项并安装。 你将会看到MSVC目录后面紧接着一个叫做clangC2的目录, 它里面包含了Clang/C2的二进制文件.。

原文

This post was written by Andrew Pardoe, Mark Levine, and Iyyappa Murugandi.

Thank you for your feedback! We’ve made some changes to the layouts based on your feedback.

Update Dec 8 2016 Finding the location of a VC++ install: Some folks have asked about how to find instances of Visual C++ on a developer’s machine. There’s no longer a registry key you can query. If you can launch a VS command prompt, you’ll find that the %VCInstallDir% environment variable points to the root of the VC++ install. If you need to query the install programmatically, you’ll need to query COM to find instances of Visual Studio and VC++. Heath Stewart has written a great blog post on this topic with sample code in many languages.

You’ll see many improvements for C++ developers in Visual Studio 2017 as soon as you load your first project. But some of the changes in the product aren’t so obvious. One in particular might surprise you: we’ve moved where the MSVC compiler toolset is laid out on disk.

Why we moved the compiler toolset

The layout of the compiler tools on the disk in VS 2015 reflects decisions made years ago. When we shipped Visual Studio .NET in 2002, we shipped compilers that targeted only the x86 architecture. The x64 and Itanium architectures had only recently been announced when development of Visual Studio .NET started. The layout of the compiler on-disk reflected a world where x86 was the only compiler you would need.

When we introduced support for the Itanium and x64 architectures we added the compilers where it made sense: in the bin directory. We already had a compiler for x86 in the bin directory, so we added subdirectories x86_ia64 and x86_amd64 for compilers that run on x86 and target 64-bit platforms as well as an amd64 directory for compilers that run on x64.

Unfortunately, it didn’t stop there. We added compilers for ARM. And we added cross-targeting compilers that are hosted on x64 and target x86 and ARM. Eventually we ended up with a directory tree that made little sense. Every time a developer wants to write automation that deals with our compilers they have to write special cases in their scripts for the x86 hosted and targeting compiler. Moreover, this layout limits our ability to do interesting things such as having two versions of a compiler or two different compilers installed side-by-side on the one build machine.

Where the compiler toolset moved to in VS 2017

First, let’s discuss the major parts of the compiler toolset. These are reflected in the top-level directory, e.g., %ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\VC. (Note that this directory, %VCINSTALLDIR%, changed from its VS 2015 location of %ProgramFiles(x86)%\Microsoft Visual Studio 14\VC.)

  • Tools: The tools are what you traditionally think of as the MSVC compiler toolset. This includes the old bin, crt, include, and lib directories and the binaries that are placed in them. These binaries include cl.exe and link.exe as well as header files and link libraries for the Visual C++ runtime.
  • Redist: The redistributable directory contains files that can be redistributed by the end developer with the built application. The CRT redistributables are located here, as well as the other runtimes such as those for AMP and OpenMP.
  • Auxiliary: The auxiliary directory contains tools and scripts that are not part of the compilation process but are needed to help with various compilation scenarios. These include files like the CppCoreCheckers or the Unit Test libraries as well as the vcvars*.bat scripts used to configure developer environments.

Note that the compiler toolset layout in Preview 5 is just that–a preview. We may end up changing the layout or even the top-level %VCINSTALLDIR% directory depending on feedback and requirements from internal and external developers.

The tools directory

Let’s take a closer look at the Tools directory–the directory contains the compiler toolsets. Unlike in VS 2015, the Microsoft Visual C++ compiler is in a directory called MSVC. If you installed the “Clang with Microsoft Codegen” option, you’ll see a directory next to MSVC called ClangC2 that contains the Clang/C2 binaries.
Visual Studio 2017中的编译器工具布局
There’s a subdirectory in the MSVC directory with a compiler version number. For VS 2017 RC, that version number is 14.10.24629. In that directory are the familiar subdirectories from the %VCINSTALLDIR% directory in VS 2015: bin, crt, include, and lib.

We’ve encountered two big differences so far. First, the MSVC directory means that the Visual C++ tools can sit beside another toolset, in this instance, ClangC2. Second, the fact that the tools are versioned inside of the MSVC directory means that we can have multiple versions of the Visual C++ compiler installed on the same build machine. Right now, there’s no simple way to install multiple versions. But we’ve designed the directory structure to make it possible in the future to easily switch between versions of the Visual C++ tools.

Hosts and targets

We find even more changes when looking inside the 14.10.24629\bin directory. While in VS 2015 the x86 toolset was the “standard” toolset and we had directories with names like amd64_arm representing the AMD64-ARM cross-targeting compiler, in VS 2017 these have been split up into directories labelled HostXXX and a subdirectory called x86 or x64. What’s the distinction here?
LetVisual Studio 2017中的编译器工具布局
Let’s pause to define a couple of terms. “Host” refers to the platform that the compiler toolset runs on. “Target” refers to the platform that the compiler builds applications to run on. In the VS 2015 world, amd64_arm included the compilers that were hosted on x64, and targeted ARM. In VS 2017 it’s more structured: all of the x64 hosted compilers live in a directory called HostX64. In that directory we find x86, x64, etc., indicating the target architecture.

The bin directory is the only one with a set of HostXXX directories. This is because the executables need to run on the host. The include directory, which contains only source files, is specific to neither the host nor the target. The lib directory is specific to only the target architecture. Likewise, many of the tools in the Auxiliary directory are specific to the target architecture but not to the host.

Benefits of the new layout

We hope you can see that there are a lot of benefits to refreshing our compiler toolset layout on disk. As Visual C++ grows to include more scenarios like Android and iOS targeting we need to include more compiler toolsets. And as we release our tools more frequently we increase the chance that our developers might want to have multiple versions of the Visual C++ compiler tools installed side-by-side. This new layout is flexible and extensible and should (hopefully!) serve us for many years to come.

As in VS 2015, the Windows SDKs are not installed alongside the Visual C++ binaries, headers and libs. They are installing in the same location under the Program Files(x86) directory. Visual C++ SDKs (such as the DIA SDK) will continue to be installed alongside the VC++ toolset.

Drawbacks of the new layout

Even though change can be good, it’s still change. We know that a lot of people have scripts that are going to break with this layout change. We decided to do this with VS 2017 as we also had to change the top-level directory to accommodate restrictions with the new installer. But any change can be a breaking change.

Please, please, please check out the scripts that you use and make sure you can migrate them to use the new layout. We want to know if you run into any problems. We want to know how hard the migration experience was for your code. Please leave comments on this blog post or send mail directly to our team at aaa@qq.com

Finding the default MSVC tools

One last note: a lot of build systems need to find the default version of the Visual C++ tools. When you install Visual Studio it creates a file, %VCINSTALLDIR%\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt, that contains the version string for the default toolset installed with VS. In a typical Preview 5 installation, %VCINSTALLDIR% would point to %Program Files(x86)%\Microsoft Visual Studio\Visual Studio 15.0\Common7\IDE\VisualCpp.
You can parse this file with one extra line and variable in your command script:

@rem set BINDIR=get directory for x86-hosted, x86-targeting binaries
set /P VCTOOLS_VERSION=<"%VCINSTALLDIR%\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"
set BINDIR=%VCINSTALLDIR%\Tools\MSVC\%VCTOOLS_VERSION%\bin\HostX86\x86

Feedback

You’ll see a number of benefits from the new layout of the compiler toolset but we know that a lot of you will have to fix up scripts that you might not have looked at for years. We’re working on making this experience simple and smooth for everyone so please do reach out to us with details of your experience. If you have feedback on any part of this design please let us know in the comments below or by mailing aaa@qq.com Thank you!

相关标签: VS2017 VC