How to make MSBuild print output from a custom tool? - visual-studio

I have a project, which use a .rules file to build some non-c++ sources. How to make MSBuild print diagnostic output from this tool? -- Precisely cerr stream.
(I silently presume, the M$ team have not reached that level of insanity, to give the custom building tools functionality without possibility to print errors from them.)

I've actually noticed you marked question with label visual-studio-2008. I guess it means your custom build tool processes non cpp files in vc2008 project? If so, I think it is not built by msbuild but with vcbuild.
In this case try to add echo on in front of command executed by custom tool.
Vcbuild creates batch files in %temp% that first line contains #echo off and then it is executed. So if you add echo on you should see complete output in output window including command line used for command execution.

Related

C++ transform T4 template ignore output file

I'm using TextTransform.exe to generate multiple C++ files. Since the tool is not supported directly within Visual Studio for C++ projects I call it by command line (inspired by T4 Generating C++ Code).
In order to generate multiple files I use https://github.com/areve/Entity-Framework-T4-Templates/blob/master/src/dev/MultiOutput.tt that's why I don't need the standard output which is normal generated by the tool.
I call TextTransform.exe like:
"C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0\TextTransform.exe"
-out "<what to put here that NO file is generated?>"
C:\Test.tt
I'm using Microsoft Windows. Maybe there is a "hack" to provide any kind of special char which would be accepted by the program but it wouldn't be possible to actually create a file out of it.
Is there a possibility to provide any command which generates NO file when I execute this command?
Update
As mentioned by #ImprobabilityCast using NUL is a way to go. It's not producing any file but the custom build where I run the tt file with is failing with the message:
Performing Custom Build Tools
CUSTOMBUILD : error : FileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path.
I reach what I want but it's not so "nice" that the build action is failing.
Not sure why you don't want the files, but...
In linux we have a wonderful thing called /dev/null that is essentially an empty void just for things like this. I did a quick search, and Windows has it's own equivilent: NUL
Thus, the command you want is:
"C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0\
TextTransform.exe" -out NUL C:\Test.tt
No. The way text transformation was built was only thought to produce a single output file. Multi output was a logical evolution for T4 templates but Microsoft has not evolved it for years now.
The code that you're using (as am I) is basically a hack around that. It uses a very ugly way of using the EnvDTE to manipulate the project system that will probably end up not working one of these days when MS decides to rewrite that system (and one could argue that day is coming).
T4-editor, for example, has a slightly different way of achieving the same thing but you can see that the output still produces the "dummy file":
http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html
I've found a satisfing solution for my problem. Since Microsoft Visual Studio allows for custom build tools to enter multiple lines I realized that I can delete the file generated by the TextTransform.exe I don't need.
So the command I put into "Command Line" contains two lines now:
"C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0\TextTransform.exe" -out "%(DefiningProjectDirectory)$(TempOutputFile)" C:\Test.tt
DEL /F "%(DefiningProjectDirectory)$(TempOutputFile)"
The first line is the actual TextTransform call which produces me all the files I want including the output file I don't need but can't stop to be created.
The second line just deletes me the file I don't need.
This command expects a project variable calles "TempOutputFile". In this way I skip any typo's. For example:
<PropertyGroup Label="Globals">
<TempOutputFile>DoNotCheckin.h</TempOutputFile>
</PropertyGroup>

How to call a custom build tool through a batch file

While trying to setup the Cap'n Proto compiler as a custom build tool in Visual Studio 2017, I came across a curious behavior, it only seems to work when called directly and not through a batch file.
What I first tried was, for each .capnp file, set the following Custom Build Tool settings:
Command line: "$(SolutionDir)run_capnpn.bat" compile -oc++ "%(FullPath)"
Description: Executing capnp.exe on %(Identity)...
Outputs: %(Identity).c++;%(Identity).h
I made the batch file because I wanted to avoid polluting my %PATH% with the capnp folder, this is what it contains:
#echo on
echo %*
SET PATH=%PATH%;C:\GitHub\capnproto\bin\c++\src\capnp\Debug
start /wait "" capnp.exe %*
exit /b %errorlevel%
However, with this setup the Custom Build Tool was only called on 1 of the 5 capnp files in my solution (all 5 files had exactly the same settings). I know this because only one pair of generated files appeared and only one message appeared in my build log.
Even weirder, if I compiled again it would do the next file, and on the following compile it would do another file. In all, it would take 5 compiles (one per file) before it considered everything to be fully built and stop calling the custom build tool.
After much trial and error and some help from other programmers on Discord, I tried adding capnp.exe to my path and call it directly (instead of going through the batch file) so for each capnp fil I changed the command line setting to:
capnp.exe compile -oc++ "%(FullPath)"
and now it all builds correctly. Is it possible to call a custom build tool through a batch file? and if so how?

debugging with visual studio using redirected standard input

I am debugging c++ console application with Visual studio. I exhausted of inserting the same input every time I debug this program. I would like to use the same input more times.
I do this without debugging in command line with command: Program.exe < 1.in
Is it possible to use debugging with standard input redirected from file???
I already tried looking in to procejt properties. I tried setting Command to $(TargetPath) < 1.in instead of $(TargetPath).
I also tried setting Command Arguments to < 1.in. Niether of these method worked.
I am using Visual Studio 2012. But this is probably same in all versions of studio.
This is a supported debugging scenario. You do have to make sure that the debugger can find the file. Leave the Command setting at $(TargetPath). A possible value for the Command Arguments setting is:
< "$(ProjectDir)test.txt"
if the input file "test.txt" is located in the project directory. Or type the full path of the file to be sure. The MSDN article that describes this feature is available here.
I just create a file called stdin.txt in the project
1) set the Build Action to Content
2) Copy to Ouput Directory: Copy if newer
Then when you build stdin.txt is copied to the same folder as the executable.
Then in project properties debug|command line arguements enter the following
< stdin.txt
There is no need to use a path macro
If you don't want to mess with the the path you can add a new file with a right click on the source files folder in the solution explorer and then paste to it the content from the wanted file. And then change the command argument to the new file name.

Adding Preprocessor directive dynamically from commandline build VS2008

I am using VS2008, and developing C/C++ projects. I am using .bat file to build my projects from commandline (VC2k8 command prompt). I need a way to include preprossor directive dynamically at build time.
I am using devenv to build from command line.
>devenv my\project\path\myproject.sln /build release > logs\build.log
Actually I want to set a macro definition based on a command line parameter to the batch file. I can keep two different .vcproj files, but that gives problem in keeping multiple project/sln files.
My batch file would something like this...
if (condition)
#define MYPROC_ENABLE_MYMODULE "yes" // To be included in the project.
else
#define MYPROC_ENABLE_MYMODULE "no"
Any help would be really appreciated.
Thanks.
One option would be to set the CL environment variable, using something like:
set CL=/DMYPROC_ENABLE_MYMODULE
The C++ compiler (cl.exe) will add the contents of the CL environment variable to its command line when it runs.
I know you can define macros if you build using msbuild, but I'm not sure you can do the same when using devenv directly.
You can make different configurations for your solution and define different preprocessor flags for the different configurations. Then you would just need to select the configuration at the command line and no need for multiple solution or project files.

How to pass the assembly name as a command-line argument when debugging

I have an NUnit test assembly (a .NET DLL). When I click Run in Visual Studio I want it to launch NUnit and run the tests in this assembly. I can do all of that.
Instead of specifying the full assembly name and path in the command line arguments, does Visual Studio support some sort of macro that expands into that for the Command Line Arguments box? Most other development tools I have used support this, but I cannot find anything in the documentation about this.
I was expecting something like: %assembly_full_path%
The reason I want to do this is so if the assembly name or build location changes, then I don't have to update the command line arguments as well.
This doesn't work as far as I can tell. Macros in the Command Line arguments box do not get expanded. Not even environment variables. Bummer.
A workaround is to create a custom tool. Tools + External Tools, Add. Title = Run tests, Command = nunit.exe, Arguments = $(TargetPath), Initial Directory = $(TargetDir). Tweak as needed. You could assign a keystroke to this new tool command, even F5.
Using VS2005, the only item I need to provide in the Command Line Arguments is the name of the dll. I suspect VS sets the default working directory to the project's output directory, as I've never specified the path and yet the tests always load correctly.

Resources