Click here to Skip to main content
15,886,199 members
Articles / MSBuild

Flex/Bison on MSBuild

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
14 Jan 2014BSD2 min read 14.5K   103   3  
Integrating GnuWin32 Flex/Bison preprocessing into the MSBuild build process under Visual Studio
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{19a3fe21-da8e-4c1f-af9e-287ee08dc921}</ProjectGuid>
    <RootNamespace>parserc</RootNamespace>
    <ProjectName>parserc</ProjectName>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>StaticLibrary</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v110</PlatformToolset>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    <ConfigurationType>StaticLibrary</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <PlatformToolset>v110</PlatformToolset>
    <WholeProgramOptimization>true</WholeProgramOptimization>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Platform)'=='Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup Condition="'$(Configuration)'=='Debug'">
    <ObjDir>objd</ObjDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)'=='Release'">
    <ObjDir>obj</ObjDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <OutDir>$(ObjDir)\</OutDir>
    <IntDir>$(ObjDir)\</IntDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <OutDir>$(ObjDir)\</OutDir>
    <IntDir>$(ObjDir)\</IntDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32|NEVER'">
    <IncludePath>$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);..\..\</IncludePath>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <WarningLevel>EnableAllWarnings</WarningLevel>
      <Optimization>Disabled</Optimization>
      <PreprocessorDefinitions>___ncplusplus;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <ExceptionHandling>false</ExceptionHandling>
      <DisableLanguageExtensions>true</DisableLanguageExtensions>
      <TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
      <SDLCheck>false</SDLCheck>
      <CompileAs>CompileAsC</CompileAs>
      <UndefinePreprocessorDefinitions>__cplusplus</UndefinePreprocessorDefinitions>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>MaxSpeed</Optimization>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <PreprocessorDefinitions>___ncplusplus;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <ExceptionHandling>false</ExceptionHandling>
      <DisableLanguageExtensions>true</DisableLanguageExtensions>
      <TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
      <SDLCheck>false</SDLCheck>
      <CompileAs>CompileAsC</CompileAs>
      <UndefinePreprocessorDefinitions>__cplusplus</UndefinePreprocessorDefinitions>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup>
    <ClCompile Include="lex.yy.c" />
    <ClCompile Include="parser.tab.c" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="cstrutil.h" />
    <ClInclude Include="debugio.h" />
    <ClInclude Include="md5.h" />
    <ClInclude Include="nodeio.h" />
    <ClInclude Include="parser_nodes.h" />
    <ClInclude Include="scanner_tokens.h" />
    <ClInclude Include="symbols.h" />
  </ItemGroup>
  <!-- Begin custom Flex/Bison preprocessor -->
  <ItemGroup>
    <None Include="lexer.l">
      <GnuType>Flex</GnuType>
    </None>
    <None Include="parser.y">
      <GnuType>Bison</GnuType>
    </None>
  </ItemGroup>
  <PropertyGroup>
    <FlexBinary>C:\Program Files (x86)\GnuWin32\bin\flex.exe</FlexBinary>
    <BisonBinary>C:\Program Files (x86)\GnuWin32\bin\bison.exe</BisonBinary>
  </PropertyGroup>
  <Target Name="gnu-build-prep" BeforeTargets="gnu-bison;gnu-flex;" Condition=" $(BisonBinary.Contains(' ')) ">
    <!-- GnuWin32 bison not only needs to see itself on the path, but it also must be run with the working directory set to its binary location.
         The working directory additionally may not contain spaces. -->
    <!-- We create and softlink a working directory at a known path guaranteed to fit these requirements, so that we can build from there later. -->
    <!-- A side effect of the linking process is that this project must be built as administrator the first time it is run on the host machine. -->
    <PropertyGroup>
      <GnuBuild>c:\temp\Build\GnuWin32</GnuBuild>
      <BisonBinDir>$([System.IO.Path]::GetDirectoryName($(BisonBinary)))</BisonBinDir>
    </PropertyGroup>
    <Exec Command="mklink /D $(GnuBuild) &quot;$(BisonBinDir)\..\&quot;" Condition=" !Exists('$(GnuBuild)\bin\$([MSBuild]::MakeRelative($(BisonBinDir),$(BisonBinary)))') " />
    <PropertyGroup>
      <BisonBinary>$(GnuBuild)\bin\$([MSBuild]::MakeRelative($(BisonBinDir),$(BisonBinary)))</BisonBinary>
      <BisonBinDir>$([System.IO.Path]::GetDirectoryName($(BisonBinary)))</BisonBinDir>
    </PropertyGroup>
  </Target>
  <Target Name="gnu-flex">
    <ItemGroup>
      <FlexDefinition Include="@(None)" Condition=" '%(GnuType)' == 'Flex' " />
    </ItemGroup>
    <Message Text="Pre-procssing with GNU Flex: `$(FlexBinary)`" Importance="High" />
    <Message Text="&quot;$(FlexBinary)&quot; -o@(FlexDefinition -> '%(Filename).yy.c')  @(FlexDefinition)" Importance="High" />
    <Exec Command="&quot;$(FlexBinary)&quot; -o@(FlexDefinition -> '%(Filename).yy.c')  @(FlexDefinition)" WorkingDirectory="@(FlexDefinition-&gt;DirectoryName())" Outputs="@(FlexDefinition -> '@(Filename).yy.c')" />
  </Target>
  <Target Name="gnu-bison" BeforeTargets="Build">
    <ItemGroup>
      <BisonDefinition Include="@(None)" Condition=" '%(GnuType)' == 'Bison' " />
    </ItemGroup>
    <Message Text="Pre-procssing with GNU Bison: `$(BisonBinary)`" Importance="High" />
    <Message Text="set PATH=%PATH%;$(BisonBinDir)&amp;$(BisonBinary) -o@(BisonDefinition -> '%(Filename).tab.c') --defines=@(BisonDefinition -> '%(Filename).h') @(BisonDefinition)" Importance="High" />
    <Exec Command="set PATH=%PATH%;$(BisonBinDir)&amp;$(BisonBinary) -o@(BisonDefinition -> '%(Filename).tab.c') --defines=@(BisonDefinition -> '%(Filename).h') @(BisonDefinition)" WorkingDirectory="@(BisonDefinition-&gt;DirectoryName())" Outputs="@(BisonDefinition -> '@(Filename).tab.c')" />
  </Target>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
  <PropertyGroup>
    <BuildDependsOn>gnu-flex; gnu-bison;$(BuildDependsOn)</BuildDependsOn>
  </PropertyGroup>
</Project>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions