<project name="OS X Native code" default="nativelibrary" basedir="/dev/null">
	<target name="init">
		<property environment="env" />
		<!-- Ask Xcode for correct path to XCode tools -->
		<!-- Will fail if XCode Command Line Tools are not installed on 10.7+ (Lion) -->
		<exec executable="xcrun" outputproperty="sdk_path" errorproperty="xcode-select.error" failonerror="false" failifexecutionfails="false">
			<arg value="--sdk" />
			<arg value="macosx" />
			<arg value="--show-sdk-path" />
		</exec>
		<!-- Default to /Developer if xcode-select fails -->
		<condition property="sdk_path" value="/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk">
			<isset property="xcode-select.error" />
		</condition>

		<property name="jvm_headers_path" value="include"/>
        <property name="jvm_headers_path_md" value="${jvm_headers_path}/darwin"/>

        <!-- TODO: detect link version -->
        <!--<property name="macos_link_ver" value="10.5"/>-->
        <property name="macos_link_ver" value="10.9"/>

		<condition property="javavmroot" value="${java.home}">
			<and>
				<not><isset property="javavmroot"/></not>
                <available file="${java.home}/${jvm_headers_path}" type="dir"/>
			</and>
        </condition>
		<condition property="javavmroot" value="${env.JAVA_HOME}">
			<and>
				<not><isset property="javavmroot"/></not>
                <available file="${env.JAVA_HOME}/${jvm_headers_path}" type="dir"/>
			</and>
        </condition>

		<!-- If we're on 10.7 or lower, use gcc instead of clang -->
		<condition property="compiler" value="clang">
			<and>
				<not><isset property="compiler"/></not>
				<available file="clang" filepath="${env.PATH}"/>
			</and>
		</condition>
		<condition property="compiler" value="gcc">
			<and>
				<not><isset property="compiler"/></not>
				<available file="gcc" filepath="${env.PATH}"/>
			</and>
		</condition>
		<condition property="compiler" value="gcc-11">
			<and>
				<not><isset property="compiler"/></not>
				<available file="gcc-11" filepath="${env.PATH}"/>
			</and>
		</condition>
		<condition property="compiler" value="gcc-4.2">
			<and>
				<not><isset property="compiler"/></not>
				<available file="gcc-4.2" filepath="${env.PATH}"/>
			</and>
		</condition>
	</target>

    <target name="compile" depends="init">
        <mkdir dir="${dstdir}" taskname="initializing ${dstdir} folder"/>
		<apply dir="${dstdir}" executable="${compiler}" os="Mac OS X" failonerror="true" dest="${dstdir}">
			<arg line="${cflags} -ObjC -O3 -Wall -Wunused -c -fPIC -I${javavmroot}/${jvm_headers_path_md} -I${javavmroot}/${jvm_headers_path} -I${lwjgl.src.native}/common -I${lwjgl.src.native}/common/opengl -I${lwjgl.src.native}/macosx"/>
		    <arg value="-I${lwjgl.target.gen.headers}"/>
			<!-- Map from *.m and *.c to .o -->
			<mapper type="regexp" from="^(.*)\.(c|m)" to="\1.o"/>
			<fileset dir="${lwjgl.src.native}/macosx" includes="*.m"/>
			<fileset dir="${lwjgl.src.native}/macosx" includes="*.c"/>
			<fileset dir="${lwjgl.src.native}/common" includes="*.c"/>
			<fileset dir="${lwjgl.src.native}/common/opengl" includes="*.c"/>
			<fileset dir="${lwjgl.target.gen.native}/openal" includes="*.c"/>
			<fileset dir="${lwjgl.target.gen.native}/opencl" includes="*.c"/>
			<fileset dir="${lwjgl.target.gen.native}/opengl" includes="*.c"/>
		</apply>
	</target>

	<target name="link" depends="init">
		<condition property="jdk_lib" value="${java.home}/lib">
			<available file="${java.home}/lib/libjawt.dylib" type="file"/>
		</condition>
		<condition property="jdk_lib" value="${java.home}/../Libraries">
			<not><isset property="jdk_lib"/></not>
		</condition>
        
		<apply dir="${objdir}" parallel="true" executable="${compiler}" os="Mac OS X" failonerror="true">
            <arg line="${linkerflags} -exported_symbols_list lwjgl.symbols -dynamiclib -o ${lwjgl.target.natives}/${libname} -framework Foundation -weak_framework AppKit -framework Carbon -framework OpenGL -framework QuartzCore -L${jdk_lib} -ljawt -L${sdk_path}/usr/lib"/>
			<fileset dir="${objdir}" includes="*.o"/>
        </apply>
        <echo message="Linked library"/>
		<apply dir="${lwjgl.target.natives}" executable="strip" os="Mac OS X" failonerror="true">
			<arg line="-S -X"/>
			<fileset file="${libname}"/>
		</apply>
        <echo message="Stripped library"/>
	</target>

	<target name="nativelibrary" depends="init">
		<echo message="       Java Home: ${java.home}"/>
		<echo message="        Compiler: ${compiler}"/>
		<echo message="      Mac OS SDK: ${sdk_path}"/>
		<echo message="JavaVM.framework: ${javavmroot}"/>

		<antcall target="-build"/>
	</target>

	<target name="-build">
		<antcall target="compile">
			<param name="dstdir" location="${lwjgl.target.gen.obj}/x86_64"/>
			<param name="sdkroot" location="${sdk_path}"/>
			<param name="cflags" value="-isysroot ${sdk_path} -arch x86_64 -mmacosx-version-min=${macos_link_ver}"/>
		</antcall>
        <exec vmlauncher="true" executable="sh" output="${lwjgl.target.gen.obj}/x86_64/lwjgl.symbols" failonerror="true" logError="true">
            <arg path="platform_build/macosx_ant/build-symbol-list"/>
            <arg path="${lwjgl.target.gen.obj}/x86_64"/>
		</exec>
		<antcall target="link">
			<param name="objdir" location="${lwjgl.target.gen.obj}/x86_64"/>
			<param name="libname" value="liblwjgl-x86_64.dylib"/>
			<param name="linkerflags" value="-isysroot ${sdk_path} -arch x86_64 -mmacosx-version-min=${macos_link_ver}"/>
		</antcall>
		<apply parallel="true" executable="lipo" os="Mac OS X" failonerror="true">
			<arg value="-create"/>
			<srcfile/>
			<arg value="-output"/>
            <arg path="${lwjgl.target.natives}/liblwjgl.dylib"/>
            <fileset file="${lwjgl.target.natives}/liblwjgl-x86_64.dylib"/>
		</apply>
	</target>
</project>
