Move libcds 1.6.0 from SVN
[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"
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         *)
212                 echo "ERROR: Unknown compiler: $ccompiler"
213                 exit $ERROR_EXIT_CODE
214                 ;;
215 esac
216
217 # Determine OS family
218 if test $OS_FAMILY = 'unknown'; then
219         OS_FAMILY=`uname |tr [A-Z] [a-z]|sed "s/-//"`
220 fi
221 case $OS_FAMILY in
222     hp-ux)
223         OS_FAMILY=hpux
224         ;;
225     solaris)
226         OS_FAMILY=sunos
227         ;;
228     mingw*)
229         OS_FAMILY=mingw
230         ;;
231     linux|sunos|hpux|aix|freebsd|mingw|darwin)
232         ;;
233     *)
234         echo "Warning: Unknown operation system: $OS_FAMILY"
235         #exit $ERROR_EXIT_CODE
236         ;;
237 esac
238
239
240 # Determine processor architecture
241 if test $processor_arch = 'unknown'; then
242         processor_arch=`uname -m|tr [A-Z] [a-z]`
243 fi
244 case $processor_arch in
245         x86_64)
246             if test $bitsToBuild = 64; then
247                 processor_arch='amd64'
248             else
249                 processor_arch='x86'
250             fi;
251             ;;
252         x86|i686)
253                 if test $bitsToBuild = 64; then
254                         processor_arch='amd64'
255                 else
256                         processor_arch='x86'
257                 fi
258                 ;;
259         sparc64)
260                 processor_arch='sparc'
261                 ;;
262         amd64|x86|ia64|sparc)
263                 ;;
264         *)
265                 processor_arch=`uname -p|tr [A-Z] [a-z]`
266                 case $processor_arch in
267                     sparc|powerpc)
268                         ;;
269                     *)
270                         echo "Warning: Unknown processor architecture: $processor_arch"
271                         #exit ${ERROR_EXIT_CODE}
272                         ;;
273                 esac                    
274                 1;;
275 esac    
276
277 # Determine compiler flags
278 case $ccompiler in
279     gcc|clang)
280         case $processor_arch in
281         amd64)
282             case $OS_FAMILY in
283             linux|freebsd|darwin)
284                 buildCXXflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
285                 buildCflags="-m64 -fPIC -march=$ArchFlag $amd64_cxx_options"
286                 buildLDflags="-m64 -fPIC"
287                 buildTestLDflags="-m64 -fPIC"
288                 ;;
289             mingw)
290                 buildCXXflags="-m64 -march=$ArchFlag $amd64_cxx_options"
291                 buildCflags="-m64 -march=$ArchFlag $amd64_cxx_options"
292                 buildLDflags="-m64"
293                 buildTestLDflags="-m64"
294                 ld_libs=""
295                 ;;
296             *)
297                 echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
298                 #exit ${ERROR_EXIT_CODE}
299                 ;;
300             esac
301             ;;
302         x86)
303             case $OS_FAMILY in
304                 linux|freebsd|darwin)
305                     buildCXXflags="-m32 -fPIC -march=$ArchFlag"
306                     buildCflags="-m32 -fPIC -march=$ArchFlag"
307                     buildLDflags="-m32 -fPIC"
308                     buildTestLDflags="-m32 -fPIC"
309                     ;;
310                 mingw)
311                     buildCXXflags="-m32 -march=$ArchFlag"
312                     buildCflags="-m32 -march=$ArchFlag"
313                     buildLDflags="-m32"
314                     buildTestLDflags="-m32"
315                     ld_libs=""
316                     ;;
317                 *)
318                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
319                     #exit ${ERROR_EXIT_CODE}
320                     ;;
321             esac
322             ;;
323         ia64)
324             bitsToBuild=64
325             case $OS_FAMILY in
326                 linux|freebsd)
327                     buildCXXflags="-mtune=itanium2 -fPIC"
328                     buildCflags="-mtune=itanium2 -fPIC"
329                     buildLDflags="-mtune=itanium2 -fPIC"
330                     buildTestLDflags="-mtune=itanium2 -fPIC"
331                     ;;
332                 hpux)
333                     buildCXXflags="-mlp64 -mtune=itanium2 -fPIC"
334                     buildCflags="-mlp64 -mtune=itanium2 -fPIC"
335                     buildLDflags="-mlp64 -mtune=itanium2 -fPIC"
336                     buildTestLDflags="-mlp64 -mtune=itanium2 -fPIC"
337                     ;;
338                 *)
339                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
340                     #exit ${ERROR_EXIT_CODE}
341                     ;;
342             esac
343             ;;
344         sparc)
345             bitsToBuild=64
346             case $OS_FAMILY in
347                 sunos)
348                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
349                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
350                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
351                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC -pthreads"
352                     cxx_test_release_options="-fPIC"
353                     ld_test_release_options="-fPIC"
354                     ;;
355                 linux)
356                     buildCXXflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
357                     buildCflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
358                     buildLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
359                     buildTestLDflags="-mcpu=v9 -mtune=ultrasparc3 -m64 -fPIC"
360                     cxx_test_release_options="-fPIC"
361                     ld_test_release_options="-fPIC"
362                     ;;
363                 *)
364                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
365                     #exit ${ERROR_EXIT_CODE}
366                     ;;
367             esac
368             ;;
369         powerpc)
370             bitsToBuild=64
371             case $OS_FAMILY in
372                 aix)
373                     buildCXXflags="-maix64 -pthread -fPIC"
374                     buildCflags="-maix64 -pthread -fPIC"
375                     buildLDflags="-maix64 -pthread -fPIC"
376                     buildTestLDflags="-maix64 -pthread -fPIC"
377                     cxx_test_release_options="-fPIC"
378                     ld_test_release_options="-fPIC"
379                     ;;
380                 *)
381                     echo "Warning: cannot determine compiler flags for processor $processor_arch, OS $OS_FAMILY, and compiler $ccompiler"
382                     #exit ${ERROR_EXIT_CODE}
383                     ;;
384             esac
385             ;;
386         *)
387             echo "Warning: cannot determine compiler flags for processor $processor_arch and compiler $ccompiler"
388             #exit ${ERROR_EXIT_CODE}
389             ;;
390         esac
391
392         cppcompiler_version=`$cppcompiler -dumpversion`
393         echo g++ version=$gcc_version
394
395         # Setup target options
396         # buildCXXflags="-std=gnu++0x $buildCXXflags"
397         cxx_debug_options="-D_DEBUG -O0 -g $cxx_debug_options"
398         cxx_release_options="-DNDEBUG $cxx_release_optimization $cxx_release_options"
399         ;;
400     *)
401         echo "ERROR: Unknown compiler: $ccompiler"
402         exit ${ERROR_EXIT_CODE}
403         ;;
404 esac
405
406 if test $BOOST_INCLUDE_PATH != ''; then
407         buildCXXflags="$buildCXXflags -I$BOOST_INCLUDE_PATH"
408 fi
409
410 if test 'x$buildTestLDflags' = 'x'; then
411         buildTestLDflags=$buildLDflags
412 fi
413
414
415 EXTRA_CXXFLAGS="$buildCXXflags $EXTRA_CXXFLAGS"
416 EXTRA_CFLAGS="$buildCflags $EXTRA_CFLAGS"
417 EXTRA_LDFLAGS="$buildLDflags $EXTRA_LDFLAGS"
418
419 EXTRA_TEST_LDFLAGS="$buildTestLDflags $EXTRA_TEST_LDFLAGS"
420
421
422 echo "Building with the following options ..."
423 echo "Processor: $processor_arch"
424 echo "Platform: $OS_FAMILY"
425 echo "C Compiler: $ccompiler"
426 echo "C++ Compiler: $cppcompiler"
427 echo "C++ Compiler version: $cppcompiler_version"
428 echo "Bits to build: $bitsToBuild"
429 echo "Compile options: $compileroptions $EXTRA_CXXFLAGS"
430 echo "Link options: $linkeroptions $EXTRA_LDFLAGS"
431 echo "Link options (for test cds-unit app): $linkeroptions $EXTRA_TEST_LDFLAGS"
432
433 BITSTOBUILD=$bitsToBuild
434 export BITSTOBUILD
435
436 #
437 # Set the C compiler and C++ compiler environment variables
438 #
439
440 CC="$ccompiler"
441 export CC
442
443 CXX="$cppcompiler"
444 export CXX
445
446 ROOT_DIR=..
447
448 GOAL_DIR=$ccompiler$TOOLSET_SUFFIX-$processor_arch-$OS_FAMILY$OS_VERSION-$bitsToBuild
449 BIN_PATH=$ROOT_DIR/bin/$GOAL_DIR
450 mkdir -p $BIN_PATH
451
452 OBJ_PATH=$ROOT_DIR/obj/$GOAL_DIR
453 mkdir -p $OBJ_PATH
454
455 echo PATH=$PATH
456 echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH
457 echo BIN_PATH=$BIN_PATH
458 echo OBJ_PATH=$OBJ_PATH
459 echo `${CXX} --version | head -1`
460 echo Build started
461
462 makegoals=
463 if test $makeclean = 'on'; then
464    echo Clean all
465    $MAKE -f Makefile clean platform=$OS_FAMILY BIN_PATH=$BIN_PATH OBJ_PATH=$OBJ_PATH
466 fi
467
468 echo ---------------------------------
469 echo Make debug library
470 CXXFLAGS="$compileroptions $cxx_debug_options $EXTRA_CXXFLAGS"
471 export CXXFLAGS
472 CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS $debugflag "
473 export CFLAGS
474 LDFLAGS="$linkeroptions -shared $ld_debug_options $ld_libs $EXTRA_LDFLAGS "
475 export LDFLAGS
476
477 mkdir -p $OBJ_PATH/debug
478
479 $MAKE -f Makefile \
480      platform=$OS_FAMILY \
481      BIN_PATH=$BIN_PATH \
482      OBJ_PATH=$OBJ_PATH/debug \
483      debug
484
485 if test $? -gt 0; then
486    exit $?
487 fi
488
489 echo ---------------------------------
490 echo Make release library
491
492 CXXFLAGS="$compileroptions $cxx_release_options $EXTRA_CXXFLAGS "
493 export CXXFLAGS
494 CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
495 export CFLAGS
496 LDFLAGS="$linkeroptions -shared $ld_resease_options $ld_libs $EXTRA_LDFLAGS "
497 export LDFLAGS
498
499 mkdir -p $OBJ_PATH/release
500
501 $MAKE -f Makefile \
502      platform=$OS_FAMILY \
503      BIN_PATH=$BIN_PATH \
504      OBJ_PATH=$OBJ_PATH/release \
505      release
506      
507 if test $? -gt 0; then
508    exit $?
509 fi
510
511
512 echo ---------------------------------
513 echo Make tests
514
515 if test $MAKE_DEBUG_TEST = '0'; then
516     CXXFLAGS="$compileroptions $cxx_release_options $cxx_test_release_options $EXTRA_CXXFLAGS "
517     export CXXFLAGS
518     CFLAGS="$compileroptions $cxx_release_options $EXTRA_CFLAGS "
519     export CFLAGS
520     LDFLAGS="$linkeroptions $ld_release_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
521     export LDFLAGS
522
523     $MAKE -f Makefile -j $makejobs \
524         platform=$OS_FAMILY \
525         BIN_PATH=$BIN_PATH \
526         OBJ_PATH=$OBJ_PATH/test \
527         $target
528         
529     if test $? -gt 0; then
530         exit $?
531     fi
532 fi    
533
534 echo ---------------------------------
535 echo Make tests debug
536
537 if test $MAKE_DEBUG_TEST = '1'; then
538     CXXFLAGS="$compileroptions $cxx_debug_options $cxx_test_release_options $EXTRA_CXXFLAGS "
539     export CXXFLAGS
540     CFLAGS="$compileroptions $cxx_debug_options $EXTRA_CFLAGS "
541     export CFLAGS
542     LDFLAGS="$linkeroptions $ld_debug_options $ld_test_release_options $ld_libs $EXTRA_TEST_LDFLAGS "
543     export LDFLAGS
544
545     $MAKE -f Makefile -j $makejobs \
546         platform=$OS_FAMILY \
547         BIN_PATH=$BIN_PATH \
548         OBJ_PATH=$OBJ_PATH/test-debug \
549         $target
550         
551     if test $? -gt 0; then
552         exit $?
553     fi
554 fi   
555