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