Build multiple C# project configurations simultaneously with .csproj variables

Imagine having a mathematics library, which provides some functionality optimized for IEEE754 number types. The code might have something along the lines of:

#if F16
    global using Scalar = System.Half;
#elif F32
    global using Scalar = System.Single;
#elif F32
    global using Scalar = System.Double;
#endif

// ...

or

#if F32
    public static float HighlyOptimizedMethod(float a, float b, float c) { ... };
#elif F64
    public static double HighlyOptimizedMethod(double a, double b, double c) { ... };
#endif

I now define those preprocessor constants F16, F32, F64 in my .csproj project file as follows:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <!-- change the following for different precisions. -->
        <!--<Scalar>F16</Scalar>-->
        <!--<Scalar>F32</Scalar>-->
        <Scalar>F64</Scalar>

        <DefineConstants>$(DefineConstants);$(Scalar)</DefineConstants>
    </PropertyGroup>
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>

        <AssemblyName Condition="'$(Scalar)' != ''">mylibrary.$(Scalar)</AssemblyName>
        <AssemblyName Condition="'$(Scalar)' == ''">mylibrary</AssemblyName>
    
        ...
    </PropertyGroup>
</Project>

My question

Is it possible to make VisualStudio/dotnet/MSBuild create the assemblies for all three configurations at the same time when building the project? i.e. would it be possible to run ‘compile’ and then have the following file list in the bin/ directory?

  • mylibrary.F16.dll
  • mylibrary.F32.dll
  • mylibrary.F64.dll

I tried fiddling around with something like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <!-- change the following for different precisions. if all are commented out, every configuration will be built. -->
   
        <!--<Scalar>F16</Scalar>-->
        <!--<Scalar>F32</Scalar>-->
        <Scalar>F64</Scalar>

        <DefineConstants>$(DefineConstants);$(Scalar)</DefineConstants>
    </PropertyGroup>
    <Target Name="BuildOthers" BeforeTargets="CoreBuild" Condition="'$(Scalar)' == ''">
        <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F16" />
        <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F32" />
        <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F64" />
    </Target>
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>

        ...

But it didn’t get it to work.
May someone be able to help me and/or point me into the right direction?

Thanks.

5

would it be possible to run ‘compile’ and then have the following file list in the bin/ directory?

Yes. You can check the following like this:

<PropertyGroup>
 <Scalar>''</Scalar>
 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</PropertyGroup>
    <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F16" />
    <Copy
       SourceFiles="objDebugnet8.0mylibrary.F16.dll"
       DestinationFolder="bin$(Configuration)Resources"
    />
    <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F32" />
    <Copy
       SourceFiles="objDebugnet8.0mylibrary.F32.dll"
       DestinationFolder="bin$(Configuration)Resources"
    />
    <MSBuild Projects="$(MSBuildProjectFile)" Properties="Configuration=$(Configuration);Scalar=F64" />
    <Copy
       SourceFiles="objDebugnet8.0mylibrary.F64.dll"
       DestinationFolder="bin$(Configuration)Resources"
    />
</Target>

But it didn’t get it to work(target BuildOthers)

Based on my test, i found the mylibrary.F12.dll and mylibrary.F32.dll are removed by target IncrementalClean.

1.delte mylibrary.F12.dll after generating mylibrary.F32.dll

2.delte mylibrary.F32.dll after generating mylibrary.F64.dll

As we know, IncrementalClean will remove files that were produced in a prior build. In this mode, it is not possible to have the three .dll file in bin folder when run msbuild. To prevent files to be removed during incremental build, you can add <copy> task to copy .dll files to bin folder.

Also you can try to disable incremental build. For more information, please read doc:–no-incremental

Besides, as the easiest way to achieve it ,you can try @Jonathan suggestion.
Here is a similar ticket you can refer:

How to create multiple assets (dll) for one project with msbuild depend on Constants value?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật