<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<!--
 Packaging script for Apache Qpid on Windows

 Builds the C++, .NET, and WCF components, and packages those along with user
 documentation and the python pieces needed to generate QMF stuff.
-->

<Project DefaultTargets="Clean;Installer"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <PropertyGroup>
	<build_dir>$(MSBuildProjectDirectory)\build</build_dir>
	<source_root>$(MSBuildProjectDirectory)\..\..</source_root>
	<staging_dir>$(MSBuildProjectDirectory)\stage</staging_dir>
	<bits Condition="'$(bits)' == ''">32</bits>
	<qpid_version>0.14</qpid_version>
	<OutputName>qpidc</OutputName>
	<OutputType>Package</OutputType>
	<WixToolPath>C:\Program Files (x86)\Windows Installer XML v3.5\bin</WixToolPath>
        <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.5\wix.targets</WixTargetsPath>
    </PropertyGroup>

    <Choose>
        <When Condition="'$(bits)' == '64'">
	    <PropertyGroup>
	        <ProgramFiles>ProgramFiles64Folder</ProgramFiles>
		<CmakeGenerator>%22Visual Studio 9 2008 Win64%22</CmakeGenerator>
		<Architecture>x64</Architecture>
		<CSProjArchitecture>x64</CSProjArchitecture>
                <DotNetBindingsArchitecture>x64</DotNetBindingsArchitecture>
	    </PropertyGroup>
	</When>
	<Otherwise>
	    <PropertyGroup>
	        <ProgramFiles>ProgramFilesFolder</ProgramFiles>
		<CmakeGenerator>%22Visual Studio 9 2008%22</CmakeGenerator>
		<Architecture>x86</Architecture>
		<CSProjArchitecture>Win32</CSProjArchitecture>
                <DotNetBindingsArchitecture>Win32</DotNetBindingsArchitecture>
    </PropertyGroup>
	</Otherwise>
    </Choose>

    <ItemGroup>
	<CppDebugProjects Include="$(build_dir)\src\qpidtypes.vcproj"/>
	<CppDebugProjects Include="$(build_dir)\src\qpidcommon.vcproj"/>
	<CppDebugProjects Include="$(build_dir)\src\qpidclient.vcproj"/>
	<CppDebugProjects Include="$(build_dir)\src\qpidmessaging.vcproj"/>
	<CppDebugProjects Include="$(build_dir)\src\qmfconsole.vcproj"/>
    </ItemGroup>

    <ItemGroup>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidtypesd.dll"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidtypesd.lib"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidtypesd.pdb"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidcommond.dll"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidcommond.lib"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidcommond.pdb"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidclientd.dll"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidclientd.lib"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidclientd.pdb"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidmessagingd.dll"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidmessagingd.lib"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qpidmessagingd.pdb"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qmfconsoled.dll"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qmfconsoled.lib"/>
	<CppDebugArtifacts Include="$(build_dir)\src\Debug\qmfconsoled.pdb"/>
    </ItemGroup>

    <ItemGroup>
	<WcfProjects Include="$(source_root)\wcf\src\Apache\Qpid\**\*.csproj"/>
	<WcfArtifacts Include="$(source_root)\wcf\src\Apache\Qpid\Channel\bin\Release\Apache.Qpid.Channel.dll"/>
	<WcfArtifacts Include="$(source_root)\wcf\src\Apache\Qpid\Channel\bin\Release\Apache.Qpid.Interop.dll"/>
	<WcfExamples Include="$(source_root)\wcf\samples\**\*"
	             Exclude="$(source_root)\wcf\samples\**\.svn\**"/>
    </ItemGroup>

    <ItemGroup>
	<DotNetBindingArtifacts Include="$(build_dir)\src\Release\org.apache.qpid.messaging.dll"/>
	<DotNetBindingArtifacts Include="$(build_dir)\src\Release\org.apache.qpid.messaging.pdb"/>
	<DotNetBindingArtifacts Include="$(build_dir)\src\Release\org.apache.qpid.messaging.sessionreceiver.dll"/>
	<DotNetBindingArtifacts Include="$(build_dir)\src\Release\org.apache.qpid.messaging.sessionreceiver.pdb"/>
    </ItemGroup>

    <ItemGroup>
	<DotNetBindingDebugArtifacts Include="$(build_dir)\src\Debug\org.apache.qpid.messaging.dll"/>
	<DotNetBindingDebugArtifacts Include="$(build_dir)\src\Debug\org.apache.qpid.messaging.pdb"/>
	<DotNetBindingDebugArtifacts Include="$(build_dir)\src\Debug\org.apache.qpid.messaging.sessionreceiver.dll"/>
	<DotNetBindingDebugArtifacts Include="$(build_dir)\src\Debug\org.apache.qpid.messaging.sessionreceiver.pdb"/>
    </ItemGroup>

    <ItemGroup>
        <BoostDlls Include="$(staging_dir)\bin\boost_*.dll"/>
    </ItemGroup>

    <ItemGroup>
        <WixExtension Include="WixUtilExtension">
            <HintPath>$(WixToolPath)\WixUtilExtension.dll</HintPath>
        </WixExtension>
        <WixExtension Include="WixUIExtension">
            <HintPath>$(WixToolPath)\WixUIExtension.dll</HintPath>
        </WixExtension>
    </ItemGroup>
    <Import Project="$(WixTargetsPath)" />


    <Target Name="Clean">
    	<RemoveDir
            Directories="$(build_dir);$(staging_dir)" />
    </Target>

    <Target Name="MakeBuildDirs">
    	<MakeDir
            Directories="$(build_dir);$(staging_dir)" />
    </Target>

    <Target Name="Configure" DependsOnTargets="MakeBuildDirs">
        <Message Text="build: $(build_dir)"/>
        <Message Text="stage: $(staging_dir)"/>
    	<Exec
	    Command="cmake -G $(CmakeGenerator) -DCMAKE_INSTALL_PREFIX=$(staging_dir) $(cmake_options) $(source_root)\cpp"
	    WorkingDirectory="$(build_dir)" />
    </Target>

    <Target Name="BuildCpp" DependsOnTargets="Configure">
        <!-- Using VCBuild here with a set of vcproj files misses the
             dependencies, so use devenv to pick up everything. -->
	<!-- Need to ignore the exit code until QMF engine builds clean -->
	<Exec
	    Command="devenv qpid-cpp.sln /build Release /Project ALL_BUILD"
	    WorkingDirectory="$(build_dir)"
	    IgnoreExitCode="true" />
    </Target>

    <Target Name="BuildCppDebug" DependsOnTargets="Configure">
    	<VCBuild
	    Projects="@(CppDebugProjects)"
	    SolutionFile="$(build_dir)/qpid-cpp.sln"
	    Configuration="Debug" />
    </Target>

    <!-- StopOnFirstFailure needed to avoid a weird error trying to compile WIN32 -->
    <Target Name="BuildWcf" DependsOnTargets="BuildCpp">
        <MSBuild
	    Projects="$(source_root)\wcf\QpidWcf.sln"
	    Properties="Configuration=Release;Platform=$(CSProjArchitecture)"
	    StopOnFirstFailure="false" />
    </Target>

    <Target Name="BuildDocs" DependsOnTargets="Configure">
    	<VCBuild
	    Projects="$(build_dir)\docs\api\docs-user-api.vcproj"
	    SolutionFile="$(build_dir)/qpid-cpp.sln"
	    Configuration="Release" />
    </Target>

    <Target Name="BuildDotNetBindings"><!-- DependsOnTargets="BuildCpp;BuildCppDebug" -->
        <MSBuild
            Projects="$(source_root)\cpp\bindings\qpid\dotnet\org.apache.qpid.messaging.sessionreceiver.sln"
            Properties="Configuration=Debug;Platform=$(Architecture)" />
        <MSBuild
            Projects="$(source_root)\cpp\bindings\qpid\dotnet\org.apache.qpid.messaging.sessionreceiver.sln"
            Properties="Configuration=Release;Platform=$(Architecture)" />
    </Target>

    <Target Name="Stage"
            DependsOnTargets="BuildCpp;BuildCppDebug;BuildWcf;BuildDocs;BuildDotNetBindings">
        <Exec
	    Command="cmake -DCMAKE_INSTALL_CONFIG_NAME=%22Release%22 -P $(build_dir)\cmake_install.cmake" />
    	<Copy
	    SourceFiles="@(CppDebugArtifacts)"
	    DestinationFolder="$(staging_dir)\bin" />
    	<Copy
	    SourceFiles="@(WcfArtifacts)"
	    DestinationFolder="$(staging_dir)\bin" />
	<Copy
	    SourceFiles="@(WcfExamples)"
	    DestinationFiles="@(WcfExamples->'$(staging_dir)\examples\%(RecursiveDir)%(Filename)%(Extension)')" />
    	<Copy
	    SourceFiles="@(DotNetBindingArtifacts)"
	    DestinationFolder="$(staging_dir)\bin" />
    	<Copy
	    SourceFiles="@(DotNetBindingDebugArtifacts)"
	    DestinationFolder="$(staging_dir)\bin\Debug" />
    </Target>

    <Target Name="Installer" DependsOnTargets="Stage">
    <!-- Would be nice to use the WiX Tasks but I'm getting what I think are
         errors mixing 32- and 64-bit artifacts.
    	<HeatDirectory
	    ToolPath="$(WixToolPath)"
	    Directory="$(staging_dir)\include\boost"
	    ComponentGroupName="group_BoostHeaders"
	    OutputFile="boost_headers.wxs" />
	<HeatFile
	    ToolPath="$(WixToolPath)"
	    File="@(BoostDlls)"
	    ComponentGroupName="group_BoostDlls"
	    DirectoryRefId="QpidBin"
	    OutputFile="boost_dlls.wxs" />
	<Candle
	    ToolPath="$(WixToolPath)"
	    DefineConstants="qpidc_version=$(qpid_version)"
	    InstallerPlatform="x64"
	    OutputFile="qpidc-$(qpid_version)-x64.msi" />
    -->
        <Exec
	    Command="heat dir $(staging_dir)\include\qpid -var var.qpid_headers_dir -dr QpidInclude -gg -cg group_QpidHeaders -out qpid_headers.wxs" />
        <Exec
	    Command="heat dir $(staging_dir)\include\boost -var var.boost_headers_dir -dr QpidInclude -gg -cg group_BoostHeaders -out boost_headers.wxs" />
	<!-- HEAT5150 warns about self-registering DLLs; don't care -->
	<Exec
	    Command="heat dir $(staging_dir)\bin\boost -var var.boost_dll_dir -dr QpidBin -srd -gg -cg group_BoostDlls -sw5150 -out boost_dlls.wxs" />
	<Exec
	    Command="heat dir $(staging_dir)\examples -var var.examples_dir -dr QpidExamples -srd -gg -cg group_Examples -out examples.wxs" />
	<Exec
	    Command="heat dir $(staging_dir)\docs\api  -var var.api_docs_dir -dr QpidDoc -gg -cg group_APIDocs -out api_docs.wxs" />
	<Exec
	    Command="candle -dqpidc_version=$(qpid_version) -dProgramFiles=$(ProgramFiles) -dstaging_dir=$(staging_dir) -dqpid_headers_dir=$(staging_dir)\include\qpid -dboost_headers_dir=$(staging_dir)\include\boost -dboost_dll_dir=$(staging_dir)\bin\boost -dexamples_dir=$(staging_dir)\examples -dapi_docs_dir=$(staging_dir)\docs\api  -ext WiXNetFxExtension qpidc.wxs qpid_headers.wxs boost_headers.wxs boost_dlls.wxs examples.wxs api_docs.wxs -arch $(Architecture)" />
	<Exec
	    Command="light -ext WiXNetFxExtension -ext WixUtilExtension -ext WixUIExtension -cultures:en-us -out qpidc-$(qpid_version)-$(Architecture).msi qpidc.wixobj qpid_headers.wixobj boost_headers.wixobj boost_dlls.wixobj examples.wixobj api_docs.wixobj" />
    </Target>

</Project>
