#!/bin/sh
#
# Copyright (c) International Business Machines  Corp., 2000
#  06/01 Robbie Williamson (robbiew@us.ibm.com)
# Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#---------------------------------------------------------------------------
#
# Test the basic functionality of the `ld` command.
#

CC=${CC:=gcc}
LD=${LD:=ld}

TST_CNT=5
TST_TESTFUNC=test
TST_SETUP=setup
TST_NEEDS_TMPDIR=1
TST_NEEDS_CMDS="$CC $LD"
. tst_test.sh

setup()
{
	for i in rf1 f1 rd1 d1 main; do
		ROD $CC -fPIC -c -o ${i}.o $TST_DATAROOT/${i}.c
	done
}

test1()
{
	EXPECT_FAIL $LD x.o y.o 2\> ld.out

	if grep -q "$LD:.*[xy]\.o.*No such file or directory" ld.out; then
		tst_res TPASS "Missing files were reported"
	else
		tst_res TFAIL "Missing files were not reported"
		cat ld.out
	fi
}

test2()
{
	EXPECT_FAIL $CC x.o y.o 2\> cc.out

	if grep -q "$CC:.*[xy]\.o.*No such file or directory" cc.out; then
		tst_res TPASS "Missing files were reported"
	else
		tst_res TFAIL "Missing files were not reported"
		cat cc.out
	fi
}

test3()
{
	EXPECT_PASS $LD -shared f1.o d1.o -o test.so

	if file test.so |grep -q shared; then
		tst_res TPASS "Shared library could be build"
	else
		tst_res TFAIL "Failed to build shared library"
	fi
}

test4()
{
	EXPECT_PASS $LD -Bdynamic -shared f1.o d1.o -o test.so
	EXPECT_FAIL $LD -Bstatic -L. main.o rd1.o test.so -o a.out
}

test5()
{
	EXPECT_PASS $LD -Bdynamic -shared main.o f1.o rf1.o -o test.so -L/usr/lib/
	EXPECT_FAIL $LD -Bstatic -r main.o f1.o rf1.o test.so -L/usr/lib/ 2\> ld.out
	cat ld.out

	if grep -q "$LD: attempted static link of dynamic object" ld.out; then
		tst_res TPASS "Got expected error message"
	else
		tst_res TFAIL "Unexpected error message"
		cat ld.out
	fi
}

tst_run