From 5901d640dd0f33c023eae29e0c8090f7d91dc37a Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 27 Feb 2025 15:10:17 +0100
Subject: [PATCH] update installer + builder

---
 build.php         | 27 +++++++++++++++++++--------
 inc_functions.php | 36 ++++++++++++++++++++++++++++--------
 inc_vars.php      |  5 ++++-
 installer.php     | 33 +++++++++++++++++----------------
 4 files changed, 68 insertions(+), 33 deletions(-)

diff --git a/build.php b/build.php
index bba3f7f..51d5aae 100755
--- a/build.php
+++ b/build.php
@@ -18,14 +18,22 @@ echo "
 
 
 _chdir($selfdir);
+_mkdir($dirPackages);
 
 
-if(!is_dir($dirBuild)){
-    mkdir($dirBuild, 0755, true);    
-}
-
+$OUTFILE="$selfdir/$dirPackages/".str_replace('.php', '', basename($INFILE));
 
-$OUTFILE="$selfdir/$dirBuild/".str_replace('.php', '', basename($INFILE));
+_h1("Check target file");
+if(file_exists($OUTFILE)){
+    echo "INFO: output file '$OUTFILE' already exists.\n";
+    $ts_in=filemtime($INFILE);
+    $ts_out=filemtime($OUTFILE);
+    if($ts_in>$ts_out){
+        echo "INFO: Source file '$INFILE' is newer than '$OUTFILE'. Compiling changes...\n";
+    } else {
+        echo "INFO: Source is unchanged since last build..\n";
+    }
+}
 
 _h1("Prepare source");
 $in=file_get_contents($INFILE);
@@ -40,8 +48,11 @@ if(file_put_contents($TMPFILE, $tempcode)){
 
 
 _h1("Compile");
-_chdir("$selfdir/$dirExternal/bin");
-_exec("./spc micro:combine '$TMPFILE' -O '$OUTFILE'");
+_chdir("$selfdir/$dirBuild");
+_exec("$SPC \
+    micro:combine \
+    '$TMPFILE' \
+    -O '$OUTFILE'");
 
 if(unlink($TMPFILE)){
     echo "Cleanup: $TMPFILE was deleted\n";
@@ -50,7 +61,7 @@ if(unlink($TMPFILE)){
 _h1("Test generated binary");
 
 _exec("file '$OUTFILE'");
-_exec("'$OUTFILE' test=123");
+_exec("'$OUTFILE' --verbose --ini=../src/simple.ini");
 
 
 _h1("Done");
diff --git a/inc_functions.php b/inc_functions.php
index f07c613..c5a1a4d 100644
--- a/inc_functions.php
+++ b/inc_functions.php
@@ -3,16 +3,19 @@
 // SHARED FUNCTIONS FOR INSTALLER AND BUILDER
 // ----------------------------------------------------------------------
 
-function _h1($s){
+function _h1($s): void
+{
     echo "\n>>>>>>>>>> $s\n";
 }
-function _abort($sMessage, $iExitcode=1){
+function _abort($sMessage, $iExitcode = 1): never
+{
     echo "❌ $sMessage.\n";
     exit($iExitcode);
 }
 
-function _chdir($sDir){
-    if (!is_dir($sDir)){
+function _chdir($sDir): void
+{
+    if (!is_dir($sDir)) {
         _abort("Directory '$sDir' not found.");
     }
     chdir($sDir);
@@ -24,16 +27,33 @@ function _chdir($sDir){
  * @param mixed $cmd
  * @return void
  */
-function _exec($cmd){
-    echo "cmd > $cmd\n";    
+function _exec($cmd): void
+{
+    echo "cmd > $cmd\n";
     exec("$cmd 2>&1", $aOut, $rc);
 
+    if(!count($aOut)) {
+        $aOut=["-- no output --"];
+    }
     echo implode("\n", $aOut)."\n";
 
-    if($rc != 0){
+    if ($rc != 0) {
         echo "rc=$rc ";
         _abort("Error: Command failed. Aborting.", $rc);
     }
-    echo "✅ OK\n\n";
+    echo "✅ OK\n";
 }
 
+function _mkdir(string $sMyDir): void
+{
+    if (!is_dir($sMyDir)) {
+        echo "DIR > '$sMyDir' ";
+        if (!mkdir($sMyDir, 0755, true)) {
+            _abort("ERROR: mkdir failed.");
+        }
+        echo "✅ OK\n\n";
+    } else {
+        echo "⏩ SKIP mkdir: already exists: '$sMyDir'\n";
+    }
+
+}
\ No newline at end of file
diff --git a/inc_vars.php b/inc_vars.php
index ed83b25..2fdeb03 100644
--- a/inc_vars.php
+++ b/inc_vars.php
@@ -8,4 +8,7 @@ $myarchitecture="x86_64";
 
 $selfdir=dirname(__FILE__);
 $dirExternal="external";
-$dirBuild="built_packages";
+$dirBuild="build";
+$dirPackages="built_packages";
+
+$SPC="$selfdir/$dirExternal/bin/spc";
diff --git a/installer.php b/installer.php
index abb0741..42dd1d1 100755
--- a/installer.php
+++ b/installer.php
@@ -46,18 +46,16 @@ echo "
 _chdir($selfdir);
 
 // ----------------------------------------------------------------------
-_h1("External data dir");
-if(!is_dir($dirExternal)){
-    mkdir($dirExternal, 0755, true);    
-}
-if(!is_dir("$dirExternal/bin")){
-    mkdir("$dirExternal/bin", 0755, true);    
-}
-_chdir("$selfdir/$dirExternal");
+_h1("Create directories");
+
+_mkdir($dirExternal);
+_mkdir("$dirExternal/bin");
+_mkdir($dirBuild);
 
 
 // ----------------------------------------------------------------------
 _h1("Get / update Appmonitor repo");
+_chdir("$selfdir/$dirExternal");
 if(!is_dir("appmonitor")){
     _exec("git clone $gitAppmonitor");
 } else {
@@ -94,26 +92,29 @@ if(file_put_contents($incfile, $out)){
 // ----------------------------------------------------------------------
 _h1("Get / update spc");
 _chdir("$selfdir/$dirExternal/bin");
-if(!file_exists("spc")){
-    _exec("wget -O spc '$spcUrl'");
+if(!file_exists($SPC)){
+    _exec("wget -O $SPC '$spcUrl'");
+} else {
+    echo "⏩ SKIP download of spc.\n";
 }
 
 if (PHP_OS == "Linux") {
-    _exec("chmod +x spc");
+    _exec("chmod +x $SPC");
 }
 
 // ----------------------------------------------------------------------
 _h1("Spc - prepare environment");
+_chdir("$selfdir/$dirBuild");
 
 if(!file_exists("$doneBuild")){
-    _exec("./spc --no-interaction doctor");
-    _exec("./spc download --no-interaction --with-php=$php_version --for-extensions '$php_libs'");
+    _exec("$SPC --no-interaction doctor");
+    _exec("$SPC download --no-interaction --with-php=$php_version --for-extensions '$php_libs'");
 
-    echo "Hint: this can take 2 minutes ...\n";
-    _exec("./spc build --no-interaction --build-micro '$php_libs'");
+    echo "💡 Hint: this can take 2 minutes ...\n";
+    _exec("$SPC build --no-interaction --build-micro '$php_libs'");
     touch("$doneBuild");
 } else {
-    echo "Micro already built - php $php_version - extensions '$php_libs'\n";
+    echo "⏩ SKIP: Micro already built - php $php_version - extensions '$php_libs'\n";
 }
 
 
-- 
GitLab