all: javaprofiler

UNAME = $(shell uname -s)
MNAME = $(shell uname -m)
EXTENS = script

CP_NO_DREF_OPT = -R


ifeq ($(UNAME), AIX)
	CP_NO_DREF_OPT = -h
	EXCLUDE_PROBEKIT = 1
	ifdef SIXTYFOURBIT
		EXTENS = AIX-PPC64
		BUILD_PLATFORM = aix_ppc64
	else
		EXTENS = AIX-PPC
		BUILD_PLATFORM = aix_ppc
	endif
endif


ifeq ($(UNAME), SunOS)
    # Solaris requires the -R option to be used in conjunction with -P
    CP_NO_DREF_OPT = -RP
    EXCLUDE_PROBEKIT = 1
    ifeq ($(MNAME), i86pc)
	    ifdef SIXTYFOURBIT
       	    BUILD_PLATFORM = solaris_x64
       	    EXTENS = SOLARIS-X64
       	else
       	    BUILD_PLATFORM = solaris_x86
       	    EXTENS = SOLARIS-X86
       	endif
    else
		ifdef SIXTYFOURBIT
                BUILD_PLATFORM = solaris-sparc-v9
                EXTENS = SPARC-V9
        else
    	BUILD_PLATFORM = solaris-sparc
		EXTENS = SPARC
		endif
    endif
endif

ifeq ($(UNAME), Linux)
    CP_NO_DREF_OPT = -d
	ifeq ($(MNAME), ia64)
       	BUILD_PLATFORM = linux_ipf
       	EXTENS = IPF
       	EXCLUDE_PROBEKIT = 1
    else
		ifeq ($(MNAME), x86_64)
            ifdef FORCE32
                BUILD_PLATFORM = linux_ia32
                EXTENS = IA-32
            else
           	    BUILD_PLATFORM = linux_em64t
           	    EXTENS = EM64T
           	endif
    	else
    		ifeq ($(MNAME), s390x)
    			ifdef FORCE31
     				BUILD_PLATFORM = linux_s390
    				EXTENS = S390
    				EXCLUDE_PROBEKIT = 1
    			else
    				BUILD_PLATFORM = linux_s390x
    				EXTENS = S390X
    				EXCLUDE_PROBEKIT = 1
    			endif
    		else
    			ifeq ($(MNAME), s390)
    				BUILD_PLATFORM = linux_s390
    				EXTENS = S390
    				EXCLUDE_PROBEKIT = 1
    			else
            		BUILD_PLATFORM = linux_ia32
            		EXTENS = IA-32
            	endif
            endif
		endif
    endif
endif
ifeq ($(UNAME), OS/390)
    # Note:	for the -P option to prevent the cp command from following symbolic links,
    #		the environment variable _UNIX03=YES must be set. The -P option can be
    #		used only in conjunction with the -R or -r options
    CP_NO_DREF_OPT = -RP
    EXCLUDE_PROBEKIT = 1
    ifdef SIXTYFOURBIT
        BUILD_PLATFORM = zos_s390x
        EXTENS = zOS64
    else
        BUILD_PLATFORM = zos_s390
        EXTENS = zOS
    endif
endif

BUILD_OPT="RELEASE=1"

javaprofiler:
	# Make ProbeAgentExtension then the JVMTI, ProbeAgentExtension is not supported on IPF
ifndef EXCLUDE_PROBEKIT
	make --directory=org.eclipse.hyades.probekit/src-native/ProbeAgentExtension -f Makefile.unix
endif
	cd org.eclipse.tptp.platform.jvmti.runtime/src-native; \
	mkdir -p plugins/org.eclipse.tptp.javaprofiler; \
	cd build; \
	chmod +x build_tptp_all.script; \
	./build_tptp_all.script ${BUILD_OPT}; \
	cd ..

clean:
	RELEASE=1
	cd org.eclipse.tptp.platform.jvmti.runtime/src-native; \
	cd build; \
	chmod +x build_tptp_all.script; \
	./build_tptp_all.script clean; \
	cd ..
	
package:
ifndef EXCLUDE_PROBEKIT
	# Package ProbeAgentExtension
	rm -rf pae-package
	mkdir -p pae-package/config
	mkdir -p pae-package/lib
	cp org.eclipse.hyades.probekit/about.html pae-package
	cp org.eclipse.hyades.probekit/src-native/ProbeAgentExtension/ProbeAgentExtension.so pae-package/lib
	cp $(PROBEKIT_LIB)/BCIEngProbe.so pae-package/lib
	cp org.eclipse.hyades.probekit/src-native/ProbeAgentExtension/pluginconfig_linux.xml pae-package/config/pluginconfig.xml
	cd pae-package; \
	zip -r9y ../pae.$(BUILD_PLATFORM).zip *; \
	cd lib; \
	zip -r9y ../../pae-only.$(BUILD_PLATFORM).zip ProbeAgentExtension.so; \
	cd ../..
endif

	# Package JVMTI
	rm -rf packaging
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/config
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/org
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/about_files
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini/analysis
	cp org.eclipse.tptp.platform.jvmti.runtime/about.html packaging/plugins/org.eclipse.tptp.javaprofiler
	cp -R org.eclipse.tptp.platform.jvmti.runtime/about_files/* packaging/plugins/org.eclipse.tptp.javaprofiler/about_files
	if [ -e ${TEMP_LICENSE_HOME}/epl-v10.html ]; then cp ${TEMP_LICENSE_HOME}/epl-v10.html packaging; \
	fi;
	if [ -e ${TEMP_LICENSE_HOME}/notice.html ]; then cp ${TEMP_LICENSE_HOME}/notice.html packaging; \
	fi;

#	Some Solaris systems are unable to copy symbolic links, to work around this, the files are zipped up and unzipped into the correct location.
ifeq ($(UNAME), SunOS)
    ifeq ($(MNAME), i86pc)
	    cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.a packaging/plugins/org.eclipse.tptp.javaprofiler
	    cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.so packaging/plugins/org.eclipse.tptp.javaprofiler
	    cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/readme.txt packaging/plugins/org.eclipse.tptp.javaprofiler
    else
	    zip -qjy tmp.zip org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.a org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.so org.eclipse.tptp.platform.jvmti.runtime/readme.txt
	    unzip tmp.zip -d packaging/plugins/org.eclipse.tptp.javaprofiler
	    rm -rf tmp.zip
    endif
else
	cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.a packaging/plugins/org.eclipse.tptp.javaprofiler
	cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/*.so packaging/plugins/org.eclipse.tptp.javaprofiler
	cp $(CP_NO_DREF_OPT) org.eclipse.tptp.platform.jvmti.runtime/readme.txt packaging/plugins/org.eclipse.tptp.javaprofiler
endif
ifeq ($(UNAME), AIX)
	cp org.eclipse.tptp.platform.jvmti.runtime/static_config/aix/pluginconfig.xml packaging/plugins/org.eclipse.tptp.javaprofiler/config
else
    ifeq ($(UNAME), OS/390)
	    cp org.eclipse.tptp.platform.jvmti.runtime/static_config/MVS/pluginconfig.xml packaging/plugins/org.eclipse.tptp.javaprofiler/config
    else
	    cp org.eclipse.tptp.platform.jvmti.runtime/static_config/linux/pluginconfig.xml packaging/plugins/org.eclipse.tptp.javaprofiler/config
    endif
endif

	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/src/Martini/Infrastructure/CGAdaptor/org/eclipse/tptp/martini/CGProxy.class packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/src/Martini/Infrastructure/HeapAdaptor/org/eclipse/tptp/martini/HeapProxy.class packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/src/Martini/Infrastructure/ThreadAdaptor/org/eclipse/tptp/martini/ThreadProxy.class packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/src/Martini/Infrastructure/HeapAdaptor/org/eclipse/tptp/martini/analysis/HeapObjectData.class packaging/plugins/org.eclipse.tptp.javaprofiler/org/eclipse/tptp/martini/analysis
	
ifndef EXCLUDE_PROBEKIT
	# Add probe's BCIEngProbe.so to javaprofiler's libs as libBCIEngProbe.so (bug 187527), it is not supported on IPF
	cp $(PROBEKIT_LIB)/BCIEngProbe.so packaging/plugins/org.eclipse.tptp.javaprofiler/libBCIEngProbe.so
endif

ifeq ($(UNAME), OS/390)
	cd packaging; \
	pax -w -f ../javaprofiler.$(BUILD_PLATFORM).pax -x ustar *; \
	cd ..
else
	cd packaging; \
	zip -r9y ../javaprofiler.$(BUILD_PLATFORM).zip *; \
	cd ..
endif

	# about.html and about_files are removed during packaging the jvmti-only zip as they are included in the JVMTI workbench plug-in at the root level of the plug-in see bug 334925
ifndef EXCLUDE_PROBEKIT
	cd packaging/plugins/org.eclipse.tptp.javaprofiler; \
	rm -f about.html; \
	rm -rf about_files; \
	zip -r9y ../../../jvmti-only.$(BUILD_PLATFORM).zip *; \
	cd ../../..
endif

	# Package the SDK
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/about_files
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/org
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Include
	mkdir -p packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Doc

	if [ -e epl-v10.html ]; then cp epl-v10.html packaging; \
	fi;
	if [ -e notice.html ]; then cp notice.html packaging; \
	fi;

	cp org.eclipse.tptp.platform.jvmti.runtime/about.html packaging/plugins/org.eclipse.tptp.javaprofiler.sdk
	cp -R org.eclipse.tptp.platform.jvmti.runtime/about_files/* packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/about_files
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/include/Martini/*.h packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Include/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/include/Martini/*Mnemonics packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Include/
	cp -R org.eclipse.tptp.platform.jvmti.runtime/src-native/doc/Martini/* packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Doc/
	rm -rf packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Doc/CVS
	rm -rf packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Doc/html/CVS
	rm -rf packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Doc/images/CVS
	cp -R packaging/plugins/org.eclipse.tptp.javaprofiler/org/* packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/org/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libJPIBootLoader.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libJPI.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libJIE.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libCGAdaptor.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libHeapAdaptor.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libThreadAdaptor.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/
	cp org.eclipse.tptp.platform.jvmti.runtime/src-native/bin/linux/release/${EXTENS}/libMartiniOSA.so packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib/

ifeq ($(UNAME), OS/390)
    # Find and package the .x files in the z/OS SDK.
	for i in `find org.eclipse.tptp.platform.jvmti.runtime/src-native/src -name *.x`; do cp $$i packaging/plugins/org.eclipse.tptp.javaprofiler.sdk/Lib; done

	if [ -e javaprofiler.$(BUILD_PLATFORM).sdk.pax ]; then rm -f javaprofiler.$(BUILD_PLATFORM).sdk.pax; \
	fi;
	cd packaging/plugins/org.eclipse.tptp.javaprofiler.sdk; \
	pax -w -f ../../../javaprofiler.$(BUILD_PLATFORM).sdk.pax -x ustar *; \
	cd ../../..
else
	if [ -e javaprofiler.$(BUILD_PLATFORM).sdk.zip ]; then rm -f javaprofiler.$(BUILD_PLATFORM).sdk.zip; \
	fi;
	cd packaging/plugins/org.eclipse.tptp.javaprofiler.sdk; \
	zip -r9y ../../../javaprofiler.$(BUILD_PLATFORM).sdk.zip *; \
	cd ../../..
endif

