Skip to content

Commit

Permalink
Merge pull request #1919 from Nexus-Mods/fix-system-extractor
Browse files Browse the repository at this point in the history
Fixed: Building NMA with System Extractor
  • Loading branch information
Sewer56 authored Sep 11, 2024
2 parents 7552bfb + 622c4d2 commit 05fedad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

<!--
Listing of all build flags.
i.e. for `dotnet build -p:DefineConstants="FLAGS"`
Functionality:
NEXUSMODS_APP_USE_SYSTEM_EXTRACTOR: Use system `7z` binary for extraction.
- This is intended for Linux package maintainers.
- Alternatively, this is available with `-p:UseSystemExtractor=true`
Deployment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,16 @@ private static string GetExtractorExecutableFileName()

private static string GetExtractorExecutable(IFileSystem fileSystem)
{
var fileName = GetExtractorExecutableFileName();
if (UseSystemExtractor) return fileName;
if (UseSystemExtractor)
{
// Depending on the user's distro and package of choice, 7z
// may have different names, so we'll check for the common ones.
return !OSInformation.IsLinux
? GetExtractorExecutableFileName() :
FindSystem7zOnLinux();
}

var fileName = GetExtractorExecutableFileName();
var directory = OSInformation.MatchPlatform(
onWindows: static () => "runtimes/win-x64/native/",
onLinux: static () => "runtimes/linux-x64/native/",
Expand All @@ -221,9 +228,24 @@ private static string GetExtractorExecutable(IFileSystem fileSystem)
/// See https://github.com/Nexus-Mods/NexusMods.App/issues/1306 for details.
/// </remarks>
private const bool UseSystemExtractor =
#if USE_SYSTEM_EXTRACTOR
#if NEXUSMODS_APP_USE_SYSTEM_EXTRACTOR
true;
#else
false;
#endif

private static string FindSystem7zOnLinux()
{
string[] potentialBinaryNames = ["7z", "7zz", "7zzs"];
var pathDirectories = Environment.GetEnvironmentVariable("PATH")!.Split(':');

foreach (var pathDir in pathDirectories)
foreach (var binaryName in potentialBinaryNames)
{
if (File.Exists(Path.Combine(pathDir, binaryName)))
return binaryName;
}

throw new Exception("Cannot find system 7z binary in PATH");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
<_NativeRuntime Condition=" '$(_NativeRuntime)' == '' And '$(_IsWindows)' == 'true' And ('$(Prefer32Bit)' == 'true' Or '$(PlatformTarget)' == 'x86')">win-x86</_NativeRuntime>
<_NativeRuntime Condition=" '$(_NativeRuntime)' == '' And '$(_IsWindows)' == 'true' And ('$(Prefer32Bit)' == 'false' Or '$(PlatformTarget)' == 'x64')">win-x64</_NativeRuntime>

<_UseSystemExtractor Condition="'$(NEXUSMODS_APP_USE_SYSTEM_EXTRACTOR)' == '1'">true</_UseSystemExtractor>
<_UseSystemExtractor Condition="$([System.String]::new('$(DefineConstants)').Contains('NEXUSMODS_APP_USE_SYSTEM_EXTRACTOR'))">true</_UseSystemExtractor>
</PropertyGroup>

<PropertyGroup Condition="'$(_UseSystemExtractor)' == 'true'">
<DefineConstants>USE_SYSTEM_EXTRACTOR</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="$(UseSystemExtractor) == 'true'">
<DefineConstants>$(DefineConstants);NEXUSMODS_APP_USE_SYSTEM_EXTRACTOR</DefineConstants>
<_UseSystemExtractor>true</_UseSystemExtractor>
</PropertyGroup>

<ItemGroup Condition="'$(_UseSystemExtractor)' == '' Or '$(_UseSystemExtractor)' == 'false'">
<!-- Copy all if platform not specified -->
<None Condition="'$(PlatformTarget)' == 'AnyCPU' Or '$(PlatformTarget)' == ''" Include="$(MSBuildThisFileDirectory)../runtimes/**" Exclude="$(MSBuildThisFileDirectory)*.targets">
Expand Down

0 comments on commit 05fedad

Please sign in to comment.