# Makefile for UNIX with compiler optimization
#
# CAUTION:  Make changes to Makefile (not makefile), then run
# "make -f Makefile depend" before running "make all".
#
CC = cc
CCOPT = -O
LIBOPT = ${CCOPT}
#
#***********************************************************************
# SHARED MEMORY
# Shared memory is only an option on systems that have incorporated the UNIX
# System V interprocess communication facilities.  The SYSV_IPC_AVAIL macro
# must be defined in ncbilcl.h for a platform.  Management of database files
# resident in shared memory is accomplished via the 'memfile' program.
#
# Note:  Your UNIX kernel may need to be reconfigured to permit allocation of
# sufficiently large shared memory segments.  For example, the default SunOS
# 4.0 and 4.1 limit on shared memory segment size is 1 MB.  The limit should
# be configured to be at least as large as the largest single file that one
# will need to place in shared memory.
#***********************************************************************
# MEMORY MAPPED FILES
# On many UNIX platforms, memory mapped files are employed when a shared
# memory segment either does not exist for a file or if System V IPC was not
# a compiled-in feature (see above).
#***********************************************************************
# MULTIPROCESSING / MULTITHREADING
# Under SGI IRIX, multiprocessing can be employed by defining MPROC_AVAIL
# at the location indicated in the SGI-specific header, ncbilcl.sgi, which
# gets renamed to ncbilcl.h when building the NCBI Toolbox.  The gish library
# must be previously compiled with the definition in effect.
#
# Similarly for Solaris 2.3+, MPROC_AVAIL must be specified in ncbilcl.sol
# to use Solaris threads.  When SunSoft's C compiler is used, all modules,
# from the NCBI Toolbox on up through the blastapp software, should be
# compiled and linked with the -mt option.  The -mt option can simply
# be added to the various Makefiles.
#
# For DCE threads (e.g., DEC OSF/1 or DEC Unix threads), MPROC_AVAIL must
# be defined in ncbilcl.alf.  When DEC's C compiler is used, all modules
# from the NCBI Toolbox on up through the blastapp software should be
# compiled and linked with the -threads option.  The -threads option can
# simply be added to the various Makefiles.  When linking, use
# MPLIB=-lpthreads
#
# Each of the above platforms requires that applications be linked with
# specialized libraries when multiprocessing/multithreading is to be used.
#
# Operating System      MPLIB make variable
# ================      ======
# SGI IRIX 4.+          -lmpc
# SGI IRIX 5.+          <not needed>
# Solaris               -lthread
# DEC OSF/1             -lpthreads
# 
#***********************************************************************
#
# Must include -lncbi library specification if BLASTASN is defined
BLASTASN = -DBLASTASN
OPTIONS = ${BLASTASN}
CFLAGS = ${CCOPT} $(INCDIRS) $(LIBDIRS) ${OPTIONS}
MKDEPFLAG = -M
# Unbundled Sun Compiler is different about this
#MKDEPFLAG = -xM

# These links contribute to a general method for inclusion
# of header files by the lib/*.c files.
NCBI = ../ncbi
GISH = ../gish
BLAST = ../blast
LINKS = ncbi gish blast

# Set -I flag(s) to point to directories containing gish.h and ncbi.h
INCDIRS = -Iinclude -Iblast -Igish/include -Incbi/include

# Set -L flag(s) to point to directories containing libncbi.a and libgish.a
LIBDIRS = -Llib -Lblast -Lgish/lib -Lncbi/lib

APPLIB = lib/libapp.a
#MPLIB = -lmpc
LIBS = -lapp -lblast -lgish -lncbi ${MPLIB} -lm

PGMS = blastp blastn tblastn blastx tblastx setdb pressdb memfile \
		gb2fasta gt2fasta sp2fasta pir2fasta pam blast3
HDRS = blastapp.h alphabet.h aabet.h ntbet.h karlin.h gcode.h shmutil.h
SRCS = blastn.c blastp.c tblastn.c blastx.c tblastx.c \
		setdb.c pressdb.c pam.c gb2fasta.c gt2fasta.c pir2fasta.c sp2fasta.c
OBJS = blastn.o blastp.o tblastn.o blastx.o tblastx.o \
		setdb.o pressdb.o pam.o gb2fasta.o gt2fasta.o pir2fasta.o sp2fasta.o

all: always $(PGMS)

ncbi:
	ln -s $(NCBI) $@
gish:
	ln -s $(GISH) $@
blast:
	ln -s $(BLAST) $@

bintar: $(PGMS)
	tar cf - $(PGMS) | compress > blast1_4.tar.Z

blast3:
	@ echo NOTE: the BLAST3 program is not included with the version 1.4 software.
	@ echo BLAST3 must be obtained from an archive of the version 1.3 programs.

blastp: blastp.c $(APPLIB)
	$(CC) $(CFLAGS) -DBLASTP -o $@ $@.c $(LIBS)

blastn: blastn.c $(APPLIB)
	$(CC) $(CFLAGS) -DBLASTN -o $@ $@.c $(LIBS)

tblastn: tblastn.c $(APPLIB)
	$(CC) $(CFLAGS) -DTBLASTN -o $@ $@.c $(LIBS)

blastx: blastx.c $(APPLIB)
	$(CC) $(CFLAGS) -DBLASTX -o $@ $@.c $(LIBS)

tblastx: tblastx.c $(APPLIB)
	$(CC) $(CFLAGS) -DTBLASTX -o $@ $@.c $(LIBS)

# memfile is only useful on systems that support System V UNIX shared memory
memfile: memfile.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# database preprocessor for blastp, blastx, and blast3
setdb:	setdb.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# database preprocessor for blastn and tblastx
pressdb: pressdb.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# PAM matrix generator
pam: pam.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ pam.c $(LIBS)

# Convert GenBank(R) format to FASTA format (faster than the AWK script)
gb2fasta: gb2fasta.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# Convert GenBank(R) CDS features into FASTA format
gt2fasta: gt2fasta.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# Convert NBRF PIR format to FASTA format (faster than the AWK script)
pir2fasta: pir2fasta.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

# Convert SWISS-PROT format to FASTA format
sp2fasta: sp2fasta.c $(APPLIB)
	$(CC) $(CFLAGS) -o $@ $@.c $(LIBS)

manual: blast1.ps

# Create PostScript version of the manual page
blast1.ps: blast1.1
	psroff -man -t blast1.1 > blast1.ps


$(APPLIB): always
always: $(LINKS)
	cd lib; $(MAKE) $(MFLAGS) CC="$(CC)" CCOPT="${LIBOPT}" OPTIONS="${OPTIONS}"

clean:
	@ cd lib; make clean
	@- rm -f $(LINKS)
	@- rm -f makefile makefile.old
	@- rm -f $(PGMS)
	@- rm -f $(OBJS)
	@- rm -f *.u

depend: lib/Makefile Makefile include $(LINKS) $(SRCS)
	cd lib; $(MAKE) $(MFLAGS) -f Makefile depend CC="$(CC)" CCOPT="${LIBOPT}" OPTIONS="${OPTIONS}" MKDEPFLAG="${MKDEPFLAG}"
	- mv -f makefile makefile.old
	@ sed -n -e '1,/[D]O NOT MAKE ANY CHANGES/p' Makefile > makefile
	@ echo "" >> makefile
	@ $(CC) $(INCDIRS) ${OPTIONS} ${MKDEPFLAG} $(SRCS) >> makefile

#DO NOT MAKE ANY CHANGES ON OR BELOW THIS LINE
