# ----------------------------------------------------------------------------- #
# Copyright (C) 2001 Berlin h-peter recktenwald <phpr@snafu.de>, QT licensed	#
# Makefile for lib4th - lib4th will NOT assemble with original nasm!		#
# ----------------------------------------------------------------------------- #
#	tried hard to do the source nasm-0.98 compatibly - and failed.
#	tried the 'official' development nasm-0.98.08 - failed, badly!
#	nasm orig and(!) devel is just one "bug", unuseable. gave it up...
# get nasm-0.98e: ftp://ftp.linuxgames.com/crystal/nasm/ (deleted?)
# or, patches 98->98-3e: http://home.snafu.de/phpr/nasm-0.98-to-3e.diff.tar.bz2
# or, debianized package http://home.snafu.de/phpr/nasm-0.98-3.1_i386.deb
# or, search my website:	http://home.snafu.de/phpr/index-lx.shtml
# or, as a last resort:		mailme:phpr@snafu.de
# ----------------------------------------------------------------------------- #
# 	compiled "lib4th" does not depend on GNU-copyrighted software		#
# ----------------------------------------------------------------------------- #
# 27 -> 28

.EXPORT_ALL_VARIABLES:
 
TOPDIR	= $(shell pwd)

include MConfig

# don't strip if debugging capability requested
ifneq ("$(D)","")
STRIP_ANY =
#STRIP_LIB =
STRIP_P4S =
endif

AU	= $(shell ls \-ld ${AUDIR}|cut \-d\> \-f2|cut \-d\- \-f2)
AV	= $(shell echo ${AU}|cut -d. -f1)
AX	= $(shell echo ${AU}|cut -d. -f2)

# extra gcc compileflags (re below)
CFLAGS	+= $(D)

# suppressing "libc" dependency:
#	"libc.a" should be found *before* "libc.so" because <ld> will always,
#	- unchangeably - be searching for a "...so" sort of library, first!
LDFLAGS += -L$(TOPDIR)/inc -lc
#ok:
#LDFLAGS += -L/usr/local/i586-pc-linux-gnu/lib -lc
#those work but do not resolve w/o libc dependency:
#	LDFLAGS = -\( /usr/local/i586-pc-linux-gnu/lib/libc.a -\)
#	LDFLAGS = -\( /usr/src/asm/asmutils/lib/libc.a -\)

# ----------------------------------------------------------------------------- #
# path+names of auxilary programs:
# ----------------------------------------------------------------------------- #

VPATH =./src:./inc:./inc/asmutils:./inc/asmutils/au:./

# supposed to finding whether install &c, utils should & can be executed
# per $(SD), by the particular user who compiles the lib4th programs...
# user-mode for install, ldconfig, ln, mkdir, <sudo> enabled if in sudoers list
#
SU = $(shell id -un|grep "root">/dev/null||{ $(SD) chmod u+r ./inc/version.inc >/dev/null 2>&1&&which $(SD); };)

# dummy to de-activating a utility prog.
TR = true

# ----------------------------------------------------------------------------- #
# <strip>ping was somewhat peculiar, mainly due to the signal handlers		#
# which occasionally were rendered non-functional, after minor(!) changes	#
# <sstrip> could also be applied to lib4th, finally, after all linking done	#
# and, isn't much of use for static programs: "4" saves just 150 bytes.		#
# ----------------------------------------------------------------------------- #

ifneq ("$(D)","")
ifneq ("$(STRIP_PRG)","")
NOSSTRIP   =
endif
endif

ifeq ("$(STRIP_ANY)$(STRIP_LIB)$(STRIP_PRG)","")
NOSTRIP	   = y
endif

# f8 and calc4th (and 4), only: strip everything which is not required.
# <install> binaries without -s option, otherwise "unknown file format" error.
ifneq ("$(NOSSTRIP)","")
SS = $(STRIP_PROG)
else
SS = $(SSTRIP_PROG)
endif
# the standard utility, applied to <4> and dyn lib:
ST = /usr/bin/strip

STRIPFLAGS   = --discard-locals
# strip nasm/gnu-ish verbosity...
STRIPSFLAGS += --remove-section=.comment
STRIPSFLAGS += --remove-section=.note
ifneq ("$(STRIP_ANY)","")
# strip lib4th & progs, common to all
#STRIPFLAGS  += --strip-debug
#STRIPFLAGS  += --discard-locals
endif

STRIPDFLAGS  = $(STRIPFLAGS)
STRIPSFLAGS  = --discard-locals
ifneq ("$(STRIP_LIB)","")
# STRIPD : dynamic lib
STRIPFLAGS  += --strip-debug
#STRIPSFLAGS += --discard-locals
STRIPDFLAGS += --remove-section=.sbss
STRIPDFLAGS += --remove-section=.bss
#v0.0.59+ (re <binutils/debian/rules>
#	-?- -?- -?-
STRIPDFLAGS += --strip-unneeded
ifneq ("$(STRIP_P4S)","")
STRIPSFLAGS  = $(STRIPFLAGS)
# STRIPS : static lib
STRIPFLAGS  += --strip-debug
#STRIPSFLAGS += --discard-locals
STRIPSFLAGS += --remove-section=.sbss
STRIPSFLAGS += --remove-section=.bss
endif
endif

STRIPPFLAGS  = $(STRIPFLAGS)
STRIP4FLAGS  = --discard-locals
ifneq ("$(STRIP_PRG)","")
# STRIPP : dyn. linked programs
STRIPPFLAGS  = $(STRIPFLAGS)
STRIPFLAGS  += --strip-debug
STRIPPFLAGS += --remove-section=.sbss
STRIPPFLAGS += --strip-unneeded
STRIP4FLAGS  = $(STRIPFLAGS)
ifneq ("$(STRIP_P4S)","")
# STRIP4 : static program, after(!) being linked
STRIP4FLAGS  = $(STRIPFLAGS)
STRIPFLAGS  += --strip-debug
STRIP4FLAGS += --strip-unneeded
STRIP4FLAGS += --remove-section=.got
STRIP4FLAGS += --remove-section=.sbss
#STRIP4FLAGS += --remove-section=.bss
endif
endif

# disable all - for <debpak>:
ifneq ("$(NOSTRIP)","")
STRIPFLAGS  = --discard-locals
STRIPDFLAGS = --discard-locals
STRIPSFLAGS = --discard-locals
STRIPPFLAGS = --discard-locals
STRIP4FLAGS = --discard-locals
endif

# ----------------------------------------------------------------------------- #
#					-/-					#
# ----------------------------------------------------------------------------- #

# asm
# ----------------------------------------------------------------------------- #
#	clear NASM env. vari. to preventing ambiguous args
NASM   =
#	nasm sub-version and extn
NASMV  = $(shell $(ASM) -r|cut -d. -f2)$(shell $(ASM) -r|cut -d. -f3|cut -d\  -f1)

#	mandatory!
ASFLAGS= -w+orphan-labels $(D) 
ASMODE = elf
ASINC +=
#	for those 'optimizing' nasm devel. versions
#	asm takes very long (30' and more! w/ K6-2, 350mcs) and, 
#		might -accidentally- result to something useable.
ASOPT  = -O99

LIBINC = -i$(TOPDIR)/inc/ -i$(TOPDIR)/inc/asmutils/ -i$(TOPDIR)/inc/dbg/ -i$(TOPDIR)/src/

#	nasm messages logfile
ifeq ("$(V)","")
ASLOG  = -s 2>&1|tee $(LISDIR)$(*:.asm=).log
else
ASLOG  = -s -E $(LISDIR)$(*:.asm=).log
#ASLOG  = > $(LISDIR)$(*:.asm=).log 2>&1
endif
#	nasm 'listing'
ifeq ($(NASMV),9820)
SLIST  = -L$(LISDIR)$*.lis
LIST   = -L$(LISDIR)$*.lis
else
SLIST  = -l$(LISDIR)$*.lis
LIST   = -l$(LISDIR)$*.lis
endif
ifeq ($(NASMV),9821)
SLIST  = -L$(LISDIR)$*.lis
LIST   = -L$(LISDIR)$*.lis
endif
ifeq ($(NASMV),9808)
ASFLAGS += $(ASOPT)
LIST   = -l$(LISDIR)$*.lis
endif
#	useless stuff, macroes 'listing'; disabling saves about 4MB of nonsense 
#	text output! replace nasm-".nolist" by "_nolist" to enabling below defn
MLIST  = -d_nolist=".nolist" 
#	lib4th.mac
MLIST += -d_nolist1=".nolist"
#	aligned code entry labels
MLIST += -d_nolist2=".nolist"
#	selected code, lib4th.mac
MLIST += -d_nolist3=".nolist"
#	lib4th
MLIST += -d_nolist4=".nolist" 
#	locals
MLIST += -d_nolist5=".nolist"
#	(other)
MLIST += -d_nolist6=".nolist"
#	uvari setup
MLIST += -d_nolist7=".nolist" 
#LIST  =
#
LIST  += $(MLIST)
SLIST += $(MLIST)

##	affix for asm temporary working file
##	(such that orig. asm can be editted while nasm running)
##ASTMP	= .t
## abandoned since source split into many (relatively) small files...

#	KBCTRL if defined leaves keyboard events active - unused
#KBCTRL	= -d__KBCTRL

#	asmutils portability switch
KERNEL	= $(shell uname -r|cut -d. -f1)$(shell uname -r|cut -d. -f2)

#	the odd one out...
#	mandrake 7.1. linux deserves special handling (wrt signals/exit)
MDK	= $(shell uname -r|grep "mdk">/dev/null&&echo "-d__MDK")

# flags
# ----------------------------------------------------------------------------- #

#	dependencies
INCDEP = $(INCSYS) dep/mupdt.dep inc/asmutils/au

#	install
#		library and textfiles
#INSTLFLAGS = $(LMFLAGS) -g$(G) -o$(U) 
INSTLFLAGS = -g$(G) -o$(U) 
#		executeable programs, not/stripped
#INSTDFLAGS = $(PMFLAGS) -g$(G) -o$(U) 
INSTDFLAGS = -g$(G) -o$(U) 
INSTSFLAGS = $(INSTDFLAGS)

# files
# ----------------------------------------------------------------------------- #
#	for presence of programs check
UTILS = $(ASM) $(CM) $(CP) $(FD) $(IN) $(LC) $(LD) $(LN) $(MV) $(RD) $(RM) $(ST) $(SS) 

SUBDIRS = src inc dep doc util tests admin 4th 4th/app

#	basename of libraryfiles, inst. to $(LIBDIR)
SOFILE = lib4th
SSFILE = lib4th-static
#	test programs, inst. to $(PRGDIR)
TEST   = f8 calc4th f8ed
#TEST  = 
#	hi-level source- and data-files, inst. to $(LIBF8DIR)
F8FILES = $(TOPDIR)/4th/*.f8 $(TOPDIR)/doc/lib4cons.f8
F8APPS = $(shell $(FD) $(TOPDIR)/4th/app/ ! -type d -print|grep -v CVS)
#
LIB4   = $(SOFILE)
PROG   = $(LIB4)/f8
PROG   = l4
#	dyn prgs
DPRGS  = f8 calc4th f8ed
#	static
SPRGS  = 4
#	doc
DOCS   = $(TOPDIR)/doc/QT-license.html $(TOPDIR)/doc/QT-lizenz.html $(TOPDIR)/doc/GPL
#HTML   = $(TOPDIR)/doc/l4\*.html
HTML   = l4\*.html
GLOS   = $(TOPDIR)/doc/glossary $(TOPDIR)/doc/glossary.idx

# debian package
DEBNAME= $(SOFILE)_$(shell $(TOPDIR)/relse.sh deb)_i386.deb

# ----------------------------------------------------------------------------- #
# nothing expected to require any modifications, below this line...		#
# ----------------------------------------------------------------------------- #
# despite, additional programs should be appended near <eof>.			#
# ----------------------------------------------------------------------------- #

# generated table of common syscalls "a4" Target OS
OSF	= os_linux.inc os_freebsd.inc os_netbsd.inc os_openbsd.inc os_beos.inc os_atheos.inc
A4SI	=
ifeq ($(OS),LINUX)
A4SI	= os_linux.inc
else
ifeq ($(OS),FREEBSD)
A4SI	= os_freebsd.inc
else
ifeq ($(OS),NETBSD)
A4SI	= os_netbsd.inc
else
ifeq ($(OS),OPENBSD)
A4SI	= os_openbsd.inc
else
ifeq ($(OS),BEOS)
A4SI	= os_beos.inc
else
ifeq ($(OS),ATHEOS)
A4SI	= os_atheos.inc
else
A4SI	= $(shell echo "$(OS):$(KERNEL) - asmutils' OS type undfined" 2>&1|tee /dev/tty)
A4SI	= os_linux.inc
endif
endif
endif
endif
endif
endif

# ----------------------------------------------------------------------------- #

# cpu specific
ifeq ("$(K6)","")
K6     =
else
K6     = -D__K6__
endif

# asmutils' part of optimization, "SOPTIM" for the static lib & prog
OPTIM  = -d__OPTIMIZE__=__O_$(OPT)__
SOPTIM = -d__OPTIMIZE__=__O_$(SOPT)__

# ----------------------------------------------------------------------------- #

# syscall & fcntl generated files, by <util/unistd-na>
SCFILES = scdefs.inc scfctl.inc scdasm.inc l4lxref.inc
# glossary
#	included in <lib4th.asm>
INCSYS  = scdasm

# library names
SOBASE = $(SOFILE).so
LVNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r)
LDNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r.v)
LRNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r.v.x)
LSNAME = $(SSFILE).so.$(shell $(TOPDIR)/relse.sh r.v.x)
SONAME = $(LDNAME)

# GNU-ld is in-consistent! working or not, documented or not, complaining or not...
#	dnnlib
ifneq ("$(D)","")
LDFLAGS += $(D)
else
LDFLAGS += -x
endif
LDFLAGS += -M > $(LISDIR)/$@.map
LDFLAGS += -shared
#	force 'common'
LDFLAGS += -d
#LDFLAGS += -melf_i386
ifneq ("$(STRIP_ANY)","")
ifneq ("$(STRIP_LIB)","")
LDFLAGS += -strip-debug
ifneq ("$(STRIP_PRG)","")
#	install aborts because of errors after <sstrip>
#	and, <make> #not# executing the OR-ed alternative(s)!
#INSTDFLAGS += -s 
endif
endif
endif
ifneq ("$(SOLIB)","")
LDFLAGS += -export-dynamic
LDFLAGS += -soname $(SONAME)
endif
# alles probiert, keine chance...: 
#LDFLAGS += -nobuiltin
# if -nobuiltin given, ld links the library tightly 'above' program memory
# such that sys_brk cannot allocate any more memory, which results to early
# abortion due to a segfault in the initial mmap procedure at lib4th setup

LPFLAGS  =
#	dyn. progs: strip all, dll
ifneq ("$(STRIP_ANY)","")
ifneq ("$(STRIP_PROG)","")
LPFLAGS += -s
endif
endif
LPFLAGS += -Bdynamic
#ld: "elf load command not page-aligned...": LPFLAGS += -nobuiltin
#ld: "GNU-info", but not really: LPFLAGS += --section-alignment 4096
#LPFLAGS += -melf_i386
# either, -melf=i388 or below, apparently required:
LPFLAGS += -dynamic-linker $(DL)

#	static lib
LSFLAGS  =
#	bloede "C"-teile...!
#	does not gain anything, either variant:
#LSFLAGS += -u start -u _start -u __start
ifneq ("$(STRIP_ANY)","")
ifneq ("$(STRIP_LIB)","")
LSFLAGS += -strip-debug
endif
endif
LSFLAGS += -Bstatic
#	no "start address" in a library, '-e0' stops <ld> complaining <= !
LSFLAGS += -e0
LSFLAGS += -soname $(LSNAME)

# static prg
L4FLAGS  =
#	statically linked programs independent of _any_ library, re "4"
L4FLAGS += -Bstatic
ifneq ("$(STRIP_ANY)","")
ifneq ("$(STRIP_P4S)","")
L4FLAGS += -s
endif
L4FLAGS += -strip-debug
endif
#	pack tightly, static only!
L4FLAGS += --gc-sections
#L4FLAGS += -melf_i386

# <ldd> will be "warning" if library not executeable, nonsense, but GNU...?
#LDFLAGS = -r
#LPFLAGS = -r
#LSFLAGS = -r
#L4FLAGS = -r

# ----------------------------------------------------------------------------- #

#	programs
STRIPP = $(TR)
# 	dyn lib
STRIPD = $(TR)
#	static lib
STRIPS = $(TR)
#	static prog "4"
STRIP4 = $(TR)
ifneq ("$(STRIP_ANY)","")
ifneq ("$(STRIP_PRG)","")
ifneq ("$(STRIPPFLAGS)","")
STRIPP = $(SS)
endif
ifneq ("$(STRIPDFLAGS)","")
STRIPD = $(ST) $(STRIPDFLAGS)
endif
ifneq ("$(STRIPSFLAGS)","")
STRIPS = $(ST) $(STRIPSFLAGS)
endif
ifneq ("$(STRIP4FLAGS)","")
STRIP4 = $(ST) $(STRIP4FLAGS)
endif
endif
endif

#	display procedings
ifeq ("$(V)","")
N = @
endif

# ----------------------------------------------------------------------------- #

# make-vari/cons control:
#	accumulate assembly/assembler mode flags
ASFLAGS += $(DEBUG)
ASFLAGS += $(MDK)
ASFLAGS += $(K6)
ASFLAGS += $(MMX)
ASFLAGS += $(GOT)
ASFLAGS += $(MDOES)
ASFLAGS += $(RANUM)
ASFLAGS += $(SRANUM)
ASFLAGS += $(ANSFPS)
ASFLAGS += $(ANSFPA)
ASFLAGS += -d$(STD4TH)
ASFLAGS += -dDATE=\"$(shell date +%Y%m%d)\"
ASFLAGS += -dYEAR=\"$(shell date +%Y)\"
ASFLAGS += -d.vers=\"$(shell $(TOPDIR)/relse.sh r.v.x)\"
ASFLAGS += -d__VER=0x$(shell $(TOPDIR)/relse.sh rvxt)
ASFLAGS += -d_nolist=".nolist "
ASFLAGS += -d__LIB=\"$(LIB4)\"
ASFLAGS += -d__PROG=\"$(PROG)\"
ASFLAGS += -d__DOCDIR=\"$(DOCDIR)\"
ASFLAGS += -d__FMODE__=\"$(STD4TH)\"
ASFLAGS += -d__LIBRARY=\"$(LIBDIR)/$(SONAME)\"
ASFLAGS += -d__LIBDIR=\"$(LIBDIR)\"
ASFLAGS += -d__LIBF8D=\"$(LIBF8D)\"
# au:
ASFLAGS += -d__AU__="$(AV)$(AX)"
ASFLAGS += -d__$(OS)__
ASFLAGS += -d__$(OM)__
ASFLAGS += -d__SYSCALL__=$(OC)
ASFLAGS += -d__KERNEL__=$(KERNEL)
# nasm
ASFLAGS += -d__NASM="$(NASMV)"
ASFLAGS += -f$(ASMODE)
ASFLAGS += $(LIBINC)
ASFLAGS += $(ASINC)

# flag to suppressing section declarations - nasm...
ASFLAGS += -d__L4=y

# always re-make:
.PHONY:	$(SCFILES) d doc docs html topic topx xref

# any means to orderly avoiding those stupid 'implied rules'... ???
#	clear implied suffices list
.SUFFIXES:
#	no other (but <make> doesn't care...):
.SUFFIXES:	.o .asm .inc .mac .dep

# ----------------------------------------------------------------------------- #
#					-/-					#
# ----------------------------------------------------------------------------- #

wasnu help config configure:
	@echo ""
	@echo ""
	@echo "	============================================================="
	@echo "	pse, read and suitably edit 'MConfig', before (re)compiling."
	@echo "	============================================================="
	@echo ""
	@echo "compile if clean or source(s) modified:"
	@echo "		<make all>	'check depend docs prog 4' w/ glossary"
	@echo ""
	@echo "(re-)compile new:"
	@echo "		<make new>	'clean all 4'"
	@echo ""
	@echo "minimal re-make, if no words' actions changed:"
	@echo "		<make lib install> cpl & install the dll"
	@echo ""
	@echo "single app w/ lib, minimal cpl"
	@echo "		<make calc4th>	minimal program to entering the 4th system"
	@echo "		<make f8>	entry into the 4th system"
	@echo "		<make 4>	self contained, static Forth system"
	@echo ""
	@echo "un/install:"
	@echo "		<make install>	backup & install binaries, libs, docs"
	@echo "		<make uninstall> remove binaries and docs but no backups"
	@echo "		<make uninst_bk> remove backups, only"
	@echo "		<make uninstall_all> remove binaries, libs, docs, backups"
	@echo "	NOTE:	run <ldconfig> as 'root' after de/installation"
	@echo ""
	@echo "documentation:"
	@echo "		<make docs>	collect 'glossary'"
	@echo "		<make html>	generate browsable html glossaries - SLOW!"
	@echo "		<make topx>	html refs, sorted by topics - SLOW!"
	@echo "		<make xref>	generate browsable html cross-refs - SLOW!"
	@echo "		<make d>	all docs background job, ca 60min k6/2, 350"
	@echo ""
	@echo "other:"
	@echo "		<make check>	check whether cpl. requirements satisfied"
	@echo "		<make depend>	generate dependency list(s)"
	@echo "		<make tests>	test whether 'f8' compiled orderly"
	@echo ""
	@echo "		<make lib>	dynamic library, only"
	@echo "		<make slib>	static library, only"
	@echo "		<make prog>	dynamically linked prgs, 'calc4th', 'f8'"
	@echo "		<make devel>	'uninstall empty depend lib prog install'"
	@echo "		<make devel_all> 'make devel' and static versions"
	@echo ""
	@echo "		<make cvspak>	generate a CVS distribution tar-pack"
	@echo "		<make debpak>	compile debianized binaries package"
	@echo "				NOTE:  'root' permissions required!"
	@echo ""
	@echo "		<make scdata>	rfsh included kernel constants, syscalls"
	@echo ""
	@echo "	============================================================="
	@echo "	pse, read and suitably edit 'MConfig', before (re)compiling."
	@echo "	and, I really would appreciate any comments or suggestions..."
	@echo "			hp:	mailto:phpr@snafu.de"
	@echo "	============================================================="
	@echo ""

# check presence of util's, generate asm dependency files,
# compile glossary from asm source,
# make dynamic library version and reference programs.
all:	check depend docs prog
	@echo ""
	@echo "check w/"
	@echo "		<make tests>"
	@echo "un/install:"
	@echo "		<make uninstall> / <make install>"
	@echo ""

# all dyn & static from scratch
new:	clean r4 all 4

# both, dynamic and static lib variants
libs:	lib slib

# dynamic lib
lib:	$(SOFILE)
	@-(cd $(LISDIR);$(TOPDIR)/util/lis2sel - $(SOFILE)&&$(TOPDIR)/util/lis2lst $(SOFILE))&
	$(V)$(STRIPD) $(LRNAME)
	@echo ""
	-$(LS) $(LRNAME)
	@echo ""

# static lib
#	do not strip; strip the linked programs, instead
slib:	$(SSFILE) $(LSNAME)
	@-(cd $(LISDIR);$(TOPDIR)/util/lis2sel $(SSFILE)&&$(TOPDIR)/util/lis2lst $(SSFILE))&
	$(V)$(STRIPS) $(LSNAME)
	@echo ""
	-$(LS) $(LSNAME)
	@echo ""

# make reference programs, if required compile the dynamic library
prog:	$(TEST)
	@-(cd $(LISDIR);$(TOPDIR)/util/lis2sel $(TEST)&&$(TOPDIR)/util/lis2lst $(TEST))

# glossary files, to doc directory
docs doc:	glossary
	@-(cd $(TOPDIR)/admin/;./MK-ts-ok)&
glossary:
	@-(cd $(TOPDIR)/admin/;./MK-g)

# html, only (verbose output, and slow, anyway)
html:
	@-($(SU) "$(KA) MK-h" 2>/dev/null;cd $(TOPDIR)/admin/;./MK-h html x x)

# ref sorted by topic
topic topx:
	@-($(SU) "$(KA) MK-t" 2>/dev/null;cd $(TOPDIR)/admin/;./MK-t x x topx)

# html, only (verbose output, and slow, anyway)
xref:
	@-($(SU) "$(KA) MK-x" 2>/dev/null;cd $(TOPDIR)/admin/;./MK-x x xref x)

# all docs, by silent background jobs
d:	glossary
	@-($(SU) "$(KA) MK-a;$(KA) MK-h;$(KA) MK-x;$(KA) MK-t" 2>/dev/null;cd $(TOPDIR)/admin/;./MK-a)&

# check pre-requisites
check:	Makefile MConfig
	@echo -en "\nfind utilities...\t"
	@(r="";for i in $(PRGDIR) $(LIBDIR) $(UTILS);\
		do \
		echo $${i}|grep "^-\|^--">/dev/null&&continue;\
		[ "$${i}" = "$(SU)" ]&&continue;\
		echo "$${i}"|grep "$(SD)">/dev/null&&n=2||n=1;\
		p=`echo "$${i} "|cut -d\  -f$${n}`;\
		test -x "$${p}"||r="$$r $$p";\
		done;\
	[ "$${r}" = "" ]&&echo "ok"||\
		{ echo -e "\amissing...\n\t$$r\n => check directory and program pointers in MConfig\a";exit 1;}; )
	@echo -en "'asmutils' package...\t"
	@($(RM) $(TOPDIR)/inc/asmutils/au;$(LN) $(AUINC) $(TOPDIR)/inc/asmutils/au;\
		test -x $(TOPDIR)/inc/asmutils/au&&echo "ok"||\
		{ echo -e "\anot found\n\tcould not link the asmutils' include-files\a";exit 1; }; )
	@echo -en "GNUisance 'deflector'...\t"
	@(	$(LN) -sf $(LIBCA)/libc.a $(TOPDIR)/inc/libc.a&&echo "ok"||\
		{ echo -e "\anot found\n\tcould not link the uselessly required 'libc.a'\a";exit 1; }; )

# generate asm dependency files
dep depend:	$(wildcard src/*.asm)
	$(V)[ -z "$(?)" ]||{ for i in $(?);do [ -d "$${i}" ]||$(TOPDIR)/admin/fdinc $$i $(TOPDIR);done;touch dep/mupdt.dep; }

files:
	$(V)for i in $(F8FILES);do $(FD) $(TOPDIR) $$i -maxdepth 0 -print >/dev/null;done

# library names
#SOBASE = $(SOFILE).so
#LVNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r)
#LDNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r.v)
#LRNAME = $(SOBASE).$(shell $(TOPDIR)/relse.sh r.v.x)
#LSNAME = $(SSFILE).so.$(shell $(TOPDIR)/relse.sh r.v.x)
#SONAME = $(LDNAME)
# installation to destn dir's, libs and progs non-writable!
#i install:	$(SOBASE) files
install:	files
	$(V)$(IN) -d $(LIBDIR)	$(DOCDIR) $(LIBF8D) $(LIBF8D)/app||:
	$(V)$(CM) a+w $(LIBDIR)/$(LSNAME) 2>/dev/null||:
	$(V)$(CM) a+w $(LIBDIR)/$(LRNAME) 2>/dev/null||:
	$(V)(cd $(PRGDIR);$(CM) a+w $(SPRGS) 2>/dev/null||:)
	$(V)(cd $(PRGDIR);$(CM) a+w $(DPRGS) 2>/dev/null||:)
	$(N)$(CM) $(LMFLAGS)    $(LRNAME) 2>/dev/null||:
	$(N)$(CM) $(LMFLAGS)    $(LSNAME) 2>/dev/null||:
	$(N)$(IN) $(INSTLFLAGS) $(LSNAME) $(LIBDIR)/$(LSNAME)||:
	$(V)$(IN) $(INSTDFLAGS)	$(DPRGS)  $(PRGDIR)/||$(CP) $(DPRGS) $(PRGDIR)/||:
	$(V)$(IN) $(INSTSFLAGS)	$(SPRGS)  $(PRGDIR)/||$(CP) $(SPRGS) $(PRGDIR)/||:
	$(V)(cd $(PRGDIR);$(STRIP4) $(SPRGS)||:;$(CM) $(PMFLAGS) $(SPRGS)||:)
	$(V)(cd $(PRGDIR);$(STRIPP) $(DPRGS)||:;$(CM) $(PMFLAGS) $(DPRGS)||:)
	$(V)$(IN) $(INSTLFLAGS)	$(LRNAME) $(LIBDIR)/$(LRNAME)||$(CP) $(LRNAME) $(LIBDIR)/$(LRNAME)||:
	$(V)(cd $(LIBDIR);\
	    $(CM) $(LMFLAGS),a-w $(LSNAME);\
	    $(CM) $(LMFLAGS),a-w $(LRNAME);\
	    $(LN) $(LRNAME)  $(LDNAME);\
	    $(LN) $(LDNAME)  $(LVNAME);\
	    $(LN) $(LVNAME)  $(SOBASE);\
	    $(LO)||\
		$(LC) $(LIBDIR)/$(LRNAME) $(LIBDIR)/$(LDNAME) $(LIBDIR)/$(LVNAME) $(LIBDIR)/$(SOBASE)||\
		{ echo -e "\n\n\tRUN ldconfig as 'root' or, include $(LIBDIR)/$(SONAME) in LD_LIBRARY_PATH\n";exit 1; };\
	    )
	$(V)$(IN) $(INSTLFLAGS) $(F8FILES) $(LIBF8D)/||$(IN) $(F8FILES) $(LIBF8D)/||$(CP) $(F8FILES) $(LIBF8D)/||:
	$(V)$(SU) $(CP)          $(F8APPS) $(LIBF8D)/app/
	$(V)$(FD)                          $(LIBF8D)/ -depth -exec $(CM) a+rX \{\} \;
	$(V)$(FD)                          $(LIBF8D)/ -depth -name \*\.f8 -exec $(CM) a-x \{\} \;
	$(V)$(IN) $(INSTLFLAGS) $(DOCS) $(DOCDIR)/||$(CP) $(DOCS) $(DOCDIR)/||:
	$(V)$(FS) $(TOPDIR)/doc/ -name $(HTML) -exec $(IN) $(INSTLFLAGS) \{\} $(DOCDIR)/ \;||$(FS) $(TOPDIR)/doc/ -name $(HTML) -exec $(CP) \{\} $(DOCDIR)/ \;||:
	$(V)$(IN) $(INSTLFLAGS) $(GLOS) $(DOCDIR)/||$(CP) $(GLOS) $(DOCDIR)/||:
	@echo -e "\n\tinstalled:\n\t\tlibrary  to $(LIBDIR)\n\t\tprograms to $(PRGDIR)">INST-L4
	@echo -e "\n\t\tsupplementary files to $(LIBF8D)\n\t\texamples to $(LIBF8D)/app/\n\t\tdocumentation to $(DOCDIR)\n\n">>INST-L4
ifeq ("$(V)","@")
	@echo -e "\n\tinstalled:\n\t\tlibrary  to $(LIBDIR)\n\t\tprograms to $(PRGDIR)"
	@echo -e "\n\t\tsupplementary files to $(LIBF8D)\n\t\texamples to $(LIBF8D)/app/\n\t\tdocumentation to $(DOCDIR)\n\n"
endif
	@sync;bell

# remove current and 'old' (previous) versions from install and working dir's
# shared library file in cpl-dir not deleted in case new one has errors, to be linked manually
uninstall:
ifneq ($(N),)
	@echo -e "\n\t<uninstall>..."
endif
	@-(killall -3 lis2sel 2>/dev/null||true)&
	@-( \
	    test -r $(TOPDIR)/inc/version.inc.old -a -r $(TOPDIR)/inc/version.inc 2>/dev/null&&{ \
		mv $(TOPDIR)/inc/version.inc $(TOPDIR)/inc/version.t;\
		mv $(TOPDIR)/inc/version.inc.old $(TOPDIR)/inc/version.inc;\
	        make uninstall&&\
		    $(RM) -f $(SSFILE).so.$(shell ./relse.sh r.v.x) $(TOPDIR)/version.inc||\
		    mv $(TOPDIR)/inc/version.inc $(TOPDIR)/inc/version.inc.old;\
	        mv $(TOPDIR)/inc/version.t $(TOPDIR)/inc/version.inc; \
	    }||: )
	$(V)$(RM) $(LIBDIR)/$(LSNAME) 2>/dev/null||:
	$(V)$(RM) $(LIBDIR)/$(SOBASE) 2>/dev/null||:
	$(V)$(RM) $(LIBDIR)/$(LVNAME) 2>/dev/null||:
	$(V)$(RM) $(LIBDIR)/$(LDNAME) 2>/dev/null||:
	$(V)$(RM) $(LIBDIR)/$(LRNAME) 2>/dev/null||:
	$(V)$(LC) >/dev/null 2>&1||:
	$(V)(cd $(PRGDIR);$(RM) $(DPRGS) $(SPRGS)) 2>/dev/null||:
	$(V)$(RM) $(DOCDIR)/* 2>/dev/null||:
	@sync
ifneq ($(N),)
	@echo -e "\t\t\t ...done\n"
endif

u uninstall_all: uninstall uninst_bk
	$(V)$(RM) $(TOPDIR)/$(SOBASE)* >/dev/null 2>&1||:
	$(V)$(RM) $(TOPDIR)/$(SSFILE)* >/dev/null 2>&1||:
	$(V)$(RM) $(LIBDIR)/$(SOBASE)* >/dev/null 2>&1||:
	$(V)$(RM) $(LIBDIR)/$(SSFILE)* >/dev/null 2>&1||:
	$(V)$(LC) >/dev/null 2>&1||:
	@sync

# remove backups in doc and lib directories
uninst_bk uninstall_bk uninst-bk:
	$(V)(cd $(PRGDIR);for i in $(DPRGS) $(SPRGS);do $(RM) $$i*~;done) 2>/dev/null||:
	$(V)$(FS) $(LIBDIR)/ -maxdepth 1 \( -name $(SOFILE)-\*~ -o -name $(SOBASE)\*~ \) -exec $(RM) -f \{\} \; 2>/dev/null||:
	$(V)$(FS) $(LIBF8D)/ $(DOCDIR)/ -depth -name \*~ -exec rm -f \{\} \; 2>/dev/null||:
	@sync
	
# try whether "f8" reference program compiled orderly
tests:
	@-(cd $(TOPDIR)/tests/;./t.sh)

# uninstall previous/old version(s) and backups(!), (re-)make and install to final dir's
# 'normal' development cycle:
devel:	uninstall uninst_bk empty dep prog install
# development cycle w/ clean source, static versions inclusive:
#	uninstall empty r4 check depend d prog 4 install
devel_all:	uninstall_all empty all install

# remove temporary and object files
clean:
	@$(SU) "$(KA) lis2sel" 2>/dev/null||:
	@$(FS) . -depth \
		\( -name \*\.gls -o -name \*\.log -o -name \*\.lis -o -name \*\.o \
		$(for i in $(SCFILES);do echo -o -name $$i;done) \
		-o -name t -o -name \*\.t -o -name \*stamp \) -exec rm -f \{\} \; 2>/dev/null

r4:
	$(V)$(RM) 4 2>/dev/null

# remove generated docs, backups, temporary and all program & library files
empty:	clean
	@$(SU) "$(KA) MK-g" 2>/dev/null||:
	@$(SU) "$(KA) MK-h" 2>/dev/null||:
	@$(FS) . -depth \
		\( -name \*\.s -o -name \*\.dep -o -name glossar\* -o -name l4\*html -o -name doc/tests-ok -o -name \*\.~ \
		-o -name $(LRNAME) -o -name $(LDNAME) -o -name $(LVNAME) -o -name $(SOBASE) -o -name $(LSNAME) \
		$(for i in $(TEST);do echo -o -name $$i;done) \) -exec rm -f \{\} \; 2>/dev/null

empty4:	empty r4

# compile debian package w/ <debian/rules build> as root, from main directory
debpak:
	@(\
	[ "$$(id -u)" = "0" ]||{ echo -e "\n\troot permissions required\n\n";exit 1; };\
	    debian/rules binary&&[ -d /hpg5/up-sna ]&&\
	    install -m644 -g$(G) -o$(U) ../$(DEBNAME) /hpg5/up-sna/$(DEBNAME);\
	    chown -R $(U) .;chgrp -R $(G) .;chmod -R a+rwX,o-w .;\
	)

# build sources tar-pack
cvspak:
	@(\
	cvs log -lN  inc/version.inc|grep -v "date:\|revision\ \|--------\|========">doc/cvslog-lN-lib4th.asm.txt;\
	cd admin;./MK-pak-cvs;\
	)

# ----------------------------------------------------------------------------- #

# make deps if *.dep or binary missing or any file newer than rsp binary
# nasm -M is as slow as full assembly - but, what else to expect...
#	$(MDEP) passed from %dep
ud:
	$(N)test -r "$(MDEP)"||$(MAKE) depend
	$(V)(while read i;\
		do test ! $$i -nt $(TOPDIR)/dep/mupdt.dep||\
		{ echo $$i;$(TOPDIR)/admin/mupdt $(MDEP) $(MDEP)||continue; };done<$(MDEP); )

mupdt.dep:	$(wildcard src/*.asm)
	$(N)@echo -en " dep: "
	$(V)for i in $?;do $(TOPDIR)/admin/mupdt $$i $$i||continue;done
	$(N)@echo " dun."

inc/asmutils/au:
	$(V)($(MAKE) check)

# syscalls from Linux source tree
# using (yet) no other than scdata.inc at <sysc.exit> in lib4th.asm
scdata:	$(INCSYS)
	$(V)( read v v v<$(LXSRC)/$(LXVER)||v=1;v="0$$(echo $${v}|sed -e 's/\"//g;s/\./0/g'),";\
	read i i i i i x<$(TOPDIR)/inc/$(INCSYS).inc||i=2;[ $${i} == $${v} ]||$(MAKE) $(INCSYS).inc; )
$(INCSYS):	$(SCFILES)
$(SCFILES):
	@(cd $(TOPDIR)/inc;$(TOPDIR)/util/unistd-na $(SRCINC))

doc/glossary doc/test-ok:
	$(V)$(MAKE) docs

%.dep:	src/%.asm mupdt.dep $(wildcard inc/asmutils/au) inc/asmutils/au $(INCSYS)
	@(export MDEP=dep/$(*:.asm=).dep;echo ":dep:">/dev/tty;$(MAKE) ud)
	@test ! $@ -nt $(TOPDIR)/dep/mupdt.dep&&$(TOPDIR)/admin/mupdt $@ $@

%.inc %.mac %.asm:	$(INCSYS)

%.o:	src/%.asm %.dep $(SOFILE).o $(INCDEP)
	@-rm -f $(LISDIR)$*.lis
	$(V)$(ASM) $(ASFLAGS) $(OPTIM) $(LIST) $< -o $(*:.asm=).o $(ASLOG)
	@test -s $(LISDIR)$(*:.asm=).log&&\
		grep -v "by\ user" $(LISDIR)$(*:.asm=).log&&echo;\
		grep -v "by\ user\|message\|warning" $(LISDIR)$(*:.asm=).log&&\
		    echo -e "=>\t=>\tASM w. errors, re $(LISDIR)$(*:.asm=).log\a"&&\
		    rm $(*:.asm=).o&&$(MAKE) wasnu&&exit 2|| :

$(SSFILE).o:	src/$(SOFILE).asm $(SOFILE).dep $(INCDEP)
	$(V)$(ASM) $(ASFLAGS) $(SLIST) -d__STATIC $(SOPTIM) -o$(SSFILE).o $< $(ASLOG)
	@test -s $(LISDIR)$(*:.asm=).log&&\
		grep -v "by\ user\|redefin" $(LISDIR)$(*:.asm=).log&&rm $(*:.asm=).o||:
	$(N)$(LS) $(*:.asm=).o 2>/dev/null||:

$(SOFILE).o:	src/lib4th.asm lib4th.dep $(INCDEP)
	@-rm -f $(LISDIR)$*.lis
	$(V)$(ASM) $(ASFLAGS) $(OPTIM) $(LIST) $< -o $(*:.asm=).o $(ASLOG)
	@test -s $(LISDIR)$(*:.asm=).log&&\
		grep -v "by\ user\|redefin" $(LISDIR)$(*:.asm=).log&&rm $(*:.asm=).o|| :

4.o:	src/calc4th.asm calc4th.dep $(INCDEP)
	@$(RM) $(LISDIR)$*.lis
	$(V)$(ASM) $(ASFLAGS) $(SOPTIM) -o$@ -d__STATIC $< $(ASLOG)
	$(N)$(LS) $@||:

# --------------------------------------------------------------------------------

# find common to all by asmutils supported hosts' syscalls
a4c.inc:	$(OSF) Makefile
	@(\
	echo "; $(A4SI)">inc/a4c.inc;o=0;\
	cat $(AUINC)/$(A4SI)|grep 'SYS_'|grep 'assign '|while read a b c d;\
	do [ -z "$$c" ]||\
	{ [ $$(($${o})) -gt $$(($${c})) ]&&\
	echo -e "\n %assign top_syscommon $$o">>inc/a4c.inc&&break; }||\
	{ \
		for i in $(OSF);do [ "$${i}" = "$(A4SI)" ]||{ grep "$${b}" $(AUINC)/$${i}>/dev/null||b=; }; done;\
		[ -z "$${b}" ]||{ o=$$c;i=$${b:4};echo -e "\tdb $${#i},\"$$i\",$$c">>inc/a4c.inc; };\
	}\
	done;\
	)

# ----------------------------------------------------------------------------- #

# libs
$(SOBASE) $(LVNAME) $(LDNAME) $(LRNAME) $(SOFILE): $(SOFILE).o $(SOFILE).dep $(INCDEP)
ifneq ($(SOLIB),)
	$(V)$(LD) $(LDFLAGS) -o $(LRNAME) -lc $<
else
	$(V)$(LD) $(LDFLAGS) -o $(LRNAME) $<
endif
	$(V)$(CM) $(LMFLAGS) $(LRNAME)
	$(V)$(LN) $(LRNAME)  $(LDNAME)
	$(V)$(LN) $(LDNAME)  $(LVNAME)
	$(V)$(LN) $(LVNAME)  $(SOBASE)

$(SSFILE): $(SSFILE).o $(SOFILE).dep $(INCDEP)
	$(LD) $(LSFLAGS) -o $(LSNAME) $<

$(LSNAME):	$(SSFILE)
	$(V)$(CM) $(LMFLAGS) $(LSNAME)

# statically linked example w. static library
4:	4.o $(LSNAME) calc4th.dep $(INCDEP)
	$(V)$(LD) $(L4FLAGS) -o$@ $@.o $(LSNAME)
	@$(STRIP4) $@
	@echo ""
	@ls -l $@||:
	@echo ""

# reference prog.
f8:	f8.o f8.dep $(SONAME)
	$(V)$(LD) $(LPFLAGS) -o$@ $< $(SONAME)
	@$(STRIPP) $@
	@echo ""
	@ls -l $@||:
	@echo ""

# screenfile editor
f8ed:	f8ed.o f8ed.dep $(SONAME)
	$(V)$(LD) $(LPFLAGS) -o$@ $< $(SONAME)
	@$(STRIPP) $@
	@echo ""
	@ls -l $@||:
	@echo ""

# forth system entry, cut down to it's minimum
calc4th: calc4th.o calc4th.dep $(SONAME)
	$(V)$(LD) $(LPFLAGS) -o$@ $< $(SONAME)
	@$(STRIPP) $@
	@echo ""
	@ls -l $@||:
	@echo ""

#
# ----------------------------------------------------------------------------- #
# Makefile-lib4th.05 <eof>
