Skip to content

Commit

Permalink
Fixed #205 via TfmIdentifier for modern env
Browse files Browse the repository at this point in the history
  • Loading branch information
3F committed Dec 17, 2024
1 parent 0e4a0da commit b3d4168
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .tools/net.r_eg.DllExport.targets
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<DllExportLibToolPath Condition="'$(DllExportLibToolPath)'==''">$(DllExportVSRoot)VC\bin</DllExportLibToolPath>
<DllExportLibToolDllPath Condition="'$(DllExportLibToolDllPath)'==''">$(DllExportVSBin)</DllExportLibToolDllPath>
<DllExportTargetFrameworkVersion Condition="'$(DllExportTargetFrameworkVersion)'==''">$(TargetFrameworkVersion)</DllExportTargetFrameworkVersion>
<DllExportTargetFrameworkIdentifier Condition="'$(DllExportTargetFrameworkIdentifier)'==''">$(TargetFrameworkIdentifier)</DllExportTargetFrameworkIdentifier>
<DllExportSdkPath Condition="'$(DllExportSdkPath)'==''">$(DllExportILAsmCustomPath);$(TargetFrameworkSDKToolsDirectory)</DllExportSdkPath>
<DllExportSkipOnAnyCpu Condition="'$(DllExportSkipOnAnyCpu)'==''">$(NoDllExportsForAnyCpu)</DllExportSkipOnAnyCpu>
<DllExportDDNSCecil Condition="'$(DllExportDDNSCecil)'==''">true</DllExportDDNSCecil>
Expand Down Expand Up @@ -177,6 +178,7 @@
LibToolPath="$(DllExportLibToolPath)"
LibToolDllPath="$(DllExportLibToolDllPath)"
TargetFrameworkVersion="$(DllExportTargetFrameworkVersion)"
TargetFrameworkIdentifier="$(DllExportTargetFrameworkIdentifier)"
SdkPath="$(DllExportSdkPath)"
SkipOnAnyCpu="$(DllExportSkipOnAnyCpu)"
OrdinalsBase="$(DllExportOrdinalsBase)"
Expand Down
6 changes: 6 additions & 0 deletions src/DllExport/Activator/DllExportActivatorTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public string TargetFrameworkVersion
set => exportTask.TargetFrameworkVersion = value;
}

public string TargetFrameworkIdentifier
{
get => exportTask.TargetFrameworkIdentifier;
set => exportTask.TargetFrameworkIdentifier = value;
}

public string Platform
{
get => exportTask.Platform;
Expand Down
69 changes: 46 additions & 23 deletions src/DllExport/Activator/ExportTaskImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace net.r_eg.DllExport.Activator
{
using TCallbackTool = Func<Version, string, string>;
using TCallbackTool = Func<TfmIdentifier, Version, string, string>;

public class ExportTaskImplementation<TTask>: IInputValues, IServiceContainer, IServiceProvider where TTask : IDllExportTask, ITask
{
Expand Down Expand Up @@ -58,6 +58,8 @@ public CpuPlatform Cpu

public string TargetFrameworkVersion { get; set; }

public string TargetFrameworkIdentifier { get; set; }

public bool? SkipOnAnyCpu { get; set; }

public DebugType EmitDebugSymbols
Expand Down Expand Up @@ -511,7 +513,7 @@ private TCallbackTool GetGetToolPathInternal(string methodName)
.MakeGenericMethod(targetDotNetFrameworkVersionType)
.Invoke(null, new object[] { method });

Func<string, int, string> getToolPath = ((n, v) =>
string getToolPath(string n, int v)
{
try
{
Expand All @@ -521,22 +523,33 @@ private TCallbackTool GetGetToolPathInternal(string methodName)
{
return null;
}
});
}

Func<string, int> getNum = delegate(string version) {
int getNum(string version)
{
return (int)Enum.Parse(targetDotNetFrameworkVersionType, version);
};
}

const string _V_LATEST = "Latest";

return ((version, toolName) =>
return (ident, version, toolName) =>
{
int num;

// TargetDotNetFrameworkVersion Enumeration: https://msdn.microsoft.com/en-us/library/ms126273.aspx
try {
num = getNum($"Version{version.Major}{version.Minor}");
try
{
num = getNum
(
ident == TfmIdentifier.NETFramework
? $"{nameof(Version)}{version.Major}{version.Minor}"
: nameof(Version) + _V_LATEST // latest released for current env
);
}
catch(ArgumentException) {
num = getNum("VersionLatest"); // try with latest released version
catch(ArgumentException)
{
if(ident != TfmIdentifier.NETFramework) throw;
num = getNum(nameof(Version) + _V_LATEST);
}

string path = getToolPath(toolName, num);
Expand Down Expand Up @@ -564,7 +577,7 @@ private TCallbackTool GetGetToolPathInternal(string methodName)
return null;
}
return path;
});
};
}

protected Type GetToolLocationHelperTypeFromRegsitry()
Expand Down Expand Up @@ -708,40 +721,50 @@ private bool ValidateToolPath(string toolFileName, string currentValue, TCallbac

private bool TryToGetToolDirForFxVersion(string toolFileName, TCallbackTool getToolPath, ref string toolDirectory)
{
Version frameworkVersion = GetFrameworkVersion();
if(frameworkVersion.Major < 1) {
Version tfmVersion = GetTfmVersion(TargetFrameworkVersion);
TfmIdentifier tfmIdent = GetTfmIdentifier(TargetFrameworkIdentifier);

if(tfmIdent == TfmIdentifier.NETFramework && tfmVersion.Major < 1)
{
return false;
}

if(getToolPath != null)
{
string path = getToolPath(frameworkVersion, toolFileName);
string path = getToolPath(tfmIdent, tfmVersion, toolFileName);
if(path != null && File.Exists(path))
{
toolDirectory = Path.GetDirectoryName(path);
return true;
}
this._ActualTask.Log.LogError(string.Format(Resources.ToolLocationHelperTypeName_could_not_find_1, (object)"Microsoft.Build.Utilities.ToolLocationHelper", (object)toolFileName));
_ActualTask.Log.LogError(string.Format(Resources.ToolLocationHelperTypeName_could_not_find_1, "Microsoft.Build.Utilities.ToolLocationHelper", toolFileName));
return false;
}
if(!(frameworkVersion >= ExportTaskImplementation<TTask>._VersionUsingToolLocationHelper))

// TODO
if(!(tfmVersion >= _VersionUsingToolLocationHelper))
{
return false;
}
this._ActualTask.Log.LogError(string.Format(Resources.Cannot_get_a_reference_to_ToolLocationHelper, (object)"Microsoft.Build.Utilities.ToolLocationHelper"));

_ActualTask.Log.LogError(string.Format(Resources.Cannot_get_a_reference_to_ToolLocationHelper, "Microsoft.Build.Utilities.ToolLocationHelper"));
return false;
}

private Version GetFrameworkVersion()
private Version GetTfmVersion(string frameworkVersion)
{
string frameworkVersion = this.TargetFrameworkVersion;
if(!ExportTaskImplementation<TTask>.PropertyHasValue(frameworkVersion))
{
return (Version)null;
}
if(!PropertyHasValue(frameworkVersion)) return null;
return new Version(frameworkVersion.TrimStart('v', 'V'));
}

private TfmIdentifier GetTfmIdentifier(string raw) => raw?.Trim(' ', '.').ToLowerInvariant() switch
{
"netcoreapp" => TfmIdentifier.NETCoreApp,
"netstandard" => TfmIdentifier.NETStandard,
null or "" or "netframework" => TfmIdentifier.NETFramework,
_ => TfmIdentifier.NETFramework, //TODO
};

// FIXME: two different places (see IlDasm.Run) for the same thing ! be careful
private bool ValidateSdkPath()
{
Expand Down
25 changes: 6 additions & 19 deletions src/DllExport/Activator/IDllExportTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,14 @@ namespace net.r_eg.DllExport.Activator
{
public interface IDllExportTask: IInputValues, IServiceProvider
{
TaskLoggingHelper Log
{
get;
}
TaskLoggingHelper Log { get; }

bool? SkipOnAnyCpu
{
get;
set;
}
bool? SkipOnAnyCpu { get; set; }

string TargetFrameworkVersion
{
get;
set;
}
string TargetFrameworkVersion { get; set; }

string Platform
{
get;
set;
}
string TargetFrameworkIdentifier { get; set; }

string Platform { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/DllExport/Activator/TfmIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*!
* Copyright (c) Robert Giesecke
* Copyright (c) Denis Kuzmin <x-3F@outlook.com> github/3F
* Copyright (c) DllExport contributors https://github.com/3F/DllExport/graphs/contributors
* Licensed under the MIT License (MIT).
* See accompanying LICENSE.txt file or visit https://github.com/3F/DllExport
*/

namespace net.r_eg.DllExport.Activator
{
internal enum TfmIdentifier
{
/// <summary>
/// TargetFrameworkIdentifier = ".NETFramework"
/// or TargetFrameworkIdentifier = "" (MSBuild 15 or less)
/// </summary>
NETFramework,

/// <summary>
/// TargetFrameworkIdentifier = ".NETStandard"
/// </summary>
NETStandard,

/// <summary>
/// TargetFrameworkIdentifier = ".NETCoreApp"
/// </summary>
NETCoreApp
}
}
1 change: 1 addition & 0 deletions src/DllExport/Wizard/UI/SimpleConfFormater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public string Parse(IProject prj)
"TargetFramework",
"TargetFrameworks",
"TargetFrameworkVersion",
"TargetFrameworkIdentifier",
"RootNamespace",
"AssemblyName",
"DebugType",
Expand Down

0 comments on commit b3d4168

Please sign in to comment.