Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine build architecture (32bit / 64bit) with ant?

We have inherited an ant build file but now need to deploy to both 32bit and 64bit systems.

The non-Java bits are done with GNUMakefiles where we just call "uname" to get the info. Is there a similar or even easier way to mimic this with ant?

like image 364
HD. Avatar asked Oct 20 '08 16:10

HD.


3 Answers

Late to the party, but what the heck...

${os.arch} only tells you if the JVM is 32/64bit. You may be running the 32bit JVM on a 64bit OS. Try this:

<var name ="os.bitness" value ="unknown"/>
<if>
<os family="windows"/>
<then>
    <exec dir="." executable="cmd" outputproperty="command.ouput">
        <arg line="/c SET ProgramFiles(x86)"/>
    </exec>
    <if>
        <contains string="${command.ouput}" substring="Program Files (x86)"/>
        <then>
            <var name ="os.bitness" value ="64"/>
        </then>
        <else>
            <var name ="os.bitness" value ="32"/>
        </else>
    </if>
</then>
<elseif>
    <os family="unix"/>
    <then>
        <exec dir="." executable="/bin/sh" outputproperty="command.ouput">
        <arg line="/c uname -m"/>
        </exec>
        <if>
            <contains string="${command.ouput}" substring="_64"/>
            <then>
                <var name ="os.bitness" value ="64"/>
            </then>
            <else>
                <var name ="os.bitness" value ="32"/>
            </else>
        </if>
    </then>
</elseif>
</if>

<echo>OS bitness: ${os.bitness}</echo>

EDIT: As @GreenieMeanie pointed out, this requires the ant-contrib library from ant-contrib.sourceforge.net

like image 81
phatypus Avatar answered Oct 07 '22 15:10

phatypus


you can get at the java system properties (http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties()) from ant with ${os.arch}. other properties of interest might be os.name, os.version, sun.cpu.endian, and sun.arch.data.model.

like image 38
Ray Tayek Avatar answered Oct 07 '22 13:10

Ray Tayek


Here is an answer that works (I tested on Kubuntu 64, Debian 32, Windows 2000 and Windows XP) without the need of external or optional ANT dependencies. It was based on @phatypus's answer.

<project name="FindArchitecture" default="check-architecture" basedir=".">

    <!-- Properties set: unix-like (if it is unix or linux), x64 (if it is 64-bits),
         register- size (32 or 64) -->
    <target name="check-architecture" depends="check-family,check-register" >
        <echo>Register size: ${register-size}</echo>
        <echo>OS Family: ${os-family}</echo>
    </target>

    <target name="check-family" >
        <condition property="os-family" value="unix" else="windows">
            <os family="unix" />
        </condition>

        <condition property="unix">
            <os family="unix" />
        </condition>
    </target>

    <target name="check-register" depends="reg-unix,reg-windows">
    </target>

    <!-- Test under GNU/Linux -->
    <target name="reg-unix" if="unix">
        <exec dir="." executable="uname" outputproperty="result">
            <arg line="-m"/>
        </exec>

        <!-- String ends in 64 -->
        <condition property="x64">
            <matches string="${result}" pattern="^.*64$"/>
        </condition>

        <condition property="register-size" value="64" else="32">
            <isset property="x64"/>
        </condition>
    </target>

    <!-- Test under MS/Windows-->
    <target name="reg-windows" unless="unix">
        <!-- 64 bit Windows versions have the variable "ProgramFiles(x86)" -->
        <exec dir="." executable="cmd" outputproperty="result">
            <arg line="/c SET ProgramFiles(x86)"/>
        </exec>

    <!-- String ends in "Program Files (x86)" -->
        <condition property="x64">
            <matches string="${result}" pattern="^.*=.*Program Files \(x86\)"/>
        </condition>

        <condition property="register-size" value="64" else="32">
            <isset property="x64"/>
        </condition>
    </target> 
</project>
like image 7
Luis Soeiro Avatar answered Oct 07 '22 15:10

Luis Soeiro