Skip to content

Commit 3237a89

Browse files
committed
[Xamarin.Android.Build.Tasks] Use a Response file for javac
Context https://devdiv.visualstudio.com/DevDiv/_workitems/edit/599161 We currently only use a response file for .java files. Looking at the docs there is no reason why we can't use them for all of the arguments. This should help with problems where windows has a max command line length.
1 parent cb6e961 commit 3237a89

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,6 @@ static string GetNdkToolchainLibraryDir (string binDir, AndroidTargetArch arch)
172172
return GetNdkToolchainLibraryDir (binDir, NdkUtil.GetArchDirName (arch));
173173
}
174174

175-
static string GetShortPath (string path)
176-
{
177-
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
178-
return QuoteFileName (path);
179-
var shortPath = KernelEx.GetShortPathName (Path.GetDirectoryName (path));
180-
return Path.Combine (shortPath, Path.GetFileName (path));
181-
}
182-
183-
static string QuoteFileName(string fileName)
184-
{
185-
var builder = new CommandLineBuilder();
186-
builder.AppendFileNameIfNotNull(fileName);
187-
return builder.ToString();
188-
}
189-
190175
static bool ValidateAotConfiguration (TaskLoggingHelper log, AndroidTargetArch arch, bool enableLLVM)
191176
{
192177
return true;
@@ -380,20 +365,19 @@ IEnumerable<Config> GetAotConfigs ()
380365

381366
var libs = new List<string>();
382367
if (NdkUtil.UsingClangNDK) {
383-
libs.Add ($"-L{GetShortPath (toolchainLibDir)}");
384-
libs.Add ($"-L{GetShortPath (androidLibPath)}");
368+
libs.Add ($"-L{Files.GetShortPath (toolchainLibDir)}");
369+
libs.Add ($"-L{Files.GetShortPath (androidLibPath)}");
385370

386371
if (arch == AndroidTargetArch.Arm) {
387372
// Needed for -lunwind to work
388373
string compilerLibDir = Path.Combine (toolchainPath, "..", "sysroot", "usr", "lib", NdkUtil.GetArchDirName (arch));
389-
libs.Add ($"-L{GetShortPath (compilerLibDir)}");
374+
libs.Add ($"-L{Files.GetShortPath (compilerLibDir)}");
390375
}
391376
}
392377

393-
libs.Add (GetShortPath (Path.Combine (toolchainLibDir, "libgcc.a")));
394-
libs.Add (GetShortPath (Path.Combine (androidLibPath, "libc.so")));
395-
libs.Add (GetShortPath (Path.Combine (androidLibPath, "libm.so")));
396-
378+
libs.Add (Files.GetShortPath (Path.Combine (toolchainLibDir, "libgcc.a")));
379+
libs.Add (Files.GetShortPath (Path.Combine (androidLibPath, "libc.so")));
380+
libs.Add (Files.GetShortPath (Path.Combine (androidLibPath, "libm.so")));
397381
ldFlags = string.Join(";", libs);
398382
}
399383

@@ -420,17 +404,17 @@ IEnumerable<Config> GetAotConfigs ()
420404
if (!string.IsNullOrEmpty (AotAdditionalArguments))
421405
aotOptions.Add (AotAdditionalArguments);
422406
if (sequencePointsMode == SequencePointsMode.Offline)
423-
aotOptions.Add ("msym-dir=" + GetShortPath (outdir));
407+
aotOptions.Add ("msym-dir=" + Files.GetShortPath (outdir));
424408
if (AotMode != AotMode.Normal)
425409
aotOptions.Add (AotMode.ToString ().ToLowerInvariant ());
426410

427-
aotOptions.Add ("outfile=" + GetShortPath (outputFile));
411+
aotOptions.Add ("outfile=" + Files.GetShortPath (outputFile));
428412
aotOptions.Add ("asmwriter");
429413
aotOptions.Add ("mtriple=" + mtriple);
430-
aotOptions.Add ("tool-prefix=" + GetShortPath (toolPrefix));
414+
aotOptions.Add ("tool-prefix=" + Files.GetShortPath (toolPrefix));
431415
aotOptions.Add ("ld-flags=" + ldFlags);
432-
aotOptions.Add ("llvm-path=" + GetShortPath (sdkBinDirectory));
433-
aotOptions.Add ("temp-path=" + GetShortPath (tempDir));
416+
aotOptions.Add ("llvm-path=" + Files.GetShortPath (sdkBinDirectory));
417+
aotOptions.Add ("temp-path=" + Files.GetShortPath (tempDir));
434418

435419
string aotOptionsStr = (EnableLLVM ? "--llvm " : "") + "--aot=" + string.Join (",", aotOptions);
436420

@@ -452,9 +436,9 @@ IEnumerable<Config> GetAotConfigs ()
452436
}
453437

454438
var assembliesPath = Path.GetFullPath (Path.GetDirectoryName (resolvedPath));
455-
var assemblyPath = QuoteFileName (Path.GetFullPath (resolvedPath));
439+
var assemblyPath = Files.QuoteFileName (Path.GetFullPath (resolvedPath));
456440

457-
yield return new Config (assembliesPath, QuoteFileName (aotCompiler), aotOptionsStr, assemblyPath, outputFile);
441+
yield return new Config (assembliesPath, Files.QuoteFileName (aotCompiler), aotOptionsStr, assemblyPath, outputFile);
458442
}
459443
}
460444
}

src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ public override bool Execute ()
5252
return retval;
5353
}
5454

55+
protected virtual void WriteOptionsToResponseFile (StreamWriter sw)
56+
{
57+
}
58+
5559
private void GenerateResponseFile ()
5660
{
5761
TemporarySourceListFile = Path.GetTempFileName ();
5862

5963
using (var sw = new StreamWriter (path:TemporarySourceListFile, append:false,
6064
encoding:new UTF8Encoding (encoderShouldEmitUTF8Identifier:false))) {
65+
66+
WriteOptionsToResponseFile (sw);
6167
// Include any user .java files
6268
if (JavaSourceFiles != null)
6369
foreach (var file in JavaSourceFiles.Where (p => Path.GetExtension (p.ItemSpec) == ".java"))

src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Collections.Generic;
1010
using Xamarin.Tools.Zip;
11+
using Xamarin.Android.Tools;
1112

1213
namespace Xamarin.Android.Tasks
1314
{
@@ -50,16 +51,19 @@ protected override string GenerateCommandLineCommands ()
5051

5152
cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8");
5253

53-
cmd.AppendSwitchIfNotNull ("-d ", ClassesOutputDirectory);
54-
55-
cmd.AppendSwitchIfNotNull ("-classpath ", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => i.ItemSpec)));
56-
cmd.AppendSwitchIfNotNull ("-bootclasspath ", JavaPlatformJarPath);
57-
cmd.AppendSwitchIfNotNull ("-encoding ", "UTF-8");
5854
cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile));
5955
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
6056
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
6157

6258
return cmd.ToString ();
6359
}
60+
61+
protected override void WriteOptionsToResponseFile (StreamWriter sw)
62+
{
63+
sw.WriteLine ($"-d {Files.GetShortPath (ClassesOutputDirectory).Replace (@"\", "\\")}");
64+
sw.WriteLine ("-classpath \"{0}\"", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => i.ItemSpec.Replace (@"\", "\\"))));
65+
sw.WriteLine ("-bootclasspath {0}", Files.GetShortPath (JavaPlatformJarPath).Replace (@"\", "\\"));
66+
sw.WriteLine ($"-encoding UTF8");
67+
}
6468
}
6569
}

src/Xamarin.Android.Build.Tasks/Utilities/Files.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,21 @@ public static bool IsPortablePdb (string filename)
401401
return false;
402402
}
403403
}
404+
405+
internal static string QuoteFileName (string fileName)
406+
{
407+
var builder = new CommandLineBuilder ();
408+
builder.AppendFileNameIfNotNull (fileName);
409+
return builder.ToString ();
410+
}
411+
412+
internal static string GetShortPath (string path)
413+
{
414+
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
415+
return QuoteFileName (path);
416+
var shortPath = KernelEx.GetShortPathName (Path.GetDirectoryName (path));
417+
return Path.Combine (shortPath, Path.GetFileName (path));
418+
}
404419
}
405420
}
406421

0 commit comments

Comments
 (0)