Skip to content

Commit 10bd198

Browse files
[jdk] Don't require javac/jar in $PATH
Windows machines do not include Java in their path by default, so it is a better experience to not require it for the build. Changes to make this happen: - `JavaCPath` and `JarPath` are configured in `Configuration.OperatingSystem.props` - Usage of `javac` and `jar` across the repo use appropriate variables now - A couple places, Windows needs `JAVA_HOME` to be set, so we are making use of a new `SetEnvironmentVar` MSBuild task - The `JdkInfo` MSBuild task used in `PrepareWindows.targets` configures `Configuration.OperatingSystem.props` for Java, and also configures `Java.Interop`
1 parent 06c0901 commit 10bd198

File tree

18 files changed

+91
-23
lines changed

18 files changed

+91
-23
lines changed

build-tools/android-toolchain/android-toolchain.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
<_AndroidMxeOutput Include="@(_AndroidMxeToolchain->'$(AndroidMxeFullPath)\%(Identity)\lib\libz.a')" />
129129
</ItemGroup>
130130
<Target Name="_AcceptAndroidSdkLicenses">
131-
<AcceptAndroidSdkLicenses AndroidSdkDirectory="$(AndroidSdkDirectory)" />
131+
<AcceptAndroidSdkLicenses AndroidSdkDirectory="$(AndroidSdkDirectory)" JavaSdkDirectory="$(JavaSdkDirectory)" />
132132
</Target>
133133
<Target Name="_CreateMxeToolchains"
134134
DependsOnTargets="_SetMxeToolchainMakefileTimeToLastCommitTimestamp"

build-tools/dependencies/dependencies.projitems

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
<RequiredProgram Include="intltoolize" Condition=" '$(NeedMxe)' == 'true' And $(AndroidSupportedHostJitAbisForConditionalChecks.Contains (':mxe-Win64:'))">
3535
<Homebrew>intltool</Homebrew>
3636
</RequiredProgram>
37-
<RequiredProgram Include="javac">
37+
<RequiredProgram Include="$(JavaCPath)">
3838
<MinimumVersion>1.8</MinimumVersion>
3939
<CurrentVersionCommand Condition=" '$(HostOS)' != 'Windows' ">$(MSBuildThisFileDirectory)..\scripts\javac-version</CurrentVersionCommand>
40-
<CurrentVersionCommand Condition=" '$(HostOS)' == 'Windows' ">javac -version 2&gt;&amp;1</CurrentVersionCommand>
40+
<CurrentVersionCommand Condition=" '$(HostOS)' == 'Windows' ">&quot;$(JavaCPath)&quot; -version 2&gt;&amp;1</CurrentVersionCommand>
4141
<UbuntuInstall>$(_AptGetInstall) openjdk-8-jdk</UbuntuInstall>
4242
</RequiredProgram>
4343
<RequiredProgram Include="make" Condition=" '$(HostOS)' != 'Windows' " />

build-tools/scripts/JavaCallableWrappers.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
<_MonoAndroidJar>$(OutputPath)mono.android.jar</_MonoAndroidJar>
3131
</PropertyGroup>
3232
<Exec
33-
Command="javac $(_Target) $(_D) -bootclasspath $(_AndroidJar)$(PathSeparator)&quot;$(_MonoAndroidJar)&quot; @$(IntermediateOutputPath)jcw\classes.txt"
33+
Command="&quot;$(JavaCPath)&quot; $(_Target) $(_D) -bootclasspath $(_AndroidJar)$(PathSeparator)&quot;$(_MonoAndroidJar)&quot; @$(IntermediateOutputPath)jcw\classes.txt"
3434
/>
3535
<Exec
3636
Condition="Exists('$(_MonoAndroidJar)')"
37-
Command="jar uf &quot;$(_MonoAndroidJar)&quot; -C &quot;$(IntermediateOutputPath)jcw\bin&quot; ."
37+
Command="&quot;$(JarPath)&quot; uf &quot;$(_MonoAndroidJar)&quot; -C &quot;$(IntermediateOutputPath)jcw\bin&quot; ."
3838
/>
3939
<Exec
4040
Condition="!Exists('$(_MonoAndroidJar)')"
41-
Command="jar cf &quot;$(_MonoAndroidJar)&quot; -C &quot;$(IntermediateOutputPath)jcw\bin&quot; ."
41+
Command="&quot;$(JarPath)&quot; cf &quot;$(_MonoAndroidJar)&quot; -C &quot;$(IntermediateOutputPath)jcw\bin&quot; ."
4242
/>
4343
</Target>
4444
<Target Name="_GenerateMonoAndroidDex16"

build-tools/scripts/PrepareWindows.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
AndroidSdkPath="$(AndroidSdkPath)"
2727
AndroidNdkPath="$(AndroidNdkPath)"
2828
JavaSdkPath="$(JavaSdkDirectory)"
29-
Output="$(_TopDir)\external\Java.Interop\bin\BuildDebug\JdkInfo.props"
29+
ConfigurationOperatingSystemProps="$(_TopDir)\Configuration.OperatingSystem.props"
30+
JdkInfoProps="$(_TopDir)\external\Java.Interop\bin\BuildDebug\JdkInfo.props"
3031
/>
3132
<Exec Command="$(_NuGet) restore Xamarin.Android.sln" WorkingDirectory="$(_TopDir)" />
3233
<Exec Command="$(_NuGet) restore external\Java.Interop\Java.Interop.sln" WorkingDirectory="$(_TopDir)" />

build-tools/scripts/Windows-Configuration.OperatingSystem.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
<HostTriplet64 Condition=" '$(HostTriplet64)' == '' ">x86_64-win32</HostTriplet64>
1010
<HostCpuCount Condition=" '$(HostCpuCount)' == '' ">$([System.Environment]::ProcessorCount)</HostCpuCount>
1111
<HostBits Condition=" '$(HostBits)' == '' ">64</HostBits>
12+
<JavaSdkDirectory>@JAVA_HOME@</JavaSdkDirectory>
13+
<JavaCPath>$(JavaSdkDirectory)\bin\javac.exe</JavaCPath>
14+
<JarPath>$(JavaSdkDirectory)\bin\jar.exe</JarPath>
1215
</PropertyGroup>
1316
</Project>

build-tools/scripts/generate-os-info

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ cat <<EOF > "$1"
120120
<HostCc64 Condition=" '\$(HostCc64)' == '' ">$HOST_CC64</HostCc64>
121121
<HostCxx32 Condition=" '\$(HostCxx32)' == '' ">$HOST_CXX32</HostCxx32>
122122
<HostCxx64 Condition=" '\$(HostCxx64)' == '' ">$HOST_CXX64</HostCxx64>
123+
<JavaCPath Condition=" '\$(JavaCPath)' == '' ">javac</JavaCPath>
124+
<JarPath Condition=" '\$(JarPath)' == '' ">jar</JarPath>
123125
</PropertyGroup>
124126
</Project>
125127
EOF

build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/AcceptAndroidSdkLicenses.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ public class AcceptAndroidSdkLicenses : Task
1111
[Required]
1212
public string AndroidSdkDirectory { get; set; }
1313

14+
public string JavaSdkDirectory { get; set; }
15+
1416
public override bool Execute ()
1517
{
18+
Log.LogMessage (MessageImportance.Low, $"Task {nameof (AcceptAndroidSdkLicenses)}");
19+
Log.LogMessage (MessageImportance.Low, $" {nameof (AndroidSdkDirectory)}: {AndroidSdkDirectory}");
20+
Log.LogMessage (MessageImportance.Low, $" {nameof (JavaSdkDirectory)}: {JavaSdkDirectory}");
21+
1622
var licdir = Path.Combine (Path.Combine (AndroidSdkDirectory, "licenses"));
1723
Directory.CreateDirectory (licdir);
1824

25+
if (!string.IsNullOrEmpty (JavaSdkDirectory)) {
26+
Environment.SetEnvironmentVariable ("JAVA_HOME", JavaSdkDirectory);
27+
}
28+
1929
string _;
2030
var path = Which.GetProgramLocation ("sdkmanager", out _, new [] { Path.Combine (AndroidSdkDirectory, "tools", "bin") });
2131
var psi = new ProcessStartInfo (path, "--licenses") { UseShellExecute = false, RedirectStandardInput = true };

build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/JdkInfo.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ namespace Xamarin.Android.BuildTools.PrepTasks
1010
public class JdkInfo : Task
1111
{
1212
[Required]
13-
public ITaskItem Output { get; set; }
13+
public ITaskItem ConfigurationOperatingSystemProps { get; set; }
14+
15+
[Required]
16+
public ITaskItem JdkInfoProps { get; set; }
1417

1518
public string AndroidNdkPath { get; set; }
1619

@@ -21,7 +24,8 @@ public class JdkInfo : Task
2124
public override bool Execute ()
2225
{
2326
Log.LogMessage (MessageImportance.Low, $"Task {nameof (JdkInfo)}");
24-
Log.LogMessage (MessageImportance.Low, $" {nameof (Output)}: {Output}");
27+
Log.LogMessage (MessageImportance.Low, $" {nameof (ConfigurationOperatingSystemProps)}: {ConfigurationOperatingSystemProps}");
28+
Log.LogMessage (MessageImportance.Low, $" {nameof (JdkInfoProps)}: {JdkInfoProps}");
2529
Log.LogMessage (MessageImportance.Low, $" {nameof (AndroidNdkPath)}: {AndroidNdkPath}");
2630
Log.LogMessage (MessageImportance.Low, $" {nameof (AndroidSdkPath)}: {AndroidSdkPath}");
2731
Log.LogMessage (MessageImportance.Low, $" {nameof (JavaSdkPath)}: {JavaSdkPath}");
@@ -40,6 +44,12 @@ public override bool Execute ()
4044

4145
Log.LogMessage (MessageImportance.Low, $" {nameof (AndroidSdk.JavaSdkPath)}: {javaSdkPath}");
4246

47+
if (File.Exists (ConfigurationOperatingSystemProps.ItemSpec)) {
48+
var xml = File.ReadAllText (ConfigurationOperatingSystemProps.ItemSpec);
49+
if (xml.Contains("@JAVA_HOME@"))
50+
File.WriteAllText (ConfigurationOperatingSystemProps.ItemSpec, xml.Replace ("@JAVA_HOME@", javaSdkPath));
51+
}
52+
4353
var jvmPath = Path.Combine (javaSdkPath, "jre", "bin", "server", "jvm.dll");
4454
if (!File.Exists (jvmPath)) {
4555
Log.LogError ($"JdkJvmPath not found at {jvmPath}");
@@ -55,8 +65,8 @@ public override bool Execute ()
5565
includeXmlTags.AppendLine ($"<JdkIncludePath Include=\"{include}\" />");
5666
}
5767

58-
Directory.CreateDirectory (Path.GetDirectoryName (Output.ItemSpec));
59-
File.WriteAllText (Output.ItemSpec, $@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
68+
Directory.CreateDirectory (Path.GetDirectoryName (JdkInfoProps.ItemSpec));
69+
File.WriteAllText (JdkInfoProps.ItemSpec, $@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
6070
<Choose>
6171
<When Condition="" '$(JdkJvmPath)' == '' "">
6272
<PropertyGroup>
@@ -67,6 +77,10 @@ public override bool Execute ()
6777
</ItemGroup>
6878
</When>
6979
</Choose>
80+
<PropertyGroup>
81+
<JavaCPath Condition="" '$(JavaCPath)' == '' "">{Path.Combine (javaSdkPath, "bin", "javac.exe")}</JavaCPath>
82+
<JarPath Condition="" '$(JarPath)' == '' "">{Path.Combine (javaSdkPath, "bin", "jar.exe")}</JarPath>
83+
</PropertyGroup>
7084
</Project>");
7185

7286
return !Log.HasLoggedErrors;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Build.Framework;
2+
using Microsoft.Build.Utilities;
3+
using System;
4+
5+
namespace Xamarin.Android.BuildTools.PrepTasks
6+
{
7+
public class SetEnvironmentVar : Task
8+
{
9+
[Required]
10+
public string Name { get; set; }
11+
12+
[Required]
13+
public string Value { get; set; }
14+
15+
public override bool Execute ()
16+
{
17+
Log.LogMessage (MessageImportance.Low, $"Task {nameof (SetEnvironmentVar)}");
18+
Log.LogMessage (MessageImportance.Low, $" {nameof (Name)}: {Name}");
19+
Log.LogMessage (MessageImportance.Low, $" {nameof (Value)}: {Value}");
20+
21+
Environment.SetEnvironmentVariable (Name, Value);
22+
23+
return !Log.HasLoggedErrors;
24+
}
25+
}
26+
}

build-tools/xa-prep-tasks/xa-prep-tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\HashFileContents.cs" />
4646
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\JdkInfo.cs" />
4747
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\PathToolTask.cs" />
48+
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\SetEnvironmentVar.cs" />
4849
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\SystemUnzip.cs" />
4950
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\Which.cs" />
5051
<Compile Include="Xamarin.Android.BuildTools.PrepTasks\GitBlame.cs" />

0 commit comments

Comments
 (0)