issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / build / build.sh
1 #!/bin/sh
2
3 # cds library build script
4 # Maxim Khiszinsky 04.01.2009
5
6 # The following variables are defined and exported at the end of this script.
7 #
8 # LDFLAGS
9 # CFLAGS
10 # CXXFLAGS
11 # CXX
12 # CC
13 # BITSTOBUILD
14
15 usage()
16 {
17     echo "Build helper script for one of the supported platforms"
18     echo "Usage: build.sh \"options\""
19     echo "       where options may be any of the following:"
20     echo "       -t make target"
21     echo "       -c <C compiler name> Possible values are: gcc,clang,icc"
22     echo "       -x <C++ compiler name> (e.g. g++, CC)"
23     echo "       -p <Processor architecture> Possible values are:"
24     echo "             x86, amd64 (x86_64), sparc, ia64"
25     echo "       -o <OS family> Possible values are:"
26     echo "             linux, sunos (solaris), hpux, darwin"
27     echo "       -D <define> define"
28     echo "       -b <bitsToBuild> (accepts '64', '32')"
29     echo "       -l <extra linker options>"
30     echo "       -z <extra compiler options>"
31     echo "       -j <number of make jobs. default 2>"
32     echo "       -h (to get help on the above commands)"
33     echo "       --with-boost <path to boost include>"
34     echo "       --debug-cxx-options <extra compiler options for debug target>"
35     echo "       --debug-ld-options <extra linker options for debug target>"
36     echo "       --release-cxx-options <extra compiler options for release target>"
37     echo "       --release-ld-options <extra linker options for release target>"
38     echo "       --clean clean all before building"
39     echo "       --debug-test make unit test in debug mode"
40     echo "       --amd64-use-128bit use 128bit (16byte) CAS on amd64"
41     echo "       --arch-tune march flag (only for x86/amd64), default = native"
42     echo "       --nodefaultlibs - no default libs (pthread, stdc++)"
43     echo "       --optimize-flags - optimization level flags for release target, default -O3"
44 }
45
46 ERROR_EXIT_CODE=1
47
48 MAKE=make
49
50 # Set up the default values for each parameter
51 debug=off                # by default debug is off
52 bitsToBuild=0            # unknown
53 makejobs=2
54 cppcompiler=g++
55 ccompiler=gcc
56 processor_arch=unknown
57 OS_FAMILY=unknown
58 ArchFlag=native
59 ld_nodefaultlibs=off
60
61 BOOST_INCLUDE_PATH=
62 makeclean=off
63
64 MAKE_DEBUG_TEST=0
65 ld_libs="-lpthread -ldl -lstdc++"
66
67 cxx_debug_options=
68 ld_debug_options=
69
70 cxx_release_options=
71 ld_release_options=
72
73 cxx_test_release_options=
74 ls_test_release_options=
75
76 cxx_release_optimization="-fno-strict-aliasing"
77 cxx_release_optimization_level="-O3"
78
79 amd64_cxx_options=
80
81 OS_VERSION=
82 TOOLSET_SUFFIX=
83
84 target=test
85
86 while [ $# -gt 0 ]
87    do
88    case $1 in
89    -t)
90                 target=$2
91                 shift 2
92                 ;;
93    -c)
94         ccompiler=$2
95                 shift 2
96                 ;;
97    -x)
98         cppcompiler=$2
99                 shift 2
100                 ;;
101    -o)
102                 OS_FAMILY=$2
103                 shift 2
104                 ;;
105    -p)
106         processor_arch=$2; shift 2
107                 ;;
108    -b)
109         bitsToBuild=$2
110                 shift 2
111                 ;;
112    -l)
113         linkeroptions="$linkeroptions $2"
114                 shift 2
115                 ;;
116    -z)
117         compileroptions="$compileroptions $2"
118                 shift 2
119                 ;;
120    -j)
121         makejobs=$2
122                 shift 2
123                 ;;
124    -h)
125         usage
126         exit $ERROR_EXIT_CODE;; 
127         
128     --clean)
129         makeclean=on
130         shift
131         ;;              
132     --with-boost)
133         BOOST_INCLUDE_PATH=$2
134         shift 2
135         ;;      
136     --debug-cxx-options)
137         cxx_debug_options=$2
138         shift 2
139         ;;
140     --debug-ld-options)
141         ld_debug_options=$2
142         shift 2
143         ;;
144     --release-cxx-options)
145         cxx_release_options=$2
146         shift 2
147         ;;
148     --optimize-flags)
149         cxx_release_optimization_level=$2
150         shift 2
151         ;;
152     --release-ld-options)
153         ld_release_options=$2
154         shift 2
155         ;;
156     --nodefaultlibs)
157         ld_libs=" "
158         shift
159         ;;
160     --with-make)
161         MAKE=$2
162         shift 2
163         ;;
164     --platform-suffix)
165         OS_VERSION=$2
166         shift 2
167         ;;
168     --toolset-suffix)
169         TOOLSET_SUFFIX=$2
170         shift 2
171         ;;
172     --debug-test)
173         MAKE_DEBUG_TEST=1
174         if test $target = 'test'; then
175                 target=test_debug
176         fi
177         shift 1
178         ;;
179    --amd64-use-128bit)
180         amd64_cxx_options='-mcx16'
181         shift 1
182         ;;
183    --arch-tune)
184         ArchFlag=$2
185         shift 2
186         ;;
187    --)
188         shift; break;; 
189
190    *)
191        echo "unknown option $1"
192        usage
193        exit $ERROR_EXIT_CODE;;
194    esac
195 done
196
197 cxx_release_optimization="$cxx_release_optimization_level $cxx_release_optimization"
198
199 # Determine compiler
200 case $ccompiler in
201         gcc)
202                 if test $cppcompiler = ''; then
203                         cppcompiler=g++
204                 fi
205                 ;;
206         clang)
207                 if test $cppcompiler = ''; then
208                         cppcompiler=clang++
209                 fi
210                 ;;
211         icc)
212                 if test $cppcompiler = ''; then
213                         cppcompiler=icc
214                 fi
215                 ;;
216         *)
217                 echo "ERROR: Unknown compiler: $ccompiler"
218                 exit $ERROR_EXIT_CODE
219                 ;;
220 esac
221
222 # Determine OS family
223 if test $OS_FAMILY = 'unknown'; then
224         OS_FAMILY=`uname |tr [A-Z] [a-z]|sed "s/-//"`
225 fi
226 case $OS_FAMILY in
227     hp-ux)
228         OS_FAMILY=hpux
229         ;;
230     solaris)
231         OS_FAMILY=sunos
232         ;;
233     mingw*)
234         OS_FAMILY=mingw
235         ;;
236     linux|sunos|hpux|aix|freebsd|mingw|darwin)
237         ;;
238     *)
239         echo "Warning: Unknown operation system: $OS_FAMILY"
240         #exit $ERROR_EXIT_CODE
241         ;;
242 esac
243
244
245 # Determine processor architecture
246 if test $processor_arch = 'unknown'; then
247         processor_arch=`uname -m|tr [A-Z] [a-z]`
248 fi
249 case $processor_arch in
250         x86_64)
251             if test $bitsToBuild = 64; then
252                 processor_arch='amd64'
253             else
254                 processor_arch='x86'
255             fi;
256             ;;
257         x86|i686)
258                 if test $bitsToBuild = 64; then
259                         processor_arch='amd64'
260                 else
261                         processor_arch='x86'
262                 fi
263                 ;;
264         sparc64)
265                 processor_arch='sparc'
266                 ;;
267         amd64|x86|ia64|sparc)
268                 ;;
269         *)
270                 processor_arch=`uname -p|tr [A-Z] [a-z]`
271                 case $processor_arch in
272                     sparc|powerpc)
273                         ;;
274                     *)
275                         echo "Warning: Unknown processor architecture: $processor_arch"
276                         #exit ${ERROR_EXIT_CODE}
277                         ;;
278                 esac                    
279                 1;;
280 esac    
281
282 # Determine compiler flags
283 case $ccompiler in
284     gcc|clang)
285         case $processor_arch in
286         amd64)
287             case $OS_FAMILY in
288             linux|freebsd|darwin)
289                 buildCXXflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
290                 buildCflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
291                 buildLDflags="-m64 -fPIC"
292                 buildTestLDflags="-m64 -fPIC"
293                 ;;
294             mingw)
295                 buildCXXflags="-m64 -march=$ArchFlag $amd64_cxx_options"
296                 buildCflags="-m64 -march=$ArchFlag $amd64_cxx_options"
297                 buildLDflags="-m64"
298                 buildTestLDflags="-m64"
299                 ld_libs=""
300                 ;;
301             *)
302                 echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
303                 #exit ${ERROR_EXIT_CODE}
304                 ;;
305             esac
306             ;;
307         x86)
308             case $OS_FAMILY in
309                 linux|freebsd|darwin)
310                     buildCXXflags="-m32 -fPIC -march=$ArchFlag"
311                     buildCflags="-m32 -fPIC -march=$ArchFlag"
312                     buildLDflags="-m32 -fPIC"
313                     buildTestLDflags="-m32 -fPIC"
314                     ;;
315                 mingw)
316                     buildCXXflags="-m32 -march=$ArchFlag"
317                     buildCflags="-m32 -march=$ArchFlag"
318                     buildLDflags="-m32"
319                     buildTestLDflags="-m32"
320                     ld_libs=""
321                     ;;
322                 *)
323                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
324                     #exit ${ERROR_EXIT_CODE}
325                     ;;
326             esac
327             ;;
328         ia64)
329             bitsToBuild=64
330             case $OS_FAMILY in
331                 linux|freebsd)
332                     buildCXXflags="-mtune=itanium2 -fPIC"
333                     buildCflags="-mtune=itanium2 -fPIC"
334                     buildLDflags="-mtune=itanium2 -fPIC"
335                     buildTestLDflags="-mtune=itanium2 -fPIC"
336                     ;;
337                 hpux)
338                     buildCXXflags="-mlp64 -mtune=itanium2 -fPIC"
339                     buildCflags="-mlp64 -mtune=itanium2 -fPIC"
340                     buildLDflags="-mlp64 -mtune=itanium2 -fPIC"
341                     buildTestLDflags="-mlp64 -mtune=itanium2 -fPIC"
342                     ;;
343                 *)
344                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
345                     #exit ${ERROR_EXIT_CODE}
346                     ;;
347             esac
348             ;;
349         sparc)
350             bitsToBuild=64
351             case $OS_FAMILY in
352                 sunos)
353                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
354                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
355                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
356                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
357                     cxx_test_release_options="-fPIC"
358                     ld_test_release_options="-fPIC"
359                     ;;
360                 linux)
361                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
362                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
363                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
364                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
365                     cxx_test_release_options="-fPIC"
366                     ld_test_release_options="-fPIC"
367                     ;;
368                 *)
369                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
370                     #exit ${ERROR_EXIT_CODE}
371                     ;;
372             esac
373             ;;
374         powerpc)
375             bitsToBuild=64
376             case $OS_FAMILY in
377                 aix)
378                     buildCXXflags="-maix64 -pthread -fPIC"
379                     buildCflags="-maix64 -pthread -fPIC"
380                     buildLDflags="-maix64 -pthread -fPIC"
381                     buildTestLDflags="-maix64 -pthread -fPIC"
382                     cxx_test_release_options="-fPIC"
383                     ld_test_release_options="-fPIC"
384                     ;;
385                 *)
386                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
387                     #exit ${ERROR_EXIT_CODE}
388                     ;;
389             esac
390             ;;
391         *)
392             echo "Warning: cannot determine compiler flags for processor $processor_arch and compiler $ccompiler"
393             #exit ${ERROR_EXIT_CODE}
394             ;;
395         esac
396
397         #cppcompiler_version=`$cppcompiler -dumpversion`
398         #echo compiler version=$cppcompiler $cppcompiler_version
399
400         # Setup target options
401         # buildCXXflags="-std=gnu++0x $buildCXXflags"
402         #cxx_debug_options="-D_DEBUG -O0 -g $cxx_debug_options"
403         #cxx_release_options="-DNDEBUG $cxx_release_optimization $cxx_release_options"
404         ;;
405     icc)
406         case $processor_arch in
407         amd64)
408             case $OS_FAMILY in
409             linux)
410                 buildCXXflags="-fPIC -march=$ArchFlag $amd64_cxx_options"
411                 buildCflags="-fPIC -march=$ArchFlag $amd64_cxx_options"
412                 buildLDflags="-fPIC"
413                 buildTestLDflags="-fPIC"
414                 ;;
415             *)
416                 echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
417                 #exit ${ERROR_EXIT_CODE}
418                 ;;
419             esac
420             ;;
421         x86)
422             case $OS_FAMILY in
423                 linux)
424                     buildCXXflags="-fPIC -march=$ArchFlag"
425                     buildCflags="-fPIC -march=$ArchFlag"
426                     buildLDflags="-fPIC"
427                     buildTestLDflags="-fPIC"
428                     ;;
429                 *)
430                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
431                     #exit ${ERROR_EXIT_CODE}
432                     ;;
433             esac
434             ;;
435         *)
436             echo "Warning: cannot determine compiler flags for processor $processor_arch and compiler $ccompiler"
437             #exit ${ERROR_EXIT_CODE}
438             ;;
439         esac
440         ;;
441     *)
442         echo "ERROR: Unknown compiler: $ccompiler"
443         exit ${ERROR_EXIT_CODE}
444         ;;
445 esac
446
447 cppcompiler_version=`$cppcompiler -dumpversion`
448 echo compiler version=$cppcompiler $cppcompiler_version
449
450 # Setup target options
451 # buildCXXflags="-std=gnu++0x $buildCXXflags"
452 cxx_debug_options="-D_DEBUG -O0 -g $cxx_debug_options"
453 cxx_release_options="-DNDEBUG $cxx_release_optimization $cxx_release_options"
454
455
456 if test $BOOST_INCLUDE_PATH != ''; then
457         buildCXXflags="$buildCXXflags -I$BOOST_INCLUDE_PATH"
458 fi
459
460 if test 'x$buildTestLDflags' = 'x'; then
461         buildTestLDflags=$buildLDflags
462 fi
463
464
465 EXTRA_CXXFLAGS="$buildCXXflags $EXTRA_CXXFLAGS"
466 EXTRA_CFLAGS="$buildCflags $EXTRA_CFLAGS"
467 EXTRA_LDFLAGS="$buildLDflags $EXTRA_LDFLAGS"
468
469 EXTRA_TEST_LDFLAGS="$buildTestLDflags $EXTRA_TEST_LDFLAGS"
470
471
472 echo "Building with the following options ..."
473 echo "Processor: $processor_arch"
474 echo "Platform: $OS_FAMILY"
475 echo "C Compiler: $ccompiler"
476 echo "C++ Compiler: $cppcompiler"
477 echo "C++ Compiler version: $cppcompiler_version"
478 echo "Bits to build: $bitsToBuild"
479 echo "Compile options: $compileroptions $EXTRA_CXXFLAGS"
480 echo "Link options: $linkeroptions $EXTRA_LDFLAGS"
481 echo "Link options (for test cds-unit app): $linkeroptions $EXTRA_TEST_LDFLAGS"
482
483 BITSTOBUILD=$bitsToBuild
484 export BITSTOBUILD
485
486 #
487 # Set the C compiler and C++ compiler environment variables
488 #
489
490 CC="$ccompiler"
491 export CC
492
493 CXX="$cppcompiler"
494 export CXX
495
496 ROOT_DIR=..
497
498 GOAL_DIR=$ccompiler$TOOLSET_SUFFIX-$processor_arch-$OS_FAMILY$OS_VERSION-$bitsToBuild
499 BIN_PATH=$ROOT_DIR/bin/$GOAL_DIR
500 mkdir -p $BIN_PATH
501
502 OBJ_PATH=$ROOT_DIR/obj/$GOAL_DIR
503 mkdir -p $OBJ_PATH
504
505 echo PATH=$PATH
506 echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH
507 echo BIN_PATH=$BIN_PATH
508 echo OBJ_PATH=$OBJ_PATH
509 echo `${CXX} --version | head -1`
510 echo Build started
511
512 makegoals=
513 if test $makeclean = 'on'; then
514    echo Clean all
515    $MAKE -f Makefile clean platform=$OS_FAMILY BIN_PATH=$BIN_PATH OBJ_PATH=$OBJ_PATH
516 fi
517
518 echo ---------------------------------
519 echo Make debug library
520 #CXXFLAGS="$compileroptions $cxx_debug_options $EXTRA_CXXFLAGS"
521 #export CXXFLAGS
522 #CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS $debugflag "
523 #export CFLAGS
524 #LDFLAGS="$linkeroptions -shared $ld_debug_options $EXTRA_LDFLAGS "
525 #export LDFLAGS
526
527 mkdir -p $OBJ_PATH/debug
528
529 CXXFLAGS="$compileroptions $cxx_debug_options $EXTRA_CXXFLAGS" \
530 CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS $debugflag " \
531 LDLIBS="$ld_libs" \
532 LDFLAGS="$linkeroptions -shared $ld_debug_options $EXTRA_LDFLAGS " \
533 $MAKE -f Makefile \
534      platform=$OS_FAMILY \
535      BIN_PATH=$BIN_PATH \
536      OBJ_PATH=$OBJ_PATH/debug \
537      debug
538
539 if test $? -gt 0; then
540    exit $?
541 fi
542
543 echo ---------------------------------
544 echo Make release library
545
546 #CXXFLAGS="$compileroptions $cxx_release_options $EXTRA_CXXFLAGS "
547 #export CXXFLAGS
548 #CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
549 #export CFLAGS
550 #LDFLAGS="$linkeroptions -shared $ld_resease_options $ld_libs $EXTRA_LDFLAGS "
551 #export LDFLAGS
552
553 mkdir -p $OBJ_PATH/release
554
555 CXXFLAGS="$compileroptions $cxx_release_options $EXTRA_CXXFLAGS " \
556 CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS " \
557 LDFLAGS="$linkeroptions -shared $ld_resease_options $EXTRA_LDFLAGS " \
558 LDLIBS="$ld_libs" \
559 $MAKE -f Makefile \
560      platform=$OS_FAMILY \
561      BIN_PATH=$BIN_PATH \
562      OBJ_PATH=$OBJ_PATH/release \
563      release
564      
565 if test $? -gt 0; then
566    exit $?
567 fi
568
569
570 echo ---------------------------------
571 echo Make tests
572
573 if test $MAKE_DEBUG_TEST = '0'; then
574     CXXFLAGS="$compileroptions $cxx_release_options $cxx_test_release_options $EXTRA_CXXFLAGS "
575     export CXXFLAGS
576     CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
577     export CFLAGS
578     LDFLAGS="$linkeroptions $ld_release_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
579     export LDFLAGS
580
581     $MAKE -f Makefile -j $makejobs \
582         platform=$OS_FAMILY \
583         BIN_PATH=$BIN_PATH \
584         OBJ_PATH=$OBJ_PATH/test \
585         $target
586         
587     if test $? -gt 0; then
588         exit $?
589     fi
590 fi    
591
592 echo ---------------------------------
593 echo Make tests debug
594
595 if test $MAKE_DEBUG_TEST = '1'; then
596     CXXFLAGS="$compileroptions $cxx_debug_options $cxx_test_release_options $EXTRA_CXXFLAGS "
597     export CXXFLAGS
598     CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS "
599     export CFLAGS
600     LDFLAGS="$linkeroptions $ld_debug_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
601     export LDFLAGS
602
603     $MAKE -f Makefile -j $makejobs \
604         platform=$OS_FAMILY \
605         BIN_PATH=$BIN_PATH \
606         OBJ_PATH=$OBJ_PATH/test-debug \
607         $target
608         
609     if test $? -gt 0; then
610         exit $?
611     fi
612 fi   
613