File: plld.sh

package info (click to toggle)
swi-prolog 3.1.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,772 kB
  • ctags: 12,869
  • sloc: ansic: 43,657; perl: 12,577; lisp: 4,359; sh: 1,534; makefile: 798; awk: 14
file content (159 lines) | stat: -rwxr-xr-x 3,663 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/sh
################################################################
# plld -- link C and Prolog files to a stand-alone executable
#
# Author: Jan Wielemaker
# E-mail: jan@swi.psy.uva.nl
#
# Copyright 1996 University of Amsterdam, all rights reserved
#
# For documentation, see the SWI-Prolog manual and man plld
################################################################

program="$0"

PL=pl
LD=
clibs=
clibdirs=
ldflags=
cfiles=
cflags="-D__SWI_PROLOG__ -D__SWI_EMBEDDED__"
plfiles=
plout="pl.out$$"			# temporary file for saved-state
cout="c.out$$"				# temporary file for emulator
out="a.out"				# the real output file
plgoal='$welcome'			# initial goal (banner)
pltoplevel=prolog			# toplevel goal
plinitfile=none				# file to load at start
makestate=true
verbose=

PLLD=true				# for sub-programs
export PLLD

usage()
{ cat << _EOM_
usage:  $program -help"
	$program [options] c-files ... pl-files ..."

options:
	-o out		final output file (default = a.out)
	-goal goal	use 'goal' as the (Prolog) entry-point
	-toplevel goal	use 'goal' as the (Prolog) toplevel (default = prolog)
	-pl prolog	SWI-Prolog executable (default = pl)
	-ld linker	linker to be used (default = <CC used for prolog>)
	-nostate	Do not create a saved state: just link the foreign
			code to the Prolog kernel.
	-initfile file	use 'file' as profile file (default = none)
	-v		verbose: print commands executed

Dispatching:
	-g | -I* | -D* | -l* | -L* | *.o | *.c | *.C | *.cpp | *.cxx:
			arguments matching these patterns are passed to
			the C-compiler
	*.pl | *.qlf:
			arguments matching these patterns are passed to
			Prolog
_EOM_
}


while [ -n "$1" ]; do
  case "$1" in
    -help)
	usage;
	exit 0;;
    -nostate)
	makestate=false;;
    -goal)
	plgoal="$2";
	shift;;
    -toplevel)
	pltoplevel="$2";
	shift;;
    -initfile)
	plinitfile="$2";
	shift;;
    -o)
	out="$2";
	shift;;
    -pl)
	PL="$2";
	shift;;
    -ld)
	LD="$2";
	shift;;
    -v)
        verbose=true;;
    -l*)
	clibs="$clibs $1";;
    -L*)
        clibdirs="$clibdirs $1";;
    -g|-I*|-D*)
	cflags="$cflags $1";;
    *.o|*.c|*.C|*.cxx|*.cpp)
	cfiles="$cfiles $1";;
    *.pl|*.qlf)
        if [ -z "$plfiles" ]; then
	  plfiles="'$1'"
	else
	  plfiles="$plfiles, '$1'"
	fi;;
    *)	
	ldflags="$ldflags $1";;
  esac
  shift
done

if [ "$verbose" = true ]; then echo "%% $PL -dump-runtime-variables"; fi
if eval `$PL -dump-runtime-variables`; then
  true
else
  echo "plld: failed to get runtime variables from $PL"
  exit 1
fi

pllibdir="$PLBASE/runtime/$PLARCH"
ldflags="$PLLDFLAGS $ldflags"

cleanup ()
{ if [ "$verbose" = true ]; then echo "%% rm -f $cout $plout"; fi
  rm -f $cout $plout;
}

error()
{ cleanup;
  exit 1;
}

trap error 1 2 15

if [ "$makestate" = false ]; then cout="$out"; fi
if [ -z "$LD" ]; then LD="$CC"; fi

LIBS="-L$pllibdir $clibdirs $clibs -lpl $PLLIBS"
ccmd="$LD -o $cout -I$PLBASE/include $cflags $cfiles $ldflags $LIBS"
plflags="-O -f none -F none -g true"

if [ "$verbose" = true ]; then echo "%% $ccmd"; fi
if $ccmd; then true; else error; fi

if [ "$makestate" = false ]; then
  exit 0;
fi

if [ "$verbose" = true ]; then
  echo "%% $PL $plflags -t "'"'"consult([$plfiles]),qsave_program('$plout',[goal='$plgoal',toplevel='$pltoplevel',init_file='$plinitfile'])"'"';
fi
if $PL $plflags -t "consult([$plfiles]),qsave_program('$plout',[goal='$plgoal',toplevel='$pltoplevel',init_file='$plinitfile'])"; then
  true
else
  error
fi
if [ "$verbose" = true ]; then echo "%% cat $cout $plout > $out"; fi
if cat $cout $plout > $out; then true; else error; fi
if [ "$verbose" = true ]; then echo "%% chmod +x $out"; fi
if chmod +x $out; then true; else error; fi
cleanup
exit 0