#!/usr/bin/sh

# 
# Prospect: a developer's system profiler.
#
# COPYRIGHT (C) 2001-2004 Hewlett-Packard Company
#
# Author: Alex Tsariounov, HP
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#

# $Id: Makefile,v 1.19 2004/01/09 20:29:27 type2 Exp $

# 
# Other binary modules prospect needs
# and the link line
#
DASM = ../dass_gdb/dass.o
DTREE = ../dtree/dtree.o
USR_LF = $(DTREE) $(DASM)

#
#
# Prospect Objects
#
USR_O = \
	malloc_debug.o \
	f_sym_name.o \
	incache.o \
	ascii_report.o \
	xml_report.o \
	opt_proc.o \
	linux_model.o \
	linux_module.o \
	rec_proc.o \
	bfile.o \
	prospect.o 

#
# The prospect executable
#
USR_EXE = prospect symbols

#
# Prospect clean targets
#
USR_CLN = $(USR_O) $(USR_EXE)

#
# Optimization
#
ifeq ($(FLAVOR),DEBUG)
USR_OF = -g 
else
USR_OF = -O2 -g 
endif

#
# Translation of user flags into acutal flags used.
# COMPILATION/LINK FLAGS definitions:
#
# DF = Ansi-C flags. Exemple: "-Ae" = ANSI-C/HP Extensions flag
# AF = C compiler architectural flag 
# SF = C compiler scheduler flag [see /opt/langtools/lib/sched.models].
# PF = Preprocessor flags
# UF = Undefed flags
# IF = Include directories flags
# GF = Profiling flags
# OF = Optimization/Debug flags
# CF = Compilation flags [Note: $(USR_CF) is prefered at the beginning].
# LF = Link flags [Note: $(USR_LF) has to be located at the end].
#
AF = $(USR_AF)
SF = -Wall $(USR_SF)
DF = $(USR_DF)
PF = $(USR_PF)
UF = $(USR_UF)
IF = $(USR_IF)
GF = $(USR_GF)
OF = $(USR_OF)
CF = $(USR_CF) $(DF) $(AF) $(SF) $(PF) $(UF) $(IF) $(GF) $(OF)
# don't distribute the efence build by mistake
#LF = -lefence -static -lelf -lm $(USR_LF)
#
LF = /usr/lib/libelf.a -lm -lc $(USR_LF)

ifeq ($(FLAVOR),static)
LF = -static -lelf -lm -lc $(USR_LF)
endif

#
# Files to act on definitions
#
.CLN = $(USR_CLN) *.i *.sl *.pic *.a 
.O  = $(USR_O) 
.C = $(.O:.o=.c) $(USR_C)
.S = $(USR_S) 


#
# Unix commands definitions
#
SUIDPROG = /usr/bin/sudo
CPF    = cp -f
RMF    = rm -f
MKDIR  = mkdir
FIND   = find
SCAN   = /usr/local/bin/cscope
CAT    = cat
CHMOD  = chmod
CHOWN  = chown
LN     = ln
AWK    = awk

#
# System include files dir
#
INCLUDES = /usr/include


################################################################################
#
#                      PROSPECT SPECIFIC TARGETS
#
################################################################################
all: $(USR_EXE)

$(DTREE):
	@echo ~~~~~~~~~~ Building ../dtree:
	@(cd ../dtree ; make)

$(DASM):
	@echo ~~~~~~~~~~ Building ../dass_gdb:
	@(cd ../dass_gdb ; make)

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

.s.o:
	@echo $@:
	$(CC) $(CF) -c $< -o $@

prospect: $(.O) $(DTREE) $(DASM)
	@echo $@:
	$(RMF) prospect 
	$(CC) $(CF) $(.O) -o $@ $(LF)
	$(MAKE) root
#	$(MAKE) scan

#
# Build the app_perf-n.h file from the app-perf.dtd file
#
app_perf-n.h: app-perf.dtd Makefile
	@echo $@:
	@$(AWK) 'BEGIN {                                                     \
           printf("/*\n");                                                   \
           printf(" * This file generated by Makefile from app-perf.dtd\n"); \
           printf(" * Do not hand edit - a make may overwrite.\n */\n");    \
           printf("static char *dtd_file[] = {\n");                         \
        }                                                                    \
	{                                                                \
            gsub(/\"/, "\\\"");                                              \
            printf("    \"%s\\n\",\n", $$0);                                 \
        }                                                                    \
        END {                                                                \
           printf("    NULL\n");                                             \
           printf("};\n");                                                   \
           printf("/* end of file */\n");                                  \
        }' < app-perf.dtd > $@

################################################################################
#
#                      OTHER USEFULL TARGETS
#
################################################################################

root: 
	@echo $@:
	@if \
		test -f $(SUIDPROG) ; \
	then \
		$(SUIDPROG) $(MAKE) root_exec ; \
	else \
		echo "\t**** -> $(SUIDPROG) not found, prospect NOT root suid'ed."; \
	fi

root_exec:
	$(CHOWN) root prospect
	$(CHMOD) 4555 prospect

install: 
	$(CPF) prospect /usr/local/bin/ 
	$(MKDIR) -p /usr/local/man/man1
	$(CPF) ../docs/prospect.1 /usr/local/man/man1/
	$(CHOWN) root /usr/local/bin/prospect
	$(CHMOD) 4555 /usr/local/bin/prospect

uninstall:
	$(RMF) /usr/local/bin/prospect
	$(RMF) /usr/local/man/man1/prospect.1

symbols: f_sym_name.c malloc_debug.o malloc_debug.h  f_sym_name.h
	@echo $@:
	$(CC) $(CF) -DEXAMPLE -DDEBUG1 -DDEBUG4 f_sym_name.c malloc_debug.o -o symbols $(LF)

clean:
	@echo $@:
	$(RMF) $(.CLN) 

distclean:
	@echo $@:
	$(RMF) $(.CLN) app_perf-n.h out* *.bak *~ cscope.out tags

scan: $(.H) $(.C) $(.S) 
	@echo $@:
	@if \
		test -f $(SCAN) ; \
	then \
		$(SCAN) $? ; echo "\tscan.out updated" ; \
	else \
		echo "\t$(SCAN) not found"; \
	fi

dep: depend
depend:
	make app_perf-n.h
	makedepend -Y../dtree -I../dass_gdb *.c 


################################################################################
#
#                      END OF PROSPECT MAKEFILE
#
################################################################################
###
# DO NOT DELETE

ascii_report.o: ../dtree/dtree.h prospect.h malloc_debug.h linux_model.h
ascii_report.o: f_sym_name.h linux_module.h incache.h
bfile.o: bfile.h linux_module.h prospect.h malloc_debug.h linux_model.h
bfile.o: f_sym_name.h
f_sym_name.o: prospect.h malloc_debug.h linux_model.h f_sym_name.h
f_sym_name.o: linux_module.h
incache.o: incache.h f_sym_name.h malloc_debug.h linux_model.h prospect.h
incache.o: linux_module.h ../dass_gdb/dass.h
linux_model.o: ../dtree/dtree.h prospect.h malloc_debug.h linux_model.h
linux_model.o: f_sym_name.h linux_module.h
linux_module.o: prospect.h malloc_debug.h linux_model.h f_sym_name.h
linux_module.o: linux_module.h rec_proc.h version.h bfile.h
malloc_debug.o: ../dtree/dtree.h malloc_debug.h
opt_proc.o: version.h prospect.h malloc_debug.h linux_model.h f_sym_name.h
opt_proc.o: linux_module.h opt_proc.h xml_report.h
prospect.o: version.h prospect.h malloc_debug.h linux_model.h f_sym_name.h
prospect.o: linux_module.h opt_proc.h rec_proc.h incache.h
rec_proc.o: ../dtree/dtree.h prospect.h malloc_debug.h linux_model.h
rec_proc.o: f_sym_name.h linux_module.h rec_proc.h ascii_report.h
xml_report.o: prospect.h malloc_debug.h linux_model.h f_sym_name.h
xml_report.o: linux_module.h rec_proc.h app_perf-n.h
