#  Copyright (C) 1992, the Florida State University
#  Distributed by the Florida State University under the terms of the
#  GNU Library General Public License.
#
#This file is part of Pthreads.
#
#Pthreads is free software; you can redistribute it and/or
#modify it under the terms of the GNU Library General Public
#License as published by the Free Software Foundation (version 2).
#
#Pthreads is distributed "AS IS" in the hope that it will be
#useful, but WITHOUT ANY WARRANTY; without even the implied
#warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#See the GNU Library General Public License for more details.
#
#You should have received a copy of the GNU Library General Public
#License along with Pthreads; see the file COPYING.  If not, write
#to the Free Software Foundation, 675 Mass Ave, Cambridge,
#MA 02139, USA.
#
#Report problems and direct all questions to:
#
#  pthreads-bugs@ada.cs.fsu.edu
#
#   @(#)Makefile	2.1 3/16/94
#

#Compile flags:
#C_INTERFACE:		for C-language interface; if this option is omitted,
#			a language-independent interface will be generated
#			which has been tested for Verdix/SunAda.
#C_CONTEXT_SWITCH:	for context switch written in C. Notice that the
#			assembly version for the SPARC is faster but by far
#			less portable (see portability notes in pthread_disp.c
#			and pthread_sched.S).
#			Internal signal handling (pthread_kill) is also much
#			slower with the context switch in C, about a factor 2.
#			speed(C context switch) =~ 1.2*speed(as context switch)
#CLEANUP_HEAP:		allocate cleanup handlers on heap (default: stack).
#DEBUG:			dump out trace information (mainly for signals).
#DEF_RR:		supports round-robin time-sliced scheduling.
#			Only works properly when MALLOC is also defined.
#DRAFT5:		defines pthread_setprio() interface bypassing attributes
#IO:			I/O operations only block current thread, not process.
#			(SunOS 4.1.x kernel configuration must include
#			light-weight processes [LWP] and aynchronous I/O [AIO];
#			under Solaris 2.x, link with -laio -ldl.)
#MALLOC:		thread-safe malloc, in conjuction with Gnu malloc lib.
#			See README for more information.
#NOERR_CHECK:		fast mutex operations without error checking.
#NO_INLINE:		no inlines for macros (internally CLEAR_KERNEL_FLAG).
#MUT_SWITCH:		forces context switch upon successful mutex_lock()
#			with regard to priorities (do not define NOERR_CHECK)
#RAND_SWITCH:		forces context switch on kernel exit at random intervals
#			and to random threads without regard to priorities
#			(do not define NOERR_CHECK)
#RR_SWITCH:		forces context switch on kernel exit (round-robin queue)
#			without regard to priorities (do not define NOERR_CHECK)
#SIGNAL_STACK:		Allows user to execute handlers in case of a stack
#			overflow. STACK_CHECK has to be on for this.
#SIM_KERNEL:		simulate kernel implementation by using dummy UNIX
#			system call in selected routines.
#SOLARIS:		support for Solaris 2.x. If you want to compile for
#			Solaris, change this Makefile by deleting/inserting
#			comments wherever the string "Solaris" occurs.
#SRP:			support of mutex ceiling under stack resource
#			policy (SRP)
#			if not defined, _POSIX_THREADS_PRIO_PROTECT has to be
#			undefined in unistd.h!
#STACK_CHECK:		lock page to cause bus error / illegal instruction
#			on stack overflow
#STAND_ALONE:		micro-kernel for VME SPARC Engine 1E; works only in
#			conjunction with SRP and no other compile options
#TIMER_DEBUG:		dump out trace information for SIGALRM signals and timer

#include directory location
INCS = ../include
PINC = $(INCS)/pthread
INC1 = -I$(INCS)
INC2 = 

#for cross-compilation
#INC2 = -I/midasusr/include

#install directory locations
INSTALL_INCLUDE = /usr/include
INSTALL_LIBS = /usr/lib

#for shipping
#CFLAGS = -DSRP -DNOERR_CHECK -DC_INTERFACE -DSTACK_CHECK -DSIGNAL_STACK
#for maximum portability (minimum assembly required)
#CFLAGS = -DSRP -DCLEANUP_HEAP -DC_INTERFACE -DSTACK_CHECK -DSIGNAL_STACK \
	-DC_CONTEXT_SWITCH
#for thread-safe malloc and round-robin scheduling option
#CFLAGS = -DSRP -DNOERR_CHECK -DC_INTERFACE -DSTACK_CHECK -DSIGNAL_STACK \
	-DMALLOC -DDEF_RR
#for PART project
CFLAGS = -DSRP -DNOERR_CHECK -DSTACK_CHECK -DSIGNAL_STACK -DDRAFT5
#for VME SPARC Engine 1E running SunOS 4.0.3e
#CFLAGS = -DSRP -DNOERR_CHECK -DC_INTERFACE -DSTAND_ALONE
#for testing
#CFLAGS = -DDEBUG -DTIMER_DEBUG -DSRP -DC_INTERFACE -DSTACK_CHECK -DSIGNAL_STACK

#for non-process blocking (only thread-blocking) I/O:
#CFLAGS += -DIO

#for shipping
CCFLAGS = -O3 $(CFLAGS)
CCFLAGS2 = -O2 $(CFLAGS)
#for testing
#CCFLAGS = -g $(CFLAGS)
#CCFLAGS2 = $(CCFLAGS)

#compile flags, always the same
CCFLAGS0 = $(CCFLAGS)

#for Gnu C compiler
#Notice the special compile flags CCFLAGS0 which circumvents a compile bug
#of gcc 2.5.2 when compiling io.c!
#For gcc 2.5.8 or later, drop the "-traditional" from COMP_ASM.
CC = gcc
CCFLAGS0 = $(CFLAGS)
COMP_ASM = $(CC) $(INC1) $(INC2) $(CFLAGS) -c $*.S
#for Sun C
#COMP_ASM = $(AS) $(INC1) $(INC2) -P $(CFLAGS) $*.S -o $*.o
#for Sun C under Solaris 2.x
#delete comment designator "#" in the line below if you use /usr/lang/cc
#CC = /usr/lang/cc

#for Solaris 2.x
#delete comment designator "#" in the line below for Solaris 2.x/SunOS 5.x
#CFLAGS += -DSOLARIS

COMP_C   = $(CC) $(INC1) $(INC2) $(CCFLAGS) -c $*.c
COMP_C0  = $(CC) $(INC1) $(INC2) $(CCFLAGS0) -c $*.c
COMP_C2  = $(CC) $(INC1) $(INC2) $(CCFLAGS2) -c $*.c

AR     = ar
RANLIB = /usr/bin/ranlib

LINTFLAGS = -u $(CFLAGS)

LIBS = ../lib/libpthreads.a

CSRC = pthread.c stack.c mutex.c cond.c pthread_init.c signal.c \
	cond.c queue.c io.c pthread_disp.c

SCRS = $(CSRC) pthread_sched.S

OBJS = pthread.o stack.o mutex.o cond.o pthread_init.o \
	signal.o pthread_sched.o queue.o io.o pthread_disp.o

HDR1 = pthread_internals.h $(INCS)/pthread.h $(PINC)/unistd.h $(PINC)/limits.h \
	$(PINC)/errno.h $(PINC)/signal.h $(PINC)/pthread_asm.h $(PINC)/setjmp.h

DEP1 = $(HDR1) Makefile

HDRS = $(HDR1) pthread_offsets.h mutex.h signal_internals.h

all: $(LIBS)

$(LIBS):: $(OBJS)
	if (test -x $(RANLIB)) ; \
        then \
	  $(AR) ru $(LIBS) *.o ; \
          $(RANLIB) $(LIBS) ; \
        else \
	  $(AR) ru $(LIBS) `lorder *.o | tsort` ; \
        fi

check_ranlib: $(IS_RANLIB)

cond.o: $(DEP1) mutex.h
	$(COMP_C)

mutex.o: $(DEP1) mutex.h
	$(COMP_C)

pthread.o: $(DEP1)
	$(COMP_C)

pthread_init.o: $(DEP1)
	$(COMP_C)

signal.o: $(DEP1) mutex.h signal_internals.h pthread_offsets.h
	$(COMP_C)

stack.o: $(DEP1)
	$(COMP_C)

queue.o: $(DEP1)
	$(COMP_C)

io.o: $(DEP1)
	$(COMP_C0)

pthread_disp.o: $(DEP1)
	$(COMP_C2)

pthread_sched.o: pthread_sched.S $(PINC)/pthread_asm.h \
		pthread_offsets.h Makefile
	$(COMP_ASM)

pthread_offsets.h: get_offsets
	./get_offsets > pthread_offsets.h

get_offsets: get_offsets.c $(DEP1) $(PINC)/setjmp.h
	$(CC) $(INC1) $(INC2) $(CCFLAGS) -o $@ $@.c

install:
	cp -R $(INCS) $(INSTALL_INCLUDE)
	cp $(LIBS) $(INSTALL_LIBS)

lint:
	lint $(LINTFLAGS) $(CSRC)

llib:
	lint $(LINTFLAGS) -Cthreads $(SRCS)

clean:
	-rm -f $(LIBS) a.out core errs *.o *.BAK *.CKP *~ #*
