dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.17k stars 1.34k forks source link

Maintaining 2 Different sets of import statements Based on OS. #4301

Open Shivaraj117 opened 5 years ago

Shivaraj117 commented 5 years ago
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Condition=" '$(OS)' == 'Windows_NT' " Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <Import Condition=" '$(OS)' != 'Windows_NT' " Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{9DAF0084-3D8C-4871-86B9-C9024D8F9DFE}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary4</RootNamespace>
    <AssemblyName>ClassLibrary4</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{9DAF0084-3D8C-4871-86B9-C9024D8F9DFE}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary2</RootNamespace>
    <AssemblyName>ClassLibrary2</AssemblyName>    
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />    
  </ItemGroup>
   <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup> 
  <Import Condition=" '$(OS)' == 'Windows_NT' " Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Condition=" '$(OS)' != 'Windows_NT' " Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
Shivaraj117 commented 5 years ago

This one works only with '$(OS)' != 'Windows_NT'.. Why doesnt this build on windows?

dsplaisted commented 5 years ago

I'd be interested to know why you're trying to compile for a different target framework depending on the operating system. I wouldn't normally recommend this.

Can you provide more information on how this is failing? If you can provide a binlog that would help.

Finally, if you really do want to set the target framework based on the OS, I would suggest trying to do it with an entirely SDK-style project, like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <TargetFramework Condition=" '$(OS)' == 'Windows_NT' ">net461</TargetFramework>
  </PropertyGroup>

</Project>
Shivaraj117 commented 5 years ago

There is a scenario where we need to run the existing project(windows) on Linux. so i was trying this approach.