[webkitperl] nativeArchitecture() needs to support remote devices
https://bugs.webkit.org/show_bug.cgi?id=213727
<rdar://problem/64892021>

Reviewed by Aakash Jain.

* Scripts/run-javascriptcore-tests: Determine the machine actually running tests and pass
that to nativeArchitecture(...).
(runTest): Pass nativeTarget and nativePort.
(runJSCStressTests): Ditto.
* Scripts/webkitdirs.pm:
(determineNativeArchitecture): Native architecture is dependent on the target device.
(determineArchitecture): Call nativeArchitecture() instead of accessing the variable directly.
(nativeArchitecture): Ensure that we return only the architecture of the specified target.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@263742 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 7402ce2..74ea627 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,20 @@
+2020-06-30  Jonathan Bedard  <jbedard@apple.com>
+
+        [webkitperl] nativeArchitecture() needs to support remote devices
+        https://bugs.webkit.org/show_bug.cgi?id=213727
+        <rdar://problem/64892021>
+
+        Reviewed by Aakash Jain.
+
+        * Scripts/run-javascriptcore-tests: Determine the machine actually running tests and pass
+        that to nativeArchitecture(...).
+        (runTest): Pass nativeTarget and nativePort.
+        (runJSCStressTests): Ditto.
+        * Scripts/webkitdirs.pm:
+        (determineNativeArchitecture): Native architecture is dependent on the target device.
+        (determineArchitecture): Call nativeArchitecture() instead of accessing the variable directly.
+        (nativeArchitecture): Ensure that we return only the architecture of the specified target.
+
 2020-06-30  Diego Pino Garcia  <dpino@igalia.com>
 
         [GTK] Unreviewed API test gardening. Update test expectations after r263738.
diff --git a/Tools/Scripts/run-javascriptcore-tests b/Tools/Scripts/run-javascriptcore-tests
index 053f02d..220e9db 100755
--- a/Tools/Scripts/run-javascriptcore-tests
+++ b/Tools/Scripts/run-javascriptcore-tests
@@ -486,17 +486,38 @@
 
 setConfigurationProductDir(Cwd::abs_path($root)) if (defined($root));
 my $archsInBuild = architecturesForProducts();
+
+my $nativeTarget = "";
+my $nativePort = 0;
+$nativeTarget = $remoteHost if ($remoteHost);
+if (defined $remoteConfigFile) {
+    open my $handle, '<', $remoteConfigFile or die "Failed to open $remoteConfigFile: $!";
+    my $configJson = do { local $/; <$handle> };
+    my $remoteConfig = decode_json($configJson);
+    if (defined $remoteConfig->{"remotes"}) {
+        my $remotes = $remoteConfig->{"remotes"};
+        my @split = split(':', @$remotes[0]->{"address"});
+        $nativeTarget = $split[0];
+        $nativePort = $split[1] if scalar(@split) > 1;
+    } elsif (defined $remoteConfig->{"remote"}) {
+        my @split = split(':', $remoteConfig->{"remote"});
+        $nativeTarget = $split[0];
+        $nativePort = $split[1] if scalar(@split) > 1;
+    }
+}
+$nativePort = 22 if $nativeTarget && !$nativePort;
+
 if (defined $archs) {
     die "$archs not supported by the provided binary, which supports '$archsInBuild'" if index($archsInBuild, $archs) == -1;
 } else {
     # Fallback is x86_64, which we replace with the native architecture if the native architecture is in the provided build
     $archs = "x86_64";
-    $archs = nativeArchitecture() if index($archsInBuild, nativeArchitecture()) != -1;
+    $archs = nativeArchitecture($nativeTarget, $nativePort) if (!isAppleMacWebKit() || index($archsInBuild, nativeArchitecture($nativeTarget, $nativePort)) != -1);
 }
 
 # For running tests, arm64e should map to arm64
 $archs = "arm64" if $archs eq "arm64e";
-if ($archs ne nativeArchitecture() && (nativeArchitecture() ne "arm64" || !isAppleMacWebKit())) {
+if ($archs ne nativeArchitecture($nativeTarget, $nativePort) && (nativeArchitecture($nativeTarget, $nativePort) ne "arm64" || !isAppleMacWebKit())) {
     die "Cannot run tests with $archs on this machine";
 }
 
@@ -568,7 +589,7 @@
     chdir($productDir) or die "Failed to switch directory to '$productDir'\n";
     my @command = (testPath($productDir, $testName));
     unshift @command, ("xcrun", "-sdk", xcodeSDK(), "sim") if willUseIOSSimulatorSDK();
-    unshift @command, ("/usr/bin/arch", "-$archs") if $archs ne nativeArchitecture();
+    unshift @command, ("/usr/bin/arch", "-$archs") if $archs ne nativeArchitecture($nativeTarget, $nativePort);
     unshift @command, wrapperPrefixIfNeeded() if isGtk() or isWPE();
 
     if ($envVars ne "") {
@@ -732,7 +753,7 @@
         "/usr/bin/env", "ruby", "Tools/Scripts/run-jsc-stress-tests",
         "-j", jscPath($productDir), "-o", $jscStressResultsDir, "--arch", $archs);
 
-    if (nativeArchitecture() != $archs) {
+    if (nativeArchitecture($nativeTarget, $nativePort) ne $archs) {
         push(@jscStressDriverCmd, "--force-architecture");
         push(@jscStressDriverCmd, $archs);
     }
diff --git a/Tools/Scripts/webkitdirs.pm b/Tools/Scripts/webkitdirs.pm
index 54ec95b..a418cbd 100755
--- a/Tools/Scripts/webkitdirs.pm
+++ b/Tools/Scripts/webkitdirs.pm
@@ -128,7 +128,7 @@
 our @EXPORT_OK;
 
 my $architecture;
-my $nativeArchitecture;
+my %nativeArchitectureMap = ();
 my $asanIsEnabled;
 my $forceOptimizationLevel;
 my $coverageIsEnabled;
@@ -351,18 +351,29 @@
     }
 }
 
-sub determineNativeArchitecture
+sub determineNativeArchitecture(;$$)
 {
-    return if defined $nativeArchitecture;
-    $nativeArchitecture = `uname -m`;
-    chomp $nativeArchitecture;
-    $nativeArchitecture = "x86_64" if (not defined $nativeArchitecture);
+    my ($target, $port) = @_;
+    $target = '' if !defined $target;
+    $port = 0 if !defined $port;
+    return if defined $nativeArchitectureMap{"$target:$port"};
+
+    my $output;
+    if ($target eq "") {
+        $output = `uname -m`;
+    } else {
+        $output = `ssh -o NoHostAuthenticationForLocalhost=yes -p $port $target 'uname  -m'`;
+    }
+    chomp $output;
+    $output = "x86_64" if (not defined $output);
 
     # FIXME: Remove this when <rdar://problem/64208532> is resolved
-    if (isAppleCocoaWebKit() && $nativeArchitecture ne "x86_64") {
-        $nativeArchitecture = "arm64";
+    if (isAppleCocoaWebKit() && $output ne "x86_64") {
+        $output = "arm64";
     }
-    die "'arm64e' is an invalid native architecture" if $nativeArchitecture eq "arm64e";
+    $output = "arm" if $output eq "armv7l";
+    die "'arm64e' is an invalid native architecture" if $output eq "arm64e";
+    $nativeArchitectureMap{"$target:$port"} = $output;
 }
 
 sub determineArchitecture
@@ -371,8 +382,7 @@
 
     determineBaseProductDir();
     determineXcodeSDK();
-    determineNativeArchitecture();
-    $architecture = $nativeArchitecture;
+    $architecture = nativeArchitecture();
 
     if (isAppleCocoaWebKit()) {
         if (open ARCHITECTURE, "$baseProductDir/Architecture") {
@@ -1051,10 +1061,13 @@
     return $passedArchitecture;
 }
 
-sub nativeArchitecture()
+sub nativeArchitecture(;$$)
 {
-    determineNativeArchitecture();
-    return $nativeArchitecture;
+    my ($target, $port) = @_;
+    $target = '' if !defined $target;
+    $port = 0 if !defined $port;
+    determineNativeArchitecture($target, $port);
+    return $nativeArchitectureMap{"$target:$port"};
 }
 
 sub architecture()