CMake run shell command after building error - visual-studio

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.

Related

How to build and run Xamarin.UWP application from command line?

How do I build and run a Xamarin.UWP application from the command line? I want it to be so that the app builds and runs the same way as the green run button in the Visual Studio 2019 GUI.
Additional notes:
I've tried opening the Visual Studio Developer Command Prompt and trying the following implementations.
msbuild -t:build "PATH_TO_PROJECT\SimTools.UWP.csproj"
Although when I run the executable that gets generated, it gives me this error:
as well as
Additionally, I've tried doing
msbuild -t:build "PATH_TO_PROJECT\SimTools.UWP.csproj" && msbuild -t:install "PATH_TO_PROJECT\SimTools.UWP.csproj"
And although it builds successfully, it says that there is no target for "install" and I do not know how to add that to the .csproj file as I've asked over at How do I add an "install" target to a Xamarin.UWP .csproj file?.

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.

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

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.

CMake Copying Multiple DLLs Failing

So I have seen the other posts regarding moving external dll files to the current project's .exe output location for use at runtime but I seem to be running into an odd issue that I can't find information on.
I am using the following custom command to copy my libfreenect2 dlls into my output directory for my project:
add_custom_command(TARGET kinect_feeds POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"libfreenect2_output_path/bin/*.dll"
$<TARGET_FILE_DIR:kinect_feeds>)
CMake sets up my project just fine, but when I go to run the command in Visual Studio it errors out when trying to copy the files. I think the issue is with the wildcard character. I used the error output in the Visual Studio to copy the complete command into by git bash window and it works as expected. Also Visual Studio has no problem moving multiple files if they are explicitly defined like so:
add_custom_command(TARGET kinect_feeds POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"libfreenect2_output_path/bin/freenect2.dll"
"libfreenect2_output_path/bin/glfw3.dll"
$<TARGET_FILE_DIR:kinect_feeds>)
My question is, do wildcard characters not work in CMake commands when being executed by Visual Studio or is there something that I am missing? For now I will just type out all of the DLL files explicitly, but I was hoping to avoid this.
I am using the latest version of CMake and Visual Studio 2015 Community Edition.
I'm running into the same issue with CMake 3.6.1 and Visual Studio 2012. I don't think Visual Studio has any impact though, because I get the errors from the command line as well
From a CMD prompt:
> cmake -E copy .\data\*.bin \temp
Error copying file ".\data\*.bin" to "\temp".
This question references a CMake bug report regarding wildcards, that was supposed to be fixed in CMake 3.5, but doesn't appear to work on Windows with CMake 3.6.1.
I think your solution to list each file individually is the current solution.

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