#!/bin/sh
#
# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
#                         University Research and Technology
#                         Corporation.  All rights reserved.
# Copyright (c) 2004-2005 The University of Tennessee and The University
#                         of Tennessee Research Foundation.  All rights
#                         reserved.
# Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
#                         University of Stuttgart.  All rights reserved.
# Copyright (c) 2004-2005 The Regents of the University of California.
#                         All rights reserved.
# $COPYRIGHT$
# 
# Additional copyrights may follow
# 
# $HEADER$
#
# This is a simple makefile for windows which makes all the components
# which are required for super computing. Too lazy to open up visual
# studio each and every time to compile a component, so just adding
# this to the svn repository
#

topdir := $(shell cygpath -m $(CURDIR))/../..
gen_base = ${topdir}/src/win32/generated_include
copy_base = ${topdir}/src/mca/
prefix = ${topdir}/src/Debug
installdir = ${topdir}/src/Debug/lib

# list of components to build with the c compiler
C_SUBDIRS = \
        allocator/bucket \
        coll/basic \
        topo/unity

# list of components to build with the cpp compiler
# (because of the problem with OBJ_CLASS_INSTANCE)
CPP_SUBDIRS = \
        allocator/basic \
        ptl/self \
        ptl/tcp \
        oob/tcp \
        ns/replica/src \
        ns/proxy/src \
        gpr/proxy

#list of components that are not working as yet
CPP_EXCLUDE = pcm/wmi

CC = cl

INCL = \
        /I"${topdir}/src/win32/generated_include" \
        /I"${topdir}/src/win32" \
        /I"${topdir}/include" \
        /I"${topdir}/src" \
        /I"${topdir}/src/event/compat/" \
        /I"${topdir}/src/event" \
        /I"${topdir}/src/event/WIN32-Code/" 

CFLAGS = \
        /DWIN32 \
        /DHAVE_CONFIG_H \
        /DOMPI_SYSCONFDIR="\"${installdir}/share\"" \
        /DOMPI_BUILDING_WIN_DSO=1 \
        /EHsc \
        /ML \
        /W0 \
        /Wp64 \
        /TC \
        /D_MBCS \
        /LD\
        /nologo \
        /c

CPPFLAGS = \
        /DWIN32 \
        /TP \
        /DHAVE_CONFIG_H \
        /DOMPI_SYSCONFDIR="\"${installdir}/share\"" \
        /DOMPI_BUILDING_WIN_DSO=1 \
        /EHsc \
        /ML \
        /W0 \
        /Wp64 \
        /D_MBCS \
        /LD \
        /nologo \
        /c

# link with ompi.lib to resolve external symbols
#OMPILIB = \
#        "${topdir}/vcproj/ompi/Debug/libmpi.lib"

OMPILIB = \
        "${topdir}/src/libmpi.lib"

LINK = link

LINKFLAGS = \
        /DLL \
        /OPT:NOICF \
        /OPT:NOREF \
        /DEFAULTLIB:${OMPILIB} \
        /nologo

ADDLIBS = \
        ws2_32.lib \
        kernel32.lib \
        user32.lib \
        gdi32.lib \
        winspool.lib \
        comdlg32.lib \
        advapi32.lib \
        shell32.lib \
        ole32.lib \
        oleaut32.lib \
        uuid.lib \
        odbc32.lib \
        odbccp32.lib

all: \
    clibs \
    cpplibs \
    special

prebuild:
	@cp '${gen_base}/coll-basic-version.h' '${copy_base}/coll/basic/'
	@cp '${gen_base}/io-romio-version.h' '${copy_base}/io/romio/'
	@cp '${gen_base}/topo-unity-version.h' '${copy_base}/topo/unity/'

clibs: ${C_SUBDIRS} prebuild
	@for dirs in ${C_SUBDIRS}; do \
	    (libname=`echo mca_$${dirs}.dll | sed 's/\/src[\/]*//g' | sed 's/\//_/g' | sed 's/_\./\./g'` ; \
			cd $$dirs; \
			echo ${CC} ${CPPFLAGS} ${INCL} *.c; \
			${CC} ${CFLAGS} ${INCL} *.c; \
			echo ${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:$${libname} *.obj; \
			${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:$${libname} *.obj); \
	done

cpplibs: ${CPP_SUBDIRS} prebuild
	@for dirs in ${CPP_SUBDIRS}; do \
	    (libname=`echo mca_$${dirs}.dll | sed 's/\/src[\/]*//g' | sed 's/\//_/g' | sed 's/_\./\./g'` ; \
			cd $$dirs; \
			echo ${CC} ${CPPFLAGS} ${INCL} *.c; \
			${CC} ${CPPFLAGS} ${INCL} *.c; \
			echo ${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:$${libname} *.obj; \
			${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:$${libname} *.obj); \
	done

special: prebuild
	(cd gpr/replica/api_layer;        echo ${CC} ${CPPFLAGS} ${INCL} *.c; ${CC} ${CPPFLAGS} ${INCL} *.c);
	(cd gpr/replica/communications;   echo ${CC} ${CPPFLAGS} ${INCL} *.c; ${CC} ${CPPFLAGS} ${INCL} *.c);
	(cd gpr/replica/functional_layer; echo ${CC} ${CPPFLAGS} ${INCL} *.c; ${CC} ${CPPFLAGS} ${INCL} *.c);
	(cd gpr/replica/transition_layer; echo ${CC} ${CPPFLAGS} ${INCL} *.c; ${CC} ${CPPFLAGS} ${INCL} *.c);
	(cd gpr/replica;                  echo ${CC} ${CPPFLAGS} ${INCL} *.c; ${CC} ${CPPFLAGS} ${INCL} *.c; \
			echo ${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:$${libname} *.obj; ${LINK} ${LINKFLAGS} ${ADDLIBS} /OUT:mca_gpr_replica *.obj transition_layer/*.obj functional_layer/*.obj communications/*.obj api_layer/*.obj);

install: win_makefile
	@echo -n "Installing components ......................."
	@for dirs in ${CPP_SUBDIRS}; do \
	    (dll=`echo mca_$${dirs}.dll | sed 's/\/src//g' | sed 's/\//_/g'`; exp=`echo $${dll} | sed 's/dll/exp/g`;  lib=`echo $${dll} | sed 's/dll/lib/g`; \
        install -d ${CURDIR}/$${dirs}/ ${installdir}; \
        install -p ${CURDIR}/$${dirs}/$${dll} ${installdir}/$${dll}; \
        if test -f ${CURDIR}/$${dirs}/$${lib}; then install -p ${CURDIR}/$${dirs}/$${lib} ${installdir}/$${lib}; fi; \
        if test -f ${CURDIR}/$${dirs}/$${exp}; then install -p ${CURDIR}/$${dirs}/$${exp} ${installdir}/$${exp}; fi;); \
	done
	@for dirs in ${C_SUBDIRS}; do \
	    (dll=`echo mca_$${dirs}.dll | sed 's/\/src//g' | sed 's/\//_/g'`; exp=`echo $${dll} | sed 's/dll/exp/g`;  lib=`echo $${dll} | sed 's/dll/lib/g`; \
        install -d ${CURDIR}/$${dirs}/ ${installdir}; \
        install -p ${CURDIR}/$${dirs}/$${dll} ${installdir}/$${dll}; \
        if test -f ${CURDIR}/$${dirs}/$${lib}; then install -p ${CURDIR}/$${dirs}/$${lib} ${installdir}/$${lib}; fi; \
        if test -f ${CURDIR}/$${dirs}/$${exp}; then install -p ${CURDIR}/$${dirs}/$${exp} ${installdir}/$${exp}; fi;); \
	done
	@echo "done"

.PHONY: clean

clean:
	@for dirs in ${C_SUBDIRS}; do \
	    (echo "Entering $$dirs"; cd $$dirs; rm -rf *.lib *.obj *.exp;); \
	done
	@for dirs in ${CPP_SUBDIRS}; do \
	    (echo "Entering $$dirs"; cd $$dirs; rm -rf *.lib *.obj *.exp;); \
	done