1#! /bin/csh
2#
3#   PC-LINUX 64bit version.
4#
5#   This script is a more or less generic library/executable
6#   builder for CSPICE products.  It assumes that it is executed
7#   from one of the "product" directories in a tree that looks like
8#   the one displayed below:
9#
10#                      package
11#                         |
12#                         |
13#       +------+------+------+------+------+
14#       |      |      |      |      |      |
15#     data    doc    etc    exe    lib    src
16#                                          |
17#                                          |
18#                         +----------+----------+------- ... ------+
19#                         |          |          |                  |
20#                     product_1  product_2  product_3    ...   product_n
21#
22#   Here's the basic strategy:
23#
24#     1)  Compile all of the .c files in the current directory
25#
26#     2)  If there are no .pgm files in the current directory this
27#         is assumed to be a library source directory.  The name
28#         of the library is the same as the name of the product.
29#         The library is placed in the "lib" directory in the tree
30#         above.  The script is then done.
31#
32#         If there are .pgm files and there were some .c
33#         files compiled the objects are gathered together in the
34#         current directory into a library called locallib.a.
35#
36#     3)  If any *.pgm files exist in the current directory, compile
37#         them and add their objects to locallib.a.  Create a C main
38#         program file from the uniform CSPICE main program main.x.
39#         Compile this main program and link its object with locallib.a,
40#         ../../cspice.a and ../../csupport.a. The output
41#         executables have an empty extension.  The executables are
42#         placed in the "exe" directory in the tree above.
43#
44#   The environment variable TKCOMPILEOPTIONS containing compile options
45#   is optionally set. If it is set prior to executing this script,
46#   those options are used. It it is not set, it is set within this
47#   script as a local variable.
48#
49#   References:
50#   ===========
51#
52#   "Unix Power Tools", page 11.02
53#      Use the "\" character to unalias a command temporarily.
54#
55#   "A Practical Guide to the Unix System"
56#
57#   "The Unix C Shell Field Guide"
58#
59#   Change History:
60#   ===============
61#
62#   Version 6.2.0  Nov. 14, 2006  Boris Semenov
63#
64#      Added -fPIC compile option.
65#
66#   Version 6.1.0  November 13, 2006  Boris Semenov
67#
68#      Updated for 64bit. Put -O2 back in.
69#
70#   Version 6.1.0  October 6, 2005  Boris Semenov
71#
72#      Put -O2 optimization back in because the problem that it caused
73#      was solved in the N0059 toolkit.
74#
75#   Version 6.0.0  April 20, 2000  Bill Taber
76#
77#      Removed O2 optimization as it caused some loops to
78#      not terminate.
79#
80#   Version 5.0.0  Feb. 09, 1999  Nat Bachman
81#
82#      Now uses O2 optimization.
83#
84#   Version 4.0.0  Nov. 02, 1998  Nat Bachman
85#
86#      Updated to use an environment variable to designate the C
87#      compiler to use.
88#
89#   Version 3.0.0  Oct. 31, 1998  Nat Bachman
90#
91#      Updated to make use of uniform C main routine main.x.
92#
93#   Version 2.0.0  Feb. 04, 1998  Nat Bachman
94#
95#      Modified to handle C code.  Sun/Solaris/Native cc Version.
96#
97#   Version 1.0.0  Dec  8, 1995  Bill Taber
98#
99
100
101#
102#   If there are any main programs in the directory, prepare them
103#   for use together with the "uniform" main.x routine.  We copy
104#   each main program to a file whose name terminates in _main.c.
105#   We then make a copy of main.x having its name made of the tail of
106#   the original .pgm file and an extension of .px.  When we compile
107#   the main programs, we'll look for this .px extension rather than
108#   the orginal .pgm.
109#
110\ls *.pgm >& /dev/null
111
112if ( $status == 0 ) then
113
114   echo " "
115
116   foreach MAIN ( *.pgm )
117
118#
119#     Copy the orginal source file for the main program into a regular
120#     source file which will be included in the local library.
121#
122#     Create a "main" source file having the name <product>.px
123#     from the generic main program source file main.x.
124#
125      set STEM    = $MAIN:r
126      set TARGET  = $STEM.px
127
128      \cp $MAIN  "$STEM"_main.c
129      \cp main.x  $TARGET
130
131endif
132
133
134#
135#  Choose your compiler.
136#
137if ( $?TKCOMPILER ) then
138
139   echo " "
140   echo "      Using compiler: "
141   echo "      $TKCOMPILER"
142
143else
144
145   set TKCOMPILER  =  "gcc"
146   echo " "
147   echo "      Setting default compiler:"
148   echo $TKCOMPILER
149
150endif
151
152
153#
154#  What compile options do we want to use? If they were
155#  set somewhere else, use those values.  The same goes
156#  for link options.
157#
158if ( $?TKCOMPILEOPTIONS ) then
159   echo " "
160   echo "      Using compile options: "
161   echo "      $TKCOMPILEOPTIONS"
162else
163#
164#  Options:
165#
166#     -ansi              Compile source as ANSI C
167#
168#     -DNON_UNIX_STDIO   Don't assume standard Unix stdio.h
169#                        implementation
170#
171#
172   set TKCOMPILEOPTIONS = "-c -ansi -m64 -O2 -fPIC -DNON_UNIX_STDIO"
173   echo " "
174   echo "      Setting default compile options:"
175   echo "      $TKCOMPILEOPTIONS"
176endif
177
178if ( $?TKLINKOPTIONS ) then
179   echo " "
180   echo "      Using link options: "
181   echo "      $TKLINKOPTIONS"
182else
183   set TKLINKOPTIONS = "-lm -m64"
184   echo " "
185   echo "      Setting default link options:"
186   echo "      $TKLINKOPTIONS"
187endif
188
189echo " "
190
191#
192#   Determine a provisional LIBRARY name.
193#
194   foreach item ( `pwd` )
195      set LIBRARY = "../../lib/"$item:t
196   end
197
198#
199#  Are there any *.c files that need to be compiled?
200#
201\ls *.c >& /dev/null
202
203if ( $status == 0 ) then
204
205   foreach SRCFILE ( *.c )
206      echo "      Compiling: "   $SRCFILE
207      $TKCOMPILER $TKCOMPILEOPTIONS $SRCFILE
208   end
209
210endif
211
212
213echo " "
214
215#
216#  If object files exist, we need to create an object library.
217#
218
219\ls *.pgm >& /dev/null
220
221if ( $status == 0 ) then
222   set LIBRARY = "locallib"
223endif
224
225\ls *.o >& /dev/null
226
227if ( $status == 0 ) then
228
229   echo "      Inserting objects in the library $LIBRARY ..."
230   ar  crv $LIBRARY.a *.o
231   ranlib  $LIBRARY.a
232   \rm                *.o
233   echo " "
234
235endif
236
237#
238#  If there are any main programs in the directory, compile
239#  them. If they have their own locallib.a link with it in addition
240#  to the default libraries.
241#
242
243\ls *.pgm >& /dev/null
244
245if ( $status == 0 ) then
246
247   echo " "
248
249   foreach MAIN ( *.px )
250
251      set STEM    = $MAIN:r
252      set TARGET  = $STEM.c
253      set MAINOBJ = $STEM.o
254      set EXECUT = ../../exe/$STEM
255
256      cp $MAIN $TARGET
257
258      echo "      Compiling and linking: " $MAIN
259
260      if ( -e locallib.a ) then
261
262         $TKCOMPILER    $TKCOMPILEOPTIONS $TARGET
263         $TKCOMPILER -o $EXECUT           $MAINOBJ             \
264                                          locallib.a           \
265                                          ../../lib/csupport.a \
266                                          ../../lib/cspice.a   \
267                                          $TKLINKOPTIONS
268
269         \rm $TARGET
270         \rm $MAINOBJ
271         \rm locallib.a
272
273      else
274
275         echo "Compiling and linking: "   $MAIN
276         $TKCOMPILER    $TKCOMPILEOPTIONS $TARGET
277         $TKCOMPILER -o $EXECUT           $MAINOBJ             \
278                                          ../../lib/csupport.a \
279                                          ../../lib/cspice.a   \
280                                         $TKLINKOPTIONS
281
282         \rm $TARGET
283         \rm $MAINOBJ
284
285      endif
286
287   end
288
289endif
290
291#
292#  Cleanup.
293#
294
295echo " "
296
297\ls *.o >& /dev/null
298
299if ( $status == 0 ) then
300   \rm *.o
301endif
302
303\ls *.px >& /dev/null
304
305if ( $status == 0 ) then
306   \rm *.px
307endif
308
309\ls *_main.c >& /dev/null
310
311if ( $status == 0 ) then
312   \rm *_main.c
313endif
314
315
316exit 0
317
318
319