msbuild - export all properties defined in block to MSBuild calls - msbuild

<Target Name="micropython_prebuild">
<PropertyGroup>
<uP_PrebuildPyExe>"$(ProjectDir)\Source\micropython\py\make_prebuild_wrapper.exe"</uP_PrebuildPyExe>
<GnuCat>$(uP_PrebuildPyExe) gnu_cat</GnuCat>
<GnuSed>$(uP_PrebuildPyExe) gnu_sed</GnuSed>
<uP_GenHdrFolder>$(ObjectFolder)\genhdr</uP_GenHdrFolder>
<uP_QSTR_GEN_EXTRA_CFLAGS>-DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA</uP_QSTR_GEN_EXTRA_CFLAGS>
<uP_SRC>$(ProjectDir)\Source\micropython</uP_SRC>
<uP_MPY_CROSS>"$(ProjectDir)\Source\micropython\mpy-cross\mpy-cross.exe"</uP_MPY_CROSS>
</PropertyGroup>
<MakeDir Directories="$(uP_GenHdrFolder)\"/>
<MSBuild
Projects="$(MSBuildProjectFile)"
Condition="'' == ''"
Targets="prebuild_mpversion"
Properties="uP_PrebuildPyExe=$(uP_PrebuildPyExe);
uP_GenHdrFolder=$(uP_GenHdrFolder)"
/>
</Target>
How do I "export" all the properties I've defined at the top to the calls to MSBuild within this target?
Otherwise, I have to set the Properties of each MSBuild.

You can also create a single property that contains all the definitions:
<Target Name="micropython_prebuild">
<PropertyGroup>
<PrebuildProperties>
uP_PrebuildPyExe="$(ProjectDir)\Source\micropython\py\make_prebuild_wrapper.exe";
GnuCat=$(uP_PrebuildPyExe) gnu_cat;
GnuSed=$(uP_PrebuildPyExe) gnu_sed;
uP_GenHdrFolder=$(ObjectFolder)\genhdr;
uP_QSTR_GEN_EXTRA_CFLAGS=-DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA;
uP_SRC=$(ProjectDir)\Source\micropython;
uP_MPY_CROSS="$(ProjectDir)\Source\micropython\mpy-cross\mpy-cross.exe";
</PrebuildProperties>
</PropertyGroup>
<MakeDir Directories="$(uP_GenHdrFolder)\"/>
<MSBuild
Projects="$(MSBuildProjectFile)"
Condition="'' == ''"
Targets="prebuild_mpversion"
Properties="$(PrebuildProperties)"
/>
</Target>

Related

Setting OutputName from wxi MSBUILD -Customizing Target Order

I'm trying to set the <OutputName> of my .msi when using Wix in VisualStudio.
I have seen the other questions related to this, but I am approaching it a bit differently. Hoping you can all clear up some things for me.
This is my .wixproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>fa14</ProjectGuid>
<SchemaVersion>2.0</SchemaVersion>
<OutputName>Installer_x64_v$(Version)</OutputName>
<OutputType>Package</OutputType>
<!--<ProjectGuid>{fa1414f5-97eb-4d39-8ff6-190cd92ca99f}</ProjectGuid>-->
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Debug</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<WixVariables>
</WixVariables>
</PropertyGroup>
<ItemGroup>
<Compile Include="Product.wxs" />
<Compile Include="Files.wxs" />
<Compile Include="UI.wxs" />
</ItemGroup>
<ItemGroup>
<WixExtension Include="WixUIExtension">
<HintPath>$(WixExtDir)\WixUIExtension.dll</HintPath>
<Name>WixUIExtension</Name>
</WixExtension>
</ItemGroup>
<Import Project="$(WixTargetsPath)" Condition=" '$(WixTargetsPath)' != '' " />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets" Condition=" '$(WixTargetsPath)' == '' AND Exists('$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets') " />
<Target Name="EnsureWixToolsetInstalled" Condition=" '$(WixTargetsImported)' != 'true' ">
<Error Text="The WiX Toolset v3.11 (or newer) build tools must be installed to build this project. To download the WiX Toolset, see http://wixtoolset.org/releases/" />
</Target>
<!--
To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Wix.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
</PropertyGroup>
<Target Name="BeforeBuild">
<ReadLinesFromFile File="Configuration.wxi" >
<Output TaskParameter="Lines" PropertyName="FileContents"/>
</ReadLinesFromFile>
<PropertyGroup>
<!-- Find occurence of string ProductVersion, return everything after 'Productversion', trimming spaces and " -->
<TrimmedWXI>$(FileContents.Substring($(FileContents.IndexOf(ProductVersion))).Replace(" ","").Replace('"',""))</TrimmedWXI>
<!-- Find the equals sign, add 1-->
<VersionStart>$([MSBuild]::Add($(TrimmedWXI.IndexOf(=)),1))</VersionStart>
<!-- Find the ?-->
<VersionEnd>$(TrimmedWXI.IndexOf(?))</VersionEnd>
<!-- Find the length of the version-->
<VersionLength>$([MSBuild]::Subtract($(VersionEnd),$(VersionStart)))</VersionLength>
<!-- Find the version-->
<Version>$(TrimmedWXI.Substring($(VersionStart),$(VersionLength)))</Version>
</PropertyGroup>
<Message Importance="High" Text=" Attempting build for $(Version)" />
</Target>
<Target Name="AfterBuild">
<Message Importance="High" Text="BuildDone" />
</Target>
</Project>
And the configuration.wxi
<?xml version="1.0" encoding="utf-8"?>
<Include>
<!-- Setup Configuration -->
<?define ProductName = "Acme" ?>
<?define ProductManufacturer = "AcmeInc." ?>
<?define ProductVersion = "9.99" ?>
<?define ProductUpgradeCode = "FAKEGUID" ?>
</Include>
Now, I am setting the <Version> property appropriately, it parses the file string properly and spits it out in the message, but when it comes to the building of the .msi, it is still blank. Looks like Version gets set after the OutputName is set, and I cant overwrite or make Version set before the OutputName.
I've done some reading and messed around with the Target and build order, but I can't quite get anything to work.
It seems like the OutputName is required to be set before everything else?
Why doesn't this get overridden in the BeforeBuild target?
Is it possible to have this ReadLinesFromFile task happen before the OutputName is set?
Thanks!

MS Build Task not accepting properties

I am trying to build my project using MSbuild. The solution builds perfectly fine if I hardcode the configurations
<MSBuild Projects="%(SolutionsToBuild.Identity)" Properties="Configuration=Debug" ContinueOnError="false"/>
<MSBuild Projects="%(SolutionsToBuild.Identity)" Properties="Configuration=Release" ContinueOnError="false"/>
but if I use batching like this the values are not assigned
<MSBuild Projects="%(SolutionsToBuild.Identity)" Properties="Configuration=%(ConfigList.Identity)" ContinueOnError="false"/>
where ConfigList
<ItemGroup>
<ConfigList Condition=" '#(ConfigList)' == '' and $(Configuration) != '' " Include="$(Configuration.Split(';'))" />
<!-- parse all requested configurations into a list -->
<ConfigList Condition=" '#(ConfigList)' == '' " Include="Debug" />
<!-- if no configurations were specified, default to Debug -->
</ItemGroup>
and Configurations are provided from CLI
msbuild build.proj -p:Configuration="Release;Debug"
Also interestingly I am getting the value in
<Message Text="Build for configuration %(ConfigList.Identity)"/>
Whole Code
<Target Name="BuildApplication">
<ItemGroup>
<ConfigList Condition=" '#(ConfigList)' == '' and $(Configuration) != '' " Include="$(Configuration.Split(';'))" />
<!-- parse all requested configurations into a list -->
<ConfigList Condition=" '#(ConfigList)' == '' " Include="Debug" />
<!-- if no configurations were specified, default to Debug -->
</ItemGroup>
<Message Text="Build for configuration %(ConfigList.Identity)"/>
<MSBuild Projects="%(SolutionsToBuild.Identity)" Properties="Configuration=%(ConfigList.Identity)" ContinueOnError="false"/>
<!--<MSBuild Projects="%(SolutionsToBuild.Identity)" Properties="Configuration=Debug" ContinueOnError="false"/>-->
</Target>
I am not sure what I am doing wrong. Any help would be appreciated. Thanks !
Sources referred:
Using MSBuild to Build Multiple Configurations
Passing properties to MSBUILD Task

MSBuild overwrite properties value with Import

I have a msbuild script which contains only PropertyGroup: DefaultVariables.msbuild
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- default values if nothing is set in Main.proj -->
<PropertyGroup>
<ProjectName Condition="'$(PublishService)'==''">DefaultService</ProjectName>
</PropertyGroup>
</Project>
The PublishService can be changed depend on environment.
I also have a Variables.msbuild with is same with above script, except the service name:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- default values if nothing is set in Main.proj -->
<PropertyGroup>
<ProjectName Condition="'$(PublishService)'==''">ErpService</ProjectName>
</PropertyGroup>
</Project
My main build script: BuildMsi.msbuild import the DefaultVariables.msbuild and has a target CreateEnvironmentSpecificInstaller which calls to Msi.msbuild
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CreateInstaller">
<PropertyGroup>
<BaseDir Condition="$(BaseDir)==''">$(MSBuildProjectDirectory)</BaseDir>
</PropertyGroup>
<Import Project="DefaultVariables.msbuild" />
<!-- Something else -->
<Target Name="CreateEnvironmentSpecificInstaller" DependsOnTargets="$(SpecificBuildSteps)">
<MSBuild Projects="$(RedistDir)\Framework\Msi.msbuild" Targets="CreateBatchScripts" StopOnFirstFailure="true" Properties="Configuration=$(Configuration)" RebaseOutputs="true" />
</Target>
<Target Name="CreateInstaller" DependsOnTargets="PrintVersion;$(GenericBuildSteps)">
<MSBuild Condition=" '$(EnvironmentName)' == '**AllEnvironments**' " Projects="$(BaseDir)\$(BtsDeploymentFrameworkDir)\BuildMsi.msbuild" Targets="CreateEnvironmentSpecificInstaller" StopOnFirstFailure="true"
Properties="Configuration=$(Configuration);" RebaseOutputs="true" />
<CallTarget Targets="RemoveGeneratedEnvironmentSettings" />
</Target>
</Project>
In Msi.msbuild script I add an Import to Variables.msbuild script, but the PublishService after this is still the DefaultService:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CreateInstaller">
<Import Project="Variables.msbuild" />
<Target Name="CreateBatchScripts">
<Message Text="PublishService = $(PublishService)" />
</Target>
</Project>
How can I overwrite this property value in runtime?
First of all, you are never giving a value to PublishService. I assume in DefaultVariables.msbuild what you wanted to do is
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- default values if nothing is set in Main.proj -->
<PropertyGroup>
<PublishService> Condition="'$(PublishService)'==''">DefaultService</PublishService>
</PropertyGroup>
</Project>
Then, I suggest you also rename the property in Variables.msbuild and remove the condition Condition="'$(PublishService)'==''. As you have given the property a default value in DefaultVariables.msbuild, the condition will not be fulfilled and thus the value will not be changed.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishService>ErpService</PublishService>
</PropertyGroup>
</Project>

Expand MSBuild property containing the name of other property

Let's say I have property $(Foo), that is defined as a result of some function, that returns the string value $(Bar). Is it possible to expand it somehow, so that $(Foo) will be expanded to the value of $(Bar)?
Given example project:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Bar>Bar Value</Bar>
<Foo>$([System.String]::Concat("$(","Bar",")"))</Foo>
<Baz>$(Foo)</Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>
<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>
</Project>
Here's what I have:
S:\>msbuild Test.proj /t:Test /nologo
Build started 18.09.2013 17:52:14.
Project "S:\Test.proj" on node 1 (Test target(s)).
Test:
Foo == $(Bar)
Baz == $(Bar)
Qux == Bar Value
Done Building Project "S:\Test.proj" (Test target(s)).
Build succeeded.
0 Warning(s)
0 Error(s)
So, $(Qux), which is defined to $(Bar) directly, is expanded correctly, but $(Foo) and $(Baz) are not. Is it possible to expand them too?
S:\>msbuild /version
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.18052]
Copyright (C) Microsoft Corporation. All rights reserved.
4.0.30319.17929
You want to simulate something like $($(Foo)) that is invalid syntax for MsBuild. But you can simulate this behavior by using items and by dynamically creating items in targets. You can’t do it in the "global scope" because of Property and Item evaluation order.
So you have to do it in some targets.
Here is sample that sets property by means of InitialTargets.
<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="MyPropertiesSetup" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Bar>Bar Value</Bar>
<!-- A value of Foo property specifies the name of the property it takes value from. -->
<Foo>Bar</Foo>
<Baz></Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>
<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>
<Target Name="MyPropertiesSetup">
<ItemGroup>
<_Foo Include="$(Foo)" />
<_Baz Include="%(_Foo.Identity)" />
</ItemGroup>
<PropertyGroup>
<Foo>$(%(_Foo.Identity))</Foo>
<Baz>$(%(_Baz.Identity))</Baz>
</PropertyGroup>
</Target>
</Project>
If you can use item for values instead properties, there is more elegant way do it (MSBuild Trickery #68 - #($(CanYouDoThis)):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Foo>Bar</Foo>
<Foo2>Bar2</Foo2>
</PropertyGroup>
<ItemGroup>
<Bar Include="Bar Value" />
<Bar2 Include="Bar2 Value" />
</ItemGroup>
<Target Name="Test">
<Message Text="Foo == '#($(Foo))'" />
<Message Text="Foo2 == '#($(Foo2))'" />
</Target>
</Project>

MSBuild copy task and condition name from file with wildcards

I have a FilesToExclude2.txt file which contains all excludes like this :
*.settings
*#*
*.vbproj*
*.csproj*
*\Errors\*
*\Errors
*\_vti_*\*
*\_vti_*
*\CVS\*
In my tasks file, there is a copy task
<Target Name="CustomModuleCopy">
<ItemGroup>
<ModuleFiles Include="$(SolutionModuleName)\$(ProjectModuleName)\**\*.*" />
<FileToExclude Include="$(BasePath)\$(SolutionModuleName)\FilesToExclude2.txt" />
</ItemGroup>
<ReadLinesFromFile File="#(FileToExclude)">
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<Copy SourceFiles="#(ModuleFiles)" DestinationFiles="#(ModuleFiles->'$(DestFolder)\$(ProjectModuleName)\%(RecursiveDir)%(Filename)%(Extension)')" Condition="'%(ModuleFiles.Identity)' != #(FileContents)" ContinueOnError="false" />
<!--CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
</Target>
Without the "condition" attribute on the copy task, everything's works fine, but I do not know how to implement the condition with wildcard and if it is possible
Any kind of help would be greatly appreciated :) this is freaking me out for few hours...
Would it not be easier to just use the exclude property instead?
as in
Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ModuleFiles Include="c:\code\**\*.*"
Exclude="#(FileContents)" />
</ItemGroup>
<!--<Copy SourceFiles="#(ModuleFiles)" DestinationFolder="Some|Destination" Condition="'%(ModuleFiles.Identity)' != #(FileContents)" ContinueOnError="false" />
CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
the following code
<Target Name="CustomModuleCopy">
<ItemGroup>
<FileToExclude Include="$(BasePath)\$(SolutionModuleName)\FilesToExclude.txt" />
</ItemGroup>
<ReadLinesFromFile File="#(FileToExclude)">
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<ItemGroup>
<ModuleFiles Include="$(SolutionModuleName)\$(ProjectModuleName)\**\*.*" Exclude="#(FileContents)" />
</ItemGroup>
<Copy SourceFiles="#(ModuleFiles)" DestinationFiles="#(ModuleFiles->'$(DestFolder)\$(ProjectModuleName)\%(RecursiveDir)%(Filename)%(Extension)')" ContinueOnError="false" />
<Message Text="Exclude = #(FileContents)" />
<!--CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
</Target>
produce this output in console :
copy /y "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Debug.config" "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Debug.config"
Copying file from "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Release.config" to "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Release.config"
copy /y "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Release.config" "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Release.config"
Exclude = *.cab;*.config;*.vb;*.cs;*.resx;*.xsx;*.bak;*.myapp;*.settings;*#*;
*.vbproj*;*.csproj*;*\Errors\*;*\Errors;*\_vti_*\*;*\_vti_*;*\CVS\*;*\CVS;