<?xml version="1.0" encoding="UTF-8"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at
   
     http://www.apache.org/licenses/LICENSE-2.0
     
   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.
-->
<project name="system-import">

  <property file="${basedir}/antscripts/platform.properties"/>
  <property environment="env"/>

  <!-- These 3 lines pull in antcontrib and cctasks -->
  <taskdef resource="net/sf/antcontrib/antlib.xml"/>
  <taskdef resource="cpptasks.tasks"/>
  <typedef resource="cpptasks.types"/>

  <typedef onerror="ignore" name="signer" classname="org.apache.commons.openpgp.ant.OpenPgpSignerTask" classpath="${basedir}/antscripts/OpenPGP.jar"/>

  <!--
     Configure the tuscanySDO.root.dir
     Take the override value from platform.properties if set
  -->
  <if>
    <and>
      <isset property="platform.tuscanySDO.root.dir"/>
      <length string="${platform.tuscanySDO.root.dir}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="tuscanySDO.root.dir" location="${platform.tuscanySDO.root.dir}"/>
    </then>
    <else>
      <property name="tuscanySDO.root.dir" location="."/>
    </else>
  </if>

  <property name="tuscanySDO.root.src.dir" location="${tuscanySDO.root.dir}/runtime"/>

  <!--
     Configure the tuscanySDO.install.dir
     Take the override value from platform.properties if set
     Else take it from the env var TUSCANY_SDOCPP
     Else use a default of ${tuscanySDO.root.dir}/deploy
  -->
  <if>
    <and>
      <isset property="platform.tuscanySDO.install.dir"/>
      <length string="${platform.tuscanySDO.install.dir}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="tuscanySDO.install.dir" location="${platform.tuscanySDO.install.dir}"/>
    </then>
    <elseif>
      <isset property="env.TUSCANY_SDOCPP"/>
      <then>
        <property name="tuscanySDO.install.dir" location="${env.TUSCANY_SDOCPP}"/>
      </then>
    </elseif>
    <else>
      <property name="tuscanySDO.install.dir" location="${tuscanySDO.root.dir}/deploy"/>
    </else>
  </if>

  <!--
     Configure the version for all tuscanySDO library generation
     If on windows, set it to empty
     Else take the override value from platform.properties if set
     Else default it to "0.0.0"
  -->
  <if>
    <os family="windows"/>
    <then>
      <property name="tuscanySDO.library.version" value=""/>
    </then>
    <elseif>
      <and>
        <isset property="platform.tuscanySDO.library.version"/>
        <length string="${platform.tuscanySDO.library.version}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="tuscanySDO.library.version" value="${platform.tuscanySDO.library.version}"/>
      </then>
    </elseif>
    <else>
      <property name="tuscanySDO.library.version" value=".0.0.0"/>
    </else>
  </if>

<!--
     Configure tuscanySDO.release.file.name that defines the generated packed file used on releases
     Take the override value from platform.properties if set
     Else take it from env var TUSCANY_SDOCPP_RELEASE_NAME
     If both are not defined, use as default "tuscany_sdo_cpp" name
  -->
 <if>
      <and>
        <isset property="platform.tuscanySDO.release.file.name"/>
        <length string="${platform.tuscanySDO.release.file.name}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="tuscanySDO.release.file.name" value="${platform.tuscanySDO.release.file.name}"/>
      </then>
      <elseif>
        <and>
          <isset property="env.TUSCANY_SDOCPP_RELEASE_NAME"/>
	<length string="${env.TUSCANY_SDOCPP_RELEASE_NAME}" when="greater" length="0" trim="true"/>
        </and>
        <then>
           <property name="tuscanySDO.release.file.name" value="${env.TUSCANY_SDOCPP_RELEASE_NAME}"/>
        </then>
      </elseif>
      <else>
        <property name="tuscanySDO.release.file.name" value="tuscany_sdo_cpp"/>
      </else>
    </if>

<!--
     Configure distribution.dir that defines where the distribution files will be placed
     Take the override value from platform.properties if set
     Else assumes ${basedir}/distribution as the distribution folder
  -->
 <if>
      <and>
        <isset property="platform.tuscanySDO.distribution.dir"/>
        <length string="${platform.tuscanySDO.distribution.dir}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="distribution.dir" location="${platform.tuscanySDO.distribution.dir}"/>
      </then>
      <else>
        <property name="distribution.dir" value="${basedir}/distribution"/>
      </else>
    </if>

<!--
     Load key id, public and secret key path from platform.properties files into key.id, public.key.path and secret.key.path variables respectively
     Also checks if the user defined the password on the command line
  -->
  <target name="check.keys">
    <if>
      <and>
        <isset property="platform.public.key.path"/>
        <isset property="platform.key.id"/>
        <isset property="platform.secret.key.path"/>
        <length string="${platform.public.key.path}" when="greater" length="0" trim="true"/>
        <length string="${platform.key.id}" when="greater" length="0" trim="true"/>
        <length string="${platform.secret.key.path}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="public.key.path" location="${platform.public.key.path}"/>
        <property name="secret.key.path" location="${platform.secret.key.path}"/>
        <property name="key.id" value="${platform.key.id}"/>
      </then>
      <else>
        <fail message="The public, secret and key id must be defined on platform.properties file"/>
      </else>
    </if>
    <if>
      <and>
        <isset property="password"/>
        <length string="${password}" when="greater" length="0" trim="true"/>      </and>
      <then>
        <property name="secret.key.password" value="${password}"/>
      </then>
      <else>
        <fail message='Secret key password was not properly set. Add on the ant command line, for example, -Dpassword="123"'/>
      </else>
    </if>
  </target>

  <!--
     Configure ${enable_ws} and ${axis2c.home.dir}, which is needed to compile the ws extension
     Take the override value from platform.properties if set
     Else take it from the env var AXIS2C_HOME
     The axis2c.home.dir property can only be obtained by calling this target
  -->
  <target name="check.ws" depends="check.libxml2">
    <if>
      <and>
        <isset property="platform.axis2c.home.dir"/>
        <length string="${platform.axis2c.home.dir}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="enable_ws" value="true"/>
        <property name="axis2c.home.dir" location="${platform.axis2c.home.dir}"/>
      </then>
      <elseif>
        <isset property="env.AXIS2C_HOME"/>
        <then>
          <property name="enable_ws" value="true"/>
          <property name="axis2c.home.dir" location="${env.AXIS2C_HOME}"/>
        </then>
      </elseif>
      <else>
        <echo message="TuscanySDO ws extension is not enabled" level="warning"/>
      </else>
    </if>
  </target>
  
  <!--
     Configure ${enable_libxml2}, which is needed to compile the ws extension
     Take the override value from platform.properties if set
     Else take it from the env var AXIS2C_HOME
     The libxml2.* properties can only be obtained by calling this target
  -->
  <target name="check.libxml2">
    <if>
      <and>
        <isset property="platform.libxml2.lib.dir"/>
        <isset property="platform.libxml2.include.dir"/>
        <length string="${platform.libxml2.lib.dir}" when="greater" length="0" trim="true"/>
        <length string="${platform.libxml2.include.dir}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="enable_libxml2" value="true"/>
        <property name="libxml2.lib.dir"     location="${platform.libxml2.lib.dir}"/>
        <property name="libxml2.include.dir" location="${platform.libxml2.include.dir}"/>
        <condition property="iconv.home.dir" value="${platform.iconv.home.dir}">
          <os family="windows"/>
        </condition>
      </then>
      <elseif>
        <and>
          <isset property="env.LIBXML2_LIB"/>
          <isset property="env.LIBXML2_INCLUDE"/>
        </and>
        <then>
          <property name="enable_libxml2" value="true"/>
          <property name="libxml2.lib.dir"     location="${env.LIBXML2_LIB}"/>
          <property name="libxml2.include.dir" location="${env.LIBXML2_INCLUDE}"/>
          <condition property="iconv.home.dir" value="${env.ICONV_HOME}">
            <os family="windows"/>
          </condition>
        </then>
      </elseif>
      <else>
        <fail message="Unable to find libxml2 installation, must be set by LIBXML2_LIB and LIBXML2_INCLUDE or in platform.properties file"/>
      </else>
    </if>
  </target>

  <!--
     Configure the debug.compile flag
     Try looking first on the command line
     Then take the override value from platform.properties if set
     Else its false
  -->
  <if>
    <isset property="debug"/>
    <then>
      <!-- Tests if its set on the ant command line "ant -Ddebug=true" -->
      <if>
        <istrue value="${debug}"/>
        <then>
          <property name="debug.compile" value="true"/>
        </then>
      </if>
    </then>
    <elseif>
      <and>
        <isset property="platform.debug.compile"/>
        <istrue value="${platform.debug.compile}"/>
        <length string="${platform.debug.compile}" when="greater" length="0" trim="true"/>
      </and>
      <then>
        <property name="debug.compile" value="true"/>
      </then>
    </elseif>
  </if>
  <property name="debug.compile" value="false"/>

  <!--
     Configure the compiler.name
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.compiler.name"/>
      <length string="${platform.compiler.name}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="compiler.name" value="${platform.compiler.name}"/>
    </then>
    <else>
      <condition property="compiler.name" value="msvc8">
        <os family="windows"/>
      </condition>

      <condition property="compiler.name" value="g++">
        <os family="unix"/>
       </condition>

      <condition property="compiler.name" value="mac_g++">
        <os family="mac"/>
      </condition>
    </else>
  </if>

  <!--
     Configure the lib.ext
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.lib.ext"/>
      <length string="${platform.lib.ext}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="lib.ext" value="${platform.lib.ext}"/>
    </then>
    <else>
      <condition property="lib.ext" value=".lib">
        <os family="windows"/>
      </condition>

      <condition property="lib.ext" value=".dylib">
        <os family="mac"/>
      </condition>

      <condition property="lib.ext" value=".so">
        <os family="unix"/>
      </condition>
    </else>
  </if>

  <!--
     Configure the dll.ext, used for windows only
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.dll.ext"/>
      <length string="${platform.dll.ext}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="dll.ext" value="${platform.dll.ext}"/>
    </then>
    <else>
      <condition property="dll.ext" value=".dll">
        <os family="windows"/>
      </condition>

      <property name="dll.ext" value=""/>

    </else>
  </if>

  <!--
     Configure the lib.prefix
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.lib.prefix"/>
      <length string="${platform.lib.prefix}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="lib.prefix" value="${platform.lib.prefix}"/>
    </then>
    <else>
      <condition property="lib.prefix" value="">
        <os family="windows"/>
      </condition>

      <condition property="lib.prefix" value="lib">
        <or>
          <os family="unix"/>
          <os family="mac"/>
        </or>
      </condition>
    </else>
  </if>


  <!--
     Configure the object.ext
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.object.ext"/>
      <length string="${platform.object.ext}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="object.ext" value="${platform.object.ext}"/>
    </then>
    <else>
      <condition property="object.ext" value=".obj">
        <os family="windows"/>
      </condition>

      <condition property="object.ext" value=".o">
        <or>
          <os family="unix"/>
          <os family="mac"/>
        </or>
      </condition>
    </else>
  </if>


  <!--
     Configure the exe.ext
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.exe.ext"/>
      <length string="${platform.exe.ext}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="exe.ext" value="${platform.exe.ext}"/>
    </then>
    <else>
      <condition property="exe.ext" value=".exe">
        <os family="windows"/>
      </condition>

      <condition property="exe.ext" value="">
        <or>
          <os family="unix"/>
          <os family="mac"/>
        </or>
      </condition>
    </else>
  </if>


  <!--
     Configure the script.ext
     Take the override value from platform.properties if set
     Else set it based on the OS
  -->
  <if>
    <and>
      <isset property="platform.script.ext"/>
      <length string="${platform.script.ext}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="script.ext" value="${platform.script.ext}"/>
    </then>
    <else>
      <condition property="script.ext" value=".bat">
        <os family="windows"/>
      </condition>

      <condition property="script.ext" value=".sh">
        <or>
          <os family="unix"/>
          <os family="mac"/>
        </or>
      </condition>
    </else>
  </if>

  <!--
     Configure the SDO implementation library and include path, which
     will be used for sdo axiom compilation. By default, sdo axiom will
     include and link the TuscanySDO implementation, but this allows for
     compiling with other SDO implementations
     Set the following:
        sdo.impl.library.name
        sdo.impl.library.path
        sdo.impl.include.path
  -->
  <if>
    <and>
      <isset property="platform.sdo.impl.library.name"/>
      <isset property="platform.sdo.impl.library.path"/>
      <isset property="platform.sdo.impl.include.path"/>
      <length string="${platform.sdo.impl.library.name}" when="greater" length="0" trim="true"/>
      <length string="${platform.sdo.impl.library.path}" when="greater" length="0" trim="true"/>
      <length string="${platform.sdo.impl.include.path}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="sdo.impl.library.name" value="${platform.sdo.impl.library.name}"/>
      <property name="sdo.impl.library.path" location="${platform.sdo.impl.library.path}"/>
      <property name="sdo.impl.include.path" location="${platform.sdo.impl.include.path}"/>
    </then>
    <else>
      <property name="sdo.impl.library.name" value="tuscany_sdo"/>
      <property name="sdo.impl.library.path" location="${tuscanySDO.install.dir}/lib"/>
      <property name="sdo.impl.include.path" location="${tuscanySDO.root.src.dir}/core/src"/>
    </else>
  </if>


  <condition property="windows" value="true">
    <os family="windows"/>
  </condition>

  <condition property="mac" value="true">
    <os family="mac"/>
  </condition>

  <!--
     Base compiler definition
     For now the base compiler is empty.
    -->
  <compiler id="Tuscany-BaseCompiler" multithreaded="true" exceptions="true" rtti="true"/>

  <!--
     Specific compiler definitions
     The compilerarg location functionality completely omits the arg: antcontrib bug 1794857
    -->

    <!-- MSVC compilers for windows -->
  <compiler id="msvc-Compiler" extends="Tuscany-BaseCompiler" name="msvc">
    <defineset define="WIN32,_CRT_SECURE_NO_DEPRECATE,_CRT_NON_CONFORMING_SWPRINTFS"/>
  </compiler>

    <!-- MSVC compilers for windows 
         The standard msvc ant contrib cc task compiler doesnt work so well,
         so we made our own until the bugs are resolved. The actual implementation
         of TuscanyMSVC8DevStudioCCompiler is in TuscanySCA/tools/ant_cpptasks
     -->
  <compiler id="msvc8-Compiler"
            extends="Tuscany-BaseCompiler"
            classname="tuscany.antCompilers.TuscanyMSVC8DevStudioCCompiler">
    <defineset define="WIN32,_CRT_SECURE_NO_DEPRECATE,_CRT_NON_CONFORMING_SWPRINTFS"/>
  </compiler>

    <!-- g++ compiler for most Unix platforms -->
  <compiler id="g++-Compiler" extends="Tuscany-BaseCompiler" name="g++"/>
  
    <!-- g++ compiler for most Unix platforms
         Used for cross compilation with the -m32 flag -->
  <compiler id="g++m32-Compiler" extends="Tuscany-BaseCompiler" name="g++">
    <compilerarg value="-m32"/>
  </compiler>

    <!-- g++ compiler for Apple mac -->
  <compiler id="mac_g++-Compiler" extends="Tuscany-BaseCompiler" name="g++">
    <defineset define="IS_DARWIN"/>
  </compiler>

  <!--
     Base linker definition
    -->
  <linker id="Tuscany-BaseLinker">
    <syslibset if="windows" libs="kernel32,user32,ws2_32"/>
  </linker>

  <!--
     Specific linker definitions
     The linker extends funcionality isnt working correctly: antcontrib bug 1794867
     Specifically, the syslibset for dl was not working on Linux
    -->
  <linker id="msvc-Linker" extends="Tuscany-BaseLinker" name="msvc">
    <syslibset libs="kernel32,user32,ws2_32"/>
  </linker>
    
  <linker id="msvc8-Linker" extends="Tuscany-BaseLinker" name="msvc">
    <syslibset libs="kernel32,user32,ws2_32"/>
  </linker>
    
  <linker id="g++-Linker" extends="Tuscany-BaseLinker" name="g++"/>

  <linker id="g++m32-Linker" extends="Tuscany-BaseLinker" name="g++">
    <linkerarg value="-m32"/>
  </linker>

  <!--
    Setup the external definitions file, if there is one
  -->
  <if>
    <and>
      <isset property="platform.external.definitions.file"/>
      <length string="${platform.external.definitions.file}" when="greater" length="0" trim="true"/>
    </and>
    <then>
      <property name="external.definitions.file" value="${platform.external.definitions.file}"/>
      <import file="${external.definitions.file}"/>
    </then>
  </if>
  <property name="external.definitions.file" value=""/>

  <target name="display.system" depends="check.ws,check.libxml2">

    <!-- Tuscany paths, etc -->
    <echo message=" "/>
    <echo message="TuscanySDO paths"/>
    <echo message="  tuscanySDO.root.dir=         ${tuscanySDO.root.dir}"/>
    <echo message="  tuscanySDO.root.src.dir=     ${tuscanySDO.root.src.dir}"/>
    <echo message="  tuscanySDO.install.dir=      ${tuscanySDO.install.dir}"/>
    <echo message="  sdo.impl.library.name=       ${sdo.impl.library.name}"/>
    <echo message="  sdo.impl.library.path=       ${sdo.impl.library.path}"/>
    <echo message="  sdo.impl.include.path=       ${sdo.impl.include.path}"/>
    <echo message="  tuscanySDO.library.version=  '${tuscanySDO.library.version}'"/>

    <!-- compilers, libs, etc -->
    <echo message=" "/>
    <echo message="TuscanySDO compiler configuration"/>
    <echo message="  compiler.name=               '${compiler.name}'"/>
    <echo message="  debug.compile=               '${debug.compile}'"/>
    <echo message="  lib.ext=                     '${lib.ext}'"/>
    <echo message="  dll.ext=                     '${dll.ext}'"/>
    <echo message="  lib.prefix=                  '${lib.prefix}'"/>
    <echo message="  object.ext=                  '${object.ext}'"/>
    <echo message="  exe.ext=                     '${exe.ext}'"/>
    <echo message="  script.ext=                  '${script.ext}'"/>
    <echo message="  external.definitions.file=   '${external.definitions.file}'"/>

    <!-- libxml2 -->
    <if>
      <isset property="enable_libxml2"/>
      <then>
        <echo message=" "/>
        <echo message="libxml2 location"/>
        <echo message="  libxml2.lib.dir=          ${libxml2.lib.dir}"/>
        <echo message="  libxml2.include.dir=      ${libxml2.include.dir}"/>
      </then>
    </if>

    <!-- axis2c -->
    <if>
      <isset property="enable_ws"/>
      <then>
        <echo message=" "/>
        <echo message="axis2c location"/>
        <echo message="  axis2c.home.dir=          ${axis2c.home.dir}"/>
      </then>
    </if>

    <echo message=" "/>

  </target>

</project>
