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