6f9add828fecc90f8ede6f87da240cf2ed466bed
[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 PROCESSED_MODULES=""
812
813 # Look up and add the dependencies for module $1
814 findModuleDep() {
815         M=$1
816         notInList $M $PROCESSED_MODULES || return
817         PROCESSED_MODULES="$PROCESSED_MODULES $M"
818         PRE_LIBRARIES=`AddList $PRE_LIBRARIES -lmodule$M`
819 # HACK: add modules to both ends of library list, to avoid
820 #  undefined symbol CkRegisterMainModule when it's in a module file.
821         POST_LIBRARIES=`AddList $POST_LIBRARIES -lmodule$M`
822         if [ "$M" = "PythonCCS" ]
823             then
824             LINK_PYTHON=1
825         fi
826
827         dep=""
828 # fixme: should search everything in the -L library paths:
829         for dir in `echo . $CHARMLIB`
830         do
831                 d="$dir/libmodule$M.dep"
832                 [ -r "$d" ] && dep=$d
833         done
834         if [ -r "$dep" ]
835         then
836                 # Add the module dependencies in this file
837                 # Debugf "Adding module" $M "dependencies from" $dep
838                 deps=`cat $dep`
839                 Debugf "Module" $M "adds dependencies:" $deps
840                 processArgs $deps
841         fi
842 }
843
844 # Repeat the module-dependency search until we find no
845 #  new modules:
846 START_MODULES=""
847 while [ "$START_MODULES" != "$MODULES" ]
848 do
849         Debugf "----------- Module dependency search ------------"
850         START_MODULES="$MODULES"
851         for m in $MODULES
852         do
853                 findModuleDep $m
854         done
855 done
856
857 Debugf "----------- Module dependency search complete ------------"
858
859
860
861 ##############################################################################
862 #
863 # Load machine-specific configuration data, then handle overrides to it.
864 #
865 ##############################################################################
866
867
868 if [ x"$CMK_NO_BUILD_SHARED" = "xfalse" ]
869 then
870         BUILD_SHARE="1"
871 fi
872
873 if [ "$BUILD_SHARE" = "1" ]
874 then
875         if [ x"$CMK_NO_BUILD_SHARED" = "xtrue" -o -z "$CHARMLIBSO" ]
876         then
877                 BUILD_SHARE="0"
878         else
879                 USE_PIC="1"
880         fi
881 fi
882
883 OPTS_PIC="$CMK_PIC"
884 if [ "$USE_PIC" = "1" ]
885 then
886        OPTS_CC="$OPTS_CC $OPTS_PIC"
887        OPTS_CXX="$OPTS_CXX $OPTS_PIC"
888        OPTS_F90="$OPTS_F90 $OPTS_PIC"
889        OPTS_LD="$OPTS_LD $OPTS_PIC"
890 fi
891
892 if [ "$BUILD_SHARE" = "1" ]
893 then
894         # ignore BUILD_SHARE if the target is not .a or .so
895         case "$OBJECT" in
896         *.a|*.so|*.so.*|*.sl|*.dylib)
897                 OPTS_LD="$OPTS_LD $CMK_LD_SHARED $OPTS_LDRO -L$CHARMLIBSO"
898                 ;;
899         *)      
900                 BUILD_SHARE="0"
901                 ;;
902         esac
903 fi
904
905 if [ -n "$OVERRIDE_CC" ]
906 then
907     CMK_CC="$OVERRIDE_CC"
908 fi
909 Debug "set 2"
910 if [ -n "$OVERRIDE_CXX" ]
911 then
912     CMK_CXX="$OVERRIDE_CXX"
913 fi
914
915 if [ -n "$OVERRIDE_F90" ]
916 then
917     CMK_CF90="$OVERRIDE_F90"
918 fi
919
920 if [ -n "$OVERRIDE_LD" ]
921 then
922     CMK_LD="$OVERRIDE_LD"
923 fi
924
925 if [ -n "$OVERRIDE_LDXX" ]
926 then
927     CMK_LDXX="$OVERRIDE_LDXX"
928 fi
929
930 if [ -n "$USE_RELIABLE_CC" ]
931 then
932         CMK_CC="$CMK_CC_RELIABLE"
933 fi
934
935 if [ -n "$USE_FASTEST_CC" ]
936 then
937     CMK_CC="$CMK_CC_FASTEST"
938 fi
939
940 if [ -n "$USE_F77" ]
941 then
942     CMK_CF90=$CMK_CF77
943     CMK_CF90_FIXED=$CMK_CF77
944     CMK_F90LIBS=$CMK_F77LIBS
945 fi
946
947 if [ -n "$CMK_F90_MODINC" ]
948 then
949     CMK_CF90_MODINC="$CMK_F90_MODINC"
950 else
951     CMK_CF90_MODINC="-M"
952 fi
953
954 if [ -n "$F90_MODDIR" ]
955 then
956     for dir in $F90_MODDIR
957     do
958       OPTS_F90="$OPTS_F90 $CMK_CF90_MODINC $dir"
959     done
960 fi
961
962 if [ -n "$CMK_F90_USE_MODDIR" ]
963 then
964     CMK_CF90="$CMK_CF90 $CMK_CF90_MODINC$CHARMINC "
965 fi
966
967 if [ -n "$SEQUENTIAL" ]
968 then
969     CMK_CC="$CMK_SEQ_CC -DCMK_SEQUENTIAL=1 "
970     CMK_LD="$CMK_SEQ_LD"
971     CMK_CXX="$CMK_SEQ_CXX -DCMK_SEQUENTIAL=1 " 
972     CMK_CF90="$CMK_SEQ_F90 " 
973     CMK_LDXX="$CMK_SEQ_LDXX" 
974     CMK_AR="$CMK_SEQ_AR"
975     CMK_RANLIB="$CMK_SEQ_RANLIB"
976 fi
977
978 if [ -n "$NATIVE" ]
979 then
980     CMK_CC="$CMK_NATIVE_CC " 
981     CMK_LD="$CMK_NATIVE_LD" 
982     CMK_CXX="$CMK_NATIVE_CXX " 
983     CMK_LDXX="$CMK_NATIVE_LDXX" 
984 fi
985
986 if [ -n "$PREPROCESS" ]
987 then
988     CMK_CF90=$CMK_FPP
989 fi
990
991 if [ -n "$PURIFY" ]
992 then
993     CMK_LD="purify $CMK_LD"
994     CMK_LDXX="purify $CMK_LDXX"
995 fi
996
997 Debug "set 4"
998 if [ "$OPTIMIZE_MODE" = "true" ]
999 then
1000     OPTS_CC="$CMK_C_OPTIMIZE $OPTS_CC"
1001     OPTS_CXX="$CMK_CXX_OPTIMIZE $OPTS_CXX"
1002     OPTS_F90="$CMK_F90_OPTIMIZE $OPTS_F90"
1003     OPTS_LD="$CMK_C_OPTIMIZE $OPTS_LD"
1004     OPTS_LDXX="$CMK_CXX_OPTIMIZE $OPTS_LDXX"
1005 fi
1006
1007 # Pick up per-architecture production mode settings
1008 if [ "$PRODUCTION_MODE" = "true" ]
1009 then
1010     OPTS_CC="$CMK_PRODUCTION $CMK_C_PRODUCTION $OPTS_CC"
1011     OPTS_CXX="$CMK_PRODUCTION $CMK_CXX_PRODUCTION $OPTS_CXX"
1012     OPTS_F90="$CMK_PRODUCTION $CMK_F90_PRODUCTION $OPTS_F90"
1013     OPTS_LD="$CMK_PRODUCTION $CMK_C_PRODUCTION $OPTS_LD"
1014     OPTS_LDXX="$CMK_PRODUCTION $CMK_CXX_PRODUCTION $OPTS_LDXX"
1015 fi
1016
1017 if [ -n "$XI_INTERNAL" ]
1018 then
1019     CMK_XIOPTS="$CMK_XIOPTS -intrinsic"
1020 fi
1021
1022 if [ "$GENDEPENDS" = "yes" ]
1023 then
1024     CMK_XIOPTS="$CMK_XIOPTS -M"
1025 fi
1026
1027 if test "$CMK_WITH_TAU" = "true"
1028 then
1029   echo "include $TAU_MAKEFILE" > _make.tau.tmp
1030   echo "print-%:; @echo \$(\$*)" >> _make.tau.tmp
1031   TAU_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_LIBS`
1032   TAU_MPI_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_LIBS` 
1033   TAU_MPI_FLIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_FLIBS`
1034   TAU_DEFS=`make --no-print-directory -f _make.tau.tmp print-TAU_DEFS`
1035   TAU_INCLUDE=`make --no-print-directory -f _make.tau.tmp print-TAU_INCLUDE`
1036
1037   rm -f _make.tau.tmp
1038 fi
1039
1040 #Add generic options to the compiler- and linker-options
1041 OPTS_CC="$OPTS $TAU_DEFS $TAU_INCLUDE $OPTS_CC"
1042 OPTS_CXX="$OPTS $TAU_DEFS $TAU_INCLUDE $OPTS_CXX"
1043 OPTS_F90="$OPTS $OPTS_F90"
1044 OPTS_LD="$OPTS $OPTS_LD"
1045 # OPTS_LDXX gets OPTS from OPTS_LD
1046
1047 if [ -n "$F90_MAIN" ] 
1048 then
1049 # for_main needs to be placed at beginning
1050         CMK_LD="$CMK_LD $CMK_F90MAINLIBS"
1051         CMK_LDXX="$CMK_LDXX $CMK_F90MAINLIBS"
1052 fi
1053
1054 # There's really no analog for this in the Bourne shell: 
1055 #  -------- C Shell ------> onintr failure
1056 # Luckily, it's irrelevant as long as nobody sends us a ^Z.
1057
1058 ##############################################################################
1059 #
1060 # Check for valid choice of LANGUAGE
1061 #
1062 ##############################################################################
1063 Debug "Checking language..."
1064
1065 if [ -z "$LANGUAGE" ]
1066 then
1067     LANGUAGE="charm++"
1068     [ -n "$SEQUENTIAL" ] && LANGUAGE=c
1069 fi
1070
1071 case "$LANGUAGE" in
1072 "c"|"C"|"c++"|"C++")
1073         [ -z "$SEQUENTIAL" -a -z "$NATIVE" ] && Abort "Language $LANGUAGE is for sequential programs"
1074         ;;
1075 esac
1076
1077 # update charm to bgcharm
1078 if [ "$LANGUAGE" = "charm++" -a -n "$CMK_BLUEGENE" ]
1079 then
1080         LANGUAGE="bgcharm++"
1081 fi
1082 if [ "$LANGUAGE" = "converse++" -a -n "$CMK_BLUEGENE" ]
1083 then
1084         LANGUAGE="bgconverse++"
1085 fi
1086 if [ "$LANGUAGE" = "bgcharm++" ]
1087 then
1088         TRACEMODE=`AddList $TRACEMODE bluegene`
1089 fi
1090
1091 ########################################################################
1092 #
1093 # We've parsed and verified the command-line parameters.
1094 # Now we prepare routines to clean up and exit.
1095 # None of these routines ever returns-- they all just exit.
1096 #
1097 ########################################################################
1098
1099
1100 Copyobj() {
1101         if [ "a$COPYTO" != "a" -a "a$COPYTO" != "a$OBJECT" ] 
1102         then
1103                 [ -d $COPYTO ] && COPYTO="$COPYTO/"
1104                 if [ "a$PROG_EXT" != "a" -a -r "$OBJECT$PROG_EXT" ] ; then
1105                         Warning "Appending $PROG_EXT to object file name"
1106                         OBJECT="$OBJECT$PROG_EXT"
1107                 fi
1108                 [ ! -d $COPYTO ] && DoNoErrCheck $RM $COPYTO 
1109                 Do $CP $OBJECT $COPYTO
1110         fi
1111         test -n "$COPY_EXIT" && Success
1112 }
1113
1114 Copymod() {
1115         for COPY in `echo $MODCOPIES`
1116         do
1117                 if [ "a$COPY" != "a$OBJECT" ] 
1118                 then
1119                         [ -d $COPY ] && COPY="$COPY/"
1120                         if [ -n "$CMK_MOD_NAME_ALLCAPS" ] ; then
1121                           OBJECT=`echo $OBJECT | tr "a-z" "A-Z"`
1122                         fi
1123                         if [ -r "$OBJECT.$CMK_MOD_EXT" ] ; then
1124                           Warning "Appending .$CMK_MOD_EXT to object file name"
1125                           OBJECT="$OBJECT.$CMK_MOD_EXT"
1126                         fi
1127                         [ ! -d $COPY ] && DoNoErrCheck $RM $COPY 
1128                         Do $CP $OBJECT $COPY
1129                 fi
1130         done
1131         Success
1132 }
1133
1134 Success() {
1135         End 0
1136 }
1137
1138
1139 ##############################################################################
1140 #
1141 # Preprocess the GEN-CPM files
1142 #
1143 ##############################################################################
1144
1145 Debug "Preprocessing GEN-CPM files"
1146
1147 for FILE in `echo $GENCPM`
1148 do
1149 # This used to be "$FILE:r", which strips the extention
1150         BASE=`stripExtention $FILE`
1151         TMP=`basename $FILE`".TMP"
1152         DELETE="$DELETE $TMP.c $TMP.i"
1153         DoNoErrCheck $RM $BASE.cpm.h $TMP.c
1154         Do touch $BASE.cpm.h
1155         Do $LN -f -s $FILE $TMP.c
1156         Do $CMK_CPP_C -I$CHARMINC $OPTS_CC $OPTS_CPP $TMP.c > $TMP.i
1157         Do $CHARMBIN/conv-cpm $TMP.i $BASE.cpm.h
1158 done
1159
1160 ##############################################################################
1161 #
1162 # Compile all specified files
1163 #
1164 # All temporary files named *.TMP.* for easy cleanup.
1165 #
1166 ##############################################################################
1167
1168 Debug "About to compile..."
1169
1170 numFiles=`echo $FILES | wc -w`
1171 for FILE in `echo $FILES`
1172 do
1173         BASE=`stripExtention \`basename $FILE\``
1174         TMP=`basename $FILE`".TMP"
1175         FILE_EXT=`getExtention $FILE`
1176         BASEO="$BASE.o"
1177         DESTO="-o $BASEO"
1178
1179         # in case of: charmc -o object.o -c foo.c,
1180         # avoid foo.c => remove foo.o => foo.o => object.o,
1181         # instead generate object.o directly.
1182         # remove and generate foo.o may be dangerous in parallel make 
1183         # for example in the case of compiling several objs from one 
1184         # same c file such as memory.c.
1185         if [ $numFiles = 1 -a -n "$OBJECT" ]
1186         then
1187                 OBJ_EXT=`getExtention $OBJECT`
1188                 if [ "$OBJ_EXT" = '.o' -o "$OBJ_EXT" = '.co' ]
1189                 then
1190                     BASEO=$OBJECT
1191                     DESTO="-o $OBJECT"
1192                 fi
1193         fi
1194
1195         if [ "$FILE_EXT" != ".o" -a "$FILE_EXT" != ".co" -a "$FILE_EXT" != ".M" -a "$FILE_EXT" != ".ci" ]
1196         then
1197                 [ $VERBOSE ] && echo "Compiling $FILE"
1198                 DoNoErrCheck $RM $BASEO $BASE.f.o
1199         fi
1200         if [ ! -z "$SKIPLINK" ] 
1201         then
1202                 DESTO=""
1203         fi
1204         NU_OBJ=""
1205         case "$FILE" in
1206         *.ci)
1207                 XIOPTS=$CMK_XIOPTS
1208                 test "$LANGUAGE" = "f90charm" && XIOPTS="$XIOPTS -f90"
1209                 if [ -n "$PREPROCESS" ]
1210                 then
1211                   Do $CMK_CPP_CHARM $OPTS_CPP $FILE | Do $CHARMBIN/charmxi $XIOPTS
1212                 else
1213                   Do $CHARMBIN/charmxi $XIOPTS $FILE
1214                 fi
1215                 ;;
1216         *.c|*.s)
1217                 Do $CMK_CC -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CC -c $FILE $DESTO
1218                 NU_OBJ=$BASEO
1219                 ;;
1220         bglmachine.C)
1221                 Do $CMK_GCXX -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $FILE $DESTO
1222                 NU_OBJ=$BASEO
1223                 ;;
1224         *.C|*.cc|*.cxx|*.cpp|*.c++|*.cu)
1225                 Do $CMK_CXX -I$CHARMINC $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $FILE $DESTO
1226                 NU_OBJ=$BASEO
1227                 ;;
1228         *.f|*.F)
1229                 test -z "$CMK_CF90_FIXED" && Abort "No F90 compiler (CMK_CF90_FIXED) defined"
1230                 Do $CMK_CF90_FIXED -I$CHARMINC $OPTS_CPP $OPTS_F90 -c $FILE $DESTO
1231                 NU_OBJ=$BASEO
1232                 ;;
1233         *.f90|*.F90)
1234                 test -z "$CMK_CF90" && Abort "No F90 compiler (CMK_CF90) defined"
1235                 test -z "$PREPROCESS" && OPTS_F90="$OPTS_F90 -c"
1236                 Do $CMK_CF90 -I$CHARMINC $OPTS_CPP $OPTS_F90 $FILE $DESTO
1237                 NU_OBJ=$BASEO
1238                 ;;
1239         *.fc|*.FC)
1240                 Abort "I'm not yet smart enough to compile $FILE"
1241                 ;;
1242         *.o|*.so|*.so.*|*.sl|*.a|*.dylib|*.co)
1243                 [ ! -s $FILE ] && Abort "$FILE: file not recognized: File truncated"
1244                 OBJECTFILES="$OBJECTFILES $FILE"
1245                 ;;
1246         *.M|*.mod)
1247                 OBJECT="$BASE"
1248                 Copymod
1249                 Success
1250                 ;;
1251         *)
1252                 Abort "file with unrecognized extension $FILE"
1253         esac
1254 # Handle new object files
1255         if [ -n "$NU_OBJ" -a -n "$OBJECT" ] 
1256         then
1257                 if [ "$OBJECT" != "$BASEO" ]
1258                 then
1259                         DELETE="$DELETE $NU_OBJ"
1260                 fi
1261                 OBJECTFILES="$OBJECTFILES $NU_OBJ"
1262         fi
1263 done
1264
1265 ##############################################################################
1266 #
1267 #                        POSSIBLY, SKIP LINK-STEP
1268 #
1269 # 1. No $OBJECT may be specified at all --- its just a compilation.
1270 # 2. If $OBJECT is a ".a" file, a library is created from $OBJECTFILES.
1271 # 3. If $OBJECT is a ".o" file, then an "LDRO" is created from $OBJECTFILES.
1272 # 4. Language may be sequential.  Do a quick and easy linkage.
1273 #
1274 #               in any of these cases, the full link step is skipped.
1275 #
1276 ##############################################################################
1277
1278 MakeSO() {
1279         Debugf "Running ldro..."
1280         DoNoErrCheck $RM $OBJECT
1281         # convert to absolute path if required
1282         if test -n "$CMK_LD_SHARED_ABSOLUTE_PATH"
1283         then
1284         dir=`dirname $OBJECT`
1285         fname=`basename $OBJECT`
1286         dir=`cd $dir; pwd`
1287         OBJECT=$dir/$fname
1288         fi
1289         Warning "Warning: creating $OBJECT "
1290         if test "$MAKE_LDXX" = "1"
1291         then
1292           soCmd="$CMK_LDXX -o $OBJECT $OPTS_LD $OPTS_LDRO $OBJECTFILES $PRE_LIBRARIES $POST_LIBRARIES $CMK_LD_SHARED_LIBS"
1293         else
1294           soCmd="$CMK_LD -o $OBJECT $OPTS_LD $OPTS_LDRO $OBJECTFILES $PRE_LIBRARIES $POST_LIBRARIES $CMK_LD_SHARED_LIBS"
1295         fi
1296         DoNoErrCheck $soCmd
1297         Do_res=$?
1298         if [ $Do_res -ne 0 ] 
1299         then
1300         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
1301         echo "!!  Failed to compile Charm++ shared library. You can disable shared  !!"
1302         echo "!!  lib compilation by providing --no-build-shared build option like: !!"
1303         echo "!!     ./build charm++ net-linux --no-build-shared -O                 !!"
1304         echo "!!  or reexecute this charmc command with --no-build-shared compile option        !!"
1305         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
1306         Abort "Command $soCmd returned error code $Do_res"
1307         fi
1308         Copyobj
1309 }
1310
1311 if [ -z "$OBJECT" -o ! -z "$SKIPLINK" ] 
1312 then
1313 # We have no target object, or are playing preprocessor tricks-- just end
1314         Debugf "No target object-- finished"
1315         Success
1316 fi
1317
1318 Debug "About to link..."
1319
1320 case "$OBJECT" in
1321 *.o)
1322         Debugf "Converting $FILES to $OBJECTFILES"
1323         nFiles=`echo $FILES | wc -w`
1324         nObj=`echo $OBJECTFILES | wc -w`
1325 # One source file, one .o: just move the .o
1326         if [ $OBJECT != $OBJECTFILES ]
1327         then
1328                 Debugf "Moving .o to destination"
1329                 DoNoErrCheck $RM $OBJECT
1330                 Do $MV $OBJECTFILES $OBJECT
1331         fi
1332         Copyobj
1333         ;;
1334 *.so|*.so.*|*.sl|*.dylib)
1335 # Make shared library
1336         # if no build share is set, exit normally.
1337         if [ "$BUILD_SHARE" = 0 ]
1338         then
1339                 echo "Warning: building shared library is skipped, possibly due to not supported, check configuration files."
1340                 End 0
1341         elif [ "$CHARM_SHARED" = "0" ]
1342         then
1343                 MakeSO
1344         fi
1345         ;;
1346 *.a)
1347 # Make regular (.a, non-shared) library
1348         # in case building charm so, build .so under lib_so as well # Gengbin
1349         if [ "$BUILD_SHARE" = "1" ]
1350         then
1351           COPY_EXIT=""
1352           OBJECT_SAVE=$OBJECT
1353           COPYTO_SAVE=$COPYTO
1354           if [ -z "$COPYTO" ]
1355           then
1356             DESTDIR=`echo $OBJECT | sed -e 's@lib/[^/]*\.a$@lib_so@'`
1357             OBJECT=`echo $OBJECT | sed -e 's@lib/\([^/]*\)\.a$@lib_so/\1.a@'`
1358           else
1359             COPYTO=`echo $COPYTO | sed -e 's@lib[/]*$@lib_so@'`
1360             DESTDIR=$COPYTO
1361           fi
1362           OBJECT=`echo $OBJECT | sed -e 's/\.a$/.'"$CMK_SHARED_SUF"'/'`
1363           [ -f $DESTDIR/.charmso ] && MakeSO
1364           COPYTO=$COPYTO_SAVE
1365           OBJECT=$OBJECT_SAVE
1366           COPY_EXIT="true"
1367         fi
1368         # build normal .a
1369         DoNoErrCheck $RM $OBJECT
1370         Do $CMK_AR $OBJECT $OBJECTFILES
1371         Do $CMK_RANLIB $OBJECT
1372         Copyobj
1373         ;;
1374 esac
1375 # If the above case returns, we're not creating a .o or .a file,
1376 # so linking is needed.
1377
1378 ############ Determine the language and libraries ##########
1379
1380 # Parse out the real languages
1381 case "$LANGUAGE" in
1382 "charm++f")
1383         LANGUAGE="charm++"
1384         USE_F90_LIBRARIES="1"
1385         ;;
1386 "f90charm")
1387         PRE_LIBRARIES="$PRE_LIBRARIES -lf90charm -lckf"
1388         POST_LIBRARIES="$POST_LIBRARIES $CMK_F90LIBS"
1389         ;;
1390 "bluegene"|"bigsim")
1391         PRE_LIBRARIES="$PRE_LIBRARIES -lconv-bigsim -lconv-bigsim-logs"
1392         ;;
1393 "ckbluegene")
1394         POST_LIBRARIES="$POST_LIBRARIES -lbluegene"
1395         ;;
1396 "bgcharm++")
1397         PRE_LIBRARIES="$PRE_LIBRARIES -lconv-bigsim -lconv-bigsim-logs -lconv-bluegene-stub"
1398         ;;
1399 "bgconverse++")
1400         MIDDLE_LIBS="$MIDDLE_LIBS -lconv-bigsim -lconv-bigsim-logs"
1401         ;;
1402 esac
1403
1404 # DMK - ACCEL SUPPORT - If this is a cell build, add the spe code object to POST_LIBRARIES
1405 if [ $CMK_CELL -eq 1 ]
1406 then
1407
1408   if [ "$LANGUAGE" == "converse++" ]
1409   then
1410     # Include a "do nothing" register_accel_spe_funcs() function
1411     POST_LIBRARIES="$POST_LIBRARIES -lnoAccelStub"
1412   fi
1413
1414   if [ "$LANGUAGE" == "charm++" ]
1415   then
1416
1417     if [ $SPU_USE_CPP -eq 1 ]
1418     then
1419         SPU_COMPILER=$CMK_SPU_CXX
1420         SPU_LINKER=$CMK_SPU_LDXX
1421     else
1422         SPU_COMPILER=$CMK_SPU_CC
1423         SPU_LINKER=$CMK_SPU_LD
1424     fi
1425
1426     # Make the spe object file to be included
1427     Do $SPU_COMPILER -c -I$CHARMINC -o main__funcLookup__.genSPECode.o main__funcLookup__.genSPECode.c -DCMK_CELL_SPE=1
1428     Do $SPU_LINKER -L$CHARMLIB -o main__funcLookup__.genSPECode main__funcLookup__.genSPECode.o $CMK_SPERT_LIBS $SPU_EXTRA_LIBS
1429     Do $CMK_PPU_EMBEDSPU spert_main main__funcLookup__.genSPECode main__funcLookup__.genSPECode.ppe.o
1430
1431     # Include the spe object file for linking into main executable
1432     POST_LIBRARIES="$POST_LIBRARIES main__funcLookup__.genSPECode.ppe.o"
1433
1434   fi
1435 fi
1436
1437 if [ "$AMPIMAIN" != "" ]
1438 then
1439         OBJECTFILES="$CHARMLIB/lib$AMPIMAIN.o $OBJECTFILES"
1440 fi
1441
1442 if [ "$SWAPGLOBALS" = "1" ]
1443 then
1444         swapo="$CHARMLIB/libglobal-swap.o"
1445         if [ ! -r "$swapo" -o ! -s "$swapo" ]
1446         then
1447                 echo "Global-swapping library $swapo missing--"
1448                 echo "  Is -swapglobals supported on this machine?"
1449                 exit 1
1450         else
1451                 OBJECTFILES="$swapo $OBJECTFILES"
1452         fi
1453 fi
1454 if [ "$SWAPGLOBALS" = "2" ]
1455 then
1456         swapo="$CHARMLIB/libglobal-copy.o"
1457         if [ ! -r "$swapo" -o ! -s "$swapo" ]
1458         then
1459                 echo "Global-copying library $swapo missing--"
1460                 echo "  Is -copyglobals supported on this machine?"
1461                 exit 1
1462         else
1463                 OBJECTFILES="$swapo $OBJECTFILES $CHARMLIB/loadsym.o"
1464         fi
1465 fi
1466
1467 if [ $USE_F77_LIBRARIES = "1" ]
1468 then
1469         POST_LIBRARIES="$POST_LIBRARIES $CMK_F77LIBS"
1470 fi
1471
1472 if [ $USE_F90_LIBRARIES = "1" ]
1473 then
1474         if [ ! -r $CHARMLIB/libconv-utilf.a ]
1475         then
1476                 echo "Fortran support library $CHARMLIB/libconv-utilf.a missing"
1477                 echo "Is your Fortran compiler working?"
1478                 exit 1
1479         fi
1480         
1481         POST_LIBRARIES="$POST_LIBRARIES -lconv-utilf $CMK_F90LIBS"
1482         if [ -z "$SEQUENTIAL" -a ! -z "$CMK_F90OBJS" ] 
1483         then
1484           for f90obj in $CMK_F90OBJS
1485           do
1486             OBJECTFILES="$OBJECTFILES $CHARMLIB/$f90obj"
1487           done
1488         fi
1489 fi
1490
1491 # check if we're linking a sequential object
1492 case "$LANGUAGE" in
1493 "c"|"C"|"f90"|"f77")
1494         if [ -n "$NATIVE" ]
1495         then
1496           Do $CMK_NATIVE_LD $OPTS_LD -o $OBJECT $OBJECTFILES \
1497                 -L$CHARMLIB $PRE_LIBRARIES $CMK_NATIVE_LIBS $POST_LIBRARIES
1498         else
1499           Do $CMK_SEQ_LD $OPTS_LD -o $OBJECT $OBJECTFILES \
1500                 -L$CHARMLIB $PRE_LIBRARIES $CMK_SEQ_LIBS $POST_LIBRARIES
1501         fi
1502         Copyobj
1503         ;;
1504 "c++"|"C++")
1505         if [ -n "$NATIVE" ]
1506         then
1507           Do $CMK_NATIVE_LDXX $OPTS_LD -o $OBJECT $OBJECTFILES \
1508                 -L$CHARMLIB $PRE_LIBRARIES $CMK_NATIVE_LIBS $POST_LIBRARIES
1509         else
1510           Do $CMK_SEQ_LDXX $OPTS_LD -o $OBJECT $OBJECTFILES \
1511                 -L$CHARMLIB $PRE_LIBRARIES $CMK_SEQ_LIBS $POST_LIBRARIES
1512         fi
1513         Copyobj
1514         ;;
1515 esac
1516
1517 ##############################################################################
1518 #
1519 # Some quick consistency checks prior to full link-step
1520 #
1521 # Check for valid choice of TRACEMODE
1522 # Check for valid choice of BALANCE
1523 # Check for compatibility among BALANCE and TRACEMODE
1524 #
1525 ##############################################################################
1526
1527 Debug "About to check TRACEMODE=$TRACEMODE and BALANCE=$BALANCE..."
1528
1529 # Check for valid choice of TRACEMODE
1530
1531 # [ -r $CHARMLIB/libtrace-projections.a ] || Abort "trace libraries not installed.  Is Charm++ built properly?"
1532
1533 if [ x"$TRACEMODE" != x ]
1534 then
1535 for trace in $TRACEMODE; do
1536 [ ! -r "$CHARMLIB/libtrace-$trace.a" ] && Abort "charmc : No such tracemode $trace"
1537 TRACE_OBJ="$TRACE_OBJ -ltrace-$trace"
1538 done
1539 TRACE_OBJ="$TRACE_OBJ $CMK_LIBZ"
1540 fi
1541
1542 if test x$LANGUAGE = x"bgcharm++"
1543 then 
1544     BALANCE=bluegene
1545 fi
1546
1547 BAL_EXT=`getExtention $BALANCE`
1548 if [ -z "$BAL_EXT" ]
1549 then
1550 # Balance has no extention-- is a library reference
1551     BAL_OBJ="$CHARMLIB/libldb-$BALANCE.o"
1552 else
1553 # Balance has some extention-- must be a .o or .a file
1554     BAL_OBJ="$BALANCE"
1555 fi
1556
1557 Debug "Finished with BAL_OBJ=$BAL_OBJ, TRACEMODE=$TRACEMODE..."
1558
1559 if [ ! -r "$BAL_OBJ" ]
1560 then
1561         if [ "$BALANCE" = "$BAL_OBJ" ]
1562         then
1563                 Abort "Could not find load balancer object $BAL_OBJ"
1564         else
1565                 (cd $CHARMLIB ; ls -al libldb-*)
1566                 Abort "Unknown load balancer $BALANCE / $BAL_OBJ"
1567         fi
1568 fi
1569
1570
1571 # Check for valid choice of MEMORY
1572
1573 MEM_OBJ="$CHARMLIB/libmemory-$MEMORY.o"
1574
1575 if [ ! -r $MEM_OBJ -o ! -s $MEM_OBJ ] 
1576 then
1577   (cd $CHARMLIB ; list=`ls libmemory-* 2>/dev/null`; test -n "$list" && for i in $list; do [ -s $i ] && ls -l $i; done )
1578   Abort "charmc : No such memory mode $MEMORY"
1579 fi
1580
1581 # For memory wrapping around the OS allocator, need to add also the wrapper object
1582 case $MEMORY in
1583   os-*)
1584   MEM_OBJ=$MEM_OBJ" $CHARMLIB/libmemory-os-wrapper.o"
1585   ;;
1586 esac
1587
1588 # Check for valid choice of THREAD
1589
1590 if [ "$CHARM_SHARED" = "1" -a -n "$CMK_LD_SHARED_THREAD" -a "$THREAD" = "default" ]
1591 then
1592   THREAD=$CMK_LD_SHARED_THREAD
1593 fi
1594 if [ "$TLSGLOBALS"  = "1" ]
1595 then
1596     # make sure we don't add an extra -tls
1597   if ! echo $THREAD | grep '\-tls$' > /dev/null 2> /dev/null
1598   then
1599     THREAD=${THREAD}-tls
1600   fi
1601 fi
1602 THREAD_OBJ="$CHARMLIB/libthreads-$THREAD.o"
1603
1604 if [ ! -r $THREAD_OBJ -o ! -s $THREAD_OBJ ] 
1605 then
1606   (cd $CHARMLIB ; list=`ls libthreads-* 2>/dev/null`; test -n "$list" && for i in $list; do [ -s $i ] && ls -l $i; done )
1607   Abort "charmc : No such thread mode $THREAD"
1608 fi
1609
1610 ################# Build the module initialization function ##################
1611 modInitName="moduleinit$$"
1612 modInitSrc="$modInitName.C"
1613 modInitObj="$modInitName.o"
1614 DoNoErrCheck $RM $modInitSrc $modInitObj > /dev/null 2>&1
1615
1616 for module in $MODULES; do
1617     echo "extern void _register$module(void);" >> $modInitSrc
1618 done
1619 for trace in $TRACEMODE; do
1620     if test $trace = "summary" 
1621     then
1622       echo "  extern void _registerTraceSummary();" >> $modInitSrc
1623     elif test $trace = "projections"
1624     then
1625       echo "  extern void _registerTraceProjections();" >> $modInitSrc
1626     elif test $trace = "simple"
1627     then
1628       echo "  extern void _registerTraceSimple();" >> $modInitSrc
1629     elif test $trace = "Tau"
1630     then
1631       echo "  extern void _registerTraceTau();" >> $modInitSrc
1632     elif test $trace = "utilization" 
1633     then 
1634       echo "  extern void _registerTraceUtilization();" >> $modInitSrc 
1635     elif test $trace = "controlPoints"
1636     then
1637       echo "  extern void _registerTraceControlPoints();" >> $modInitSrc
1638     elif test $trace = "all"
1639     then
1640       echo "  extern void _registerTraceProjections();" >> $modInitSrc
1641       echo "  extern void _registerTraceSummary();" >> $modInitSrc
1642     fi
1643 done
1644 if test -n "$BALANCER"
1645 then
1646     echo "extern void LBDefaultCreate(const char *);" >> $modInitSrc
1647 fi
1648 echo "void _registerExternalModules(char **argv) {" >> $modInitSrc
1649 for module in $MODULES; do
1650     [ -n "$VERBOSE" ] && echo "Adding registration for module $module"
1651     echo "  _register$module();" >> $modInitSrc
1652 done
1653 for trace in $TRACEMODE; do
1654     if test $trace = "summary"
1655     then
1656       echo "  _registerTraceSummary();" >> $modInitSrc
1657     elif test $trace = "projections"
1658     then
1659       echo "  _registerTraceProjections();" >> $modInitSrc
1660     elif test $trace = "Tau"
1661     then
1662       TRACE_WITH_TAU=1
1663       echo "  _registerTraceTau();" >> $modInitSrc
1664     elif test $trace = "simple"
1665     then
1666       echo "  _registerTraceSimple();" >> $modInitSrc
1667     elif test $trace = "utilization" 
1668     then 
1669       echo "  _registerTraceUtilization();" >>  $modInitSrc 
1670     elif test $trace = "controlPoints"
1671     then
1672       echo "  _registerTraceControlPoints();" >> $modInitSrc
1673     elif test $trace = "all"
1674     then
1675       echo "  _registerTraceProjections();" >> $modInitSrc
1676       echo "  _registerTraceSummary();" >> $modInitSrc
1677     fi
1678 done
1679 if test -n "$BALANCER"
1680 then
1681     [ -n "$VERBOSE" ] && echo "Adding load balancer for $BALANCER"
1682     for deflb in $BALANCER
1683     do
1684       echo "  LBDefaultCreate(\"$deflb\");" >> $modInitSrc
1685     done
1686 fi
1687 echo "}" >> $modInitSrc
1688
1689 # creating projections
1690 echo "void _createTraces(char **argv) {" >> $modInitSrc
1691 for trace in $TRACEMODE; do
1692     [ -n "$VERBOSE" ] && echo "Adding registration for trace $trace"
1693     echo "extern void _createTrace$trace(char **argv);" >> $modInitSrc
1694     echo "_createTrace$trace(argv);" >> $modInitSrc
1695 done
1696 echo "}" >> $modInitSrc
1697
1698 Do $CMK_CXX $OPTS_CPP_INTERNAL $OPTS_CPP $OPTS_CXX -c $modInitSrc -o $modInitObj
1699
1700 ###############################################################################
1701 #
1702 #                               Finally, LINK
1703 #
1704 ###############################################################################
1705
1706 Debug "About to perform final link..."
1707
1708 MAKE_LDXX="0"
1709 MAKE_LD="0"
1710
1711 CORE_LIBS="-lconv-core -lconv-util $TRACE_OBJ "
1712
1713 # get TAU stub makefile variables
1714 if test "$CMK_WITH_TAU" = "true"
1715 then
1716   echo "include $TAU_MAKEFILE" > _make.tau.tmp
1717   echo "print-%:; @echo \$(\$*)" >> _make.tau.tmp
1718   TAU_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_LIBS`
1719   TAU_MPI_LIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_LIBS` 
1720   TAU_MPI_FLIBS=`make --no-print-directory -f _make.tau.tmp print-TAU_MPI_FLIBS`
1721   TAU_DEFS=`make --no-print-directory -f _make.tau.tmp print-TAU_DEFS`
1722   TAU_INCLUDE=`make --no-print-directory -f _make.tau.tmp print-TAU_INCLUDE`
1723
1724   rm -f _make.tau.tmp
1725 fi
1726 # done getting TAU variables
1727
1728 case "$LANGUAGE" in
1729 "f90charm")
1730         MAKE_LDXX="1"
1731         MIDDLE_LIBS="-lck -lconv-cplus-y $CORE_LIBS "
1732         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1733         if [ "$TAU_TRACE_MPI" = "1" ]
1734         then
1735                 TAU_LIBS="$TAU_MPI_FLIBS $TAU_LIBS"
1736         else
1737                 TAU_LIBS="$TAU_LIBS"
1738         fi
1739         ;;
1740 "charm"|"charm++"|"f90charm"|"ckbluegene"|"bgcharm++")
1741         MAKE_LDXX="1"
1742         MIDDLE_LIBS="-lck -lconv-cplus-y $CORE_LIBS "
1743         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1744         if [ "$TAU_TRACE_MPI" = "1" ]
1745         then
1746                 TAU_LIBS="$TAU_MPI_LIBS $TAU_LIBS"
1747         else
1748                 TAU_LIBS="$TAU_LIBS"
1749         fi
1750         ;;
1751 "converse")
1752         MAKE_LD="1"
1753         MIDDLE_LIBS="-lconv-cplus-n $CORE_LIBS -ltrace-converse "
1754         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1755         ;;
1756 "converse++"|"bluegene"|"bigsim"|"bgconverse++")
1757         MAKE_LDXX="1"
1758         MIDDLE_LIBS="$MIDDLE_LIBS -lconv-cplus-y $CORE_LIBS -ltrace-converse "
1759         CMK_SYSLIBS="$CMK_SYSLIBS -lm"
1760         ;;
1761 *)
1762         Abort "Unrecognized choice of language $LANGUAGE"
1763         ;;
1764 esac
1765
1766 if [ "$BUILD_SHARE" = "0" ]
1767 then
1768         MIDDLE_LIBS="$MEM_OBJ $THREAD_OBJ $MIDDLE_LIBS"
1769 fi
1770
1771 if [ "$CHARM_SHARED" = "1" ]
1772 then
1773   [ -z "$CHARMLIBSO" ] &&  Abort "Charm++ lib_so directory not found!"
1774   # build user executable/shared lib linking with charm .so
1775   OPTS_LD="$OPTS_LD $CMK_LD_LIBRARY_PATH"
1776   if [ "$BUILD_SHARE" = "1" ]
1777   then
1778     LANG_LIBS="-L$CHARMLIBSO $OBJECTFILES $PRE_LIBRARIES"
1779   else
1780     LANG_LIBS="-L$CHARMLIBSO $BAL_OBJ $OBJECTFILES $modInitObj $PRE_LIBRARIES"
1781   fi
1782 else
1783   LANG_LIBS="-L$CHARMLIB -I$CHARMINC $BAL_OBJ $OBJECTFILES $modInitObj $PRE_LIBRARIES"
1784 fi
1785 if [ "$TRACE_WITH_TAU" = 1 ]
1786 then
1787         echo "Linking with the TAU libraries: $TAU_LIBS"
1788         ALL_LIBS="$LANG_LIBS $MIDDLE_LIBS $CMK_LIBS $POST_LIBRARIES $CMK_SYSLIBS $TAU_LIBS"
1789 else
1790         ALL_LIBS="$LANG_LIBS $MIDDLE_LIBS $CMK_LIBS $POST_LIBRARIES $CMK_SYSLIBS"
1791 fi
1792 Debugf "All libraries are: $ALL_LIBS"
1793
1794 if [ "$BUILD_SHARE" = "1" -a "$CHARM_SHARED" = "1" ]
1795 then
1796   # build an applicaiton .so linking with charm .so
1797   OPTS_LD="$OPTS_LD $CMK_LD_SHARED $OPTS_LDRO -L$CHARMLIBSO"
1798   Debugf "Running ldro..."
1799   DoNoErrCheck $RM $OBJECT
1800   [ "$MAKE_LDXX" = "1" ] && Do $CMK_LDXX $OPTS_LD $OPTS_LDXX -o $OBJECT $ALL_LIBS
1801   [ "$MAKE_LD" = "1" ] && Do $CMK_LD $OPTS_LD -o $OBJECT $ALL_LIBS
1802   Copyobj
1803 else
1804 # Make a regular executable: call the appropriate linker
1805   [ "$MAKE_LDXX" = "1" ] && Do $CMK_LDXX $OPTS_LD $OPTS_LDXX -o $OBJECT $ALL_LIBS
1806   [ "$MAKE_LD" = "1" ] && Do $CMK_LD $OPTS_LD -o $OBJECT $ALL_LIBS
1807 fi
1808
1809 ########################################################################
1810 #
1811 # Copy conv-host to user directory if it is present in installation.
1812 #
1813 ########################################################################
1814
1815 if test "$COPY_CHARMRUN" = "true"
1816 then
1817   targ=charmrun$CMK_POST_EXE
1818   [ ! -x $CHARMBIN/$targ -a -n "$CMK_POST_EXE" ] && targ=charmrun
1819   convhost=conv-host$CMK_POST_EXE
1820
1821   if [ -x $CHARMBIN/$targ ]
1822   then
1823         DoNoErrCheck $RM $targ
1824         Do $CP $CHARMBIN/$targ $targ
1825   fi
1826 fi
1827
1828 [ -z "$SAVE" ] && DoNoErrCheck $RM $modInitSrc $modInitObj > /dev/null 2>&1
1829
1830 Copyobj
1831
1832