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