Skip to content

Commit c10e061

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 resposen file for .java files. Looking at the docs there is no reason why we can't use them for all of the arguments. Using a response file should get rid of the following errors error MSB4018: System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. for `javac` anyway.
1 parent bc2b6fb commit c10e061

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,6 @@ static string GetNdkToolchainLibraryDir(string binDir)
148148
return libPath;
149149
}
150150

151-
static string GetShortPath (string path)
152-
{
153-
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
154-
return QuoteFileName (path);
155-
var shortPath = KernelEx.GetShortPathName (Path.GetDirectoryName (path));
156-
return Path.Combine (shortPath, Path.GetFileName (path));
157-
}
158-
159-
static string QuoteFileName(string fileName)
160-
{
161-
var builder = new CommandLineBuilder();
162-
builder.AppendFileNameIfNotNull(fileName);
163-
return builder.ToString();
164-
}
165-
166151
static bool ValidateAotConfiguration (TaskLoggingHelper log, AndroidTargetArch arch, bool enableLLVM)
167152
{
168153
return true;
@@ -388,9 +373,9 @@ IEnumerable<Config> GetAotConfigs ()
388373
Diagnostic.Error (5101, ex.Message);
389374
}
390375
var libs = new List<string>() {
391-
GetShortPath (Path.Combine(GetNdkToolchainLibraryDir(toolchainPath), "libgcc.a")),
392-
GetShortPath (Path.Combine(androidLibPath, "libc.so")),
393-
GetShortPath (Path.Combine(androidLibPath, "libm.so"))
376+
Files.GetShortPath (Path.Combine(GetNdkToolchainLibraryDir(toolchainPath), "libgcc.a")),
377+
Files.GetShortPath (Path.Combine(androidLibPath, "libc.so")),
378+
Files.GetShortPath (Path.Combine(androidLibPath, "libm.so"))
394379
};
395380
ldFlags = string.Join(";", libs);
396381
}
@@ -411,17 +396,17 @@ IEnumerable<Config> GetAotConfigs ()
411396
if (!string.IsNullOrEmpty (AotAdditionalArguments))
412397
aotOptions.Add (AotAdditionalArguments);
413398
if (sequencePointsMode == SequencePointsMode.Offline)
414-
aotOptions.Add ("msym-dir=" + GetShortPath (outdir));
399+
aotOptions.Add ("msym-dir=" + Files.GetShortPath (outdir));
415400
if (AotMode != AotMode.Normal)
416401
aotOptions.Add (AotMode.ToString ().ToLowerInvariant ());
417402

418-
aotOptions.Add ("outfile=" + GetShortPath (outputFile));
403+
aotOptions.Add ("outfile=" + Files.GetShortPath (outputFile));
419404
aotOptions.Add ("asmwriter");
420405
aotOptions.Add ("mtriple=" + mtriple);
421-
aotOptions.Add ("tool-prefix=" + GetShortPath (toolPrefix));
406+
aotOptions.Add ("tool-prefix=" + Files.GetShortPath (toolPrefix));
422407
aotOptions.Add ("ld-flags=" + ldFlags);
423-
aotOptions.Add ("llvm-path=" + GetShortPath (sdkBinDirectory));
424-
aotOptions.Add ("temp-path=" + GetShortPath (tempDir));
408+
aotOptions.Add ("llvm-path=" + Files.GetShortPath (sdkBinDirectory));
409+
aotOptions.Add ("temp-path=" + Files.GetShortPath (tempDir));
425410

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

@@ -443,9 +428,9 @@ IEnumerable<Config> GetAotConfigs ()
443428
}
444429

445430
var assembliesPath = Path.GetFullPath (Path.GetDirectoryName (resolvedPath));
446-
var assemblyPath = QuoteFileName (Path.GetFullPath (resolvedPath));
431+
var assemblyPath = Files.QuoteFileName (Path.GetFullPath (resolvedPath));
447432

448-
yield return new Config (assembliesPath, QuoteFileName (aotCompiler), aotOptionsStr, assemblyPath, outputFile);
433+
yield return new Config (assembliesPath, Files.QuoteFileName (aotCompiler), aotOptionsStr, assemblyPath, outputFile);
449434
}
450435
}
451436
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ public override bool Execute ()
6363
return retval;
6464
}
6565

66+
protected virtual void WriteOptionsToResponseFile (StreamWriter sw)
67+
{
68+
}
69+
6670
private void GenerateResponseFile ()
6771
{
6872
TemporarySourceListFile = Path.GetTempFileName ();
6973

7074
using (var sw = new StreamWriter (path:TemporarySourceListFile, append:false,
7175
encoding:new UTF8Encoding (encoderShouldEmitUTF8Identifier:false))) {
76+
77+
WriteOptionsToResponseFile (sw);
7278
// Include any user .java files
7379
if (JavaSourceFiles != null)
7480
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
@@ -7,6 +7,7 @@
77
using Microsoft.Build.Utilities;
88
using System.Text;
99
using System.Collections.Generic;
10+
using Xamarin.Android.Tools;
1011

1112
namespace Xamarin.Android.Tasks
1213
{
@@ -56,16 +57,19 @@ protected override string GenerateCommandLineCommands ()
5657

5758
cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8");
5859

59-
cmd.AppendSwitchIfNotNull ("-d ", ClassesOutputDirectory);
60-
61-
cmd.AppendSwitchIfNotNull ("-classpath ", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => i.ItemSpec)));
62-
cmd.AppendSwitchIfNotNull ("-bootclasspath ", JavaPlatformJarPath);
63-
cmd.AppendSwitchIfNotNull ("-encoding ", "UTF-8");
6460
cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile));
6561
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
6662
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
6763

6864
return cmd.ToString ();
6965
}
66+
67+
protected override void WriteOptionsToResponseFile (StreamWriter sw)
68+
{
69+
sw.WriteLine ($"-d {ClassesOutputDirectory}");
70+
sw.WriteLine ("-classpath {0}", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => Files.GetShortPath (i.ItemSpec).Replace (@"\", @"\\"))));
71+
sw.WriteLine ("-bootclasspath {0}", Files.GetShortPath (JavaPlatformJarPath).Replace (@"\", @"\\"));
72+
sw.WriteLine ($"-encoding UTF8");
73+
}
7074
}
7175
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,21 @@ public static bool IsPortablePdb (string filename)
310310
return false;
311311
}
312312
}
313+
314+
internal static string QuoteFileName(string fileName)
315+
{
316+
var builder = new CommandLineBuilder();
317+
builder.AppendFileNameIfNotNull(fileName);
318+
return builder.ToString();
319+
}
320+
321+
internal static string GetShortPath (string path)
322+
{
323+
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
324+
return QuoteFileName (path);
325+
var shortPath = KernelEx.GetShortPathName (Path.GetDirectoryName (path));
326+
return Path.Combine (shortPath, Path.GetFileName (path));
327+
}
313328
}
314329
}
315330

0 commit comments

Comments
 (0)