Conditionally exclude files from VisualStudio 2013 C++ project based on file name - visual-c++

I'm trying to get Visual Studio 2013's msbuild .vcxproj to automatically mark certain .cpp project files as <ExcludedFromBuild>true</ExcludedFromBuild> based on the .cpp filename.
My goal is to allow my program (~100 developers, ~1000 vcxproj) to easily support Debug only compilations of unit test code, that would all be written in .cpp files that had a _utest.cpp suffix. Any .cpp files that ended in _utest.cpp would be automatically excluded from a release build, and the programmer could see that in Visual Studio's Solution Explorer when they were switched to a Release Solution Configuration.
I have been able to prevent the _utest.cpp-suffixed files from building in Release by adding this to my shared .props file
<Target Name="Remove _utest.cpp" BeforeTargets="ClCompile" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ItemGroup>
<ClCompile Remove="*_utest.cpp" />
</ItemGroup>
</Target>
but that doesn't give a visual indicator in Solution Explorer that the files are not part of the Release build.
I already tried a condition in an ItemDefinitionGroup that was based on %(Identity) but that didn't work
<ItemDefinitionGroup Condition="'%(Identity)'=='throttle_utest.cpp'">
<ClCompile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
</ItemDefinitionGroup>
(Visual Studio refused to load this one with an error "The reference to the built-in metadata Identity at position 1 is not allowed")
or
<ItemDefinitionGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(%(Identity), '_utest\.cpp$'))">
<ClCompile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
</ItemDefinitionGroup>
(Visual Studio loaded with this change, but did nothing to exclude the matching files from building).
Is there a way I can conditionally exclude files based on filename and get a visual indicator in Solution Explorer?

I think something like this should work.
Right under the <Project> tag.
<ItemGroup Condition="'$(Configuration)'!='Debug'">
<ClCompile Include="*_utest.cpp">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
</ItemGroup>

This works for me (was done entirely in VS2013 UI - right-click on Properties of source file and select Excluded from build: True for Release Configuration):
<ClCompile Include="foo_utest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
NOTE: According to https://learn.microsoft.com/en-us/cpp/ide/vcxproj-file-structure?view=vs-2017 :
The Visual C++ project system currently does not support wildcards in project items.
For example, this is not supported:
<ClCompile Include="*.cpp"/>
Therefore each file should be excluded individually.

Related

JetBrains Rider run Pre/Post Build events when building in Rider

A project I'm working on has a csproj file with Pre/Post build events that only run when its being built in Visual Studio. It looks like this:
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(BuildingInsideVisualStudio)' == 'true' ">
<!-- Pre build stuff -->
</Target>
Is there an equivalent condition for when a project is being built inside Rider, something like '$(BuildingInsideRider)'?
I reached out to Jetbrains support with the same question, and they told me the property name is '$(BuildingByReSharper)'. It's also worth noting that it's only useable in Rider 2021.1.3 and newer.

How to add macro under visual studio 2012

I just found that cocos2d-x 3.0 beta has defined own macro "$(EngineRoot)" under vs2012 proj, I want to know how to do it!
I can not upload the img...
Assuming you generate a cocos2d project using the cocos.py script as recommended in the docs, you'll find that EngineRoot is defined in the project's cocos2d_headers.props file near the top as follows:
<PropertyGroup Label="UserMacros">
<EngineRoot>$(MSBuildThisFileDirectory)..\..\</EngineRoot>
</PropertyGroup>
You should be able to do the same in your own (non-generated) project.

How to exclude assembly reference using custom conditional properties in Xamarin.iOS project

I have solution in Xamarin.iOS with project that references very large DLL (binding for very large native library). So build time for solution is very large. After simple modification in any source file I need to wait for linking. So my idea was to exclude the reference from project using custom property and also to make define that I will use in .cs files to exclude code that depends on large assembly. But I'm unable to use custom condition to exclude the reference. The following strategy will not work for Xamarin.iOS (but will work in Visual Studio):
Create file CommonProperties.prop:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LinkWithLargeAssembly>True</LinkWithLargeAssembly>
</PropertyGroup>
</Project>
So the idea is: When it is not critical I can define LinkWithLargeAssembly as False and link my project fast. CommonProperties.prop can be imported in any assembly that depends on features of the large library.
In the project file .csproj import above file and try to exclude reference (for example monotouch):
...
<Import Project="CommonProperties.prop" />
...
<ItemGroup>
<Reference Include="monotouch" Condition=" '$(LinkWithLargeAssembly)' == 'True' " />
</ItemGroup>
...
I've tried also to define property $(LinkWithLargeAssembly) directly in the project file without import. Also I've tried to use already defined properties for example $(RootNamespace) and $(AssemblyName). But Condition attribute works only for properties $(Configuration) and $(Platform). That is following code will include and exclude monotouch depending on the configuration:
<ItemGroup>
<Reference Include="monotouch" Condition=" '$(Configuration)' == 'Debug' " />
</ItemGroup>
Is it possible to customize assembly reference including using my own conditional properties?
I solved the problem by adding new build configuration that was copied from Debug. I named it DebugNoLargeLib. So I can exclude reference with the following code because $(Configuration) property will be parsed correctly:
<ItemGroup>
<Reference Include="SomeLargeLib.dll" Condition=" '$(Configuration)' != 'DebugNoLargeLib' " />
</ItemGroup>
After I added preprocessor directive NO_LARGE_LIB to the Compiler section for configuration DebugNoLargeLib.
So now I'm able to link without large library and exclude code depending on it from compilation.
But I think this is mistake from Xamarin side that project files processing is not fully supported according to Microsoft specs.

Customizing the specific output files for various Typescript input files

I've got a web project using TypeScript that has some reasonably complex requirements for the compiled output files. So for instance, I need all the *.ts files in one directory to compile down to one single .js file, and all the *.ts files in another directory to compile down to a different .js file. (It's more complex than that, but you get the idea.)
I've been able to get this working using the tsc.exe command-line, using input files and what-not, but I'd like to be able to use MSBuild .targets files - among other things, using tsc.exe from the command-line seems to be pretty poorly supported on continuous integration servers, where it can be located who-knows-where, and certainly isn't likely to be in the path.
According to this answer here, it seems like I should be able to do this using custom build targets. So I've created a custom version of Microsoft.TypeScript.targets, and in addition to the default "CompileTypeScript" target, I've created a second one, "PayboardApiV10", so that the relevant part looks like so:
<Target Name="CompileTypeScript" Condition="'$(BuildingProject)' != 'false'">
<Message Text="Compiling TypeScript files normally" Importance="high"/>
<VsTsc
ToolPath="$(TscToolPath)"
ToolExe="$(TscToolExe)"
Configurations="$(TypeScriptBuildConfigurations)"
FullPathsToFiles="#(TypeScriptCompile)"
YieldDuringToolExecution="$(TscYieldDuringToolExecution)"
OutFile="$(TypeScriptOutFile)"
OutDir="$(TypeScriptOutDir)"
>
<Output TaskParameter="GeneratedJavascript" ItemName="GeneratedJavascript" />
</VsTsc>
</Target>
<Target Name="PayboardApiV10" Condition="'$(BuildingProject)' != 'false'">
<Message Text="Compiling TypeScript files for Payboard API v1.0" Importance="high" />
<VsTsc
ToolPath="$(TscToolPath)"
ToolExe="$(TscToolExe)"
Configurations="$(TypeScriptBuildConfigurations)"
FullPathsToFiles="#(TypeScriptCompile)"
YieldDuringToolExecution="$(TscYieldDuringToolExecution)"
OutFile="Payboard.js"
OutDir="$(ProjectDir)api\v1.0\"
>
<Output TaskParameter="GeneratedJavascript" ItemName="GeneratedJavascript" />
</VsTsc>
</Target>
And then I've specified a "CustomTool" in my project configuration for the specific files that I'd like to get picked up by the "PayboardApiV10" build target, like so:
I should note that I have no idea if I'm doing this bit correctly. I can't seem to find any documentation on it, and the only examples I've been able to find are from that previous answer. And more to the point, when I run my builds, all the TS files in my project get caught up in the first build target, including the ones for which I've specified "MSBuild:PayboardApiV10" for the custom tool. The "PayboardApiV10" tool never seems to get run, i.e., I never see the message "Compiling TypeScript files for Payboard API v1.0".
So two questions:
(1) Is there a better way to do what I'm trying to do?
(2) If this is generally the right way to do it, any ideas as to what I'm doing wrong?
The direction you're going is the optimal way (on save). In the meantime you can use post-build events to transform the typescript. Right click on your project and select properties. Select Build Events and in the Post-Build area you can specify command line parameters to use tsc.exe.
On the direction you're going (compile on save) I think the project file may be missing the following on each file you want compiled:
<TypeScriptCompile Include="app.ts" />
The configuration is also likely missing the environment settings.
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptIncludeComments>true</TypeScriptIncludeComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
</PropertyGroup>
More information on both methods is available at this TypeScript wiki page.
I ended up asking this same question over on the TypeScript forums (https://typescript.codeplex.com/discussions/455781). The conclusions I drew from the conversation there:
There isn't a great way to do it now.
The best hacky way to do it now is probably the way that I was doing it, namely, with batch files, post-build events, and checking the compiled files into source. Other folks recommended Grunt, or the ASP.NET MVC bundling mechanism (the latter won't work in my scenario); I've also used the r.js minifier in the past. And these will work; but to reiterate, none of these are really very good solutions.
The best way to do it in the future will be to create "library projects" of TypeScript files, so that all TS files in a given project get compiled together; and then you can reference the library TS projects from your main Web project, and will automatically get the compiled files merged into your main ~/Scripts folder. But that will require support from the TS team - which Jon Turner basically indicated would be coming, though he didn't say when. (See also https://typescript.codeplex.com/workitem/571.)

Exclude file from stylecop checking

I want to exclude /Properties/AssemblyInfo.cs from stylecop checking.
Can we do that?
the problem is that i have integrated stylecop with nant. In nant i am stamping product version so the information from Assembly info changes and it gives warning in nant email. I am using stylecopcmd
for nant integration.
There are a couple of ways to do this. The recommended method is with file lists. Here is an example:
<StyleCopSettings Version="4.3">
<SourceFileList>
<SourceFile>AssemblyInfo.cs</SourceFile>
<Settings>
<GlobalSettings>
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
</GlobalSettings>
</Settings>
</SourceFileList>
</StyleCopSettings>
Alternatively you can use the ExcludeFromStyleCop setting in the project file as documented here:
<Compile Include="AssemblyInfo.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>

Resources