<?php
// find_xcode_executable.php
// RJM Programming
// November, 2026
// Use with macOS and Xcode
// Helped out by https://www.google.com/search?q=can+a+command+line+xcode+command+determine+the+executable+path+for+an+xcode+project+name%3F&rlz=1C5OZZY_en&oq=can+a+command+line+xcode+command+determine+the+executable+path+for+an+xcode+project+name%3F&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIHCAEQIRiPAjIHCAIQIRiPAtIBCTM2NTUyajBqN6gCALACAA&sourceid=chrome&source=chrome.ob&ie=UTF-8
// cd "/Applications/MAMP/htdocs/DccumentProject/My Next File C Program/" ; xcodebuild -project "My Next File C Program.xcodeproj" -scheme "My Next File C Program" -showBuildSettings | grep -E "^\s*(TARGET_BUILD_DIR|EXECUTABLE_PATH) ="
//2026-11-22 17:57:15.240 xcodebuild[30786:4838683] [MT] IDERunDestination: Supported platforms for the buildables in the current scheme is empty.
//--- xcodebuild: WARNING: Using the first of multiple matching destinations:
//{ platform:macOS, arch:arm64, id:00008103-000328410EA2001E, name:My Mac }
//{ platform:macOS, arch:x86_64, id:00008103-000328410EA2001E, name:My Mac }
//{ platform:macOS, name:Any Mac }
//    EXECUTABLE_PATH = My Next File C Program
//    TARGET_BUILD_DIR = /Users/user/Library/Developer/Xcode/DerivedData/My_Next_File_C_Program-blfjoaxttlpyniaghlsxiantcafy/Build/Products/Debug
// find . -name '*.xcodeproj' 2> /dev/null
//Applications/MAMP/htdocs/Swift Document App/Swift Document App.xcodeproj
//Applications/MAMP/htdocs/Playground/.build/checkouts/Files/Files.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/My File C Program/My File C Program.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/Sample Document Project/Sample Document Project.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/TestiOSApp/TestiOSApp.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/My Screen Saver/My Screen Saver.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/My Next File C Program/My Next File C Program.xcodeproj
//Applications/MAMP/htdocs/DccumentProject/My C Command Line Project/My C Command Line Project.xcodeproj
//Applications/MAMP/htdocs/Swift Command Line/Swift Command Line.xcodeproj
//Applications/MAMP/htdocs/Fibonacci/Fibonacci.xcodeproj
//Applications/MAMP/htdocs/AudioUnit/Sample Audio Unit/Sample Audio Unit.xcodeproj
ini_set('max_execution_time', 60000); 
$dactor="Terminal";
$dverb="do script";
$undis="";
if (strtoupper(PHP_OS) == 'DARWIN') {
  $undis="\n  document.getElementById('ilog').style.visibility='visible';   \n";
} else {
  $undis="\n  if (('' + navigator.platform).toLowerCase().indexOf('mac') == 0) { window.open('http://localhost:8888/find_xcode_executable.php' + (locpath == '' ? '?locpath=' : '?locpath=' + encodeURIComponent(locpath)) + (locargs == '' ? '' : locargs), '_blank','top=50,left=50,width=' + eval(-100 + eval('' + screen.width)) + ',height=' + eval(-100 + eval('' + screen.height)) + '');  } else {  window.open('http://localhost/find_xcode_executable.php' + (locpath == '' ? '?locpath=' : '?locpath=' + encodeURIComponent(locpath)) + (locargs == '' ? '' : locargs), '_blank','top=50,left=50,width=' + eval(-100 + eval('' + screen.width)) + ',height=' + eval(-100 + eval('' + screen.height)) + '');     }   \n";
}
$startin=".";
if (isset($_GET['isurl']) || isset($_POST['isurl'])) {
 $dactor="Safari";
 $dverb="open location";
}
if (isset($_GET['startin'])) {
 if ($_GET['startin'] != '') {
  $startin=str_replace('+',' ',urldecode($_GET['startin']));
 }
}
$starterforone="";
$two=2;

function ourpassthru($passwhat) {
  if (strpos($passwhat, "\\\"http") !== false || strpos($passwhat, "\\\"//") !== false) {
    if (strtoupper(PHP_OS) == 'DARWIN') {
     $rest=explode('"',explode("\\\"", $passwhat)[2])[0];
     echo "tab 1 of window ";
     $upref="";
     if (strpos($passwhat, "\\\"//") !== false) {
       $upref="https:";
     }
     if (strpos($rest, '&') !== false) {
     return exec('open "' . $upref . explode("\\\"", $passwhat)[1] . trim($rest) . '"');
     } else {
     return exec('open ' . $upref . explode("\\\"", $passwhat)[1] . trim($rest));
     }
    } else {
     $rest=explode('"',explode("\\\"", $passwhat)[2])[0];
     echo "<html><body onload=\"document.getElementById('mya').click();\"><a target=_blank href=\"" . explode("\\\"", $passwhat)[1] . trim($rest) . "\" id=mya style=text-decoration:none;>tab 1 of window</a>";
     exit;
    }
  }
  if (!isset($argc)) {
    if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
       return exec($passwhat);
    }
  }
  return passthru($passwhat);
}

if (isset($argc)) {  // osascript -e 'do shell script "/path/to/executable --flag \"argument with spaces\""'
   //$command="osascript -e 'tell application \"" . $argv[1] . "\"";   //"  to activate';
   $pex=$argv[1];
   if ($pex == "script") {
     $starterforone=$pex . ' ' . $argv[2] . ' ';
     $pex=$argv[3];
   }
   $command="osascript -e 'tell application \"" . $dactor . "\" to " . $dverb . " \"" . $starterforone . "\\" . "\"" . str_replace('"','',$pex) . "\\\"";   //"  to activate';
   for ($ii=$two; $ii<$argc; $ii++) {
    //$command.=' ' . "\\\"" . $argv[$ii] . "\\\"";
    $command.=' ' . "" . $argv[$ii] . "";
   }

$descriptors = [
    0 => STDIN,  // Inherit standard input (keyboard)
    1 => STDOUT, // Inherit standard output (terminal screen)
    2 => STDERR, // Inherit standard error
];

   $process=proc_open($command . $dae . "\"'", $descriptors, $pipes);

   if (is_resource($process)) {
    $exitCode = proc_close($process);
   }
   exit;
}     

if (isset($_POST['execexec'])) {
  if (isset($_POST['argsare'])) {
   $pex=str_replace('+',' ',urldecode($_POST['execexec']));
   if (strpos('~' . $pex, '~script ') !== false) {
     $starterforone='script ' . explode(' ',explode('script ', $pex)[1])[0] . ' ';
     $pex=str_replace($starterforone,"",$pex);
   }
   $command="osascript -e 'tell application \"" . $dactor . "\" to " . $dverb . " \"" . $starterforone . "\\" . "\"" . str_replace('"','',$pex) . "\\\"";   //"  to activate';
   if ($_POST['argsare'] != '') {
   //$command.=' ' . "\\\"" . str_replace('"','',str_replace('+',' ',urldecode($_POST['argsare']))) . "\\\"";
   $command.=' ' . "" . str_replace('"','',str_replace('+',' ',urldecode($_POST['argsare']))) . "";
   }
   ourpassthru($command . "\"'");
   //passthru("php " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "find_xcode_executable.php " . str_replace('+',' ',urldecode($_POST['execexec'])) . ' ' . str_replace('+',' ',urldecode($_POST['argsare'])) . "");
   exit;
  } else {
   $command="osascript -e 'tell application \"" . $dactor . "\" to " . $dverb . " \"\\" . "\"" . str_replace('"','',str_replace('+',' ',urldecode($_POST['execexec']))) . "\\\"";   //"  to activate';
   ourpassthru($command . "\"'");
   //passthru("php " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "find_xcode_executable.php " . str_replace('+',' ',urldecode($_POST['execexec'])) . "");
   exit;
  }
}

$sofarcnt=1;
//echo "find " . $startin . " -name '*.xcodeproj' 2> /dev/null";
//exit;
$list="";
if (strtoupper(PHP_OS) == 'DARWIN') {
  $list=str_replace("." . DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR, shell_exec("cd " . $_SERVER['DOCUMENT_ROOT'] . " ; " . "find " . $startin . " -name '*.xcodeproj' -maxdepth 7 2> /dev/null"));
}
$recs=explode("\n", $list);
$selis="<select size=1 style=width:600px; id=mysel onchange='displayexec(this);'><option value=''>Please select an Xcode project to find executable path for</option></select>";

if ($list != '') {
for ($i=0; $i<sizeof($recs); $i++) {
  $execexec="";
  $projname="";
  $execprj=explode(DIRECTORY_SEPARATOR, $recs[$i])[-1 + sizeof(explode(DIRECTORY_SEPARATOR, $recs[$i]))];
  $pathis=explode($execprj, $recs[$i])[0];
  //echo 'cd "' . $pathis . '" ; xcodebuild -project "' . $execprj . '" -scheme "' . str_replace('.xcodeproj','',$execprj) . '"  -showBuildSettings | grep -E "^\s*(TARGET_BUILD_DIR|EXECUTABLE_PATH) ="';
  //exit;
  $findings=shell_exec('cd "' . $pathis . '" ; xcodebuild -project "' . $execprj . '" -scheme "' . str_replace('.xcodeproj','',$execprj) . '"  -showBuildSettings | grep -E "^\s*(TARGET_BUILD_DIR|EXECUTABLE_PATH) ="');
  $frecs=explode("\n", $findings);
  for ($j=0; $j<sizeof($frecs); $j++) {
     if (strpos($frecs[$j], "EXECUTABLE_PATH") !== false) {
       $projname=explode("EXECUTABLE_PATH=", str_replace(' = ','=',$frecs[$j]))[1];
     }
     if (strpos($frecs[$j], "TARGET_BUILD_DIR") !== false) {
       $execexec='"' . explode("TARGET_BUILD_DIR=", str_replace(' = ','=',$frecs[$j]))[1] . DIRECTORY_SEPARATOR . $projname . '"';
     }
  }
  if ($execexec != "") {
     $sofarcnt++;
     $selis=str_replace("</select>", "<option value='" . $execexec . "'>" . $projname . "</option></select>", str_replace('' . (-1 + $sofarcnt),'' . (0 +  $sofarcnt),$selis));
  }
}
}


echo "<html>
<head>
<title>Xcode project to find executable path - RJM Programming - November, 2026</title>
<style>
  select { background-color: #f0f0f0;  }
  #execexec { background-color: #fadadd;  }
  #argsare { background-color: #d6fffe;  }
</style>
<scr" . "ipt>
  var locpath=location.search.split('locpath=')[1] ? decodeURIComponent(location.search.split('locpath=')[1].split('&')[0]) : '';
  var locargs=location.search.split('locargs=')[1] ? decodeURIComponent(location.search.split('locargs=')[1].split('&')[0]) : '';
  var currow=1;

  function displayexec(osel) {
   if (osel.value.trim() != '') {
    document.getElementById('execexec').value=osel.value;
    maybeurl(document.getElementById('execexec'));
   }
  }
  
  function chkchk(ochk) {
    var wasv=document.getElementById('execexec').value;
    var wasav=document.getElementById('argsare').value;
    if (ochk.checked) {
       document.getElementById('execexec').value='script log.log ' + wasv;
       document.getElementById('argsare').value=wasav + ' --flags';
    } else {
       document.getElementById('execexec').value=document.getElementById('execexec').value.replace(/^script\ log\.log /g,'');
       document.getElementById('argsare').value=document.getElementById('argsare').value.replace(/\ \-\-flags$/g,'');
    }
  }
  
  function assess(oinp) {
    if (oinp.value.replace(/^http/g,'').replace(/^\/\//g,'') != oinp.value) {
      if (oinp.value.indexOf('?') != -1) {
      document.getElementById('argsare').value='?' + oinp.value.split('?')[1];
      document.getElementById('execexec').value=oinp.value.split('?')[0];
      } else if (oinp.value.indexOf('#') != -1) {
      document.getElementById('argsare').value='#' + oinp.value.replace(oinp.value.split('#')[0] + '#','');
      document.getElementById('execexec').value=oinp.value.split('#')[0];
      } else {
      document.getElementById('execexec').value=oinp.value;
      }
      oinp.value='';
      maybeurl(document.getElementById('execexec'));
    } else {
      oinp.value='';
    }
  }
  
  function maybeurl(oinp) {
    locargs='';
    locpath='';
    if (oinp.value.replace(/^http/g,'').replace(/^\/\//g,'') != oinp.value) {
      document.getElementById('dmurl').innerHTML='<input type=hidden name=isurl id=isurl value=y></input>';
    } else if (oinp.value.trim() != '') {
      locargs=document.getElementById('argsare').value;
      locpath=document.getElementById('execexec').value;
      document.getElementById('dmurl').innerHTML='';
      " . $undis . "
    }
  }
  
  function maybein() {
    if (locargs != '') {
      document.getElementById('locargs').value=locargs;
    }
    if (locpath != '') {
      document.getElementById('execexec').value=locpath;
      maybeurl(document.getElementById('execexec'));
    }
    locargs='';
    locpath='';
  }

</scr" . "ipt>
</head>
<body onload=maybein(); title='URL suffix such as ?startin=/ will scour whole disk rather than just off MAMP Document Root for *.xcodeproj Xcode project files.'>
<h1>Xcode project to find executable path</h1>
<h3>RJM Programming - November, 2026</h3><br><br><table style=width:100%;><tr><th style=width:70%;>Desktop program ... or ...</th><th style=width:30%;>Online URL ...</th></tr><td style=vertical-align:top;>" . $selis . "</td><td style=vertical-align:top;><input type=url value='' style=width:100%; placeholder='URL ...' value='' onblur=assess(this);></input></td></tr></table><br><br>
<form action=./find_xcode_executable.php method=POST>
<table style=width:100%; border=5><tbody id=mytbody>
<tr><th style=width:80%;>Executable path ...</th><th style=width:20%;>Arguments and/or pipes and/or redirects and/or switches and/or <input onchange=chkchk(this); id=ilog type=checkbox style=visibility:hidden;>Logging</input> ...</th></th></tr>
<tr id=tr1><td id=tr1td1><input style=width:100%; onblur=maybeurl(this); type=text value='' id=execexec name=execexec placeholder='Executable path'></input></td><td id=tr1td2><input style=width:100%; type=text value='' id=argsare name=argsare placeholder='Argument(s)'></input></td></tr>
</tbody></table>
<br><br><div id=dmurl></div><input type=submit style=background-color:yellow;color:blue; value='Execute ...'></input>
</form>
</body>
</html>";
?>
