# $Id$

-include .config
name=threadcpu
WORKINGDIR=$(shell pwd)
DIRNAME=$(shell basename `pwd`)
VERSION=$(shell basename `pwd` |sed 's,$(name)-,,')
VERSION=$(shell date +%Y%m%d)
RPM_SPECS_DIR=$(shell rpm -E '%_specdir')
RPM_SOURCES_DIR=$(shell rpm -E '%_sourcedir')

DEBUG=
ifeq ($(CONFIG_USE_DIET),y)
  DIET=diet
else
  DIET=
endif
ifeq ($(USE_EMBEDDEDBUILD),yes)
  EMBEDDED_CFLAGS=-DSMALL_BUILD
else
  EMBEDDED_CFLAGS=
endif
HOSTCC=gcc
HOSTCC=$(shell if type gcc-4.1 >/dev/null 2>&1;then echo gcc-4.1;else echo gcc;fi)
INCLUDES=-I$(WORKINGDIR)/lib -I$(WORKINGDIR)
NASM=/bin/false
ifeq ($(shell arch|egrep -q 'x86_64|i686' && echo y),y)
  CPU_OPT=-march=nocona -mtune=nocona -msse2 --param=ssp-buffer-size=4
  NASM=nasm
else
  ifeq ($(shell arch|grep -q 'arm' && echo y),y)
    CPU_OPT=-march=armv6 -mfpu=vfp -mfloat-abi=hard
  else
    CPU_OPT=
  endif
endif
BASE_CFLAGS=-std=c99 -pedantic -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -DNO_PAGE_ALLOC_ERROR
OPTFLAGS=-O2 $(CPU_OPT) -fomit-frame-pointer
GCC_WARNS_NEW=-Waddress -Warray-bounds=1 -Wbool-compare -Wchar-subscripts -Wcomment -Wenum-compare -Wformat -Wimplicit -Wimplicit-int -Wimplicit-function-declaration -Wlogical-not-parentheses -Wmain -Wmaybe-uninitialized -Wmemset-transposed-args -Wmissing-braces -Wnonnull -Wnonnull-compare -Wopenmp-simd -Wparentheses -Wpointer-sign -Wreturn-type -Wsequence-point -Wsizeof-pointer-memaccess -Wstrict-aliasing -Wstrict-overflow=1 -Wswitch -Wtautological-compare -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wvolatile-register-var
GCC_WARNS_COMPATIBLE=-Wchar-subscripts -Wcomment -Wformat -Wimplicit -Wimplicit-int -Wimplicit-function-declaration -Wmain -Wmissing-braces -Wnonnull -Wparentheses -Wpointer-sign -Wreturn-type -Wsequence-point -Wstrict-aliasing -Wswitch -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wvolatile-register-var
#GCC_WARNS=$(shell if echo 'main(){}'|gcc $(GCC_WARNS_NEW) -E - &>/dev/null; then echo " $(GCC_WARNS_NEW)"; else echo " $(GCC_WARNS_COMPATIBLE)";fi)
IS_OLD_GCC=$(shell { echo 'main(){}'|$(HOSTCC) $(GCC_WARNS_NEW) -E - >/dev/null 2>&1; echo $$?; })
ifeq ($(IS_OLD_GCC),1)
  GCC_WARNS=$(GCC_WARNS_COMPATIBLE)
else
  GCC_WARNS=$(GCC_WARNS_NEW)
endif
#EXTRACFLAGS=-Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -pipe
EXTRACFLAGS=$(GCC_WARNS) -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -pipe
#-O -fomit-frame-pointer -Wall -D_MONOLITHIC_
#-Os -Wall -D_DEBUG_
#-O2 -fomit-frame-pointer -Wall -march=pentium4
#-O2 -fstack-protector -fomit-frame-pointer -falign-functions=1 -falign-jumps=1 -falign-loops=1 -Wall -march=pentium4 -DNO_PAGE_ALLOC_ERROR
#-O2 -fomit-frame-pointer -Wall -march=nocona -mtune=nocona -DNO_PAGE_ALLOC_ERROR
#-O2 -march=nocona -mtune=nocona -msse2 -fomit-frame-pointer -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -pipe -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_GNU_SOURCE -DNO_PAGE_ALLOC_ERROR
#-O2 -march=nocona -mtune=nocona -msse2 -fomit-frame-pointer -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4 -pipe -I. -I.. -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_GNU_SOURCE -DNO_PAGE_ALLOC_ERROR
HOSTCFLAGS=$(BASE_CFLAGS) $(OPTFLAGS) $(EXTRACFLAGS) $(EMBEDDED_CFLAGS) $(INCLUDES)
HOSTLD=ld
HOSTLDFLAGS=

CFLAGS=$(HOSTCFLAGS)
ALLCFLAGS=$(CFLAGS)
LDCFLAGS=$(ALLCFLAGS) -Wl,-z,noexecstack
CC=$(DIET) $(HOSTCC)
EXTRA_CFLAGS=
FUSEFLAGS=$(shell pkg-config fuse --cflags --libs || echo ===FUSEFLAGS_pkg-config_ERROR===)
LD=$(HOSTLD)
LDFLAGS=$(HOSTLDFLAGS)
ifneq ($(DIET),)
  EXTRA_CFLAGS+=-Wl,-lcompat
else
  EXTRA_CFLAGS+=-Wl,-lrt
endif
FUSE_FLAGS=$(shell pkg-config fuse --cflags --libs)
NASM_BIN_FLAGS=-f bin
NASM_ELF_FLAGS=-f elf
#SSTRIP=./sstrip-vladov
SSTRIP=tools/sstrip
HOSTSSTRIP=tools/sstrip-host
STRIP_ARGS=-s -R .note -R .comment
STRIP=$(HOSTSSTRIP)
USE_SSTRIP="sstrip"
ifeq ($(USE_SSTRIP),"sstrip")
  STRIP=$(HOSTSSTRIP)
else
  STRIP=strip $(STRIP_ARGS)
endif
RM=/bin/rm
CP=/bin/cp -PpR 
MV=/bin/mv
LN=/bin/ln
INSTALL=install

DESTDIR=/
prefix=$(DESTDIR)/opt/$(name)
prefix=$(DESTDIR)/usr
exec_prefix=$(prefix)
bindir=$(prefix)/bin
mandir=$(prefix)/share/man
docdir=$(prefix)/share/doc
sbindir=$(prefix)/sbin
sysconfdir=$(DESTDIR)/etc
sysconfigdir=$(DESTDIR)/etc/sysconfig
datadir=$(prefix)/share
includedir=$(prefix)/include
libdir=$(prefix)/lib
libexecdir=$(prefix)/libexec
localstatedir=/var
sharedstatedir=$(prefix)/com
infodir=$(prefix)/share/info

LIBFWB=lib/libfwb/libfwb.a

LIBOBJS= \
	lib/libfwb/lib/alloc.o \
	lib/libfwb/lib/buffer_catfromfile.o \
	lib/libfwb/lib/buffer_close.o \
	lib/libfwb/lib/buffer_filecmp.o \
	lib/libfwb/lib/buffer_flushlines.o \
	lib/libfwb/lib/buffer_flush.o \
	lib/libfwb/lib/buffer_getchar.o \
	lib/libfwb/lib/buffer_getline.o \
	lib/libfwb/lib/buffer_openread.o \
	lib/libfwb/lib/buffer_openwrite.o \
	lib/libfwb/lib/buffer_putc.o \
	lib/libfwb/lib/buffer_put.o \
	lib/libfwb/lib/buffer_putmem.o \
	lib/libfwb/lib/buffer_putstr.o \
	lib/libfwb/lib/buffer_putuint32.o \
	lib/libfwb/lib/buffer_putuint64.o \
	lib/libfwb/lib/buffer_putuint8.o \
	lib/libfwb/lib/fakemath.o \
	lib/libfwb/lib/fchown32m.o \
	lib/libfwb/lib/field_indexc.o \
	lib/libfwb/lib/field_index.o \
	lib/libfwb/lib/file2buf.o \
	lib/libfwb/lib/get_home_from_passwd.o \
	lib/libfwb/lib/get_name_from_passwd.o \
	lib/libfwb/lib/getuid32m.o \
	lib/libfwb/lib/get_uid_from_passwd.o \
	lib/libfwb/lib/hexstr2uint32.o \
	lib/libfwb/lib/map_file.o \
	lib/libfwb/lib/map_passwd.o \
	lib/libfwb/lib/procnetipaddr_to_humanreadable.o \
	lib/libfwb/lib/putlong.o \
	lib/libfwb/lib/putu64.o \
	lib/libfwb/lib/setgid32m.o \
	lib/libfwb/lib/setresgid32m.o \
	lib/libfwb/lib/setresuid32m.o \
	lib/libfwb/lib/setuid32m.o \
	lib/libfwb/lib/sputlongs.o \
	lib/libfwb/lib/sputuint32.o \
	lib/libfwb/lib/sputuint64.o \
	lib/libfwb/lib/stpcpy.o \
	lib/libfwb/lib/str2uint32.o \
	lib/libfwb/lib/str2uint63.o \
	lib/libfwb/lib/str2uint64.o \
	lib/libfwb/lib/str_catc.o \
	lib/libfwb/lib/str_catn.o \
	lib/libfwb/lib/str_cat.o \
	lib/libfwb/lib/str_chrnul.o \
	lib/libfwb/lib/str_chr.o \
	lib/libfwb/lib/str_diffn.o \
	lib/libfwb/lib/str_diff.o \
	lib/libfwb/lib/str_diff_right.o \
	lib/libfwb/lib/str_field.o \
	lib/libfwb/lib/str_index2.o \
	lib/libfwb/lib/str_index.o \
	lib/libfwb/lib/str_len.o \
	lib/libfwb/lib/write1.o \
	lib/libfwb/lib/write2.o \
	lib/libfwb/lib/write2file.o \


.c.o:
	@echo CC -c -o $@ $<
	$(CC) $(DEBUG) $(ALLCFLAGS) -c -o $@ $<

usage: auto
	@echo -e '\n\n === targets: ===\n'
	@echo 'menuconfig: configure threadcpu'
	@echo 'all: build threadcpu'
	@echo 'install: build and install threadcpu'
	@echo 'auto: auto-create default config'
	@echo 'manhtml: convert man-page to HTML-page'
	@echo 'tgz: create .tar.gz archive'
	@echo
	@#echo OPTFLAGS=$(OPTFLAGS) $(EXTRACFLAGS) $(EMBEDDED_CFLAGS) $(INCLUDES)
	@#echo HOSTCFLAGS=$(HOSTCFLAGS)
	@#echo CFLAGS=$(CFLAGS)
	@#echo
	@sleep 3
	$(MAKE) all

.PHONY: clean libfwb all usage auto install installdirs tools sstrip lib tgz

include kconfig/Makefile

$(KCONFIG_TOP): generated/Config.in
generated/Config.in: */*.c buildscripts/genconfig.sh
	buildscripts/genconfig.sh


all: auto sstrip libfwb lib tools

auto:
	@echo doing "make auto"
	@[ -e .config ] ||  { $(CP) .config.auto .config;echo " === default config written to .config, you may wish to run 'make menuconfig' ==="; }
	@$(MAKE) silentoldconfig
	@sleep 1

install: auto all installdirs tools-install

installdirs:
	-$(INSTALL) -d $(bindir) $(mandir)/man1

lib:
	@$(MAKE) DIET="$(DIET)" HOSTCC="$(HOSTCC)" CFLAGS="$(ALLCFLAGS)" -C lib

lib-clean:
	$(MAKE) -C lib clean

libfwb: lib/libfwb/lib/*.[ch]
	@echo $(MAKE) DIET="$(DIET)" HOSTCC="$(HOSTCC)" -C lib/libfwb
	@$(MAKE) DIET="$(DIET)" HOSTCC="$(HOSTCC)" CFLAGS="$(ALLCFLAGS)" -C lib/libfwb

libfwb-clean:
	$(MAKE) -C lib/libfwb clean

tools: sstrip threadcpu

tools-install: threadcpu-install

ping:
	-$(NASM) -w+orphan-labels -w+macro-params -iinc/ -O99v -f bin -D__LINUX__ -D__KERNEL__=24 -D__SYSCALL__=__S_KERNEL__ -D__OPTIMIZE__=__O_SIZE__ -D__ELF__ -D__ELF_MACROS__ tools/ping.asm
	-@chmod 755 tools/ping

ping-clean:
	@$(RM) -f tools/ping
 
sstrip: tools/sstrip.c
	@echo CC -o tools/sstrip tools/sstrip.c
	@-$(HOSTCC) $(HOSTCFLAGS) -o $(HOSTSSTRIP) tools/sstrip.c
	@-$(CC) $(LDCFLAGS) -o tools/sstrip tools/sstrip.c
	@-$(HOSTSSTRIP) tools/sstrip
	#@-$(CP) tools/sstrip tools/sstrip-tmp
	#@-tools/sstrip-tmp tools/sstrip
	#@$(RM) -f tools/sstrip-tmp

sstrip-install:
	$(INSTALL) -p $(SSTRIP) $(bindir)

sstrip-clean:
	$(RM) -f $(SSTRIP) tools/sstrip.o
	$(RM) -f $(SSTRIP) tools/sstrip-host tools/sstrip.o

macbuild:
	$(MAKE) USE_SSTRIP="" STRIP_ARGS="-x" CPU_OPT="" DIET="" actionwhenfileold daemonize daemonizep ftsm direntries dirscan ipv4conv postfix-mailqsize sfgrep showip svscanboot
	$(MAKE) USE_SSTRIP="" STRIP_ARGS="-x" CPU_OPT="" DIET="" EXTRA_CFLAGS="" postfix-saslusernames
	@echo ==== successfully compiled tools: ====
	/bin/ls -l nagios-checks/postfix-mailqsize tools/actionwhenfileold tools/daemonize tools/daemonize-c tools/daemonizep tools/ftsm tools/direntries tools/dirscan tools/ipv4conv tools/postfix-saslusernames tools/sfgrep tools/showip tools/svscanboot2 tools/svscanboot3

threadcpu: sstrip libfwb tools/threadcpu.c
	@echo CC -o tools/threadcpu tools/threadcpu.c
	@$(CC) $(DEBUG) $(LDCFLAGS) -o tools/threadcpu tools/threadcpu.c $(EXTRA_CFLAGS) $(LIBFWB)
	@-$(STRIP) tools/threadcpu

threadcpu-install: threadcpu
	$(INSTALL) -d $(bindir) $(mandir)/man1 $(docdir)/$(name)
	$(INSTALL) -p tools/threadcpu $(bindir)
	$(INSTALL) -m 644 tools/threadcpu.1 $(mandir)/man1/; $(RM) -f $(mandir)/man1/threadcpu.1.gz; gzip -9 -f $(mandir)/man1/threadcpu.1
	$(INSTALL) -m 644 CHANGELOG COPYING README $(docdir)/$(name)/

threadcpu-clean:
	$(RM) -f tools/threadcpu tools/threadcpu.o

str_chrnul: lib/str_chrnul.c lib/str_chrnul.S
	@echo -e "trying assembler... \c";if as -V -Qy -o lib/str_chrnul.o lib/str_chrnul.S;then echo using str_chrnul assembly code;else echo using str_chrnul C code;gcc -c -o lib/str_chrnul.o lib/str_chrnul.c;fi

check: libfwb
	$(MAKE) DIET="$(DIET)" HOSTCC="$(HOSTCC)" CFLAGS="$(ALLCFLAGS)" -C lib/libfwb check

arch: tools/archi586.asm tools/archi686.asm tools/archx86_64.asm
	-$(NASM) $(NASM_BIN_FLAGS) tools/archi586.asm
	-$(NASM) $(NASM_BIN_FLAGS) tools/archi686.asm
	-$(NASM) $(NASM_BIN_FLAGS) tools/archx86_64.asm
	@-chmod 755 tools/archi586 tools/archi686 tools/archx86_64

clean:: libfwb-clean lib-clean threadcpu-clean manhtml-clean
	@echo $(RM) -f "\$$(LIBOBJS)" tags */*.o
	$(MAKE) -C lib/libfwb clean
	$(MAKE) -C kconfig clean
	@$(RM) -f $(LIBOBJS) tags */*.o tools/sstrip tools/sstrip-host
	@$(RM) -rf .config.old generated include/config include/linux
	@$(RM) -rf docs html latex
 
distclean: clean
	@$(RM) -rf .config generated .config.old .config.h include/config include/linux
	@-rmdir include
	#$(MAKE) savets

devclean: distclean
	tar cf ../$(DIRNAME)-$(VERSION)-dev.tar $$(ls log tmp tools/*suphp* 2>/dev/null) || true
	@$(RM) -fr log tmp tools/*suphp*

devrevert:
	tar xf ../$(DIRNAME)-$(VERSION)-dev.tar

# old stuff:
dyns: $(USE_SSTRIP)
	@$(RM) -f $(NAGIOS_CAPPS)
	@for f in $(NAGIOS_CAPPS);do $(HOSTCC) $(CFLAGS) -o "$$f".dyn "$$f".c;done
	for f in $(NAGIOS_CAPPS); \
	do \
	  $(STRIP) $(STRIP_ARGS) "$$f".dyn; \
	  ln "$$f".dyn "$$f" || /bin/true; \
	done

mangz:
	for f in $(MANPAGES); \
	do \
	  gzip -9 -f <$$f >$$f.gz; \
	  touch -r $$f $$f.gz; \
	done

desc:
	@for f in $(MANPAGES);do head -3 $$f|tail -1;done|sed 's,\\,,'

dist: tgz

tgz: distclean savets devclean manhtml
	@echo
	@echo Dir: $(DIRNAME) Version: $(VERSION)
	@sed -i 's,^Version:.*,Version: $(VERSION),' $(name).spec
	@sed -i '1s,threadcpu .*,threadcpu ($(VERSION)-1) unstable; urgency=low,' debian/changelog
	cd ..; \
	  $(RM) -rf $(DIRNAME)-$(VERSION); \
	  $(CP) $(DIRNAME) $(DIRNAME)-$(VERSION); \
	  find $(DIRNAME)-$(VERSION) -name "*.swp" -exec $(RM) -rf {} \; 2>/dev/null; \
	  find $(DIRNAME)-$(VERSION) -name .svn -exec $(RM) -rf {} \; 2>/dev/null; \
	  $(RM) -rf $(DIRNAME)-$(VERSION)/.git* 2>/dev/null; \
	  find $(DIRNAME)-$(VERSION) -type d -exec rmdir -p {} \; 2>/dev/null; \
	  tar cf - $(DIRNAME)-$(VERSION) | gzip -9 -f >$(DIRNAME)-$(VERSION).tar.gz; \
	  $(CP) -af $(DIRNAME)-$(VERSION).tar.gz $(DIRNAME)_$(VERSION).orig.tar.gz; \
	  $(RM) -rf $(DIRNAME)-$(VERSION)

bintgz: tgz all
	@echo Dir: $(DIRNAME) Version: $(VERSION)
	cd ..; \
	  $(RM) -rf $(DIRNAME)-$(VERSION); \
	  $(CP) $(DIRNAME) $(DIRNAME)-$(VERSION); \
	  find $(DIRNAME)-$(VERSION) -name "*.swp" -exec $(RM) -rf {} \; 2>/dev/null; \
	  find $(DIRNAME)-$(VERSION) -name .svn -exec $(RM) -rf {} \; 2>/dev/null; \
	  $(RM) -rf $(DIRNAME)-$(VERSION)/.git* 2>/dev/null; \
	  find $(DIRNAME)-$(VERSION) -type d -exec rmdir -p {} \; 2>/dev/null; \
	  gzip -9 -f $(DIRNAME)-$(VERSION)/*/*.1; \
	  tar cf - \
	    $(DIRNAME)-$(VERSION)/README \
	    $(DIRNAME)-$(VERSION)/COPYING \
	    $(DIRNAME)-$(VERSION)/CHANGELOG \
	    $(DIRNAME)-$(VERSION)/tools/threadcpu \
	    $(DIRNAME)-$(VERSION)/*/*.1.gz \
	    $(DIRNAME)-$(VERSION)/*/*.1.html \
	    | gzip -9 -f >$(DIRNAME)-$(VERSION)-bin.tar.gz; \
	  $(RM) -rf $(DIRNAME)-$(VERSION)

rpm: tgz
	$(CP) $(name).spec $(RPM_SPECS_DIR)/
	cd ..;$(CP) $(DIRNAME)-$(VERSION).tar.gz $(RPM_SOURCES_DIR)/
	cd $(RPM_SPECS_DIR); rpmbuild -ba --with diet $(name).spec --nodeps

manhtml:
	rman -v # PolyglotMan
	for f in */*.[1];do echo $$f;rman -f html $$f|sed '/HREF="[^#]/s,A HREF,/A HREF,' >$$f.html;touch -r $$f $$f.html;done

manhtml-clean:
	$(RM) -f */*.[1].html

savets: distclean
	find . -type f -o -type d|egrep -v "^\.\/\.git|\.svn\/|\.svn$$|\.swp$$"|grep -v "\.timestamps"|sort|while read f;do echo $$(date +%s -r "$$f") "$$f";done >.timestamps

restorets:
	while read ts f;do touch -d@$$ts "$$f";done<.timestamps

