change charm to not build shared library form by default. build with --build-shared...
[charm.git] / src / scripts / charmc
1 #!/bin/sh
2 ##############################################################################
3 # charmc: Compiler for Converse/Charm/Charm++ programs
4 #
5 # Converted to Bourne Shell by Orion Sky Lawlor, 10/21/1999
6 #
7 # Initialize the main controlling variables, setup error handler
8 # ALL variables used by this script should be initialized here.
9 #
10 ##############################################################################
11
12 #Prepare aliases for the external commands we use--
13 # Note that adding /bin/ to everything breaks Cygwin32.
14 RM="rm -f"
15 CP="cp -p"
16 MV="mv"
17 LN="ln"
18 WC="wc"
19 SED="sed"
20
21 MACHTYPE=""
22 SEQUENTIAL=""
23 NATIVE=""
24 LANGUAGE=""
25 BALANCE="rand"
26 TRACEMODE=""
27 TRACE_OBJ=
28 MEMORY="default"
29 THREAD="default"
30 VERBOSE=""
31 DEBUG_SCRIPT=""
32 SAVE=""
33 PURIFY=""
34 SKIPLINK=""
35 OVERRIDE_CC=""
36 OVERRIDE_CXX=""
37 OVERRIDE_F90=""
38 OVERRIDE_LD=""
39 OVERRIDE_LDXX=""
40 #Generic options, passed to everything
41 OPTS=""
42 #Specific options
43 #charm internal flag, only pass to c and cxx, not f90
44 OPTS_CPP_INTERNAL="-D__CHARMC__=1 "
45 OPTS_CPP=
46 OPTS_LDRO="" 
47 OPTS_CC=""
48 OPTS_CXX="" 
49 OPTS_F90=""
50 OPTS_LD=""  
51 OPTS_LDXX=""
52 OPTS_PIC=""
53 USE_PIC="0"
54
55 CHARMDEBUG=""
56 LINK_PYTHON=""
57 OBJECT=""
58 EXPLICIT_OBJECT="no"
59 EXPLICIT_COMPILATION="no"
60 POST_LANGUAGE=""
61 POST_LIBRARIES=""    
62 PRE_LIBRARIES=""  
63 USE_F90_LIBRARIES="0"  
64 USE_F77_LIBRARIES="0"  
65 F90_MODDIR=
66 AMPIMAIN=""
67 COPYTO=""        
68 MODCOPIES=""        
69 MODULES=""
70 BALANCER=""
71 FILES=""       
72 OBJECTFILES=""  
73 DELETE=""    
74 MAINOBJ=""   
75 GENCPM=""
76 OPTIMIZE_MODE=""
77 PRODUCTION_MODE=""
78 USE_RELIABLE_CC=""
79 USE_FASTEST_CC=""
80 PROG_EXT=""
81 GENDEPENDS="no"
82
83 COPY_CHARMRUN=true
84 USE_BUILD_OPTIONS=0
85
86 COPY_EXIT="true"
87 CHARM_SHARED="0"
88 BUILD_SHARE="0"
89 SWAPGLOBALS="0"
90
91 TAU_MAKEFILE=Make.tau
92 TAU_TRACE_MPI=1
93
94 CMK_CELL=0
95 SPU_USE_CPP=0
96 SPU_EXTRA_LIBS=""
97
98 ####################################################################
99 #
100 #  Utility routines used below
101 #
102 ###################################################################
103
104 # PrintUsage: prints a helpful command-line usage message and quits
105 # Args: any additional messages
106 printUsage() {
107     printVersion
108     echo 
109     echo "Usage: charmc [ flags ] <files>"
110     echo
111     echo "  flags:"
112     echo "        -o <target> -g -O"
113     echo "        -D<define> -I<include path> -L<lib path> -l<library> -s"
114     echo "        -machine <mach> -seq -host -language <lang> "
115     echo "        -balance <mode> -tracemode <mode> -memory <mode>"
116     echo "        -modules <comma-separated list of C++ modules>"
117     echo "        -verbose -save -purify -cp <dest> -gen-cpm <package>"
118     echo "        -use-reliable-cc -use-fastest-cc"
119     echo "        -cc <new cc> -c++ <new CC>  -ld <new ld> -ld++ <new LD>"
120     echo "        -cpp-option <opt> -ldro-option <opt> -cc-option <opt>"
121     echo "        -c++-option <opt> -ld-option <opt> -ld++-option <opt>"
122     echo "        -fmoddir <f90mod path>"
123     echo "        -pg <opt> -build-shared -charm-shared -no-trace-mpi"
124     echo
125     echo "  Cell specific flags:"
126     echo "        -spu-cpp -spu-lib <libname>"
127     echo
128     echo "  Charmc compiles C, C++, f77, f90, Converse, Converse++, Charm, "
129     echo "and Charm++ programs.  The flags generally match those of cc or f77."
130     echo "Version 5.0, Parallel Programming Lab, UIUC, 1999"
131     echo $*
132     exit 1
133 }
134
135 # End blows away the temporary files (unless SAVE is true) and exits
136 # Args: <exit code>
137 End() {
138     if [ -z "$SAVE" ]
139         then
140         for FILE in `echo $FILES`
141         do
142             #BASE=`stripExtention $FILE`
143             TMP=`basename $FILE`".TMP"
144             $RM $TMP.P.C $TMP.cpp $TMP.space $TMP.xlat $TMP.o
145             $RM $TMP.c $TMP.c.0.h $TMP.c.1.h $TMP.c.2.h
146             $RM $TMP.i $TMP.$CMK_CPP_SUFFIX
147         done
148         $RM core $DELETE
149         if test -n "$modInitObj"
150         then
151             [ -n "$VERBOSE" ] && echo $RM $modInitSrc $modInitObj
152             DoNoErrCheck $RM $modInitSrc $modInitObj > /dev/null 2>&1
153         fi
154     fi
155     exit $1
156 }
157
158 # Write this error message to stderr
159 # ("1>&2" redirects the echo output to stderr).
160 Warning() {
161         echo "$@" 1>&2
162 }
163
164 # This procedure prints an error message and exits.
165 # Args: written to stderr
166 Abort() {
167         Warning "Fatal Error by charmc in directory "`pwd`
168         Warning "   $@"
169         Warning "charmc exiting..."
170         End 1
171 }
172
173
174 # Instead of an ECHO_CMD variable, I define a procedure Do,
175 # which (possibly) echos, runs, and tests the errors of the given command.
176 # Args: executed as given
177 DoNoErrCheck() {
178         [ -n "$VERBOSE" ] && echo "charmc: Executing $@" 1>&2
179         eval "$@"
180 }
181
182 Do() {
183         DoNoErrCheck "$@"
184         Do_res=$?
185 # The UNIX result code better be zero, or we die
186         [ $Do_res -eq 0 ] || Abort "Command $@ returned error code $Do_res"
187 }
188
189 # This procedure removes the extention (the ".c" in "./main.c") from
190 # its first argument, and prints the result. Unlike the builtin command
191 # basename, it keeps the directory path.
192 # Args: <name to strip>
193 stripExtention() {
194         se_base=`basename $1`
195 #       se_base=`echo $1 | awk -F/ '{print $NF}'`
196         se_strip=`echo $se_base | awk -F. '{ORS="";print $1;for (i=2;i<NF;i++) print "."$i}'`
197         se_ret=`echo $1 | awk -F/ '{ORS="";for (i=1;i<NF;i++) print $i"/"}'`"$se_strip"
198         echo $se_ret
199 }
200
201 # GetExtention returns the extention on the given file name
202 # or "" if none.  (e.g. "./bob/snack.c" returns ".c")
203 # Args: <name to find extention of>
204 getExtention() {
205 #       se_base=`echo $1 | awk -F/ '{print $NF}'`
206 #        se_ret=`echo $se_base | awk -F. '{if (NF<=1) print ""; else print "."$NF}'`
207 #       echo $se_ret
208         se_ret=`echo $1 | sed -e 's/.*\././'`
209         if test "$se_ret" = $1
210         then
211           echo ""
212         else
213           echo $se_ret
214         fi
215 }
216
217 Debugf() {
218         [ -n "$VERBOSE" ] && echo "charmc>" $@
219 }
220
221 Debug() {
222     if [ -n "$DEBUG_SCRIPT" ]
223     then
224         echo 
225         echo "------- Charmc Debugging info: $* -------"
226         echo "CHARMBIN=$CHARMBIN"
227         echo "CHARMINC=$CHARMINC"
228         echo "CHARMLIB=$CHARMLIB"
229         echo "FILES=$FILES"
230         echo "DELETE=$DELETE"
231         echo "OBJECT=$OBJECT"
232         echo "LANGUAGE=$LANGUAGE"
233 #       echo "Working directory="`pwd`
234     fi
235 }
236
237 # Return success if $1 is not in any of the other arguments
238 notInList() {
239         search=$1
240         shift
241         while [ $# -gt 0 ]
242         do
243                 if [ "$search" = "$1" ]
244                 then
245                         # It's already in there-- fail
246                         return 1
247                 fi
248                 shift
249         done
250         # It's missing--succeed
251         return 0
252 }
253
254 # Add arguments to our modules list, avoiding duplicates
255 AddModules() {
256     for M in "$@"
257     do
258       notInList $M $MODULES && MODULES="$MODULES $M"
259     done
260 }
261
262 # Add this word to this list, avoiding duplicates
263 AddList() {
264     ret=""
265     for M in "$@"
266     do
267       notInList $M $ret && ret="$ret $M"
268     done
269     echo $ret
270 }
271
272 # Append the last word to this list, avoiding duplicates
273 # the difference to previous AddList() is that when duplication happens,
274 # only keep the last one, instead of keeping the first one as in AddList()
275 # this helps to solve the multiple -lfoo in the link command. 
276 AppendList() {
277         ret=""
278         search=$1
279         shift
280         while [ $# -gt 0 ] 
281         do
282                 if notInList $search $@
283                 then
284                         ret="$ret $search"
285                 fi
286                 search=$1
287                 shift
288         done
289         echo $ret $search
290 }
291
292
293 # TEMP_BASE is appended with this script's process ID (PID),
294 # so multiple charmcs can run in parallel without overwriting
295 # each other's temporary files.
296 TEMP_BASE="/tmp/charmc_tmp.$$"
297
298 # Try to find CHARMBIN by looking in directory where charmc is
299 findCharmBin() {
300 if [ -z "$CHARMBIN" ]
301 then
302         SCRIPT=$1
303         CHARMBIN=`dirname $SCRIPT`
304 #       CHARMBIN=`cd $CHARMBIN; pwd`
305 # NOTE: this script no longer tries to follow soft links
306 # to find the "original" charmc-- if this is bad, 
307 # translate these lines to Bourne shell from C shell:
308 # -------------------- Begin C Shell ------------------------->
309 #    if ($SCRIPT:h != $SCRIPT:t) then
310 #        cd $SCRIPT:h
311 #        set SCRIPT=$SCRIPT:t
312 #    else
313 #        foreach dir ($path)
314 #            if (-x $dir/$SCRIPT && ! -d $dir/$SCRIPT) then
315 #                cd $dir
316 #                break
317 #            endif
318 #        end
319 #    endif
320 #    while (x`find $SCRIPT -type l -print` == x$SCRIPT)
321 #        set SCRIPT=`ls -al ./$SCRIPT:t | sed -e "s@.*-> @@"`
322 #        if ($SCRIPT:h != $SCRIPT:t) then
323 #            cd $SCRIPT:h
324 #            set SCRIPT=$SCRIPT:t
325 #        endif
326 #    end
327 #    set CHARMBIN=`pwd`
328 # <------------------ End C Shell -----------------------------
329 fi
330 }
331
332 printVersion()
333 {
334         findCharmBin $0
335         version=`cat $CHARMBIN/../include/VERSION`
336         echo Charm++ Version $version
337 }
338
339
340 ##############################################################################
341 #
342 # The following section identifies CHARMBIN, the charm binary-directory.
343 #
344 ##############################################################################
345
346 ORIGDIR=`pwd`
347
348 # Try to find CHARMBIN by looking in directory where charmc is
349
350 findCharmBin $0
351
352 UNAME=`uname -s`
353 FIRST_THREE=`uname -s | awk '{print substr($1,1,3)}' `
354 if [ "$FIRST_THREE" = "CYG" -o "$UNAME" = "Interix" ]
355 then
356 # Running on a windows system-- append .exe to file names
357         PROG_EXT=".exe"
358 fi
359
360 CHARMLIB="$CHARMBIN/../lib"
361 CHARMINC="$CHARMBIN/../include"
362 CHARMLIBSO=
363 if test -d "$CHARMBIN/../lib_so" 
364 then
365   CHARMLIBSO=`cd $CHARMBIN/../lib_so 2>/dev/null && pwd`
366   #getting absolute path is harder than thought because of symbolic links and ..
367   #ksh needs cd -P to resolve werid symbolic links, however -P is not portable
368   #csh is ok too if it exists 
369   test -z "$CHARMLIBSO" && CHARMLIBSO=`cd -P $CHARMBIN/../lib_so 2>/dev/null && pwd`
370   test -z "$CHARMLIBSO" && CHARMLIBSO=`csh -c "cd $CHARMBIN/../lib_so >& /dev/null && pwd"`
371   test -z "$CHARMLIBSO" && echo "$CHARMBIN/../lib_so: not found" && exit 1
372 fi
373
374 ##############################################################################
375 #
376 # Parse the arguments
377 #
378 # Don't do any analysis in here, just the parsing.
379 #
380 ##############################################################################
381
382 [ $# -eq 0 ] && printUsage "Error: No arguments given."
383
384 processArgs() {
385 while [ ! $# -eq 0 ]
386 do
387         arg="$1"
388         shift
389
390         case "$arg" in
391         "-V")
392                 printVersion
393                 exit 0
394                 ;;
395
396         "-machine")
397                 MACHTYPE="$1"
398                 shift
399                 ;;
400
401         "-seq")
402                 SEQUENTIAL=true
403                 ;;
404
405         "-host")
406                 NATIVE=true
407                 ;;
408
409         "-language")
410                 # Parse out some fake languages (that are actually modules)
411                 case "$1" in
412                 "ampi") AddModules tcharmmain ampi ;;
413                 "ampif") AddModules tcharmmain ampif ; USE_F90_LIBRARIES="1" ;;
414                 "armci") AddModules tcharmmain armci ;;
415                 "armcif") AddModules tcharmmain armci ; USE_F90_LIBRARIES="1" ;;
416                 "fem") AddModules tcharmmain fem ; AMPIMAIN="femmain";;
417                 "femf") AddModules tcharmmain fem ; AMPIMAIN="femmain"; USE_F90_LIBRARIES="1" ;;
418                 "ParFUM_TOPS") AddModules tcharmmain ParFUM ParFUM_TOPS ; AMPIMAIN="ParFUMmain";;
419     "ParFUM") AddModules tcharmmain ParFUM ; AMPIMAIN="ParFUMmain";;
420     "ParFUMf") AddModules tcharmmain ParFUM ; AMPIMAIN="ParFUMmain"; USE_F90_LIBRARIES="1" ;;
421                 "mblock") AddModules tcharmmain mblock ;;
422                 "mblockf") AddModules tcharmmain mblock ; USE_F90_LIBRARIES="1" ;;
423                 *) 
424                         LANGUAGE="$1"
425                         POST_LANGUAGE=1
426                         ;;
427                 esac
428                 shift
429                 ;;
430
431         "-debug")
432                 # Requested support from charmdebug: add some modules and libraries
433                 CHARMDEBUG=1
434                 MEMORY="charmdebug"
435                 ;;
436
437         "-module"|"-modules")
438                 AddModules `echo $1 | sed -e 's/,/ /g'`
439                 shift
440                 ;;
441
442         "-balancer")
443 #                test -n "$BALANCER" && Abort "More than one -balancer specified!"
444                 BALANCER="$BALANCER $1"
445                 mod=`echo $1| sed -e 's/:/ /' -e 's/,/ /g'`
446                 AddModules $mod
447                 shift
448                 ;;
449
450         "-balance")
451                 BALANCE="$1"
452                 shift
453                 ;;
454
455         "-queue")
456                 Warning "Warning: -queue currently being ignored."
457                 shift
458                 ;;
459
460         "-memory")
461                 MEMORY="$1"
462                 shift
463                 ;;
464
465         "-thread")
466                 THREAD="$1"
467                 shift
468                 ;;
469
470         "-tracemode")
471                 TRACEMODE_NEW=`AddList $TRACEMODE $1`
472                 if [ "$TRACEMODE_NEW" = "$TRACEMODE" ]
473                 then
474                         Warning "Warning: Duplicate tracemode $1 ignored"
475                 else
476                         TRACEMODE=$TRACEMODE_NEW
477                 fi
478                 shift
479                 ;;
480         "-no-trace-mpi")
481                 TAU_TRACE_MPI=0
482                 ;;
483         
484         "-swapglobal"|"-swapglobals")
485                 SWAPGLOBALS="1"
486                 USE_PIC="1"
487                 ;;
488
489         "-copyglobal"|"-copyglobals")
490                 SWAPGLOBALS="2"
491                 USE_PIC="1"
492                 ;;
493
494         "-tlsglobal"|"-tlsglobals")
495                 TLSGLOBALS="1"
496                 OPTS_CC="$OPTS_CC -mno-tls-direct-seg-refs"
497                 OPTS_CXX="$OPTS_CXX -mno-tls-direct-seg-refs"
498                 OPTS_F90="$OPTS_F90 -mno-tls-direct-seg-refs"
499                 OPTS_LD="$OPTS_LD -static -Wl,--allow-multiple-definition"
500                 OPTS_LDXX="$OPTS_LDXX -static -Wl,--allow-multiple-definition"
501                 ;;
502
503         "-verbose")
504                 echo "Verbose mode set"
505                 VERBOSE=true
506                 ;;
507
508         "-spu-cpp")
509                 SPU_USE_CPP=1
510                 ;;
511
512         "-spu-lib")
513                 SPU_EXTRA_LIBS="$SPU_EXTRA_LIBS -l$1"
514                 shift
515                 ;;
516
517         "-intrinsic")
518                 XI_INTERNAL=true
519                 ;;
520
521         "-debug-script")
522                 echo "Will give excessive charmc debugging output..."
523                 DEBUG_SCRIPT=true
524                 ;;
525
526         "-save")
527                 SAVE=true
528                 ;;
529
530         "-purify")
531                 PURIFY=true
532                 ;;
533
534         "-use-reliable-cc")
535                 USE_RELIABLE_CC=1
536                 ;;
537
538         "-use-fastest-cc")
539                 USE_FASTEST_CC=1
540                 ;;
541
542         "-cc")
543                 OVERRIDE_CC="$1"
544                 shift
545                 ;;
546
547         "-c++")
548                 OVERRIDE_CXX="$1"
549                 shift
550                 ;;
551
552         "-f90")
553                 OVERRIDE_F90="$1"
554                 shift
555                 ;;
556
557         "-ld")
558                 OVERRIDE_LD="$1"
559                 shift
560                 ;;
561
562         "-ld++")
563                 OVERRIDE_LDXX="$1"
564                 shift
565                 ;;
566
567         "-cpp-option")
568                 OPTS_CPP="$OPTS_CPP $1"
569                 shift
570                 ;;
571
572         "-ldro-option")
573                 OPTS_LDRO="$OPTS_LDRO $1"
574                 shift
575                 ;;
576
577         "-cc-option")
578                 OPTS_CC="$OPTS_CC $1"
579                 shift
580                 ;;
581
582         "-c++-option")
583                 OPTS_CXX="$OPTS_CXX $1"
584                 shift
585                 ;;
586
587         "-f90-option")
588                 OPTS_F90="$OPTS_F90 $1"
589                 shift
590                 ;;
591
592         "-ld-option")
593                 OPTS_LD="$OPTS_LD $1"
594                 shift
595                 ;;
596
597         "-ld++-option")
598                 OPTS_LDXX="$OPTS_LDXX $1"
599                 shift
600                 ;;
601
602         "-rpath")
603                 OPTS_LDXX="$OPTS_LDXX -rpath $1"
604                 shift
605                 ;;
606         
607         "-fortran"|"-flib"|"-flibs")
608                 USE_F90_LIBRARIES="1" 
609                 ;;
610         "-f77")
611                 USE_F77="1" 
612                 ;;
613         "-fortran77"|"-f77lib"|"-f77libs")
614                 USE_F77_LIBRARIES="1" 
615                 ;;
616         "-E")
617 # Run preprocessor only
618                 PREPROCESS="yes"
619                 SKIPLINK="yes"
620                 OPTS="$OPTS $arg"
621                 ;;
622         "-preprocess")
623 # Run preprocessor as an extra step, continue after preprocessing
624                 PREPROCESS="yes"
625                 ;;
626         "-P"|"-S")
627 # Run preprocessor/assembler only
628                 SKIPLINK="yes"
629                 OPTS="$OPTS $arg"
630                 ;;
631         "-M")
632                 SKIPLINK="yes"
633                 OPTS="$OPTS $arg"
634                 GENDEPENDS="yes"
635                 ;;
636         "-default-to-aout")
637                 if [ "$EXPLICIT_OBJECT $EXPLICIT_COMPILATION" = "no no" ]
638                 then
639                       OBJECT="a.out"
640                 fi
641                 ;;
642         "-c")
643                 if [ "$EXPLICIT_OBJECT" = "no" ]
644                 then
645                       OBJECT=""
646                 fi
647                 EXPLICIT_COMPILATION="yes"
648                 ;;
649
650         "-o")
651                 EXPLICIT_OBJECT="yes"
652                 OBJECT=$1
653                 shift
654                 ;;
655
656         "-cp")
657                 COPYTO="$1"
658                 shift
659                 ;;
660
661         "-cpmod")
662                 MODCOPIES="$1 $MODCOPIES"
663                 shift
664                 ;;
665
666         "-gen-cpm")
667                 GENCPM="$GENCPM $1"
668                 shift
669                 ;;
670
671         -I)
672                 OPTS_CPP="$OPTS_CPP -I$1"
673                 shift
674                 ;;
675
676         -D*|-I*)
677                 if echo "$arg" | grep ' '  > /dev/null 2>/dev/null 
678                 then
679                   OPTS_CPP="$OPTS_CPP "\"$arg\"
680                 else
681                   OPTS_CPP="$OPTS_CPP $arg"
682                 fi
683                 ;;
684
685         -LANG*)
686                 Warning "passing unrecognized option $arg to all compilers and linkers"
687                 OPTS="$OPTS $arg"
688                 ;;
689         -shared|-G)
690                 BUILD_SHARE="1"
691                 ;;
692         -L*|-shared|-G)
693                 OPTS_LD="$OPTS_LD $arg"
694                 ;;
695         -charm-shared|-cs)
696                 CHARM_SHARED="1"
697                 ;;
698         -build-shared)
699                 # used internally when building Charm++ to create lib_so
700                 # obsolete, do nothing since BUILD_SHARE is on by default
701                 BUILD_SHARE="1"
702                 ;;
703         -no-build-shared)
704                 # used internally not to build Charm++ lib_so
705                 BUILD_SHARE="0"
706                 ;;
707         "-optimize")
708                 OPTIMIZE_MODE=true
709                 ;;
710
711         "-no-optimize")
712                 OPTIMIZE_MODE=false
713                 ;;
714
715         "-production")
716                 PRODUCTION_MODE=true
717                 ;;
718         "-no-production")
719                 PRODUCTION_MODE=false
720                 ;;
721         
722         "-pg"|"-g"|-W*|-O*)
723                 OPTS="$OPTS $arg"
724                 ;;
725
726         "-fmoddir")
727                 F90_MODDIR="$F90_MODDIR $1"
728                 shift
729                 ;;
730
731         -l*|*.a)
732                 if [ -n "$POST_LANGUAGE" ]
733                 then
734                         POST_LIBRARIES=`AppendList $POST_LIBRARIES $arg`
735                 else
736                         PRE_LIBRARIES=`AppendList $PRE_LIBRARIES $arg`
737                 fi
738                 ;;
739
740         -s)
741                 OPTS_LD="$OPTS_LD   $arg"
742                 ;;
743         -no-charmrun)
744                 COPY_CHARMRUN=false
745                 ;;
746         -use-build-options)
747                 USE_BUILD_OPTIONS=1
748                 ;;
749         -h|--help)
750                 printUsage
751                 ;;
752         -f90main)
753                 F90_MAIN=1
754                 ;;
755         -nof90main)
756                 F90_MAIN=
757                 ;;
758         -*|+*)
759 #               Warning "passing unrecognized option $arg to all compilers and linkers"
760                 OPTS="$OPTS  $arg"
761                 ;;
762         *.*)
763                 [ -n "$VERBOSE" ] && echo "Adding file $arg..."
764                 FILES="$FILES $arg"
765                 ;;
766 # Default
767         *)
768 #               printUsage  "Error: Unrecognized argument $arg"
769 #               Warning "passing unrecognized option $arg to all compilers and linkers"
770                 OPTS="$OPTS  $arg"
771                 ;;
772         esac
773 done
774 }
775
776 trap 'End 1' 2
777
778 # Process original command-line arguments
779 eval processArgs "$@"
780
781
782 ##############################################################################
783 #
784 # Load module dependencies.
785 #
786 ##############################################################################
787
788 Debug "About to read machine config script"
789 Debug "Read config script, setting defaults..."
790
791 [ ! -r $CHARMINC/conv-config.sh ] && Abort "Cannot find conv-config.sh in $CHARMINC"
792 . $CHARMINC/conv-config.sh $CHARMINC
793
794 Debug "Setting vars..."
795
796 if [ "$USE_BUILD_OPTIONS" = "1" ]  
797 then
798         [ -n "$VERBOSE" ] && echo "Charmc applying build time options: $BUILDOPTS"
799         OPTS="$OPTS $BUILDOPTS"
800 fi
801
802 # If we are debugging, add "-g -O0" to the C/C++ flags
803 if [ -n "$CHARMDEBUG" ]; then
804        if [ -n "$CMK_BUILD_PYTHON" ]; then
805                 AddModules PythonCCS charmdebug_python
806                 POST_LIBRARIES=`AddList $POST_LIBRARIES -lpython${CMK_BUILD_PYTHON}`
807         fi
808         OPTS_CPP="$OPTS_CPP -O0 -g"
809 fi
810
811 PROCESSED_MODULES=""
812
813 # Look up and add the dependencies for module $1
814 findModuleDep() {
815         M=$1
816         notInList $M $PROCESSED_MODULES || return
817         PROCESSED_MODULES="$PROCESSED_MODULES $M"
818         PRE_LIBRARIES=`AddList $PRE_LIBRARIES -lmodule$M`
819 # HACK: add modules to both ends of library list, to avoid
820 #  undefined symbol CkRegisterMainModule when it's in a module file.
821         POST_LIBRARIES=`AddList $POST_LIBRARIES -lmodule$M`
822         if [ "$M" = "PythonCCS" ]
823             then
824             LINK_PYTHON=1
825         fi
826
827         dep=""
828 # fixme: should search everything in the -L library paths:
829         for dir in `echo . $CHARMLIB`
830         do
831                 d="$dir/libmodule$M.dep"
832                 [ -r "$d" ] && dep=$d
833         done
834         if [ -r "$dep" ]
835         then
836                 # Add the module dependencies in this file
837                 # Debugf "Adding module" $M "dependencies from" $dep
838                 deps=`cat $dep`
839                 Debugf "Module" $M "adds dependencies:" $deps
840                 processArgs $deps
841         fi
842 }
843
844 # Repeat the module-dependency search until we find no
845 #  new modules:
846 START_MODULES=""
847 while [ "$START_MODULES" != "$MODULES" ]
848 do
849         Debugf "----------- Module dependency search ------------"
850         START_MODULES="$MODULES"
851         for m in $MODULES
852         do
853                 findModuleDep $m
854         done
855 done
856
857 Debugf "----------- Module dependency search complete ------------"
858
859
860
861 ##############################################################################
862 #
863 # Load machine-specific configuration data, then handle overrides to it.
864 #
865 ##############################################################################
866
867
868 if [ x"$CMK_NO_BUILD_SHARED" = "xfalse" ]
869 then
870         BUILD_SHARE="1"
871 fi
872
873 if [ "$BUILD_SHARE" = "1" ]
874 then
875         if [ x"$CMK_NO_BUILD_SHARED" = "xtrue" -o -z "$CHARMLIBSO" ]
876         then
877                 BUILD_SHARE="0"
878         else
879                 USE_PIC="1"
880         fi
881 fi
882
883 OPTS_PIC="$CMK_PIC"
884 if [ "$USE_PIC" = "1" ]
885 then
886        OPTS_CC="$OPTS_CC $OPTS_PIC"
887        OPTS_CXX="$OPTS_CXX $OPTS_PIC"
888        OPTS_F90="$OPTS_F90 $OPTS_PIC"
889        OPTS_LD="$OPTS_LD $OPTS_PIC"
890 fi
891
892 if [ "$BUILD_SHARE" = "1" ]
893 then
894         # ignore BUILD_SHARE if the target is not .a or .so
895         case "$OBJECT" in
896         *.a|*.so|*.so.*|*.sl|*.dylib)
897                 OPTS_LD="$OPTS_LD $CMK_LD_SHARED $OPTS_LDRO -L$CHARMLIBSO"
898                 ;;
899         *)      
900                 BUILD_SHARE="0"
901                 ;;
902         esac
903 fi
904
905 if [ -n "$OVERRIDE_CC" ]
906 then
907     CMK_CC="$OVERRIDE_CC"
908 fi
909 Debug "set 2"
910 if [ -n "$OVERRIDE_CXX" ]
911 then
912     CMK_CXX="$OVERRIDE_CXX"
913 fi
914
915 if [ -n "$OVERRIDE_F90" ]
916 then
917     CMK_CF90="$OVERRIDE_F90"
918 fi
919
920 if [ -n "$OVERRIDE_LD" ]
921 then
922     CMK_LD="$OVERRIDE_LD"
923 fi
924
925 if [ -n "$OVERRIDE_LDXX" ]
926 then
927     CMK_LDXX="$OVERRIDE_LDXX"
928 fi
929
930 if [ -n "$USE_RELIABLE_CC" ]
931 then
932         CMK_CC="$CMK_CC_RELIABLE"
933 fi
934
935 if [ -n "$USE_FASTEST_CC" ]
936 then
937     CMK_CC="$CMK_CC_FASTEST"
938 fi
939
940 if [ -n "$USE_F77" ]
941 then
942     CMK_CF90=$CMK_CF77
943     CMK_CF90_FIXED=$CMK_CF77
944     CMK_F90LIBS=$CMK_F77LIBS
945 fi
946
947 if [ -n "$CMK_F90_MODINC" ]
948 then
949     CMK_CF90_MODINC="$CMK_F90_MODINC"
950 else
951     CMK_CF90_MODINC="-M"
952 fi
953
954 if [ -n "$F90_MODDIR" ]
955 then
956     for dir in $F90_MODDIR
957     do
958       OPTS_F90="$OPTS_F90 $CMK_CF90_MODINC $dir"
959     done
960 fi
961
962 if [ -n "$CMK_F90_USE_MODDIR" ]
963 then
964     CMK_CF90="$CMK_CF90 $CMK_CF90_MODINC$CHARMINC "
965 fi
966
967 if [ -n "$SEQUENTIAL" ]
968 then
969     CMK_CC="$CMK_SEQ_CC -DCMK_SEQUENTIAL=1 "
970     CMK_LD="$CMK_SEQ_LD"
971     CMK_CXX="$CMK_SEQ_CXX -DCMK_SEQUENTIAL=1 " 
972     CMK_CF90="$CMK_SEQ_F90 " 
973     CMK_LDXX="$CMK_SEQ_LDXX" 
974     CMK_AR="$CMK_SEQ_AR"
975     CMK_RANLIB="$CMK_SEQ_RANLIB"
976 fi
977
978 if [ -n "$NATIVE" ]
979 then
980     CMK_CC="$CMK_NATIVE_CC " 
981     CMK_LD="$CMK_NATIVE_LD" 
982     CMK_CXX="$CMK_NATIVE_CXX " 
983     CMK_LDXX="$CMK_NATIVE_LDXX" 
984 fi
985
986 if [ -n "$PREPROCESS" ]
987 then
988     CMK_CF90=$CMK_FPP
989 fi
990
991 if [ -n "$PURIFY" ]
992 then
993     CMK_LD="purify $CMK_LD"
994     CMK_LDXX="purify $CMK_LDXX"
995 fi
996
997 Debug "set 4"
998 if [ "$OPTIMIZE_MODE" = "true" ]
999 then
1000     OPTS_CC="$CMK_C_OPTIMIZE $OPTS_CC"
1001     OPTS_CXX="$CMK_CXX_OPTIMIZE $OPTS_CXX"
1002     OPTS_F90="$CMK_F90_OPTIMIZE $OPTS_F90"
1003     OPTS_LD="$CMK_C_OPTIMIZE $OPTS_LD"
1004     OPTS_LDXX="$CMK_CXX_OPTIMIZE $OPTS_LDXX"
1005 fi
1006
1007 # Pick up per-architecture production mode settings
1008 if [ "$PRODUCTION_MODE" = "true" ]
1009 then
1010     OPTS_CC="$CMK_PRODUCTION $CMK_C_PRODUCTION $OPTS_CC"
1011     OPTS_CXX="$CMK_PRODUCTION $CMK_CXX_PRODUCTION $OPTS_CXX"
1012     OPTS_F90="$CMK_PRODUCTION $CMK_F90_PRODUCTION $OPTS_F90"
1013     OPTS_LD="$CMK_PRODUCTION $CMK_C_PRODUCTION $OPTS_LD"
1014     OPTS_LDXX="$CMK_PRODUCTION $CMK_CXX_PRODUCTION $OPTS_LDXX"
1015 fi
1016
1017 if [ -n "$XI_INTERNAL" ]
1018 then
1019     CMK_XIOPTS="$CMK_XIOPTS -intrinsic"
1020 fi
1021
1022 if [ "$GENDEPENDS" = "yes" ]
1023 then
1024     CMK_XIOPTS="$CMK_XIOPTS -M"
1025 fi
1026
1027 if test "$CMK_WITH_TAU" = "true"
1028 then
1029   echo "include $TAU_MAKEFILE" > _make.tau.tmp
1030   echo "print-%:; @echo \$(\$*)" >> _make.tau.tmp
1031   TAU_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_LIBS`
1032   TAU_MPI_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_LIBS` 
1033   TAU_MPI_FLIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_FLIBS`
1034   TAU_DEFS=`make --no-print-directory -f _make.tau.tmp print-TAU_DEFS`
1035   TAU_INCLUDE=`make --no-print-directory -f _make.tau.tmp print-TAU_INCLUDE`
1036
1037   rm -f _make.tau.tmp
1038 fi
1039
1040 #Add generic options to the compiler- and linker-options
1041 OPTS_CC="$OPTS $TAU_DEFS $TAU_INCLUDE $OPTS_CC"
1042 OPTS_CXX="$OPTS $TAU_DEFS $TAU_INCLUDE $OPTS_CXX"
1043 OPTS_F90="$OPTS $OPTS_F90"
1044 OPTS_LD="$OPTS $OPTS_LD"
1045 # OPTS_LDXX gets OPTS from OPTS_LD
1046
1047 if [ -n "$F90_MAIN" ] 
1048 then
1049 # for_main needs to be placed at beginning
1050         CMK_LD="$CMK_LD $CMK_F90MAINLIBS"
1051         CMK_LDXX="$CMK_LDXX $CMK_F90MAINLIBS"
1052 fi
1053
1054 # There's really no analog for this in the Bourne shell: 
1055 #  -------- C Shell ------> onintr failure
1056 # Luckily, it's irrelevant as long as nobody sends us a ^Z.
1057
1058 ##############################################################################
1059 #
1060 # Check for valid choice of LANGUAGE
1061 #
1062 ##############################################################################
1063 Debug "Checking language..."
1064
1065 if [ -z "$LANGUAGE" ]
1066 then
1067     LANGUAGE="charm++"
1068     [ -n "$SEQUENTIAL" ] && LANGUAGE=c
1069 fi
1070
1071 case "$LANGUAGE" in
1072 "c"|"C"|"c++"|"C++")
1073         [ -z "$SEQUENTIAL" -a -z "$NATIVE" ] && Abort "Language $LANGUAGE is for sequential programs"
1074         ;;
1075 esac
1076
1077 # update charm to bgcharm
1078 if [ "$LANGUAGE" = "charm++" -a -n "$CMK_BLUEGENE" ]
1079 then
1080         LANGUAGE="bgcharm++"
1081 fi
1082 if [ "$LANGUAGE" = "converse++" -a -n "$CMK_BLUEGENE" ]
1083 then
1084         LANGUAGE="bgconverse++"
1085 fi
1086 if [ "$LANGUAGE" = "bgcharm++" ]
1087 then
1088         TRACEMODE=`AddList $TRACEMODE bluegene`
1089 fi
1090
1091 ########################################################################
1092 #
1093 # We've parsed and verified the command-line parameters.
1094 # Now we prepare routines to clean up and exit.
1095 # None of these routines ever returns-- they all just exit.
1096 #
1097 ########################################################################
1098
1099
1100 Copyobj() {
1101         if [ "a$COPYTO" != "a" -a "a$COPYTO" != "a$OBJECT" ] 
1102         then
1103                 [ -d $COPYTO ] && COPYTO="$COPYTO/"
1104                 if [ "a$PROG_EXT" != "a" -a -r "$OBJECT$PROG_EXT" ] ; then
1105                         Warning "Appending $PROG_EXT to object file name"
1106                         OBJECT="$OBJECT$PROG_EXT"
1107                 fi
1108                 [ ! -d $COPYTO ] && DoNoErrCheck $RM $COPYTO 
1109                 Do $CP $OBJECT $COPYTO
1110         fi
1111         test -n "$COPY_EXIT" && Success
1112 }
1113
1114 Copymod() {
1115         for COPY in `echo $MODCOPIES`
1116         do
1117                 if [ "a$COPY" != "a$OBJECT" ] 
1118                 then
1119                         [ -d $COPY ] && COPY="$COPY/"
1120                         if [ -n "$CMK_MOD_NAME_ALLCAPS" ] ; then
1121                           OBJECT=`echo $OBJECT | tr "a-z" "A-Z"`
1122                         fi
1123                         if [ -r "$OBJECT.$CMK_MOD_EXT" ] ; then
1124                           Warning "Appending .$CMK_MOD_EXT to object file name"
1125                           OBJECT="$OBJECT.$CMK_MOD_EXT"
1126                         fi
1127                         [ ! -d $COPY ] && DoNoErrCheck $RM $COPY 
1128                         Do $CP $OBJECT $COPY
1129                 fi
1130         done
1131         Success
1132 }
1133
1134 Success() {
1135         End 0
1136 }
1137
1138
1139 ##############################################################################
1140 #
1141 # Preprocess the GEN-CPM files
1142 #
1143 ##############################################################################
1144
1145 Debug "Preprocessing GEN-CPM files"
1146
1147 for FILE in `echo $GENCPM`
1148 do
1149 # This used to be "$FILE:r", which strips the extention
1150         BASE=`stripExtention $FILE`
1151         TMP=`basename $FILE`".TMP"
1152         DELETE="$DELETE $TMP.c $TMP.i"
1153         DoNoErrCheck $RM $BASE.cpm.h $TMP.c
1154         Do touch $BASE.cpm.h
1155         Do $LN -f -s $FILE $TMP.c
1156         Do $CMK_CPP_C -I$CHARMINC $OPTS_CC $OPTS_CPP $TMP.c > $TMP.i
1157         Do $CHARMBIN/conv-cpm $TMP.i $BASE.cpm.h
1158 done
1159
1160 ##############################################################################
1161 #
1162 # Compile all specified files
1163 #
1164 # All temporary files named *.TMP.* for easy cleanup.
1165 #
1166 ##############################################################################
1167
1168 Debug "About to compile..."
1169
1170 numFiles=`echo $FILES | wc -w`
1171 for FILE in `echo $FILES`
1172 do
1173         BASE=`stripExtention \`basename $FILE\``
1174         TMP=`basename $FILE`".TMP"
1175         FILE_EXT=`getExtention $FILE`
1176         BASEO="$BASE.o"
1177         DESTO="-o $BASEO"
1178
1179         # in case of: charmc -o object.o -c foo.c,
1180         # avoid foo.c => remove foo.o => foo.o => object.o,
1181         # instead generate object.o directly.
1182         # remove and generate foo.o may be dangerous in parallel make 
1183         # for example in the case of compiling several objs from one 
1184         # same c file such as memory.c.
1185         if [ $numFiles = 1 -a -n "$OBJECT" ]
1186         then
1187                 OBJ_EXT=`getExtention $OBJECT`
1188                 if [ "$OBJ_EXT" = '.o' -o "$OBJ_EXT" = '.co' ]
1189                 then
1190                     BASEO=$OBJECT
1191                     DESTO="-o $OBJECT"
1192                 fi
1193         fi
1194
1195         if [ "$FILE_EXT" != ".o" -a "$FILE_EXT" != ".co" -a "$FILE_EXT" != ".M" -a "$FILE_EXT" != ".ci" ]
1196         then
1197                 [ $VERBOSE ] && echo "Compiling $FILE"
1198                 DoNoErrCheck $RM $BASEO $BASE.f.o
1199         fi
1200         if [ ! -z "$SKIPLINK" ] 
1201         then
1202                 DESTO=""
1203         fi
1204         NU_OBJ=""
1205         case "$FILE" in
1206         *.ci)
1207                 XIOPTS=$CMK_XIOPTS
1208                 test "$LANGUAGE" = "f90charm" && XIOPTS="$XIOPTS -f90"
1209                 if [ -n "$PREPROCESS" ]
1210                 then
1211                   Do $CMK_CPP_CHARM $OPTS_CPP $FILE | Do $CHARMBIN/charmxi $XIOPTS
1212                 else
1213                   Do $CHARMBIN/charmxi $XIOPTS $FILE
1214                 fi
1215                 ;;
1216         *.c|*.s)
1217                 Do $CMK_CC -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CC -c $FILE $DESTO
1218                 NU_OBJ=$BASEO
1219                 ;;
1220         bglmachine.C)
1221                 Do $CMK_GCXX -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $FILE $DESTO
1222                 NU_OBJ=$BASEO
1223                 ;;
1224         *.C|*.cc|*.cxx|*.cpp|*.c++|*.cu)
1225                 Do $CMK_CXX -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $FILE $DESTO
1226                 NU_OBJ=$BASEO
1227                 ;;
1228         *.f|*.F)
1229                 test -z "$CMK_CF90_FIXED" && Abort "No F90 compiler (CMK_CF90_FIXED) defined"
1230                 Do $CMK_CF90_FIXED -I$CHARMINC $OPTS_CPP $OPTS_F90 -c $FILE $DESTO
1231                 NU_OBJ=$BASEO
1232                 ;;
1233         *.f90|*.F90)
1234                 test -z "$CMK_CF90" && Abort "No F90 compiler (CMK_CF90) defined"
1235                 test -z "$PREPROCESS" && OPTS_F90="$OPTS_F90 -c"
1236                 Do $CMK_CF90 -I$CHARMINC $OPTS_CPP $OPTS_F90 $FILE $DESTO
1237                 NU_OBJ=$BASEO
1238                 ;;
1239         *.fc|*.FC)
1240                 Abort "I'm not yet smart enough to compile $FILE"
1241                 ;;
1242         *.o|*.so|*.so.*|*.sl|*.a|*.dylib|*.co)
1243                 [ ! -s $FILE ] && Abort "$FILE: file not recognized: File truncated"
1244                 OBJECTFILES="$OBJECTFILES $FILE"
1245                 ;;
1246         *.M|*.mod)
1247                 OBJECT="$BASE"
1248                 Copymod
1249                 Success
1250                 ;;
1251         *)
1252                 Abort "file with unrecognized extension $FILE"
1253         esac
1254 # Handle new object files
1255         if [ -n "$NU_OBJ" -a -n "$OBJECT" ] 
1256         then
1257                 if [ "$OBJECT" != "$BASEO" ]
1258                 then
1259                         DELETE="$DELETE $NU_OBJ"
1260                 fi
1261                 OBJECTFILES="$OBJECTFILES $NU_OBJ"
1262         fi
1263 done
1264
1265 ##############################################################################
1266 #
1267 #                        POSSIBLY, SKIP LINK-STEP
1268 #
1269 # 1. No $OBJECT may be specified at all --- its just a compilation.
1270 # 2. If $OBJECT is a ".a" file, a library is created from $OBJECTFILES.
1271 # 3. If $OBJECT is a ".o" file, then an "LDRO" is created from $OBJECTFILES.
1272 # 4. Language may be sequential.  Do a quick and easy linkage.
1273 #
1274 #               in any of these cases, the full link step is skipped.
1275 #
1276 ##############################################################################
1277
1278 MakeSO() {
1279         Debugf "Running ldro..."
1280         DoNoErrCheck $RM $OBJECT
1281         # convert to absolute path if required
1282         if test -n "$CMK_LD_SHARED_ABSOLUTE_PATH"
1283         then
1284         dir=`dirname $OBJECT`
1285         fname=`basename $OBJECT`
1286         dir=`cd $dir; pwd`
1287         OBJECT=$dir/$fname
1288         fi
1289         Warning "Warning: creating $OBJECT "
1290         if test "$MAKE_LDXX" = "1"
1291         then
1292           soCmd="$CMK_LDXX -o $OBJECT $OPTS_LD $OPTS_LDRO $OBJECTFILES $PRE_LIBRARIES $POST_LIBRARIES $CMK_LD_SHARED_LIBS"
1293         else
1294           soCmd="$CMK_LD -o $OBJECT $OPTS_LD $OPTS_LDRO $OBJECTFILES $PRE_LIBRARIES $POST_LIBRARIES $CMK_LD_SHARED_LIBS"
1295         fi
1296         DoNoErrCheck $soCmd
1297         Do_res=$?
1298         if [ $Do_res -ne 0 ] 
1299         then
1300         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
1301         echo "!!  Failed to compile Charm++ shared library. You can disable shared  !!"
1302         echo "!!  lib compilation by providing --no-build-shared build option like: !!"
1303         echo "!!     ./build charm++ net-linux --no-build-shared -O                 !!"
1304         echo "!!  or reexecute this charmc command with --no-build-shared compile option        !!"
1305         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
1306         Abort "Command $soCmd returned error code $Do_res"
1307         fi
1308         Copyobj
1309 }
1310
1311 if [ -z "$OBJECT" -o ! -z "$SKIPLINK" ] 
1312 then
1313 # We have no target object, or are playing preprocessor tricks-- just end
1314         Debugf "No target object-- finished"
1315         Success
1316 fi
1317
1318 Debug "About to link..."
1319
1320 case "$OBJECT" in
1321 *.o)
1322         Debugf "Converting $FILES to $OBJECTFILES"
1323         nFiles=`echo $FILES | wc -w`
1324         nObj=`echo $OBJECTFILES | wc -w`
1325 # One source file, one .o: just move the .o
1326         if [ $OBJECT != $OBJECTFILES ]
1327         then
1328                 Debugf "Moving .o to destination"
1329                 DoNoErrCheck $RM $OBJECT
1330                 Do $MV $OBJECTFILES $OBJECT
1331         fi
1332         Copyobj
1333         ;;
1334 *.so|*.so.*|*.sl|*.dylib)
1335 # Make shared library
1336         # if no build share is set, exit normally.
1337         if [ "$BUILD_SHARE" = 0 ]
1338         then
1339                 echo "Warning: building shared library is skipped, possibly due to not supported, check configuration files."
1340                 End 0
1341         elif [ "$CHARM_SHARED" = "0" ]
1342         then
1343                 MakeSO
1344         fi
1345         ;;
1346 *.a)
1347 # Make regular (.a, non-shared) library
1348         # in case building charm so, build .so under lib_so as well # Gengbin
1349         if [ "$BUILD_SHARE" = "1" ]
1350         then
1351           COPY_EXIT=""
1352           OBJECT_SAVE=$OBJECT
1353           COPYTO_SAVE=$COPYTO
1354           if [ -z "$COPYTO" ]
1355           then
1356             DESTDIR=`echo $OBJECT | sed -e 's@lib/[^/]*\.a$@lib_so@'`
1357             OBJECT=`echo $OBJECT | sed -e 's@lib/\([^/]*\)\.a$@lib_so/\1.a@'`
1358           else
1359             COPYTO=`echo $COPYTO | sed -e 's@lib[/]*$@lib_so@'`
1360             DESTDIR=$COPYTO
1361           fi
1362           OBJECT=`echo $OBJECT | sed -e 's/\.a$/.'"$CMK_SHARED_SUF"'/'`
1363           [ -f $DESTDIR/.charmso ] && MakeSO
1364           COPYTO=$COPYTO_SAVE
1365           OBJECT=$OBJECT_SAVE
1366           COPY_EXIT="true"
1367         fi
1368         # build normal .a
1369         DoNoErrCheck $RM $OBJECT
1370         Do $CMK_AR $OBJECT $OBJECTFILES
1371         Do $CMK_RANLIB $OBJECT
1372         Copyobj
1373         ;;
1374 esac
1375 # If the above case returns, we're not creating a .o or .a file,
1376 # so linking is needed.
1377
1378 ############ Determine the language and libraries ##########
1379
1380 # Parse out the real languages
1381 case "$LANGUAGE" in
1382 "charm++f")
1383         LANGUAGE="charm++"
1384         USE_F90_LIBRARIES="1"
1385         ;;
1386 "f90charm")
1387         PRE_LIBRARIES="$PRE_LIBRARIES -lf90charm -lckf"
1388         POST_LIBRARIES="$POST_LIBRARIES $CMK_F90LIBS"
1389         ;;
1390 "bluegene"|"bigsim")
1391         PRE_LIBRARIES="$PRE_LIBRARIES -lconv-bigsim -lconv-bigsim-logs"
1392         ;;
1393 "ckbluegene")
1394         POST_LIBRARIES="$POST_LIBRARIES -lbluegene"
1395         ;;
1396 "bgcharm++")
1397         PRE_LIBRARIES="$PRE_LIBRARIES -lconv-bigsim -lconv-bigsim-logs -lconv-bluegene-stub"
1398         ;;
1399 "bgconverse++")
1400         MIDDLE_LIBS="$MIDDLE_LIBS -lconv-bigsim -lconv-bigsim-logs"
1401         ;;
1402 esac
1403
1404 # DMK - ACCEL SUPPORT - If this is a cell build, add the spe code object to POST_LIBRARIES
1405 if [ $CMK_CELL -eq 1 ]
1406 then
1407
1408   if [ "$LANGUAGE" == "converse++" ]
1409   then
1410     # Include a "do nothing" register_accel_spe_funcs() function
1411     POST_LIBRARIES="$POST_LIBRARIES -lnoAccelStub"
1412   fi
1413
1414   if [ "$LANGUAGE" == "charm++" ]
1415   then
1416
1417     if [ $SPU_USE_CPP -eq 1 ]
1418     then
1419         SPU_COMPILER=$CMK_SPU_CXX
1420         SPU_LINKER=$CMK_SPU_LDXX
1421     else
1422         SPU_COMPILER=$CMK_SPU_CC
1423         SPU_LINKER=$CMK_SPU_LD
1424     fi
1425
1426     # Make the spe object file to be included
1427     Do $SPU_COMPILER -c -I$CHARMINC -o main__funcLookup__.genSPECode.o main__funcLookup__.genSPECode.c -DCMK_CELL_SPE=1
1428     Do $SPU_LINKER -L$CHARMLIB -o main__funcLookup__.genSPECode main__funcLookup__.genSPECode.o $CMK_SPERT_LIBS $SPU_EXTRA_LIBS
1429     Do $CMK_PPU_EMBEDSPU spert_main main__funcLookup__.genSPECode main__funcLookup__.genSPECode.ppe.o
1430
1431     # Include the spe object file for linking into main executable
1432     POST_LIBRARIES="$POST_LIBRARIES main__funcLookup__.genSPECode.ppe.o"
1433
1434   fi
1435 fi
1436
1437 if [ "$AMPIMAIN" != "" ]
1438 then
1439         OBJECTFILES="$CHARMLIB/lib$AMPIMAIN.o $OBJECTFILES"
1440 fi
1441
1442 if [ "$SWAPGLOBALS" = "1" ]
1443 then
1444         swapo="$CHARMLIB/libglobal-swap.o"
1445         if [ ! -r "$swapo" -o ! -s "$swapo" ]
1446         then
1447                 echo "Global-swapping library $swapo missing--"
1448                 echo "  Is -swapglobals supported on this machine?"
1449                 exit 1
1450         else
1451                 OBJECTFILES="$swapo $OBJECTFILES"
1452         fi
1453 fi
1454 if [ "$SWAPGLOBALS" = "2" ]
1455 then
1456         swapo="$CHARMLIB/libglobal-copy.o"
1457         if [ ! -r "$swapo" -o ! -s "$swapo" ]
1458         then
1459                 echo "Global-copying library $swapo missing--"
1460                 echo "  Is -copyglobals supported on this machine?"
1461                 exit 1
1462         else
1463                 OBJECTFILES="$swapo $OBJECTFILES $CHARMLIB/loadsym.o"
1464         fi
1465 fi
1466
1467 if [ $USE_F77_LIBRARIES = "1" ]
1468 then
1469         POST_LIBRARIES="$POST_LIBRARIES $CMK_F77LIBS"
1470 fi
1471
1472 if [ $USE_F90_LIBRARIES = "1" ]
1473 then
1474         if [ ! -r $CHARMLIB/libconv-utilf.a ]
1475         then
1476                 echo "Fortran support library $CHARMLIB/libconv-utilf.a missing"
1477                 echo "Is your Fortran compiler working?"
1478                 exit 1
1479         fi
1480         
1481         POST_LIBRARIES="$POST_LIBRARIES -lconv-utilf $CMK_F90LIBS"
1482         if [ -z "$SEQUENTIAL" -a ! -z "$CMK_F90OBJS" ] 
1483         then
1484           for f90obj in $CMK_F90OBJS
1485           do
1486             OBJECTFILES="$OBJECTFILES $CHARMLIB/$f90obj"
1487           done
1488         fi
1489 fi
1490
1491 # check if we're linking a sequential object
1492 case "$LANGUAGE" in
1493 "c"|"C"|"f90"|"f77")
1494         if [ -n "$NATIVE" ]
1495         then
1496           Do $CMK_NATIVE_LD $OPTS_LD -o $OBJECT $OBJECTFILES \
1497                 -L$CHARMLIB $PRE_LIBRARIES $CMK_NATIVE_LIBS $POST_LIBRARIES
1498         else
1499           Do $CMK_SEQ_LD $OPTS_LD -o $OBJECT $OBJECTFILES \
1500                 -L$CHARMLIB $PRE_LIBRARIES $CMK_SEQ_LIBS $POST_LIBRARIES
1501         fi
1502         Copyobj
1503         ;;
1504 "c++"|"C++")
1505         if [ -n "$NATIVE" ]
1506         then
1507           Do $CMK_NATIVE_LDXX $OPTS_LD -o $OBJECT $OBJECTFILES \
1508                 -L$CHARMLIB $PRE_LIBRARIES $CMK_NATIVE_LIBS $POST_LIBRARIES
1509         else
1510           Do $CMK_SEQ_LDXX $OPTS_LD -o $OBJECT $OBJECTFILES \
1511                 -L$CHARMLIB $PRE_LIBRARIES $CMK_SEQ_LIBS $POST_LIBRARIES
1512         fi
1513         Copyobj
1514         ;;
1515 esac
1516
1517 ##############################################################################
1518 #
1519 # Some quick consistency checks prior to full link-step
1520 #
1521 # Check for valid choice of TRACEMODE
1522 # Check for valid choice of BALANCE
1523 # Check for compatibility among BALANCE and TRACEMODE
1524 #
1525 ##############################################################################
1526
1527 Debug "About to check TRACEMODE=$TRACEMODE and BALANCE=$BALANCE..."
1528
1529 # Check for valid choice of TRACEMODE
1530
1531 # [ -r $CHARMLIB/libtrace-projections.a ] || Abort "trace libraries not installed.  Is Charm++ built properly?"
1532
1533 if [ x"$TRACEMODE" != x ]
1534 then
1535 for trace in $TRACEMODE; do
1536 [ ! -r "$CHARMLIB/libtrace-$trace.a" ] && Abort "charmc : No such tracemode $trace"
1537 TRACE_OBJ="$TRACE_OBJ -ltrace-$trace"
1538 done
1539 TRACE_OBJ="$TRACE_OBJ $CMK_LIBZ"
1540 fi
1541
1542 if test x$LANGUAGE = x"bgcharm++"
1543 then 
1544     BALANCE=bluegene
1545 fi
1546
1547 BAL_EXT=`getExtention $BALANCE`
1548 if [ -z "$BAL_EXT" ]
1549 then
1550 # Balance has no extention-- is a library reference
1551     BAL_OBJ="$CHARMLIB/libldb-$BALANCE.o"
1552 else
1553 # Balance has some extention-- must be a .o or .a file
1554     BAL_OBJ="$BALANCE"
1555 fi
1556
1557 Debug "Finished with BAL_OBJ=$BAL_OBJ, TRACEMODE=$TRACEMODE..."
1558
1559 if [ ! -r "$BAL_OBJ" ]
1560 then
1561         if [ "$BALANCE" = "$BAL_OBJ" ]
1562         then
1563                 Abort "Could not find load balancer object $BAL_OBJ"
1564         else
1565                 (cd $CHARMLIB ; ls -al libldb-*)
1566                 Abort "Unknown load balancer $BALANCE / $BAL_OBJ"
1567         fi
1568 fi
1569
1570
1571 # Check for valid choice of MEMORY
1572
1573 MEM_OBJ="$CHARMLIB/libmemory-$MEMORY.o"
1574
1575 if [ ! -r $MEM_OBJ -o ! -s $MEM_OBJ ] 
1576 then
1577   (cd $CHARMLIB ; list=`ls libmemory-* 2>/dev/null`; test -n "$list" && for i in $list; do [ -s $i ] && ls -l $i; done )
1578   Abort "charmc : No such memory mode $MEMORY"
1579 fi
1580
1581 # For memory wrapping around the OS allocator, need to add also the wrapper object
1582 case $MEMORY in
1583   os-*)
1584   MEM_OBJ=$MEM_OBJ" $CHARMLIB/libmemory-os-wrapper.o"
1585   ;;
1586 esac
1587
1588 # Check for valid choice of THREAD
1589
1590 if [ "$CHARM_SHARED" = "1" -a -n "$CMK_LD_SHARED_THREAD" -a "$THREAD" = "default" ]
1591 then
1592   THREAD=$CMK_LD_SHARED_THREAD
1593 fi
1594 THREAD_OBJ="$CHARMLIB/libthreads-$THREAD.o"
1595
1596 if [ ! -r $THREAD_OBJ -o ! -s $THREAD_OBJ ] 
1597 then
1598   (cd $CHARMLIB ; list=`ls libthreads-* 2>/dev/null`; test -n "$list" && for i in $list; do [ -s $i ] && ls -l $i; done )
1599   Abort "charmc : No such thread mode $THREAD"
1600 fi
1601
1602 ################# Build the module initialization function ##################
1603 modInitName="moduleinit$$"
1604 modInitSrc="$modInitName.C"
1605 modInitObj="$modInitName.o"
1606 DoNoErrCheck $RM $modInitSrc $modInitObj > /dev/null 2>&1
1607
1608 for module in $MODULES; do
1609     echo "extern void _register$module(void);" >> $modInitSrc
1610 done
1611 for trace in $TRACEMODE; do
1612     if test $trace = "summary" 
1613     then
1614       echo "  extern void _registerTraceSummary();" >> $modInitSrc
1615     elif test $trace = "projections"
1616     then
1617       echo "  extern void _registerTraceProjections();" >> $modInitSrc
1618     elif test $trace = "simple"
1619     then
1620       echo "  extern void _registerTraceSimple();" >> $modInitSrc
1621     elif test $trace = "Tau"
1622     then
1623       echo "  extern void _registerTraceTau();" >> $modInitSrc
1624     elif test $trace = "utilization" 
1625     then 
1626       echo "  extern void _registerTraceUtilization();" >> $modInitSrc 
1627     elif test $trace = "controlPoints"
1628     then
1629       echo "  extern void _registerTraceControlPoints();" >> $modInitSrc
1630     elif test $trace = "all"
1631     then
1632       echo "  extern void _registerTraceProjections();" >> $modInitSrc
1633       echo "  extern void _registerTraceSummary();" >> $modInitSrc
1634     fi
1635 done
1636 if test -n "$BALANCER"
1637 then
1638     echo "extern void LBDefaultCreate(const char *);" >> $modInitSrc
1639 fi
1640 echo "void _registerExternalModules(char **argv) {" >> $modInitSrc
1641 for module in $MODULES; do
1642     [ -n "$VERBOSE" ] && echo "Adding registration for module $module"
1643     echo "  _register$module();" >> $modInitSrc
1644 done
1645 for trace in $TRACEMODE; do
1646     if test $trace = "summary"
1647     then
1648       echo "  _registerTraceSummary();" >> $modInitSrc
1649     elif test $trace = "projections"
1650     then
1651       echo "  _registerTraceProjections();" >> $modInitSrc
1652     elif test $trace = "Tau"
1653     then
1654       TRACE_WITH_TAU=1
1655       echo "  _registerTraceTau();" >> $modInitSrc
1656     elif test $trace = "simple"
1657     then
1658       echo "  _registerTraceSimple();" >> $modInitSrc
1659     elif test $trace = "utilization" 
1660     then 
1661       echo "  _registerTraceUtilization();" >>  $modInitSrc 
1662     elif test $trace = "controlPoints"
1663     then
1664       echo "  _registerTraceControlPoints();" >> $modInitSrc
1665     elif test $trace = "all"
1666     then
1667       echo "  _registerTraceProjections();" >> $modInitSrc
1668       echo "  _registerTraceSummary();" >> $modInitSrc
1669     fi
1670 done
1671 if test -n "$BALANCER"
1672 then
1673     [ -n "$VERBOSE" ] && echo "Adding load balancer for $BALANCER"
1674     for deflb in $BALANCER
1675     do
1676       echo "  LBDefaultCreate(\"$deflb\");" >> $modInitSrc
1677     done
1678 fi
1679 echo "}" >> $modInitSrc
1680
1681 # creating projections
1682 echo "void _createTraces(char **argv) {" >> $modInitSrc
1683 for trace in $TRACEMODE; do
1684     [ -n "$VERBOSE" ] && echo "Adding registration for trace $trace"
1685     echo "extern void _createTrace$trace(char **argv);" >> $modInitSrc
1686     echo "_createTrace$trace(argv);" >> $modInitSrc
1687 done
1688 echo "}" >> $modInitSrc
1689
1690 Do $CMK_CXX $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $modInitSrc -o $modInitObj
1691
1692 ###############################################################################
1693 #
1694 #                               Finally, LINK
1695 #
1696 ###############################################################################
1697
1698 Debug "About to perform final link..."
1699
1700 MAKE_LDXX="0"
1701 MAKE_LD="0"
1702
1703 CORE_LIBS="-lconv-core -lconv-util $TRACE_OBJ "
1704
1705 # get TAU stub makefile variables
1706 if test "$CMK_WITH_TAU" = "true"
1707 then
1708   echo "include $TAU_MAKEFILE" > _make.tau.tmp
1709   echo "print-%:; @echo \$(\$*)" >> _make.tau.tmp
1710   TAU_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_LIBS`
1711   TAU_MPI_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_LIBS` 
1712   TAU_MPI_FLIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_FLIBS`
1713   TAU_DEFS=`make --no-print-directory -f _make.tau.tmp print-TAU_DEFS`
1714   TAU_INCLUDE=`make --no-print-directory -f _make.tau.tmp print-TAU_INCLUDE`
1715
1716   rm -f _make.tau.tmp
1717 fi
1718 # done getting TAU variables
1719
1720 case "$LANGUAGE" in
1721 "f90charm")
1722         MAKE_LDXX="1"
1723         MIDDLE_LIBS="-lck -lconv-cplus-y $CORE_LIBS "
1724         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1725         if [ "$TAU_TRACE_MPI" = "1" ]
1726         then
1727                 TAU_LIBS="$TAU_MPI_FLIBS $TAU_LIBS"
1728         else
1729                 TAU_LIBS="$TAU_LIBS"
1730         fi
1731         ;;
1732 "charm"|"charm++"|"f90charm"|"ckbluegene"|"bgcharm++")
1733         MAKE_LDXX="1"
1734         MIDDLE_LIBS="-lck -lconv-cplus-y $CORE_LIBS "
1735         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1736         if [ "$TAU_TRACE_MPI" = "1" ]
1737         then
1738                 TAU_LIBS="$TAU_MPI_LIBS $TAU_LIBS"
1739         else
1740                 TAU_LIBS="$TAU_LIBS"
1741         fi
1742         ;;
1743 "converse")
1744         MAKE_LD="1"
1745         MIDDLE_LIBS="-lconv-cplus-n $CORE_LIBS -ltrace-converse "
1746         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1747         ;;
1748 "converse++"|"bluegene"|"bigsim"|"bgconverse++")
1749         MAKE_LDXX="1"
1750         MIDDLE_LIBS="$MIDDLE_LIBS -lconv-cplus-y $CORE_LIBS -ltrace-converse "
1751         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1752         ;;
1753 *)
1754         Abort "Unrecognized choice of language $LANGUAGE"
1755         ;;
1756 esac
1757
1758 if [ "$BUILD_SHARE" = "0" ]
1759 then
1760         MIDDLE_LIBS="$MEM_OBJ $THREAD_OBJ $MIDDLE_LIBS"
1761 fi
1762
1763 if [ "$CHARM_SHARED" = "1" ]
1764 then
1765   [ -z "$CHARMLIBSO" ] &&  Abort "Charm++ lib_so directory not found!"
1766   # build user executable/shared lib linking with charm .so
1767   OPTS_LD="$OPTS_LD $CMK_LD_LIBRARY_PATH"
1768   if [ "$BUILD_SHARE" = "1" ]
1769   then
1770     LANG_LIBS="-L$CHARMLIBSO $OBJECTFILES $PRE_LIBRARIES"
1771   else
1772     LANG_LIBS="-L$CHARMLIBSO $BAL_OBJ $OBJECTFILES $modInitObj $PRE_LIBRARIES"
1773   fi
1774 else
1775   LANG_LIBS="-L$CHARMLIB -I$CHARMINC $BAL_OBJ $OBJECTFILES $modInitObj $PRE_LIBRARIES"
1776 fi
1777 if [ "$TRACE_WITH_TAU" = 1 ]
1778 then
1779         echo "Linking with the TAU libraries: $TAU_LIBS"
1780         ALL_LIBS="$LANG_LIBS $MIDDLE_LIBS $CMK_LIBS $POST_LIBRARIES $CMK_SYSLIBS $TAU_LIBS"
1781 else
1782         ALL_LIBS="$LANG_LIBS $MIDDLE_LIBS $CMK_LIBS $POST_LIBRARIES $CMK_SYSLIBS"
1783 fi
1784 Debugf "All libraries are: $ALL_LIBS"
1785
1786 if [ "$BUILD_SHARE" = "1" -a "$CHARM_SHARED" = "1" ]
1787 then
1788   # build an applicaiton .so linking with charm .so
1789   OPTS_LD="$OPTS_LD $CMK_LD_SHARED $OPTS_LDRO -L$CHARMLIBSO"
1790   Debugf "Running ldro..."
1791   DoNoErrCheck $RM $OBJECT
1792   [ "$MAKE_LDXX" = "1" ] && Do $CMK_LDXX $OPTS_LD $OPTS_LDXX -o $OBJECT $ALL_LIBS
1793   [ "$MAKE_LD" = "1" ] && Do $CMK_LD $OPTS_LD -o $OBJECT $ALL_LIBS
1794   Copyobj
1795 else
1796 # Make a regular executable: call the appropriate linker
1797   [ "$MAKE_LDXX" = "1" ] && Do $CMK_LDXX $OPTS_LD $OPTS_LDXX -o $OBJECT $ALL_LIBS
1798   [ "$MAKE_LD" = "1" ] && Do $CMK_LD $OPTS_LD -o $OBJECT $ALL_LIBS
1799 fi
1800
1801 ########################################################################
1802 #
1803 # Copy conv-host to user directory if it is present in installation.
1804 #
1805 ########################################################################
1806
1807 if test "$COPY_CHARMRUN" = "true"
1808 then
1809   targ=charmrun$CMK_POST_EXE
1810   [ ! -x $CHARMBIN/$targ -a -n "$CMK_POST_EXE" ] && targ=charmrun
1811   convhost=conv-host$CMK_POST_EXE
1812
1813   if [ -x $CHARMBIN/$targ ]
1814   then
1815         DoNoErrCheck $RM $targ
1816         Do $CP $CHARMBIN/$targ $targ
1817   fi
1818 fi
1819
1820 [ -z "$SAVE" ] && DoNoErrCheck $RM $modInitSrc $modInitObj > /dev/null 2>&1
1821
1822 Copyobj
1823
1824