Merge pull request #26 from krinkinmu/build-script-wip
[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 <target> 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    -D)
125         EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS -D$2"
126                 shift 2
127                 ;;
128    -h)
129         usage
130         exit $ERROR_EXIT_CODE;; 
131         
132     --clean)
133         makeclean=on
134         shift
135         ;;              
136     --with-boost)
137         BOOST_INCLUDE_PATH=$2
138         shift 2
139         ;;      
140     --debug-cxx-options)
141         cxx_debug_options=$2
142         shift 2
143         ;;
144     --debug-ld-options)
145         ld_debug_options=$2
146         shift 2
147         ;;
148     --release-cxx-options)
149         cxx_release_options=$2
150         shift 2
151         ;;
152     --optimize-flags)
153         cxx_release_optimization_level=$2
154         shift 2
155         ;;
156     --release-ld-options)
157         ld_release_options=$2
158         shift 2
159         ;;
160     --nodefaultlibs)
161         ld_libs=" "
162         shift
163         ;;
164     --with-make)
165         MAKE=$2
166         shift 2
167         ;;
168     --platform-suffix)
169         OS_VERSION=$2
170         shift 2
171         ;;
172     --toolset-suffix)
173         TOOLSET_SUFFIX=$2
174         shift 2
175         ;;
176     --debug-test)
177         MAKE_DEBUG_TEST=1
178         if test $target = 'test'; then
179                 target=test_debug
180         fi
181         shift 1
182         ;;
183    --amd64-use-128bit)
184         amd64_cxx_options='-mcx16'
185         shift 1
186         ;;
187    --arch-tune)
188         ArchFlag=$2
189         shift 2
190         ;;
191    --)
192         shift; break;; 
193
194    *)
195        echo "unknown option $1"
196        usage
197        exit $ERROR_EXIT_CODE;;
198    esac
199 done
200
201 cxx_release_optimization="$cxx_release_optimization_level $cxx_release_optimization"
202
203 # Determine compiler
204 case $ccompiler in
205         gcc)
206                 if test $cppcompiler = ''; then
207                         cppcompiler=g++
208                 fi
209                 ;;
210         clang)
211                 if test $cppcompiler = ''; then
212                         cppcompiler=clang++
213                 fi
214                 ;;
215         icc)
216                 if test $cppcompiler = ''; then
217                         cppcompiler=icc
218                 fi
219                 ;;
220         *)
221                 echo "ERROR: Unknown compiler: $ccompiler"
222                 exit $ERROR_EXIT_CODE
223                 ;;
224 esac
225
226 # Determine OS family
227 if test $OS_FAMILY = 'unknown'; then
228         OS_FAMILY=`uname |tr [A-Z] [a-z]|sed "s/-//"`
229 fi
230 case $OS_FAMILY in
231     hp-ux)
232         OS_FAMILY=hpux
233         ;;
234     solaris)
235         OS_FAMILY=sunos
236         ;;
237     mingw*)
238         OS_FAMILY=mingw
239         ;;
240     linux|sunos|hpux|aix|freebsd|mingw|darwin)
241         ;;
242     *)
243         echo "Warning: Unknown operation system: $OS_FAMILY"
244         #exit $ERROR_EXIT_CODE
245         ;;
246 esac
247
248
249 # Determine processor architecture
250 if test $processor_arch = 'unknown'; then
251         processor_arch=`uname -m|tr [A-Z] [a-z]`
252 fi
253 case $processor_arch in
254         x86_64)
255             if test $bitsToBuild = 64; then
256                 processor_arch='amd64'
257             else
258                 processor_arch='x86'
259             fi;
260             ;;
261         x86|i686)
262                 if test $bitsToBuild = 64; then
263                         processor_arch='amd64'
264                 else
265                         processor_arch='x86'
266                 fi
267                 ;;
268         sparc64)
269                 processor_arch='sparc'
270                 ;;
271         amd64|x86|ia64|sparc)
272                 ;;
273         *)
274                 processor_arch=`uname -p|tr [A-Z] [a-z]`
275                 case $processor_arch in
276                     sparc|powerpc)
277                         ;;
278                     *)
279                         echo "Warning: Unknown processor architecture: $processor_arch"
280                         #exit ${ERROR_EXIT_CODE}
281                         ;;
282                 esac                    
283                 1;;
284 esac    
285
286 # Determine compiler flags
287 case $ccompiler in
288     gcc|clang)
289         case $processor_arch in
290         amd64)
291             case $OS_FAMILY in
292             linux|freebsd|darwin)
293                 buildCXXflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
294                 buildCflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
295                 buildLDflags="-m64 -fPIC"
296                 buildTestLDflags="-m64 -fPIC"
297                 ;;
298             mingw)
299                 buildCXXflags="-m64 -march=$ArchFlag $amd64_cxx_options"
300                 buildCflags="-m64 -march=$ArchFlag $amd64_cxx_options"
301                 buildLDflags="-m64"
302                 buildTestLDflags="-m64"
303                 ld_libs=""
304                 ;;
305             *)
306                 echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
307                 #exit ${ERROR_EXIT_CODE}
308                 ;;
309             esac
310             ;;
311         x86)
312             case $OS_FAMILY in
313                 linux|freebsd|darwin)
314                     buildCXXflags="-m32 -fPIC -march=$ArchFlag"
315                     buildCflags="-m32 -fPIC -march=$ArchFlag"
316                     buildLDflags="-m32 -fPIC"
317                     buildTestLDflags="-m32 -fPIC"
318                     ;;
319                 mingw)
320                     buildCXXflags="-m32 -march=$ArchFlag"
321                     buildCflags="-m32 -march=$ArchFlag"
322                     buildLDflags="-m32"
323                     buildTestLDflags="-m32"
324                     ld_libs=""
325                     ;;
326                 *)
327                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
328                     #exit ${ERROR_EXIT_CODE}
329                     ;;
330             esac
331             ;;
332         ia64)
333             bitsToBuild=64
334             case $OS_FAMILY in
335                 linux|freebsd)
336                     buildCXXflags="-mtune=itanium2 -fPIC"
337                     buildCflags="-mtune=itanium2 -fPIC"
338                     buildLDflags="-mtune=itanium2 -fPIC"
339                     buildTestLDflags="-mtune=itanium2 -fPIC"
340                     ;;
341                 hpux)
342                     buildCXXflags="-mlp64 -mtune=itanium2 -fPIC"
343                     buildCflags="-mlp64 -mtune=itanium2 -fPIC"
344                     buildLDflags="-mlp64 -mtune=itanium2 -fPIC"
345                     buildTestLDflags="-mlp64 -mtune=itanium2 -fPIC"
346                     ;;
347                 *)
348                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
349                     #exit ${ERROR_EXIT_CODE}
350                     ;;
351             esac
352             ;;
353         sparc)
354             bitsToBuild=64
355             case $OS_FAMILY in
356                 sunos)
357                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
358                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
359                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
360                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
361                     cxx_test_release_options="-fPIC"
362                     ld_test_release_options="-fPIC"
363                     ;;
364                 linux)
365                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
366                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
367                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
368                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
369                     cxx_test_release_options="-fPIC"
370                     ld_test_release_options="-fPIC"
371                     ;;
372                 *)
373                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
374                     #exit ${ERROR_EXIT_CODE}
375                     ;;
376             esac
377             ;;
378         powerpc)
379             bitsToBuild=64
380             case $OS_FAMILY in
381                 aix)
382                     buildCXXflags="-maix64 -pthread -fPIC"
383                     buildCflags="-maix64 -pthread -fPIC"
384                     buildLDflags="-maix64 -pthread -fPIC"
385                     buildTestLDflags="-maix64 -pthread -fPIC"
386                     cxx_test_release_options="-fPIC"
387                     ld_test_release_options="-fPIC"
388                     ;;
389                 *)
390                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
391                     #exit ${ERROR_EXIT_CODE}
392                     ;;
393             esac
394             ;;
395         *)
396             echo "Warning: cannot determine compiler flags for processor $processor_arch and compiler $ccompiler"
397             #exit ${ERROR_EXIT_CODE}
398             ;;
399         esac
400
401         #cppcompiler_version=`$cppcompiler -dumpversion`
402         #echo compiler version=$cppcompiler $cppcompiler_version
403
404         # Setup target options
405         # buildCXXflags="-std=gnu++0x $buildCXXflags"
406         #cxx_debug_options="-D_DEBUG -O0 -g $cxx_debug_options"
407         #cxx_release_options="-DNDEBUG $cxx_release_optimization $cxx_release_options"
408         ;;
409     icc)
410         case $processor_arch in
411         amd64)
412             case $OS_FAMILY in
413             linux)
414                 buildCXXflags="-fPIC -march=$ArchFlag $amd64_cxx_options"
415                 buildCflags="-fPIC -march=$ArchFlag $amd64_cxx_options"
416                 buildLDflags="-fPIC"
417                 buildTestLDflags="-fPIC"
418                 ;;
419             *)
420                 echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
421                 #exit ${ERROR_EXIT_CODE}
422                 ;;
423             esac
424             ;;
425         x86)
426             case $OS_FAMILY in
427                 linux)
428                     buildCXXflags="-fPIC -march=$ArchFlag"
429                     buildCflags="-fPIC -march=$ArchFlag"
430                     buildLDflags="-fPIC"
431                     buildTestLDflags="-fPIC"
432                     ;;
433                 *)
434                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
435                     #exit ${ERROR_EXIT_CODE}
436                     ;;
437             esac
438             ;;
439         *)
440             echo "Warning: cannot determine compiler flags for processor $processor_arch and compiler $ccompiler"
441             #exit ${ERROR_EXIT_CODE}
442             ;;
443         esac
444         ;;
445     *)
446         echo "ERROR: Unknown compiler: $ccompiler"
447         exit ${ERROR_EXIT_CODE}
448         ;;
449 esac
450
451 cppcompiler_version=`$cppcompiler -dumpversion`
452 echo compiler version=$cppcompiler $cppcompiler_version
453
454 # Setup target options
455 # buildCXXflags="-std=gnu++0x $buildCXXflags"
456 cxx_debug_options="-D_DEBUG -O0 -g $cxx_debug_options"
457 cxx_release_options="-DNDEBUG $cxx_release_optimization $cxx_release_options"
458
459
460 if test "x$BOOST_INCLUDE_PATH" != "x"; then
461         buildCXXflags="$buildCXXflags -I$BOOST_INCLUDE_PATH"
462 fi
463
464 if test "x$buildTestLDflags" = "x"; then
465         buildTestLDflags=$buildLDflags
466 fi
467
468
469 EXTRA_CXXFLAGS="$buildCXXflags $EXTRA_CXXFLAGS"
470 EXTRA_CFLAGS="$buildCflags $EXTRA_CFLAGS"
471 EXTRA_LDFLAGS="$buildLDflags $EXTRA_LDFLAGS"
472
473 EXTRA_TEST_LDFLAGS="$buildTestLDflags $EXTRA_TEST_LDFLAGS"
474
475
476 echo "Building with the following options ..."
477 echo "Processor: $processor_arch"
478 echo "Platform: $OS_FAMILY"
479 echo "C Compiler: $ccompiler"
480 echo "C++ Compiler: $cppcompiler"
481 echo "C++ Compiler version: $cppcompiler_version"
482 echo "Bits to build: $bitsToBuild"
483 echo "Compile options: $compileroptions $EXTRA_CXXFLAGS"
484 echo "Link options: $linkeroptions $EXTRA_LDFLAGS"
485 echo "Link options (for test cds-unit app): $linkeroptions $EXTRA_TEST_LDFLAGS"
486
487 BITSTOBUILD=$bitsToBuild
488 export BITSTOBUILD
489
490 #
491 # Set the C compiler and C++ compiler environment variables
492 #
493
494 CC="$ccompiler"
495 export CC
496
497 CXX="$cppcompiler"
498 export CXX
499
500 ROOT_DIR=..
501
502 GOAL_DIR=$ccompiler$TOOLSET_SUFFIX-$processor_arch-$OS_FAMILY$OS_VERSION-$bitsToBuild
503 BIN_PATH=$ROOT_DIR/bin/$GOAL_DIR
504 mkdir -p $BIN_PATH
505
506 OBJ_PATH=$ROOT_DIR/obj/$GOAL_DIR
507 mkdir -p $OBJ_PATH
508
509 echo PATH=$PATH
510 echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH
511 echo BIN_PATH=$BIN_PATH
512 echo OBJ_PATH=$OBJ_PATH
513 echo `${CXX} --version | head -1`
514 echo Build started
515
516 makegoals=
517 if test $makeclean = 'on'; then
518    echo Clean all
519    $MAKE -f Makefile clean platform=$OS_FAMILY BIN_PATH=$BIN_PATH OBJ_PATH=$OBJ_PATH
520 fi
521
522 echo ---------------------------------
523 echo Make debug library
524 #CXXFLAGS="$compileroptions $cxx_debug_options $EXTRA_CXXFLAGS"
525 #export CXXFLAGS
526 #CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS $debugflag "
527 #export CFLAGS
528 #LDFLAGS="$linkeroptions -shared $ld_debug_options $EXTRA_LDFLAGS "
529 #export LDFLAGS
530
531 mkdir -p $OBJ_PATH/debug
532
533 CXXFLAGS="$compileroptions $cxx_debug_options $EXTRA_CXXFLAGS" \
534 CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS $debugflag " \
535 LDLIBS="$ld_libs" \
536 LDFLAGS="$linkeroptions -shared $ld_debug_options $EXTRA_LDFLAGS " \
537 $MAKE -f Makefile \
538      platform=$OS_FAMILY \
539      BIN_PATH=$BIN_PATH \
540      OBJ_PATH=$OBJ_PATH/debug \
541      debug \
542   || exit $?
543
544 echo ---------------------------------
545 echo Make release library
546
547 #CXXFLAGS="$compileroptions $cxx_release_options $EXTRA_CXXFLAGS "
548 #export CXXFLAGS
549 #CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
550 #export CFLAGS
551 #LDFLAGS="$linkeroptions -shared $ld_resease_options $ld_libs $EXTRA_LDFLAGS "
552 #export LDFLAGS
553
554 mkdir -p $OBJ_PATH/release
555
556 CXXFLAGS="$compileroptions $cxx_release_options $EXTRA_CXXFLAGS " \
557 CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS " \
558 LDFLAGS="$linkeroptions -shared $ld_resease_options $EXTRA_LDFLAGS " \
559 LDLIBS="$ld_libs" \
560 $MAKE -f Makefile \
561      platform=$OS_FAMILY \
562      BIN_PATH=$BIN_PATH \
563      OBJ_PATH=$OBJ_PATH/release \
564      release \
565   || exit $?
566
567
568 echo ---------------------------------
569 echo Make tests
570
571 if test $MAKE_DEBUG_TEST = '0'; then
572     CXXFLAGS="$compileroptions $cxx_release_options $cxx_test_release_options $EXTRA_CXXFLAGS "
573     export CXXFLAGS
574     CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
575     export CFLAGS
576     LDFLAGS="$linkeroptions $ld_release_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
577     export LDFLAGS
578
579     $MAKE -f Makefile -j $makejobs \
580         platform=$OS_FAMILY \
581         BIN_PATH=$BIN_PATH \
582         OBJ_PATH=$OBJ_PATH/test \
583         $target \
584      || exit $?
585 fi    
586
587 echo ---------------------------------
588 echo Make tests debug
589
590 if test $MAKE_DEBUG_TEST = '1'; then
591     CXXFLAGS="$compileroptions $cxx_debug_options $cxx_test_release_options $EXTRA_CXXFLAGS "
592     export CXXFLAGS
593     CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS "
594     export CFLAGS
595     LDFLAGS="$linkeroptions $ld_debug_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
596     export LDFLAGS
597
598     $MAKE -f Makefile -j $makejobs \
599         platform=$OS_FAMILY \
600         BIN_PATH=$BIN_PATH \
601         OBJ_PATH=$OBJ_PATH/test-debug \
602         $target \
603      || exit $?
604 fi   
605