wewa
wewa

Reputation: 1678

Scripting the DLL build

I want to build a library with commonly used functions. Therefore I've created a Microsoft Visual C# 2010 Project. This project contains several *.cs files, each of them contain a single class. At the moment my project contains two files Common.cs and Vars.cs. To build the dll out of my project, I run following command in the command line.

C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /target:library /reference:some.dll /out:Library.dll Library\Common.cs Library\Vars.cs

My problem is, that I have to edit this build command if I add additional *.cs files.

How can I create a build script to automate this procedure?


EDIT: This is the solution that worked for me:

I created a file called build.xml. This is the content of this file.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->
<PropertyGroup>
    <!-- This AppName thing is the base name of your DLL or EXE -->
    <AppName>MyLibraryName</AppName>
</PropertyGroup>

<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
    <Configuration>Debug</Configuration>
    <!-- Default -->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <Optimize>false</Optimize>
    <DebugSymbols>true</DebugSymbols><!-- <OutputPath>.\bin</OutputPath>  -->
    <OutputPath>.\</OutputPath>
    <OutDir>.\</OutDir>
    <IntermediateOutputPath>.\</IntermediateOutputPath>
</PropertyGroup>

<!-- Specify the inputs by type and file name -->
<ItemGroup>
    <CSFile Include="LibraryEtasHil\*.cs"/>
</ItemGroup>

<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
   <Reference Include="mscorlib" />
   <Reference Include="System" />
   <Reference Include="System.Core" />
   <Reference Include="System.Data" />
   <Reference Include="System.Data.Linq" />
   <Reference Include="System.ServiceModel" />
   <Reference Include="System.ServiceModel.Web" />
   <Reference Include="System.Runtime.Serialization" />
   <!-- <Reference Include=".\ObjectDumper.dll" /> -->
   <Reference Include="dummy.dll" />
</ItemGroup>

<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">

    <Message Text="Reference = @(Reference)" />
    <Message Text="ReferencePath = @(ReferencePath)" />

    <!-- Message Text="MS Build Tools path:  $(MSBuildToolsPath)" / -->

    <!-- Run the Visual C# compilation on all the .cs files. -->

    <CSC
        Sources="@(CSFile)"
        References="@(ReferencePath)"
        OutputAssembly="$(OutputPath)\$(AppName).dll"
        EmitDebugInformation="$(DebugSymbols)"
        TargetType="library"
        Toolpath="$(MSBuildToolsPath)"
        Nologo="true"
    />
</Target>

<!-- redefine the Clean target, from the Microsoft.csharp.targets file.  (Last definition wins) -->
<Target Name="Clean">
    <Delete Files="$(OutputPath)\$(AppName).dll"/>
    <Delete Files="$(OutputPath)\$(AppName).pdb"/>
    <Delete Files="%(CSFile.identity)~"/>
    <Delete Files="build.xml~"/>
</Target>

To build the dll, you simply have to execute following command in the Visual studio command prompt.

msbuild build.xml

Upvotes: 1

Views: 2005

Answers (2)

wewa
wewa

Reputation: 1678

Thank you for your suggestions. I was able to create a working build script for msbuild. I created a file called build.xml. This is the content of this file.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CompileAll" ToolsVersion="3.5">

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Import Project="c:\.net3.5\Microsoft.Csharp.targets" /    -->
<PropertyGroup>
    <!-- This AppName thing is the base name of your DLL or EXE -->
    <AppName>MyLibraryName</AppName>
</PropertyGroup>

<!-- This build file compiles each .cs file into its own exe -->
<PropertyGroup Condition="'$(Configuration)'==''">
    <Configuration>Debug</Configuration>
    <!-- Default -->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <Optimize>false</Optimize>
    <DebugSymbols>true</DebugSymbols><!-- <OutputPath>.\bin</OutputPath>  -->
    <OutputPath>.\</OutputPath>
    <OutDir>.\</OutDir>
    <IntermediateOutputPath>.\</IntermediateOutputPath>
</PropertyGroup>

<!-- Specify the inputs by type and file name -->
<ItemGroup>
    <CSFile Include="LibraryEtasHil\*.cs"/>
</ItemGroup>

<!-- specify reference assemblies for all builds in this project -->
<ItemGroup>
   <Reference Include="mscorlib" />
   <Reference Include="System" />
   <Reference Include="System.Core" />
   <Reference Include="System.Data" />
   <Reference Include="System.Data.Linq" />
   <Reference Include="System.ServiceModel" />
   <Reference Include="System.ServiceModel.Web" />
   <Reference Include="System.Runtime.Serialization" />
   <!-- <Reference Include=".\ObjectDumper.dll" /> -->
   <Reference Include="dummy.dll" />
</ItemGroup>

<Target Name="CompileAll" DependsOnTargets="ResolveAssemblyReferences">

    <Message Text="Reference = @(Reference)" />
    <Message Text="ReferencePath = @(ReferencePath)" />

    <!-- Message Text="MS Build Tools path:  $(MSBuildToolsPath)" / -->

    <!-- Run the Visual C# compilation on all the .cs files. -->

    <CSC
        Sources="@(CSFile)"
        References="@(ReferencePath)"
        OutputAssembly="$(OutputPath)\$(AppName).dll"
        EmitDebugInformation="$(DebugSymbols)"
        TargetType="library"
        Toolpath="$(MSBuildToolsPath)"
        Nologo="true"
    />
</Target>

<!-- redefine the Clean target, from the Microsoft.csharp.targets file.  (Last definition wins) -->
<Target Name="Clean">
    <Delete Files="$(OutputPath)\$(AppName).dll"/>
    <Delete Files="$(OutputPath)\$(AppName).pdb"/>
    <Delete Files="%(CSFile.identity)~"/>
    <Delete Files="build.xml~"/>
</Target>

To build the dll, you simply have to execute following command in the Visual studio command prompt.

msbuild build.xml

Upvotes: 0

Quibblesome
Quibblesome

Reputation: 25409

Build the project file instead. A project file is actually an MSBuild file so you can just MsBuild the project file from the command line to get the output.

In addition if you learn how the project file works then you also learn MSBuild which is a build script language that you can use to customise builds in any way you like.

Open source alternatives are Ant and Maven but its MSBuild works well with Microsoft products so it can be easier to just stick with MsBuild.

Upvotes: 1

Related Questions