<?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
if (isset($argc)) {  // osascript -e 'do shell script "/path/to/executable --flag \"argument with spaces\""'
   //$command="osascript -e 'tell application \"" . $argv[1] . "\"";   //"  to activate';
   $command="osascript -e 'tell application \"Terminal\" to do script \"\\" . "\"" . $argv[1] . "\\\"";   //"  to activate';
   for ($ii=2; $ii<$argc; $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 . "\"'", $descriptors, $pipes);

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

if (isset($_POST['execexec'])) {
  if (isset($_POST['argsare'])) {
   $command="osascript -e 'tell application \"Terminal\" to do script \"\\" . "\"" . str_replace('"','',str_replace('+',' ',urldecode($_POST['execexec']))) . "\\\"";   //"  to activate';
   if ($_POST['argsare'] != '') {
   $command.=' ' . "\\\"" . str_replace('"','',str_replace('+',' ',urldecode($_POST['argsare']))) . "\\\"";
   }
   passthru($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 \"Terminal\" to do script \"\\" . "\"" . str_replace('"','',str_replace('+',' ',urldecode($_POST['execexec']))) . "\\\"";   //"  to activate';
   passthru($command . "\"'");
   //passthru("php " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "find_xcode_executable.php " . str_replace('+',' ',urldecode($_POST['execexec'])) . "");
   exit;
  }
}

$sofarcnt=1;
$list=str_replace("." . DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR, shell_exec("cd " . $_SERVER['DOCUMENT_ROOT'] . " ; " . "find . -name '*.xcodeproj' 2> /dev/null"));
$recs=explode("\n", $list);
$selis="<select size=1 style=width:100%; id=mysel onchange='displayexec(this);'><option value=''>Please select an Xcode project to find executable path for</option></select>";

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>
<scr" . "ipt>
  function displayexec(osel) {
   if (osel.value.trim() != '') {
    document.getElementById('execexec').value=osel.value;
   }
  }
</scr" . "ipt>
</head>
<body>
<h1>Xcode project to find executable path</h1>
<h3>RJM Programming - November, 2026</h3><br><br>" . $selis . "<br><br>
<form action=./find_xcode_executable.php method=POST>
<table style=width:100%; border=5>
<tr><th style=width:80%;>Executable path ...</th><th style=width:20%;>Argument(s) ...</th></th></tr>
<tr><td><input style=width:100%; type=text value='' id=execexec name=execexec placeholder='Executable path'></input></td><td><input style=width:100%; type=text value='' id=argsare name=argsare placeholder='Argument(s)'></input></td></tr>
</table>
<br><br><input type=submit style=background-color:yellow;color:blue; value='Execute ...'></input>
</form>
</body>
</html>";
?>
