Why building project using msbuild is way slower than using visual studio IDE? - visual-studio

I'm trying to use the msbuild to build visual studio project using command line.
I used this commands
SET VCTargetsPath=C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120
msbuild.exe ../../../embedded/ports/visualC12/config-from-host.vcxproj
/p:Configuration=Release /p:Platform=Win32 /t:rebuild
Using IDE : it took around 2 min
Using cmdline: it took around 20 min
In command line it looks like it build a lot of projects that are not built in the IDE
Any suggestions?

In command line it looks like it build a lot of projects that are not built in the IDE Any suggestions?
That because you are using the property /t:rebuild in your command.
This switch performs the same function as the Rebuild Solution menu command within the integrated development environment (IDE)-will clean and then build the solution from scratch, ignoring anything it's done before. So MSBuild will build all projects regardless of whether them were built before or not.
When you build projects in IDE with build option ranther than Rebuild, it will perform an incremental build: if it doesn't think it needs to rebuild a project, it won't. It may also use partially-built bits of the project if they haven't changed. That is the reason for a lot of projects build in command line but are not built in the IDE.
To make the build faster, you can change the property to /t:build in command line or select rebuild option when you build in IDE.
Besides, there are many factors that affect the speed of building, for example, parallel. When we build multiple projects in IDE, the default value of parallel is 8, Tools->options->Projects and Solutions->Build and Run:
MSBuild command line is also support parallel, /maxcpucount Switch
msbuild.exe myproj.proj /maxcpucount:3
So when you compare the build speed between the command line and the IDE, you have to make sure that all the relevant settings are the same for command line and IDE.
Hope this helps.

Related

command line solution build using devenv rebuilding more projects than visual studio solution build

I am trying to mimic visual studio solution build from command prompt using batch script but there is significant difference between manual solution build(ctrl+shift+b) inside visual studio and command line solution build using devenv in terms of project rebuild counts. More projects are getting rebuilt from command line in comparison to visual studio solution build.
I am using this for command line build in batch file:
call"C:\ProgramFiles(x86)\MicrosoftVisualStudio\2019\Professional\VC\Auxiliary\Build\vcvars32.bat"
devenv solution_name.sln /build "Debug"
command line solution build output looks like this: devenv solution build using cmd prompt
visual studio solution build output looks like this: manual vs solution build output
I have tried changing the configurations too but it didn't help, I am curious why this might be happening and is there a way to get same result from command line build as of manual vs solution build?

CMake run shell command after building error

I have a Visual Studio 2019 CMake project and i need to run a postbuild script which copys a file (The file is not generated by the build process). What i did so far is add a custom command in CMake with
add_custom_command(TARGET testExec POST_BUILD COMMAND "../postbuild.bat")
The postbuild.bat would copy the file. This works great most of the time but when my build fails due to some compile error, the postbuild script won't be executed.
How can i run the postbuild script even if the build fails ? I know there is a similar question here but sadly no proper solution. If there is a way to configure a postbuild event directly inside a Visual Studio CMake project this would also be suitable, but it seems like this is not possible (because in a cmake project i don't have a project file).
Since The copied file is not generated by the build you can use PRE_BUILD. On Visual Studio Generators, it runs before any other rules are executed within the target.(https://cmake.org/cmake/help/latest/command/add_custom_command.html).
The other solution could be to use add_custom_command(OUTPUT as the file seems independent of the build.

Select MSVS build configuration when generating a buildsystem with CMake

I have a multiplatform CMake project, and occasionally I have to build it manually for Windows. I generate a buildsystem like this:
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" -A x64 ../path/to/source
Then I open *sln file and press F7 to build. It runs for 40 minutes, and after that I understand that I didn't select proper configuration in the combobox. It's annoying! When command line option was -DCMAKE_BUILD_TYPE=Release, but combobox was set to Debug, build fails after spending a decent time.
Is it possible to generate an MSVS project with build configuration selected from command line?
Note that I'm aware of msbuild command and it's -p:Configuration=xxxxx flag. The question is about cases when for some reason you need to build from Visual Studio's GUI.
Changing the selected configuration for the GUI is not possible with CMake at this moment.
The main reason for this is that this information is stored in the .suo file generated by Visual Studio. CMake itself only generates the project and solution files, while the .suo file will be generated by Visual Studio itself.
Alternatively, use CMake's command line build option for this. After configuring your project and generating the VS .sln file from CMake as usual, simply run:
cmake --build <path_to_build_directory> --config Release
This works independently of the selected generator and is the most reliable way of building CMake projects.

Configuration file differences between building with Visual Studio and the MSBuild command-line tool

I have a solution that contains a project with AutoGenerateBindingRedirects set to "true". When I build it via Visual Studio 2013, the .config file in the output directory contains an generated assembly binding redirect for EntityFramework, and the project runs. However, on the build server, which calls MSBuild, this property is not followed, which causes the project to fail to start. Does anyone have any idea on why there might be differences in the build results between the two methods?
For reference, the build server is executing a command like
MSBuild MySolution.sln /p:Configuration=Release,DefineConstants="SOMETHING" /t:Rebuild /tv:4.0
I get the same results when invoking this on my development machine, too, so it seems to be a peculiarity with MSBuild and/or Visual Studio. I've tried variations like
MSBuild MySolution.sln /p:Configuration=Release,DefineConstants="SOMETHING",AutoGenerateBindingRedirects=true /t:Rebuild /tv:4.0
to no avail.

Under vs10 msbuild.exe how can a specific project within a solution.sln be Ignored?

Under vs10 msbuild.exe how can a specific project within a solution.sln be Ignored?
I am building a large project that is moving to Visual Studio 10.0. In the IDE I can uncheck a project within the configuration manager and it will not build it. I need to mimic that behavior with a command line call to msbuild.exe.
The project I want to bypass is an *.dll plugin with a link error. I am stuck on stupid with the link error at the moment and since it stands alone, I can run the main program with out it and just live with a warning at run time that it isn't present.
I need some switch magic concerning calls to msbuild.exe.
If you have a certain configuration in the sln (configured in VS Configuration Manager) that you want to build with MSBuild, you can do so with the following command line:
msbuild /p:Configuration=MyConfiguration MySolution.sln

Resources