#
# Makefile for Wlib, the W library
#
# ++bsittler 2000-07-04:
# - now Wlib.h is automatically generated so _WMAJ, _WMIN and _WPL are correct
# ++eero 2009-08.30:
# - use proper library versioning for shared library

include ../.config

#
# targets, depending on the machine
#

CFLAGS = $(BASECFLAGS) $(CLIENTCFLAGS) -DTRACE

#
# some definitions
#

SRCS = $(wildcard *.c)
OBJS = $(patsubst %.c, %.o, $(SRCS))
SOBJS = $(patsubst %.c, %.O, $(SRCS))
BOBJS = $(patsubst %.c, %.ob, $(SRCS))
GOBJS = $(patsubst %.c, %.og, $(SRCS))

#
# rules
#

all: $(WLIB)

# Wlib.h header, built from Wlib_in.h
Wlib.h: Wlib_in.h ../Makefile
	@echo Generating $@ from $<
	@echo "/*" > $@
	@echo " * THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT IT!!!" >> $@
	@echo " * Instead, edit" $< "and make" $@ "to re-generate." >> $@
	@echo " */" >> $@
	@echo "" >> $@
	@echo "#define _WMAJ " $(WMAJ) >> $@
	@echo "#define _WMIN " $(WMIN) >> $@
	@echo "#define _WPL  " $(WPL)  >> $@
	@echo "" >> $@
	@cat $< >> $@

# versioned Linux shared library
libW.so.$(WMAJ).$(WMIN): Wlib.h $(SOBJS)
	$(CC) -shared -Wl,-soname,$@ $(SOBJS) -o $@

libW.so: libW.so.$(WMAJ).$(WMIN)
	$(LN) -sf $< $@

libW.a: Wlib.h $(OBJS)
	$(RM) $@
	$(AR) $@ $(OBJS)
ifdef RANLIB
	$(RANLIB) $@
endif

libWg.a: Wlib.h $(GOBJS)
	$(RM) $@
	$(AR) $@ $(GOBJS)
ifdef RANLIB
	$(RANLIB) $@
endif

blibW.a: Wlib.h $(BOBJS)
	$(RM) $@
	$(AR) $@ $(BOBJS)
ifdef RANLIB
	$(RANLIB) $@
endif

# some MiNT linkers use this name format
bW.olb: Wlib.h $(BOBJS)
	$(RM) $@
	$(AR) $@ $(BOBJS)
ifdef RANLIB
	$(RANLIB) $@
endif


.SUFFIXES: .O .ob .og

.c.O: Wlib.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -fpic -c $< -o $@

.c.ob: Wlib.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

.c.og: Wlib.h
	$(CC) $(CPPFLAGS) $(CFLAGS) -g -c $< -o $@

clean:
	$(RM) *.o *.O *.ob *.og $(JUNK)

veryclean: clean
	$(RM) *.a *.so *.a.* *.so.* *.olb Wlib.h

install: Wlib.h $(WLIB)
ifdef INCDIR
	$(INSTALLDIRS) $(INCDIR)
	$(INSTALL) -m 644 Wlib.h $(INCDIR)
endif
ifdef LIBDIR
	$(INSTALLDIRS) $(LIBDIR)
ifeq ($(WLIB),libW.so)
	# shared libraries are versioned
	$(INSTALL) -m 644 $(WLIB).$(WMAJ).$(WMIN) $(LIBDIR)
	cd $(LIBDIR); ln -s $(WLIB).$(WMAJ).$(WMIN) $(WLIB)
else
	$(INSTALL) -m 644 $(WLIB) $(LIBDIR)
endif
endif
