Hook git command into visual studio pre build step - c++

I'm using git as version control and I'm trying to hook the SHA1 of the current commit into a visual studio build as a define.
So basically I want to turn the output of the command
git rev-parse HEAD
into a define that I can embed in the output binary.
Thanks in advance.

Frankly, I prefer a pre-build events in Visual Studio over Git post-commit. There is additional benefit of knowing if you building from a dirty repository or not. Did the write-up on that today. Here are the general steps from build to execution:
The build process has a pre-build script, which gathers version and repository status information. In our case, VC++ uses the Windows PowerShell version_info.ps1 script.
The pre-build script generates a source code file, which is expected by the rest of the codebase. The generated file has everything needed to allow or deny running the version. In our case it is a C++ header file version.h.
A code contains function which checks if the build is legitimate to run, and logs and stops the process as needed. That example code is in the versionLogAndVet function of the version.cpp example file.
At run time, the versionLogAndVet function allows to run only permitted combinations of repository status / build configuration. It also logs version information.
The specifics and code examples are at the full post.

Using a post-commit githook, you can put it into a file. I think git on windows comes with a bourne shell, so the following would work in a post-commit hook.
#!/bin/sh
GIT_SHA_INCLUDE_FILE=foo.h
echo -n '#define GIT_SHA ' > $GIT_SHA_INCLUDE_FILE
git rev-parse HEAD >> $GIT_SHA_INCLUDE_FILE
And save that as .git/hooks/post-commit
The file should remain untracked since it's (nearly) impossible to include the hash of the commit within the commit itself.

Related

How to recompile a single .cc file in a project built previously with CMake tool in Ubuntu 20.04?

I am using the ORB_SLAM3 project (https://github.com/UZ-SLAMLab/ORB_SLAM3) as a baseline for a monocular odometry system.
To understand how the ORB_SLAM3 software ingests the EuRoCV dataset, I am modifying some of the initial codes in the mono_euroc.cc file available in /Examples/Monocular folder.
However, each time I change the .cc file, I cannot compile just the mono_euroc.cc file by itself, but need to run the ./build.sh command from the parent directory which executes the entire CMake. The process which takes a while to complete.
My question is, is there a tool within CMake that would allow me to only change the "mono_euroc.cc" file directly from the "/Examples/Monocular" subdirectory rather than having to constantly invoke the "./build.sh" from the parent directory?
For the time being, I am following this process. I opened two terminal windows, both pointing to the parent directory (i.e ~/Dev/ORB_SLAM3). Everytime I change something in the target file (here it is the ./Examples/Monocular/euroc_mono) I execture the ./build.sh command in one and run the file on the other. I can confirm that though the cmake command looks over all the files, it only builds the one that was changed. I guess this method works when one is using the CMake tool to build a C++ project in Linux.

How do I automatically make after cmake finished generating the files

I am currently trying to set up an "automatic build process" on mac for my C++ "Hello World Project" that uses Cmake & visual studio code.
My current workflow to build in terminal or with vscode's tasks.json is the following, and I find them very tedious to do.
cmake ... (to generate the cmake & make files inside the build folder)
make (inside the build folder to create the application's executable)
./{application's executable}
Thus, I have have looked into the following
vs code's prelaunch task
cmake's add_custom_commands
custom bash script
python scripts
But I'm a bit lost with the above try-outs, and need some helps. My end goal is to automatically build and run the updated code by either pressing F5 in vscode or calling a custom ./{command} in terminal.
Combining everyone's suggestions, the approach I took is to create a task.json that contains the following command-flow
cmd1: cmake to generate the makefiles & cmake files to a build folder
cmd2: make -j 8 to build with multiple cores
cmd1 && cmd2
Then I made two different launches in launch.json. The major difference between them is the prelauch task. One of them is "cmd1&&cmd2", and another one is only "cmd2". That way I can have two hotkeys like F5 and F7 for me to choose. If the folder structure & files weren't added or removed I will hit F7 which only calls the "cmd2" pre-launch task, otherwise F5.
I think a better approach is to either write a bash script or dig deeper in cmakelist.txt which I believe it has the "if statements" that determine when should it do "cmd1 &&cmd2" or just "cmd2" alone. That way, I don't need two hotkeys to build/debug and run in vscode (don't know if it is possible, I haven't try yet.)
If anyone has done it, please comment below or create a new answer.
Thank you all!
You can run
cmake --build
after the first time you configured the build. Optional parameters include the build directory and targets to build.

Run Borland turbo c++ 10 IDE project from command line

I'm trying to integrate a legacy borland turbo c++ project into jenkins task and I need to be able to compile the project from command line.
Is there any way to get the compiler CLI information from the project so that I could make a batch file that compiles it?
SO: Windows 7
You can get the commands being invoked by C++ Builder by exporting a makefile for the project.
Under menu goto Project->Export a Makefile.
Once exported open a cmd.exe shell to the generated makefile's location.
Run the makefile with borland's make.exe tool:
make -B -K -n -f"projectMakefileGoesHere"
-B will traverse all the dependencies ignoring age.
-K will keep any temp response files used during the build.
-n do a dry-run printing the commands that would've been called in an actual build.
The link commands will be in the MAKE0xxx.### response file where "x" is a number assigned by make.
For later versions of C++ Builder, the .cbproj project is actually a msbuild project file. You can use msbuild to print the build commands used for the project. eg.
msbuild "project.cbproj" -p:Configuration=Debug -clp:ShowCommandLine -v:n
Unfortunately, msbuild doesn't have a dry-run option so it'll end up building the project. Another idea is to create a simple logging program that replaces bcc32.exe compiler and ilink32.exe linker. With this, you can see exactly what options and switches are being passed to the tools.

Replacement for CMake

I'm using CMake into build rules in visual studio to preprocess my files before submitting them to the compiler but my customers don't like it.
Is there any CMake alternative or method to preprocess my files and having them integrated into visual studio?
As I understand the question you are using CMake to generate files consumed by a VS project that you created by hand. For whatever reason your customers of your VS project don't like CMake. Presumably you have wired this up by creating custom build steps that invoke CMake to generate the files in your project, either as Build Events on the project, or as custom build steps on various files in the project, such as the inputs to the scripts.
VS lets you run any arbitrary set of commands as part of a Build Event or a Custom Build Step. So you could replace CMake with a suitable script or custom executable written by yourself. It is hard to be specific without knowing exactly what about CMake isn't appealing to your customers.
You can use JavaScript through Windows Script Host to create fairly complex scripts that process custom build steps. They can take command-line arguments, access the file system, etc. For some reason WSH JavaScript is a big secret among most developers and they think that all they have available to them in Windows out-of-the-box are DOS batch files and power shell scripts. JavaScript has been shipping for over 10 years as part of the OS and you can debug the scripts in Visual Studio's script debugger, which is very nice.

TFS not clearing build agent folders after migration to TFS2010

I am having an issue with TFS. When we had TFS2008, the build machine was able to clear files from the Build Agent Folders before creating a new build. However, after the migration to TFS2010, the build machine cannot clear this folder and we are getting builds with old files that have been deleted from source control.
Is there any way to get this functionality back? We are currently working with the TFS2008 build scripts and the UpgradeTemplate.xaml in TFS2010.
Thanks
In your TFS Build Definition, what is the "Clean Workspace" on the "Process" tab set to?
It has three options:
All
Outputs
None
An explanation of each options (taken from TFS):
Set to All to delete all existing outputs and sources and do a full
rebuild; Outputs to delete all existing outputs but get only those
source files that have changed since the last build (Incremental Get);
or None to leave existing outputs and sources in place and build any
changes incrementally.
You should set this to All, to ensure you are performing a clean build each time.
The only other post I found didn't have an answer. So instead, I reverted back to running a RMDIR command at the BeforeEndToEndIteration level of the build script.
<Target Name="BeforeEndToEndIteration">
<Exec WorkingDirectory="S:\src" Command="RMDIR /s /q "S:\src\Sandbox_awdbu\""/>
</Target>
This command will delete the build agent folder before the Get Latest command is performed by the build service.
It's not a great solution but it works. This solution will work but I would suggest moving onto the template instead of keeping the old TFS2008 build scripts.