기본 콘텐츠로 건너뛰기

[ MSBUILD] 개념 잡기 - #2 환경에 따라서 빌드 구성하기.

  기본적인 설정과 동작에 관련된 부분은 이전 게시글에서 알아 보았다. 이번에는 좀 더 다양한 정보를 기준으로 활용할 수 있는 빌드 스크립트를 구성하고 실행해 보도록 한다.

New basic build script

  파일의 이름은 "msbuild-basic-template.xml" 하고 아래의 내용과 같이 새로운 구성의 빌드 스크립트를 구성하도록 하자.

<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="Build"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- 활용할 특수문자 정의 -->
    <PropertyGroup>
        <Semicolon>%3b</Semicolon>
        <Ampersand>&amp;</Ampersand>
        <LeftAngleBracket>&lt;</LeftAngleBracket>
        <RightAngleBracket>&gt;</RightAngleBracket>
        <StraightQuotationMark>&quot;</StraightQuotationMark>
        <Quot>$(StraightQuotationMark)</Quot>
        <Apostrophe>&apos;</Apostrophe>
    </PropertyGroup>

    <!-- 기본 폴더 정의 -->
    <PropertyGroup>
        <RootDir>$(MSBuildProjectDirectory)</RootDir>
    </PropertyGroup>

    <!-- 빌드 툴 정의 -->
    <PropertyGroup>
        <DevEnv>devenv.exe</DevEnv>
    </PropertyGroup>

    <!-- 설정과 환경에 대한 빌드 구성 정보 정의 (Configuration : DEBUG/RELEASE, Platform : Any CPU/x86/Win32/x64 -->
    <PropertyGroup>
        <Platform Condition=" '$(Platform)' == '' AND '$(MSBuildBinPath)'=='C:\Windows\Microsoft.NET\Framework64\v2.0.50727' ">x64</Platform>
        <Platform Condition=" '$(Platform)' == '' ">Win32</Platform>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <BuildCondition>$(Configuration)|$(Platform)</BuildCondition>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(BuildCondition)' == 'Debug|Win32' ">
    </PropertyGroup>

    <PropertyGroup Condition=" '$(BuildCondition)' == 'Release|x64' ">
    </PropertyGroup>

    <ItemGroup>
        <ProjectReferences Include="$(RootDir)\TestProject.sln">
            <Configuration>$(Configuration)</Configuration>
            <Platform>$(Platform)</Platform>
            <BuildCondition>%(Configuration)|%(Platform)</BuildCondition>
        </ProjectReferences>
    </ItemGroup>

    <Target Name="Clean">
        <Message Text="타겟: Clean" />
        <Exec Command="$(DevEnv) $(Quot)%(ProjectReferences.FullPath)$(Quot) /Clean $(Quot)%(ProjectReferences.BuildCondition)$(Quot)"
              ContinueOnError="false"
              IgnoreExitCode="false"
        />
    </Target>

    <Target Name="Build">
        <Message Text="타겟: Build" />
        <Message Text="빌드 조건: '$(BuildCondition)'"
                 Importance="high" />

        <Exec Command="$(DevEnv) $(Quot)%(ProjectReferences.FullPath)$(Quot) /Build $(Quot)%(ProjectReferences.BuildCondition)$(Quot)"
              ContinueOnError="false"
              IgnoreExitCode="false"
        />
    </Target>

    <Target Name="Rebuild"
            DependsOnTargets="Clean; Build">
        <Message Text="타겟: Rebuild" />
    </Target>
</Project>
    위의 코드를 기준으로 필요한 기능들을 상황에 따라서 추가해서 작업을 하면 된다.

Folders

  만일 특정한 솔루션을 빌드하여야 하는데 특정 폴더들에 대해서 작업을 하여야 하는 상황이라면 아래와 같이 폴더 부분을 조정해 주면 된다.

<!-- 기본 폴더 정의 -->
<PropertyGroup>
    <RootDir>$(MSBuildProjectDirectory)</RootDir>
    <!-- 공유 폴터 -->
    <SharedDir>$(RootDir)\..\Shared</SharedDir>
</PropertyGroup>
  당연한 말이지만 특정 폴더와 같이 자동화라는 것이 무색하게 지정을 해서는 안 된다.

Conditional Configuration

  이제 빌드를 위한 설정과 플랫폼등에 대한 정보를 설정하면 된다.

<!-- 설정과 환경에 대한 빌드 구성 정보 정의 (Configuration : DEBUG/RELEASE, Platform : Any CPU/x86/Win32/x64 -->
<PropertyGroup>
    <Platform Condition=" '$(Platform)' == '' AND '$(MSBuildBinPath)'=='C:\Windows\Microsoft.NET\Framework64\v2.0.50727' ">x64</Platform>
    <Platform Condition=" '$(Platform)' == '' ">Win32</Platform>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <BuildCondition>$(Configuration)|$(Platform)</BuildCondition>
</PropertyGroup>
  Platform 은 x64, x86 (또는 Win32) 의 빌드인지를 의미하는 것이다. 위의 설정은 $(Platform) 이 사전에 정의된 값이 없는 경우에 조건을 지정해서 값을 지정하는 설정이다.

Solution Setting

<ItemGroup>
    <ProjectReferences Include="$(RootDir)\TestProject.sln">
        <Configuration>$(Configuration)</Configuration>
 <Platform>$(Platform)</Platform>
 <BuildCondition>%(Configuration)|%(Platform)</BuildCondition>
    </ProjectReferences>
</ItemGroup>
  TestProject.sln 이라는 솔루션 파일을 설정하고 상위에서 정의된 $(Configuration), $(Platform) 값에 따라서 빌드를 하게 된다. 이런 조합이 나올 수 있는 경우는 다음과 같다.

  • Debug|Win32
  • Release|Win32
  • Debug|x64
  • Release|x64
Solution Build

<Target Name="Build">
    <Message Text="타겟: Build" />
    <Message Text="빌드 조건: '$(BuildCondition)'"
             Importance="high" />

    <Exec Command="$(DevEnv) $(Quot)%(ProjectReferences.FullPath)$(Quot) /Build $(Quot)%(ProjectReferences.BuildCondition)$(Quot)"
          ContinueOnError="false"
          IgnoreExitCode="false"
    />
</Target>
  <Exec> 에서 설정된 것과 같이 $(DevEnv) 를 호출해서 실행하게 된다. 구성된 Command 가 복잡해 보이지만 결국 풀어서 쓰면 아래와 같은 실행 명령인 것이다.

"devenv.exe "C:\Temp\TestProjects.sln" /Build "Debug|Win32"
  위에 정의된 $(DevEnv), $(Quot), %(ProjectReferences.FullPath), %(ProjectReferences.BuildCondition) 등의 변수를 치환하면 좀 더 이해하기 쉽게 생각할 수 있다.

Execute with Configuration, Platform

  다음과 같이 Platform 의 경우는 값을 설정해 줄 수가 있다.

[ Win32 빌드 배치 파일 ]
@echo off
SETLOCAL
call "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x86
"%WINDIR%\Microsoft.NET\Framework\v3.5\msbuild.exe" %*
SET ERR_LEVEL=%errorlevel%
ENDLOCAL
exit /b %ERR_LEVEL%

[ x64 빌드 배치 파일 ]
@echo off
SETLOCAL
call "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x64
"%WINDIR%\Microsoft.NET\Framework64\v3.5\msbuild.exe" %*
SET ERR_LEVEL=%errorlevel%
ENDLOCAL
exit /b %ERR_LEVEL%
  위의 빌드 배치 파일과 같이 붉은 색으로 표시된 정보를 설정하므로써 Platform 정보와 관련된 MSBuild 실행 파일을 구분해서 처리하면 된다.

  그리고 Debug 또는 Release 에 대한 Confiugration 정보는 빌드 배치 파일에 인자로 설정하면 전달하여 처리가 가능하다.

msbuild_vs2013_x86 build.xml /p:Configuration=Debug
  이상으로 환경에 따라서 빌드를 구성하는 방법에 대해서 기존 게시물에 추가해서 알아 보았다. 기존 설정과 별로 달라진 것은 없지만 이렇게 하나씩 필요한 상황들을 추가하면서 처리를 하면 된다.

MSBuild 예약 속성들

  Visual Studio 2013 을 설치해서 사용하는 환경을 기준으로 아래에 정리한 내용과 같이 이미 MSBuild에서 예약으로 설정되어 있는 값들이 존재한다.

Property
Description
Reserved or Well-Known
MSBuildBinPath
MSBuild 실행 파일이 존재하는 경로를 의미한다.
(Ex, C:\Windows\Microsoft.Net\Framework\versionNumber).
맨 뒤의 "\"는 포함되면 안 된다.
예약
MSBuildExtensionsPath
Introduced in the .NET Framework 4: there is no difference between the default values of MSBuildExtensionsPath andMSBuildExtensionsPath32. You can set the environment variableMSBUILDLEGACYEXTENSIONSPATH to a non-null value to enable the behavior of the default value of MSBuildExtensionsPath in earlier versions.
In the .NET Framework 3.5 and earlier, the default value ofMSBuildExtensionsPath points to the path of the MSBuild subfolder under the \Program Files\ or \Program Files (x86) folder, depending on the bitness of the current process. For example, for a 32-bit process on a 64-bit machine, this property points to the \Program Files (x86) folder. For a 64-bit process on a 64-bit machine, this property points to the \Program Files folder.
Do not include the final backslash on this property.
This location is a useful place to put custom target files. For example, your target files could be installed at \Program Files\MSBuild\MyFiles\Northwind.targets and then imported in project files by using this XML code:
<Import Project="$(MSBuildExtensionsPath)\MyFiles\Northwind.targets"/>
Well-Known
MSBuildExtensionsPath32
The path of the MSBuild subfolder under the \Program Files or \Program Files (x86) folder. This path always points to the 32-bit \Program Files folder on a 32-bit machine and \Program Files (x86) on a 64-bit machine. See also MSBuildExtensionsPath and MSBuildExtensionsPath64.
Do not include the final backslash on this property.
Well-Known
MSBuildExtensionsPath64
The path of the MSBuild subfolder under the \Program Files folder. For a 64-bit machine, this path always points to the \Program Files folder. For a 32-bit machine, this path is blank. See also MSBuildExtensionsPath andMSBuildExtensionsPath32.
Do not include the final backslash on this property.
Well-Known
MSBuildLastTaskResult
true if the previous task completed without any errors (even if there were warnings), or false if the previous task had errors. Typically, when an error occurs in a task, the error is the last thing that happens in that project. Therefore, the value of this property is never false, except in these scenarios:
Reserved
MSBuildNodeCount
The maximum number of concurrent processes that are used when building. This is the value that you specified for /maxcpucount on the command line. If you specified /maxcpucount without specifying a value, then MSBuildNodeCount specifies the number of processors in the computer. For more information, see MSBuild Command-Line Referenceand Building Multiple Projects in Parallel with MSBuild.
Reserved
MSBuildProgramFiles32
The location of the 32-bit program folder; for example, C:\Program Files (x86).
Do not include the final backslash on this property.
Reserved
MSBuildProjectDefaultTargets
The complete list of targets that are specified in the DefaultTargetsattribute of the Project element. For example, the following Projectelement would have an MSBuildDefaultTargets property value of A;B;C:
<Project DefaultTargets="A;B;C" >
Reserved
MSBuildProjectDirectory
MSBuild 가 솔루션이나 프로젝트를 빌드할 때 사용할 폴더를 의미하는 것으로 보통은 빌드 스크립트가 존재하는 경로라고 이해하면 된다.
(Ex. C:\MyCompany\MyProduct)
마지막에 "\"는 포함되면 안 된다.
예약
MSBuildProjectDirectoryNoRoot
The value of the MSBuildProjectDirectory property, excluding the root drive.
Do not include the final backslash on this property.
Reserved
MSBuildProjectExtension
The file name extension of the project file, including the period; for example, .proj.
Reserved
MSBuildProjectFile
The complete file name of the project file, including the file name extension; for example, MyApp.proj.
Reserved
MSBuildProjectFullPath
The absolute path and complete file name of the project file, including the file name extension; for example, C:\MyCompany\MyProduct\MyApp.proj.
Reserved
MSBuildProjectName
The file name of the project file without the file name extension; for example, MyApp.
Reserved
MSBuildStartupDirectory
The absolute path of the folder where MSBuild is called. By using this property, you can build everything below a specific point in a project tree without creating dirs.proj files in every directory. Instead, you have just one project—for example, c:\traversal.proj, as shown here:
<Project ...>
    <ItemGroup>
        <ProjectFiles  
           Include="$
           (MSBuildStartupDirectory)
           **\*.csproj"/>
    </ItemGroup>
    <Target Name="build">
        <MSBuild    
        Projects="@(ProjectFiles)"/>
    </Target>
</Project>
To build at any point in the tree, type:
msbuild c:\traversal.proj
Do not include the final backslash on this property.
Reserved
MSBuildThisFile
The file name and file extension portion of MSBuildThisFileFullPath.
Reserved
MSBuildThisFileDirectory
The directory portion of MSBuildThisFileFullPath.
Include the final backslash in the path.
Reserved
MSBuildThisFileDirectoryNoRoot
The directory portion of MSBuildThisFileFullPath, excluding the root drive.
Include the final backslash in the path.
Reserved
MSBuildThisFileExtension
The file name extension portion of MSBuildThisFileFullPath.
Reserved
MSBuildThisFileFullPath
The absolute path of the project or targets file that contains the target that is running.
Tip Tip
You can specify a relative path in a targets file that's relative to the targets file and not relative to the original project file.
Reserved
MSBuildThisFileName
The file name portion of MSBuildThisFileFullPath, without the file name extension.
Reserved
MSBuildToolsPath
The installation path of the MSBuild version that's associated with the value of MSBuildToolsVersion.
Do not include the final backslash in the path.
This property cannot be overridden.
Reserved
MSBuildToolsVersion
The version of the MSBuild Toolset that is used to build the project.
Note Note
An MSBuild Toolset consists of tasks, targets, and tools that are used to build an application. The tools include compilers such as csc.exe and vbc.exe. For more information, see MSBuild Toolset (ToolsVersion), and Standard and Custom Toolset Configurations.
Reserved

댓글