Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fix/uwp build tools (#699)
Browse files Browse the repository at this point in the history
* fist pass at trying to update and fix the uwp build tools.

Likely we need to break this out into the uwp upm package properly

* added arm64 build support to xrtk build window
  • Loading branch information
StephenHodgson authored Nov 26, 2020
1 parent 6acfb1e commit 3a3ce5a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public static class ProcessExtensions
/// <param name="args">The passed arguments.</param>
/// <param name="output">The output of the process.</param>
/// <param name="application">The Application to run through the command line. Default application is "cmd.exe"</param>
/// <param name="usePlatformArgs">Add the platform command switch to the arguments?</param>
/// <returns>Output string.</returns>
/// <remarks>This process will block the main thread of the editor if command takes too long to run. Use <see cref="RunAsync(Process,string,string,bool,CancellationToken)"/> for a background process.</remarks>
public static bool Run(this Process process, string args, out string output, string application = "")
/// <remarks>This process will block the main thread of the editor if command takes too long to run. Use <see cref="RunAsync(Process,string,string,bool,CancellationToken,bool)"/> for a background process.</remarks>
public static bool Run(this Process process, string args, out string output, string application = "", bool usePlatformArgs = true)
{
if (string.IsNullOrEmpty(args))
{
Expand All @@ -34,7 +35,10 @@ public static bool Run(this Process process, string args, out string output, str
return false;
}

SetupPlatformArgs(ref args, ref application);
if (usePlatformArgs)
{
SetupPlatformArgs(ref args, ref application);
}

process.StartInfo = new ProcessStartInfo
{
Expand Down Expand Up @@ -100,10 +104,14 @@ public static async Task<ProcessResult> RunAsync(this Process process, string ar
/// <param name="args">The Process arguments.</param>
/// <param name="showDebug">Should output debug code to Editor Console?</param>
/// <param name="cancellationToken"></param>
/// <param name="setPlatformArgs">Add the command platform switch to the arguments?</param>
/// <returns><see cref="ProcessResult"/></returns>
public static async Task<ProcessResult> RunAsync(this Process process, string args, string application = "", bool showDebug = false, CancellationToken cancellationToken = default)
public static async Task<ProcessResult> RunAsync(this Process process, string args, string application = "", bool showDebug = false, CancellationToken cancellationToken = default, bool setPlatformArgs = true)
{
SetupPlatformArgs(ref args, ref application);
if (setPlatformArgs)
{
SetupPlatformArgs(ref args, ref application);
}

return await RunAsync(process, new ProcessStartInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private enum Architecture
x86 = 0,
x64 = 1,
ARM = 2,
ARM64 = 3,
}

#endregion Internal Types
Expand Down Expand Up @@ -362,7 +363,7 @@ private void UnityBuildGUI()
if (GUILayout.Button("Open in Visual Studio", GUILayout.Width(HALF_WIDTH)))
{
// Open SLN
string slnFilename = Path.Combine(BuildDeployPreferences.BuildDirectory, $"{PlayerSettings.productName}.sln");
string slnFilename = Path.Combine(BuildDeployPreferences.BuildDirectory, $"{PlayerSettings.productName}\\{PlayerSettings.productName}.sln");

if (File.Exists(slnFilename))
{
Expand Down Expand Up @@ -506,6 +507,10 @@ private void AppxBuildGUI()
{
buildArchitecture = Architecture.ARM;
}
else if (currentArchitectureString.ToLower().Equals("arm64"))
{
buildArchitecture = Architecture.ARM64;
}

buildArchitecture = (Architecture)EditorGUILayout.EnumPopup("Build Platform", buildArchitecture, GUILayout.Width(HALF_WIDTH));

Expand Down Expand Up @@ -574,7 +579,7 @@ private void AppxBuildGUI()
GUILayout.FlexibleSpace();

// Open AppX packages location
string appxDirectory = curScriptingBackend == ScriptingImplementation.IL2CPP ? $"/AppPackages/{PlayerSettings.productName}" : $"/{PlayerSettings.productName}/AppPackages";
string appxDirectory = $"\\{PlayerSettings.productName}\\AppPackages";
string appxBuildPath = Path.GetFullPath($"{BuildDeployPreferences.BuildDirectory}{appxDirectory}");
GUI.enabled = Builds.Count > 0 && !string.IsNullOrEmpty(appxBuildPath);

Expand Down Expand Up @@ -611,7 +616,7 @@ private void AppxBuildGUI()
if (GUILayout.Button("Build APPX", GUILayout.Width(HALF_WIDTH)))
{
// Check if solution exists
string slnFilename = Path.Combine(BuildDeployPreferences.BuildDirectory, $"{PlayerSettings.productName}.sln");
string slnFilename = Path.Combine(BuildDeployPreferences.BuildDirectory, $"{PlayerSettings.productName}\\{PlayerSettings.productName}.sln");

if (File.Exists(slnFilename))
{
Expand Down Expand Up @@ -659,7 +664,6 @@ private void DeployGUI()
}

GUI.enabled = true;

GUILayout.FlexibleSpace();

var previousLabelWidth = EditorGUIUtility.labelWidth;
Expand Down Expand Up @@ -1123,25 +1127,24 @@ private static void UpdateBuilds()
{
Builds.Clear();

var curScriptingBackend = PlayerSettings.GetScriptingBackend(BuildTargetGroup.WSA);
string appxDirectory = curScriptingBackend == ScriptingImplementation.IL2CPP ? $"AppPackages\\{PlayerSettings.productName}" : $"{PlayerSettings.productName}\\AppPackages";
string appxDirectory = $"{PlayerSettings.productName}\\AppPackages\\{PlayerSettings.productName}";

try
{
AppPackageDirectories.Clear();
string[] buildList = Directory.GetDirectories(BuildDeployPreferences.AbsoluteBuildDirectory, "*", SearchOption.AllDirectories);
foreach (string appBuild in buildList)
var buildList = Directory.GetDirectories(BuildDeployPreferences.AbsoluteBuildDirectory, "*", SearchOption.AllDirectories);

foreach (var appBuild in buildList)
{
if (appBuild.Contains(appxDirectory) && !appBuild.Contains($"{appxDirectory}\\"))
{
AppPackageDirectories.AddRange(Directory.GetDirectories(appBuild));
}
}

IEnumerable<string> selectedDirectories =
from string directory in AppPackageDirectories
orderby Directory.GetLastWriteTime(directory) descending
select Path.GetFullPath(directory);
var selectedDirectories = from string directory in AppPackageDirectories
orderby Directory.GetLastWriteTime(directory) descending
select Path.GetFullPath(directory);
Builds.AddRange(selectedDirectories);
}
catch (DirectoryNotFoundException)
Expand All @@ -1157,12 +1160,12 @@ orderby Directory.GetLastWriteTime(directory) descending
private static string CalcMostRecentBuild()
{
UpdateBuilds();
DateTime mostRecent = DateTime.MinValue;
string mostRecentBuild = string.Empty;
var mostRecent = DateTime.MinValue;
var mostRecentBuild = string.Empty;

foreach (var fullBuildLocation in Builds)
{
DateTime directoryDate = Directory.GetLastWriteTime(fullBuildLocation);
var directoryDate = Directory.GetLastWriteTime(fullBuildLocation);

if (directoryDate > mostRecent)
{
Expand All @@ -1177,12 +1180,14 @@ private static string CalcMostRecentBuild()
private void UpdatePortalConnections()
{
targetIps = new string[portalConnections.Connections.Count];

if (currentConnectionInfoIndex > portalConnections.Connections.Count - 1)
{
currentConnectionInfoIndex = portalConnections.Connections.Count - 1;
}

targetIps[0] = LOCAL_MACHINE;

for (int i = 1; i < targetIps.Length; i++)
{
if (string.IsNullOrEmpty(portalConnections.Connections[i].MachineName))
Expand All @@ -1194,6 +1199,7 @@ private void UpdatePortalConnections()
}

var devicePortalConnections = new DevicePortalConnections();

for (var i = 0; i < portalConnections.Connections.Count; i++)
{
devicePortalConnections.Connections.Add(portalConnections.Connections[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public static void ParseBuildCommandLine(ref IBuildInfo buildInfo)
break;
case "-x86":
case "-x64":
case "-ARM":
case "-ARM64":
buildInfo.BuildPlatform = arguments[i].Substring(1);
break;
case "-debug":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static async Task<bool> BuildAppxAsync(UwpBuildInfo buildInfo, Cancellati
Debug.Log("Starting Unity Appx Build...");

IsBuilding = true;
string slnFilename = Path.Combine(buildInfo.OutputDirectory, $"{PlayerSettings.productName}.sln");
string slnFilename = Path.Combine(buildInfo.OutputDirectory, $"{PlayerSettings.productName}\\{PlayerSettings.productName}.sln");

if (!File.Exists(slnFilename))
{
Expand All @@ -66,7 +66,7 @@ public static async Task<bool> BuildAppxAsync(UwpBuildInfo buildInfo, Cancellati
}

// Get and validate the msBuild path...
var msBuildPath = await FindMsBuildPathAsync();
var msBuildPath = await FindMsBuildPathAsync(cancellationToken);

if (!File.Exists(msBuildPath))
{
Expand All @@ -88,15 +88,10 @@ public static async Task<bool> BuildAppxAsync(UwpBuildInfo buildInfo, Cancellati
return IsBuilding = false;
}

string storagePath = Path.GetFullPath(Path.Combine(Path.Combine(Application.dataPath, ".."), buildInfo.OutputDirectory));
string solutionProjectPath = Path.GetFullPath(Path.Combine(storagePath, $@"{PlayerSettings.productName}.sln"));

// Now do the actual appx build
var processResult = await new Process().RunAsync(
$"\"{solutionProjectPath}\" /t:{(buildInfo.RebuildAppx ? "Rebuild" : "Build")} /p:Configuration={buildInfo.Configuration} /p:Platform={buildInfo.BuildPlatform} /verbosity:m",
msBuildPath,
!Application.isBatchMode,
cancellationToken);
var storagePath = Path.GetFullPath(Path.Combine(Path.Combine(Application.dataPath, ".."), buildInfo.OutputDirectory));
var solutionProjectPath = Path.GetFullPath(Path.Combine(storagePath, $"{PlayerSettings.productName}/{PlayerSettings.productName}.sln"));
var appxBuildArgs = $"\"{solutionProjectPath}\" /t:{(buildInfo.RebuildAppx ? "Rebuild" : "Build")} /p:Configuration={buildInfo.Configuration} /p:Platform={buildInfo.BuildPlatform} /verbosity:m";
var processResult = await new Process().RunAsync(appxBuildArgs, msBuildPath, !Application.isBatchMode, cancellationToken, false);

switch (processResult.ExitCode)
{
Expand Down Expand Up @@ -154,7 +149,7 @@ public static async Task<bool> BuildAppxAsync(UwpBuildInfo buildInfo, Cancellati
return processResult.ExitCode == 0;
}

private static async Task<string> FindMsBuildPathAsync()
private static async Task<string> FindMsBuildPathAsync(CancellationToken cancellationToken)
{
var result = await new Process().RunAsync(
new ProcessStartInfo
Expand All @@ -164,28 +159,27 @@ private static async Task<string> FindMsBuildPathAsync()
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = $@"/C vswhere -all -products * -requires Microsoft.Component.MSBuild -property installationPath",
WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio\Installer"
});
WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio\Installer",
Arguments = "/c vswhere -all -products * -requires Microsoft.Component.MSBuild -property installationPath"
}, true, cancellationToken);

foreach (var path in result.Output)
{
if (!string.IsNullOrEmpty(path))
{
string[] paths = path.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (string.IsNullOrEmpty(path)) { continue; }

if (paths.Length > 0)
{
// if there are multiple visual studio installs,
// prefer enterprise, then pro, then community
var bestPath = paths.OrderBy(p => p.ToLower().Contains("enterprise"))
.ThenBy(p => p.ToLower().Contains("professional"))
.ThenBy(p => p.ToLower().Contains("community")).First();

return bestPath.Contains("2019")
? $@"{bestPath}\MSBuild\Current\Bin\MSBuild.exe"
: $@"{bestPath}\MSBuild\15.0\Bin\MSBuild.exe";
}
var paths = path.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

if (paths.Length > 0)
{
// if there are multiple visual studio installs,
// prefer enterprise, then pro, then community
var bestPath = paths.OrderBy(p => p.ToLower().Contains("enterprise"))
.ThenBy(p => p.ToLower().Contains("professional"))
.ThenBy(p => p.ToLower().Contains("community")).First();

return bestPath.Contains("2019")
? $@"{bestPath}\MSBuild\Current\Bin\MSBuild.exe"
: $@"{bestPath}\MSBuild\15.0\Bin\MSBuild.exe";
}
}

Expand Down

0 comments on commit 3a3ce5a

Please sign in to comment.