# Copyright (C) 1992 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library 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; either version 2 of the
# License, or (at your option) any later version.

# The GNU C Library 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
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If
# not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#
# Makefile for libexec/libdl/tests directory; makes testcases.
# Donn Terry, Softway Systems, Inc.

# Things tested implicitly by this Makefile:
#  . rpath semantics
#  . soname semantics

SHELL =		/bin/sh

CFLAGS =	$(DEBUG_FLAGS) -O2
RANLIB =	@RANLIB@

DEBUG_FLAGS=-g

MACHINE_ARCH=alpha

UND=""""

CC = $$X/local_bin/gcc -B$$X/local_bin/ -D_ALL_SOURCE
CPP = $$X/local_bin/g++ -B $$X/local_bin/ -I $$X/libio -I $$X/egcs.source/libio -I $$X/local_bin
F77 = $$X/local_bin/g77 -B $$X/local_bin/

INCLUDES = -I. -I ${MACHINE_ARCH} -I./include
CODEFLAGS = -D__PIC__ -fpic -finline

# test case rules.

PROGS = testcase0 testcase1 testcase2 testcase3 testcase3pic testcase4 \
     fortmain cppmain fpmain lcmain weakmain artestcase artestcase1.so

all: $(PROGS)

check: all
	# Eliminate collect2 gripes (so we can use for both 2.7.2 and 2.9x)
	-LD_LIBRARY_PATH=$$X/local_bin ksh testit "$(CC)" "$(F77)" $(UND) >res 2>&1 | grep -v "collect2" || exit 0
	-diff expected res
	@echo "# of LOD" $$(diff expected res | wc -l)


testcase0: testcase0.o ../ld.so
	$(CC) -Wl,-Bdynamic -g -Wl,--rpath=. testcase0.o -o testcase0 \
	  -Wl,--image-base,0x021f0000 -Wl,--dynamic-linker,../ld.so

testcase0.o:
	echo 'main() {printf("hello world\n");exit(0);}' >testcase0.c
	$(CC) -g -c testcase0.c 

# We load testcase1 at the location where it gets put when run by
# ./ld.so ./testcase1
# This lets us test that ./ld.so is (minimally) not busted without having
# to install it.  Until we have a .reloc section, this is required to get
# everything to run correctly.
#
# (To find the new address: break at "Unexpected relocation..." in rtldfinish.c,# and examine (obviously) progMap->l_addr.)

testcase1: testcase1.o testcase1.so ../ld.so ../libdl.so
	$(CC) -Wl,-Bdynamic -g -Wl,--rpath=.. testcase1.o ../libdl.so -o testcase1 \
	  -Wl,--image-base,0x022e0000 -Wl,--dynamic-linker,../ld.so

testcase1.so: testcase1l.o ../ld.so ../libdl.so
	$(CC) -fpic -o testcase1.so --shared testcase1l.o

testcase1.o testcase1l.o:
	$(CC) -g -fpic -c testcase1l.c
	$(CC) -g -c -I .. testcase1.c

testcase2: testcase2a.so testcase2b.so ../ld.so ../libdl.so
	$(CC) -g -Wl,-Bdynamic -I.. testcase2.c ../libdl.so -Wl,--rpath=.. -o testcase2 -Wl,--dynamic-linker,../ld.so

testcase2a.so testcase2b.so:
	$(CC) -fpic -DRES=1 -o testcase2a.so --shared -g -I .. testcase2l.c 
	$(CC) -fpic -DRES=2 -o testcase2b.so --shared -g -I .. testcase2l.c

testcase3: libtest1.so bogus/libtest1.so caller.c ../ld.so
	$(CC) caller.c -c -g
	$(CC) caller.o -Wl,-Bdynamic -L. -ltest1 -g -o testcase3 -Wl,--dynamic-linker,../ld.so -Wl,--rpath=..

testcase3pic: libtest1.so bogus/libtest1.so caller.c ../ld.so
	$(CC) caller.c -fpic -c -g -o caller.pic.o
	$(CC) caller.pic.o -Wl,-Bdynamic -L. -ltest1 -g -o testcase3pic -Wl,--dynamic-linker,../ld.so -Wl,--rpath=..

artestcase: artestcase1.o ../libdl.a
	$(CC) --static -g artestcase1.o ../libdl.a -o artestcase

artestcase1.so: artestcase1l.o
	$(CC) --no-standard-libraries -fpic -o artestcase1.so --shared artestcase1l.o

artestcase1.o artestcase1l.o:
	$(CC) -g -fpic -c artestcase1l.c
	$(CC) -g -c -I .. artestcase1.c

libtest1.so:
	$(CC) -fpic -g -c called.c
	$(CC) --shared -Wl,--soname=libtest1.so -g called.o -o libtest1.so -Wl,--dynamic-linker,../ld.so

bogus/libtest1.so: called.c
	mkdir -p bogus
	$(CC) -fpic -g -c -DMAKE_BOGUS called.c
	$(CC) --shared -Wl,--soname=libtest1.so -g called.o -o bogus/libtest1.so
	
testcase4: testcase4.1.so testcase4.2.so testcase4.c ../ld.so
	$(CC) -fpic -g -c testcase4.c
	$(CC) testcase4.o -Wl,-Bdynamic testcase4.1.so testcase4.2.so -g  \
	    -rpath . -o testcase4 -Wl,--dynamic-linker,../ld.so

testcase4.1.so: testcase4.1.c
	$(CC) -fpic -g -c testcase4.1.c
	$(CC) --shared -g testcase4.1.o -o testcase4.1.so

testcase4.2.so: testcase4.2.c
	$(CC) -fpic -g -c testcase4.2.c
	$(CC) --shared -g testcase4.2.o -o testcase4.2.so

fortmain: fortlib.so ../ld.so fortmain.f
	$(F77) -g -c fortmain.f
	$(F77) -Wl,-Bdynamic -g fortmain.o -rpath . fortlib.so -o fortmain -Wl,--dynamic-linker,../ld.so
	$(F77) -Wl,-Bdynamic -g -rpath . fortlib.so fortmain.o -o fortmainsw -Wl,--dynamic-linker,../ld.so

fortlib.so: ../ld.so fortsub.f
	$(F77) -fpic -g -c fortsub.f
	$(F77) -Wl,-Bdynamic --shared -g fortsub.o -o fortlib.so \
	    -Wl,--dynamic-linker,../ld.so

cppmain: cppmain.cc cpplib.so ../ld.so
	$(CPP) -g -c cppmain.cc
	$(CPP) -Wl,-Bdynamic -g cppmain.o -rpath . cpplib.so -o cppmain  -Wl,--dynamic-linker,../ld.so
	$(CPP) -Wl,-Bdynamic -g -rpath . cpplib.so cppmain.o -o cppmainsw  -Wl,--dynamic-linker,../ld.so

cpplib.so: cppsub.cc ../ld.so
	$(CPP) -fpic -g -c cppsub.cc
	$(CPP) -Wl,-Bdynamic --shared -g cppsub.o -o cpplib.so \
	    -Wl,--dynamic-linker,../ld.so

fpmain: fpmain.c fplib1.so fplib2.so fplib3.so ../ld.so
	$(CC) -g -c fpmain.c
	$(CC) -Wl,-Bdynamic -g fpmain.o -rpath . fplib1.so fplib2.so fplib3.so -o fpmain -Wl,--dynamic-linker,../ld.so

#intentionally not PIC
fplib1.so: fp1.c ../ld.so
	$(CC) -g -c fp1.c
	$(CC) -Wl,-Bdynamic --shared -g fp1.o -o fplib1.so \
	    -Wl,--dynamic-linker,../ld.so

#intentionally not PIC
fplib2.so: fp2.c ../ld.so
	$(CC) -g -c fp2.c
	$(CC) -Wl,-Bdynamic --shared -g fp2.o -o fplib2.so \
	    -Wl,--dynamic-linker,../ld.so

#intentionally not PIC
fplib3.so: fp3.c ../ld.so
	$(CC) -g -c fp3.c
	$(CC) -Wl,-Bdynamic --shared -g fp3.o -o fplib3.so \
	    -Wl,--dynamic-linker,../ld.so -Wl,-Bsymbolic
	
# Altho the Makefile is dependent on lclib2, the link line is NOT
lcmain: lcmain.c lclib1.so lclib2.so ../ld.so
	$(CC) -g -c lcmain.c
	$(CC) -Wl,-Bdynamic -g lcmain.o -rpath . lclib1.so -o lcmain -Wl,--dynamic-linker,../ld.so

lclib1.so: lc1.c lclib2.so ../ld.so
	$(CC) -g -c lc1.c
	$(CC) -Wl,-Bdynamic --shared -g lc1.o -o lclib1.so lclib2.so \
	    -Wl,--dynamic-linker,../ld.so

# tests --symbolic option (strictly a syntatic check in this case)
lclib2.so: lc2.c ../ld.so
	$(CC) -g -c lc2.c
	$(CC) -Wl,-Bdynamic --shared -g lc2.o -o lclib2.so \
	    -Wl,--dynamic-linker,../ld.so --symbolic

weakmain:
	$(CC) -g -c _weak.c weak_a.s weakdriver.c strong.c callweak.c \
	    weak_d_a.s strong_d.c _weak_d.c
	$(CC) -v -Wl,-Bdynamic --shared -g callweak.o _weak.o weak_a.o \
	    strong.o weak_d_a.o strong_d.o _weak_d.o -o weak.so 
	$(CC) -v -Wl,-Bdynamic -g -Wl,--rpath=. weakdriver.o weak.so \
	  -Wl,--dynamic-linker,../ld.so -o weakmain \

#.c.o:
#	$(CC) $(CFLAGS) $(INCLUDES) $(CODEFLAGS) -c $< 

mostlyclean clean:
	rm -f *.a *.o core errs *~ \#* TAGS *.E a.out errors 
	rm -f *.so
	rm -f $(PROGS)

distclean:	clean 
	rm -f config.cache config.log config.status
	rm -f Makefile depend

Makefile:	Makefile.in config.status
		$(SHELL) config.status

config.status:	configure
		$(SHELL) config.status --recheck

