Bearsampp / .teams

Private documentation repo for the Organization
https://bearsampp.com
GNU General Public License v3.0
0 stars 0 forks source link

build.xml for php needs better error checking #90

Closed N6REJ closed 6 months ago

N6REJ commented 6 months ago

build.xml will currently allow .dlls that do now pass sigchec -q MYFILENAME.dll so we need to modify it to test it against sigchecks "description" and "MachineType" values to be sure they are not "n/a"

using this target as the basis of the test ( which currently doesn't function ) modify build.xml

Currently output is:


BUILD FAILED
E:\Bearsampp-development\dev\build\build-bundle.xml:183: The following error occurred while executing this line:
E:\Bearsampp-development\dev\build\build-bundle.xml:154: The following error occurred while executing this line:
E:\Bearsampp-development\module-php\build.xml:149: The following error occurred while executing this line:
E:\Bearsampp-development\module-php\build.xml:180: Sigcheck failed for file E:\Bearsampp-development\bearsampp-build\tmp\dl/php_xdebug-3.3.2-8.2-vs16-x86_64.dll: Description='${description}', MachineType='${machine.type}'

youi can see that the variables are not being populated


  <target name="sigcheck" if="${phpext.dest.endsWith('.dll')}">
    <!-- Check sigcheck output -->
    <exec executable="sigcheck" outputproperty="sigcheck.output" errorproperty="sigcheck.error">
      <arg value="-q"/>
      <arg value="${phpext.dest}"/>
    </exec>

    <!-- Extract description and machine type using Ant's built-in features -->
    <loadfile property="description" srcFile="${phpext.dest}">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern=".*Description:\s*(.*)\s*MachineType:\s*(.*)\s*Verified.*" replace="\1" flags="gs"/>
        </tokenfilter>
      </filterchain>
    </loadfile>
    <echo message="Description: ${description}"/>

    <loadfile property="machine.type" srcFile="${phpext.dest}">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern=".*MachineType:\s*(.*)\s*Verified.*" replace="\1" flags="gs"/>
        </tokenfilter>
      </filterchain>
    </loadfile>
    <echo message="MachineType: ${machine.type}"/>

    <!-- Check if description is empty -->
    <fail unless="description" message="Description is empty"/>

    <!-- Execute PowerShell command if description is empty -->
    <if>
      <equals arg1="${description}" arg2="" trim="true"/>
      <then>
        <exec executable="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" failonerror="true">
          <arg value="-NoLogo"/>
          <arg value="-NoProfile"/>
          <arg value="-Command"/>
          <arg value="(New-Object -ComObject SAPI.SpVoice).Speak('OH NO! the php_@{phpext} file is corrupted, the description is null')" />
        </exec>
      </then>
    </if>

    <!-- Execute PowerShell command if machine type is not 64-bit -->
    <if>
      <not>
        <equals arg1="${machine.type}" arg2="64-bit" trim="true" casesensitive="false"/>
      </not>
      <then>
        <exec executable="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" failonerror="true">
          <arg value="-NoLogo"/>
          <arg value="-NoProfile"/>
          <arg value="-Command"/>
          <arg value="(New-Object -ComObject SAPI.SpVoice).Speak('OH NO! the php_@{phpext} file is not built for x64')" />
        </exec>
      </then>
    </if>
  </target>

Here is the full build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="module-php" basedir=".">

  <dirname property="project.basedir" file="${ant.file.module-php}"/>
  <property name="root.dir" location="${project.basedir}/.."/>
  <property name="build.properties" value="${project.basedir}/build.properties"/>
  <property file="${build.properties}"/>

  <!-- Bearsampp dev -->
  <property name="dev.path" location="${root.dir}/dev"/>
  <fail unless="dev.path" message="Project 'dev' not found in ${dev.path}"/>
  <echo message="Bearsampp dev found in ${dev.path}" level="debug"/>

  <!-- Import build-commons.xml -->
  <import file="${dev.path}/build/build-commons.xml"/>
  <!-- Import build-bundle.xml -->
  <import file="${dev.path}/build/build-bundle.xml"/>

  <property name="php-ext.path" location="${project.basedir}/ext"/>
  <property name="pear-install.path" location="${project.basedir}/pear"/>

  <target name="sigcheck" if="${phpext.dest.endsWith('.dll')}">
    <!-- Check sigcheck output -->
    <exec executable="sigcheck" outputproperty="sigcheck.output" errorproperty="sigcheck.error">
      <arg value="-q"/>
      <arg value="${phpext.dest}"/>
    </exec>

    <!-- Extract description and machine type using Ant's built-in features -->
    <loadfile property="description" srcFile="${phpext.dest}">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern=".*Description:\s*(.*)\s*MachineType:\s*(.*)\s*Verified.*" replace="\1" flags="gs"/>
        </tokenfilter>
      </filterchain>
    </loadfile>
    <echo message="Description: ${description}"/>

    <loadfile property="machine.type" srcFile="${phpext.dest}">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern=".*MachineType:\s*(.*)\s*Verified.*" replace="\1" flags="gs"/>
        </tokenfilter>
      </filterchain>
    </loadfile>
    <echo message="MachineType: ${machine.type}"/>

    <!-- Check if description is empty -->
    <fail unless="description" message="Description is empty"/>

    <!-- Execute PowerShell command if description is empty -->
    <if>
      <equals arg1="${description}" arg2="" trim="true"/>
      <then>
        <exec executable="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" failonerror="true">
          <arg value="-NoLogo"/>
          <arg value="-NoProfile"/>
          <arg value="-Command"/>
          <arg value="(New-Object -ComObject SAPI.SpVoice).Speak('OH NO! the php_@{phpext} file is corrupted, the description is null')" />
        </exec>
      </then>
    </if>

    <!-- Execute PowerShell command if machine type is not 64-bit -->
    <if>
      <not>
        <equals arg1="${machine.type}" arg2="64-bit" trim="true" casesensitive="false"/>
      </not>
      <then>
        <exec executable="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" failonerror="true">
          <arg value="-NoLogo"/>
          <arg value="-NoProfile"/>
          <arg value="-Command"/>
          <arg value="(New-Object -ComObject SAPI.SpVoice).Speak('OH NO! the php_@{phpext} file is not built for x64')" />
        </exec>
      </then>
    </if>
  </target>

  <target name="release.build">
    <basename property="bundle.folder" file="${bundle.path}"/>
    <replaceproperty src="bundle.folder" dest="bundle.version" replace="${bundle.name}" with=""/>

    <getmoduleuntouched name="${bundle.name}" version="${bundle.version}" propSrcDest="bundle.srcdest" propSrcFilename="bundle.srcfilename"/>
    <assertfile file="${bundle.srcdest}/php.exe"/>

    <property name="php.prep.path" location="${bundle.tmp.prep.path}/${bundle.name}${bundle.version}"/>
    <delete dir="${php.prep.path}"/>
    <mkdir dir="${php.prep.path}"/>
    <copy todir="${php.prep.path}" overwrite="true">
      <fileset dir="${bundle.srcdest}" excludes="
        dev/**,
        extras/**,
        PEAR/**,
        *.reg,
        install.txt,
        go-pear.bat,
        news.txt,
        php.gif,
        php.ini*,
        snapshot.txt"
      />
    </copy>
    <copy todir="${php.prep.path}" overwrite="true">
      <fileset dir="${bundle.path}" defaultexcludes="yes"/>
    </copy>

    <!-- Install PEAR -->
    <if>
      <available file="${php.prep.path}/pear.properties" type="file"/>
      <then>
        <property prefix="phppear" file="${php.prep.path}/pear.properties"/>

        <!-- Copy pear-install scripts -->
        <echo message="Copying pear-install scripts..."/>
        <delete dir="${php.prep.path}/pear-install"/>
        <copy todir="${php.prep.path}/pear-install">
          <fileset dir="${pear-install.path}" defaultexcludes="yes"/>
        </copy>
        <assertfile file="${php.prep.path}/pear-install/pear-install.bat"/>

        <!-- Download phar -->
        <download url="${phppear.pear}" returnProperty="phppear.dest"/>
        <copy file="${phppear.dest}"
          tofile="${php.prep.path}/pear-install/install-pear-nozlib.phar" overwrite="true"/>

        <!-- Launch phar -->
        <echo message="Processing phar..."/>
        <exec executable="${php.prep.path}/pear-install/pear-install.bat"
          dir="${php.prep.path}/pear-install"
          failonerror="true"
        />

        <delete dir="${php.prep.path}/pear-install"/>
        <delete file="${php.prep.path}/pear.properties"/>
      </then>
    </if>

    <!-- Extensions -->
    <if>
      <available file="${php.prep.path}/exts.properties" type="file"/>
      <then>
        <echo message="Processing extensions..."/>
        <delete file="${build.tmp.path}/php_extensions.tmp"/>
        <touch file="${build.tmp.path}/php_extensions.tmp"/>
        <property prefix="phpexts" file="${php.prep.path}/exts.properties"/>
        <propertyselector property="phpexts" match="phpexts\.(.*)" select="\1"/>
        <for list="${phpexts}" param="phpext">
          <sequential>
            <var name="phpext.version" unset="true"/>
            <var name="phpext.dest" unset="true"/>
            <var name="phpext.dll" unset="true"/>
            <propertyregex property="phpext.version" input="${phpexts.@{phpext}}" regexp="php_.*-(.*?(-dev))" select="\1"/>
            <propertyregex property="phpext.version" input="${phpexts.@{phpext}}" regexp="php_.*-(.*)-(5\.|7\.)" select="\1"/>

            <!-- Download ext -->
            <download url="${phpexts.@{phpext}}" returnProperty="phpext.dest"/>
            <echo message="Extension path: ${phpext.dest}"/>
            <condition property="phpext.dll" value="${phpext.dest}/php_@{phpext}.dll"><available file="${phpext.dest}/php_@{phpext}.dll" type="file"/></condition>
            <condition property="phpext.dll" value="${phpext.dest}"><matches string="${phpext.dest}" pattern="\.dll$"/></condition>
            <fail unless="phpext.dll" message="Main dll not found"/>

            <!-- Check if phpext.dest ends with .dll -->
            <condition property="is.dll.file">
              <matches pattern="\.dll$" string="${phpext.dest}"/>
            </condition>

            <!-- Call sigcheck only if phpext.dest is a .dll file -->
            <if>
              <isset property="is.dll.file"/>
              <then>
                <!-- Check sigcheck output -->
                <exec executable="sigcheck" outputproperty="sigcheck.output" errorproperty="sigcheck.error">
                  <arg value="-q"/>
                  <arg value="${phpext.dest}"/>
                </exec>

                <!-- Display error message if sigcheck fails -->
                <fail message="Sigcheck failed for file ${phpext.dest}: Description='${description}', MachineType='${machine.type}'${line.separator}${sigcheck.error}" if="sigcheck.error"/>
              </then>
            </if>

            <!-- Copy ext -->
            <echo message="Copy @{phpext} ${phpext.version} extension..."/>
            <copy file="${phpext.dll}" tofile="${php.prep.path}/ext/php_@{phpext}.dll" overwrite="true"/>
            <if>
              <equals arg1="@{phpext}" arg2="imagick"/>
              <then>
                <copy todir="${php.prep.path}/imagick">
                  <fileset dir="${phpext.dest}" includes="CORE_*.dll"/>
                </copy>
              </then>
            </if>

             <!-- Echo extension string to temp file -->
            <if>
              <not><equals arg1="@{phpext}" arg2="xdebug"/></not>
              <then>
                <echo message="extension=@{phpext}${line.separator}" file="${build.tmp.path}/php_extensions.tmp" append="true"/>
              </then>
            </if>
          </sequential>
        </for>

        <!-- Inject in php.ini -->
        <if>
          <length file="${build.tmp.path}/php_extensions.tmp" when="equal" length="0" />
          <then>
            <property name="php.extensions" value=""/>
          </then>
          <else>
            <loadfile property="php.extensions" srcFile="${build.tmp.path}/php_extensions.tmp"/>
          </else>
        </if>
        <echo message="${line.separator}PHP extensions to inject in php.ini :${line.separator}${php.extensions}"/>
        <copy todir="${php.prep.path}" overwrite="true">
          <filterset>
           <filter token="PHP_EXTENSIONS" value="${php.extensions}"/>
          </filterset>
          <resources>
            <file file="${bundle.path}/php.ini"/>
            <file file="${bundle.path}/php.ini.ber"/>
          </resources>
        </copy>

        <delete file="${php.prep.path}/exts.properties"/>
      </then>
      <else>
        <copy todir="${php.prep.path}" overwrite="true">
          <filterset>
            <filter token="PHP_EXTENSIONS" value=""/>
          </filterset>
          <resources>
            <file file="${bundle.path}/php.ini"/>
            <file file="${bundle.path}/php.ini.ber"/>
          </resources>
        </copy>
      </else>
    </if>

    <!-- Dependencies -->
    <if>
      <available file="${php.prep.path}/deps.properties" type="file"/>
      <then>
        <echo message="Processing dependencies..."/>
        <property prefix="phpdeps" file="${php.prep.path}/deps.properties"/>
        <propertyselector property="phpdeps" match="phpdeps\.(.*)" select="\1"/>
        <for list="${phpdeps}" param="phpdep">
          <sequential>
            <var name="phpdep.dest" unset="true"/>

            <!-- Download dep -->
            <download url="${phpdeps.@{phpdep}}" returnProperty="phpdep.dest"/>
            <echo message="Dependency path: ${phpdep.dest}"/>

            <!-- Copy dep -->
            <echo message="Copy @{phpdep} dependency..."/>
            <if>
              <equals arg1="@{phpdep}" arg2="imagemagick"/>
              <then>
                <copy todir="${php.prep.path}/imagick" overwrite="true">
                  <fileset dir="${phpdep.dest}" includes="
                    *.exe,
                    *.dll"
                  />
                </copy>
              </then>
            </if>
          </sequential>
        </for>
        <delete file="${php.prep.path}/deps.properties"/>
      </then>
    </if>
  </target>
</project>
jwaisner commented 6 months ago

Resolved and merged into main.