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