MochiLibraries / Biohazrd

A framework for automatically generating binding wrappers for C/C++ libraries
MIT License
60 stars 9 forks source link

Add a translation option to require a specific version (or version range) for MSVC tool when building on Windows #199

Open PathogenDavid opened 3 years ago

PathogenDavid commented 3 years ago

Right now generator authors can pin to a specific MSVC tool version or Windows SDK version either by:

Environment.SetEnvironmentVariable("VCToolsInstallDir", @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\");

However this is not super intuitive or discoverable, it'd be nice if Biohazrd provided official means of specifying a MSVC tools version preference.

We already do this for the Windows SDK in the private Win32 and DirectX generators, see InfectedWin32.Generator.WindowsSdkHelper.

This would enable a handful of useful features:

When we fix this, we should remove the workaround added in https://github.com/InfectedLibraries/Biohazrd/issues/98 and change things to avoid the affected versions and error if they're the only ones available. (Could probably be a warning, but the issue affects most non-trivial libraries and having it be noticeable is probably better than not.)

PathogenDavid commented 3 years ago

For the sake of limiting bus factor, here's WindowsSdkHelper mentioned above: https://gist.github.com/PathogenDavid/29794af32ac1c88fdd489d27a2debb39

It doesn't actually override what Clang uses using environment variables but instead uses --no-standard-includes and -isystem based on the logic Clang uses internally.

Usage looks like this:

// We currently target Windows SDK 10.0.14393.0, which is Windows 10 1607
WindowsSdkHelper sdk = new("10.0.14393.0");
TranslatedLibraryBuilder builder = new()
{
    Options = new TranslationOptions()
    {
        SystemHeadersAreAlwaysOutOfScope = false
    }
};
sdk.ConfigureBuilder(builder);

builder.AddFile(sdk.GetHeaderFilePath("um", "Windows.h"));
builder.AddFile(sdk.GetHeaderFilePath("um", "ShellScalingApi.h"));
// ... etc