aboutsummaryrefslogtreecommitdiff
path: root/build/tools/builder-funcs.sh
blob: 81aebeae2f02f0ea3ea9765f92bfce62c9bb8f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#  This file contains various shell function definitions that can be
#  used to either build a static and shared libraries from sources, or
#  generate a Makefile to do it in parallel.
#

_BUILD_TAB=$(echo " " | tr ' ' '\t')

builder_command ()
{
    if [ -z "$_BUILD_MK" ]; then
        if [ "$VERBOSE2" = "yes" ]; then
            echo "$@"
        fi
        "$@"
    else
        echo "${_BUILD_TAB}${_BUILD_HIDE}$@" >> $_BUILD_MK
    fi
}


builder_log ()
{
    if [ "$_BUILD_MK" ]; then
        echo "${_BUILD_TAB}${_BUILD_HIDE}echo $@" >> $_BUILD_MK
    else
        log "$@"
    fi
}

# $1: Build directory
# $2: Optional Makefile name
builder_begin ()
{
    _BUILD_DIR_NEW=
    _BUILD_DIR=$1
    if [ ! -d "$_BUILD_DIR" ]; then
        mkdir -p "$_BUILD_DIR"
        fail_panic "Can't create build directory: $_BUILD_DIR"
        _BUILD_DIR_NEW=true
    else
        rm -rf "$_BUILD_DIR/*"
        fail_panic "Can't cleanup build directory: $_BUILD_DIR"
    fi
    _BUILD_TARGETS=
    _BUILD_PREFIX=
    _BUILD_MK=$2
    if [ -n "$_BUILD_MK" ]; then
        log "Creating temporary build Makefile: $_BUILD_MK"
        rm -f $_BUILD_MK &&
        echo "# Auto-generated by $0 - do not edit!" > $_BUILD_MK
        echo ".PHONY: all" >> $_BUILD_MK
        echo "all:" >> $_BUILD_MK
    fi
    # HIDE is used to hide the Makefile output, unless --verbose --verbose
    # is used.
    if [ "$VERBOSE2" = "yes" ]; then
        _BUILD_HIDE=""
    else
        _BUILD_HIDE=@
    fi

    builder_begin_module
}

# $1: Variable name
# out: Variable value
_builder_varval ()
{
    eval echo "\$$1"
}

_builder_varadd ()
{
    local _varname="$1"
    local _varval="$(_builder_varval $_varname)"
    shift
    if [ -z "$_varval" ]; then
        eval $_varname=\"$@\"
    else
        eval $_varname=\$$_varname\" $@\"
    fi
}


builder_set_prefix ()
{
    _BUILD_PREFIX="$@"
}

builder_begin_module ()
{
    _BUILD_CC=
    _BUILD_CXX=
    _BUILD_AR=
    _BUILD_C_INCLUDES=
    _BUILD_CFLAGS=
    _BUILD_CXXFLAGS=
    _BUILD_LDFLAGS=
    _BUILD_BINPREFIX=
    _BUILD_DSTDIR=
    _BUILD_SRCDIR=.
    _BUILD_OBJECTS=
    _BUILD_STATIC_LIBRARIES=
    _BUILD_SHARED_LIBRARIES=
}

builder_set_binprefix ()
{
    _BUILD_BINPREFIX=$1
    _BUILD_CC=${1}gcc
    _BUILD_CXX=${1}g++
    _BUILD_AR=${1}ar
}

builder_set_binprefix_llvm ()
{
    _BUILD_BINPREFIX=$1
    _BUILD_CC=${1}clang
    _BUILD_CXX=${1}clang++
}

builder_set_builddir ()
{
    _BUILD_DIR=$1
}

builder_set_srcdir ()
{
    _BUILD_SRCDIR=$1
}

builder_set_dstdir ()
{
    _BUILD_DSTDIR=$1
}

builder_ldflags ()
{
    _builder_varadd _BUILD_LDFLAGS "$@"
}

builder_ldflags_exe ()
{
    _builder_varadd _BUILD_LDFLAGS_EXE "$@"
}

builder_cflags ()
{
    _builder_varadd _BUILD_CFLAGS "$@"
}

builder_cxxflags ()
{
    _builder_varadd _BUILD_CXXFLAGS "$@"
}

builder_c_includes ()
{
    _builder_varadd _BUILD_C_INCLUDES "$@"
}

# $1: optional var to hold the original cflags before reset
builder_reset_cflags ()
{
    local _varname="$1"
    if [ -n "$_varname" ] ; then
        eval $_varname=\"$_BUILD_CFLAGS\"
    fi
    _BUILD_CFLAGS=
}

# $1: optional var to hold the original cxxflags before reset
builder_reset_cxxflags ()
{
    local _varname="$1"
    if [ -n "$_varname" ] ; then
        eval $_varname=\"$_BUILD_CXXFLAGS\"
    fi
    _BUILD_CXXFLAGS=
}

# $1: optional var to hold the original c_includes before reset
builder_reset_c_includes ()
{
    local _varname="$1"
    if [ -n "$_varname" ] ; then
        eval $_varname=\"$_BUILD_C_INCLUDES\"
    fi
    _BUILD_C_INCLUDES=
}

builder_link_with ()
{
    local LIB
    for LIB; do
        case $LIB in
            *.a)
                _builder_varadd _BUILD_STATIC_LIBRARIES $LIB
                ;;
            *.so)
                _builder_varadd _BUILD_SHARED_LIBRARIES $LIB
                ;;
            *)
                echo "ERROR: Unknown link library extension: $LIB"
                exit 1
        esac
    done
}

builder_sources ()
{
    local src srcfull obj cc cflags text
    if [ -z "$_BUILD_DIR" ]; then
        panic "Build directory not set!"
    fi
    if [ -z "$_BUILD_CC" ]; then
        _BUILD_CC=${CC:-gcc}
    fi
    if [ -z "$_BUILD_CXX" ]; then
        _BUILD_CXX=${CXX:-g++}
    fi
    for src in "$@"; do
        srcfull=$_BUILD_SRCDIR/$src
        if [ ! -f "$srcfull" ]; then
            echo "ERROR: Missing source file: $srcfull"
            exit 1
        fi
        obj=$src
        cflags="$_BUILD_CFLAGS"
        for inc in $_BUILD_C_INCLUDES; do
            cflags=$cflags" -I$inc"
        done
        cflags=$cflags" -I$_BUILD_SRCDIR"
        case $obj in
            *.c)
                obj=${obj%%.c}
                text="C"
                cc=$_BUILD_CC
                ;;
            *.cpp)
                obj=${obj%%.cpp}
                text="C++"
                cc=$_BUILD_CXX
                cflags="$cflags $_BUILD_CXXFLAGS"
                ;;
            *.cc)
                obj=${obj%%.cc}
                text="C++"
                cc=$_BUILD_CXX
                cflags="$cflags $_BUILD_CXXFLAGS"
                ;;
            *.S|*.s)
                obj=${obj%%.$obj}
                text="ASM"
                cc=$_BUILD_CC
                ;;
            *)
                echo "Unknown source file extension: $obj"
                exit 1
                ;;
        esac

        # Source file path can include ../ path items, ensure
        # that the generated object do not back up the output
        # directory by translating them to __/
        obj=$(echo "$obj" | tr '../' '__/')

        # Ensure we have unwind tables in the generated machine code
        # This is useful to get good stack traces
        cflags=$cflags" -funwind-tables"

        obj=$_BUILD_DIR/$obj.o
        if [ "$_BUILD_MK" ]; then
            echo "$obj: $srcfull" >> $_BUILD_MK
        fi
        builder_log "${_BUILD_PREFIX}$text: $src"
        builder_command mkdir -p $(dirname "$obj")
        builder_command $NDK_CCACHE $cc -c -o "$obj" "$srcfull" $cflags
        fail_panic "Could not compile ${_BUILD_PREFIX}$src"
        _BUILD_OBJECTS=$_BUILD_OBJECTS" $obj"
    done
}

builder_static_library ()
{
    local lib libname
    libname=$1
    if [ -z "$_BUILD_DSTDIR" ]; then
        panic "Destination directory not set"
    fi
    lib=$_BUILD_DSTDIR/$libname
    lib=${lib%%.a}.a
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $lib"
        echo "$lib: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    if [ -z "${_BUILD_AR}" ]; then
        _BUILD_AR=${AR:-ar}
    fi
    builder_log "${_BUILD_PREFIX}Archive: $libname"
    rm -f "$lib"
    builder_command ${_BUILD_AR} crsD "$lib" "$_BUILD_OBJECTS"
    fail_panic "Could not archive ${_BUILD_PREFIX}$libname objects!"
}

builder_host_static_library ()
{
    local lib libname
    libname=$1
    if [ -z "$_BUILD_DSTDIR" ]; then
        panic "Destination directory not set"
    fi
    lib=$_BUILD_DSTDIR/$libname
    lib=${lib%%.a}.a
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $lib"
        echo "$lib: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    if [ -z "$BUILD_AR" ]; then
        _BUILD_AR=${AR:-ar}
    fi
    builder_log "${_BUILD_PREFIX}Archive: $libname"
    rm -f "$lib"
    builder_command ${_BUILD_AR} crsD "$lib" "$_BUILD_OBJECTS"
    fail_panic "Could not archive ${_BUILD_PREFIX}$libname objects!"
}

builder_shared_library ()
{
    local lib libname suffix libm
    libname=$1
    suffix=$2
    armeabi_v7a_float_abi=$3

    if [ -z "$suffix" ]; then
        suffix=".so"
    fi
    libm="-lm"
    if [ "$armeabi_v7a_float_abi" = "hard" ]; then
        libm="-lm_hard"
    fi
    lib=$_BUILD_DSTDIR/$libname
    lib=${lib%%${suffix}}${suffix}
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $lib"
        echo "$lib: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    builder_log "${_BUILD_PREFIX}SharedLibrary: $libname"

    # Important: -lgcc must appear after objects and static libraries,
    #            but before shared libraries for Android. It doesn't hurt
    #            for other platforms.
    builder_command ${_BUILD_CXX} \
        -Wl,-soname,$(basename $lib) \
        -shared \
        $_BUILD_OBJECTS \
        $_BUILD_STATIC_LIBRARIES \
        -lgcc \
        $_BUILD_SHARED_LIBRARIES \
        -lc $libm \
        $_BUILD_LDFLAGS \
        -o $lib
    fail_panic "Could not create ${_BUILD_PREFIX}shared library $libname"
}

# Same as builder_shared_library, but do not link the default libs
builder_nodefaultlibs_shared_library ()
{
    local lib libname suffix
    libname=$1
    suffix=$2
    if [ -z "$suffix" ]; then
        suffix=".so"
    fi
    lib=$_BUILD_DSTDIR/$libname
    lib=${lib%%${suffix}}${suffix}
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $lib"
        echo "$lib: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    builder_log "${_BUILD_PREFIX}SharedLibrary: $libname"

    builder_command ${_BUILD_CXX} \
        -Wl,-soname,$(basename $lib) \
        -shared \
        $_BUILD_OBJECTS \
        $_BUILD_STATIC_LIBRARIES \
        $_BUILD_SHARED_LIBRARIES \
        $_BUILD_LDFLAGS \
        -o $lib
    fail_panic "Could not create ${_BUILD_PREFIX}shared library $libname"
}

builder_host_shared_library ()
{
    local lib libname
    libname=$1
    lib=$_BUILD_DSTDIR/$libname
    lib=${lib%%.so}.so
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $lib"
        echo "$lib: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    builder_log "${_BUILD_PREFIX}SharedLibrary: $libname"

    if [ -z "$_BUILD_CXX" ]; then
        _BUILD_CXX=${CXX:-g++}
    fi

    # Important: -lgcc must appear after objects and static libraries,
    #            but before shared libraries for Android. It doesn't hurt
    #            for other platforms.
    builder_command ${_BUILD_CXX} \
        -shared -s \
        $_BUILD_OBJECTS \
        $_BUILD_STATIC_LIBRARIES \
        $_BUILD_SHARED_LIBRARIES \
        $_BUILD_LDFLAGS \
        -o $lib
    fail_panic "Could not create ${_BUILD_PREFIX}shared library $libname"
}

builder_host_executable ()
{
    local exe exename
    exename=$1
    exe=$_BUILD_DSTDIR/$exename$HOST_EXE
    if [ "$_BUILD_MK" ]; then
        _BUILD_TARGETS=$_BUILD_TARGETS" $exe"
        echo "$exe: $_BUILD_OBJECTS" >> $_BUILD_MK
    fi
    builder_log "${_BUILD_PREFIX}Executable: $exename$HOST_EXE"

    if [ -z "$_BUILD_CXX" ]; then
        _BUILD_CXX=${CXX:-g++}
    fi

    # Important: -lgcc must appear after objects and static libraries,
    #            but before shared libraries for Android. It doesn't hurt
    #            for other platforms.
    builder_command ${_BUILD_CXX} \
        -s \
        $_BUILD_OBJECTS \
        $_BUILD_STATIC_LIBRARIES \
        $_BUILD_SHARED_LIBRARIES \
        $_BUILD_LDFLAGS \
        -o $exe
    fail_panic "Could not create ${_BUILD_PREFIX}executable $libname"
}


builder_end ()
{
    if [ "$_BUILD_MK" ]; then
        echo "all: $_BUILD_TARGETS" >> $_BUILD_MK
        run make -j$NUM_JOBS -f $_BUILD_MK
        fail_panic "Could not build project!"
    fi

    if [ "$_BUILD_DIR_NEW" ]; then
        log2 "Cleaning up build directory: $_BUILD_DIR"
        rm -rf "$_BUILD_DIR"
        _BUILD_DIR_NEW=
    fi
}

# Same as builder_begin, but to target Android with a specific ABI
# $1: ABI name (e.g. armeabi)
# $2: Build directory
# $3: Gcc version
# $4: Optional llvm version
# $5: Optional Makefile name
builder_begin_android ()
{
    local ABI BUILDDIR LLVM_VERSION MAKEFILE
    local ARCH SYSROOT FLAGS
    local BINPREFIX GCC_TOOLCHAIN LLVM_TRIPLE GCC_VERSION
    if [ -z "$NDK_DIR" ]; then
        panic "NDK_DIR is not defined!"
    elif [ ! -d "$NDK_DIR/platforms" ]; then
        panic "Missing directory: $NDK_DIR/platforms"
    fi
    ABI=$1
    BUILDDIR=$2
    GCC_VERSION=$3
    LLVM_VERSION=$4
    MAKEFILE=$5
    ARCH=$(convert_abi_to_arch $ABI)

    if [ "$(arch_in_unknown_archs $ARCH)" = "yes" ]; then
        LLVM_VERSION=$DEFAULT_LLVM_VERSION
    fi
    if [ -n "$LLVM_VERSION" ]; then
        # override GCC_VERSION to pick $DEFAULT_LLVM_GCC_VERSION instead
        GCC_VERSION=$DEFAULT_LLVM_GCC_VERSION
    fi
    for TAG in $HOST_TAG $HOST_TAG32; do
        BINPREFIX=$NDK_DIR/$(get_toolchain_binprefix_for_arch $ARCH $GCC_VERSION $TAG)
        if [ -f ${BINPREFIX}-gcc ]; then
            break;
        fi
    done
    if [ -n "$LLVM_VERSION" ]; then
        GCC_TOOLCHAIN=`dirname $BINPREFIX`
        GCC_TOOLCHAIN=`dirname $GCC_TOOLCHAIN`
        BINPREFIX=$NDK_DIR/$(get_llvm_toolchain_binprefix $LLVM_VERSION $TAG)
    fi

    SYSROOT=$NDK_DIR/$(get_default_platform_sysroot_for_arch $ARCH)

    builder_begin "$BUILDDIR" "$MAKEFILE"
    builder_set_prefix "$ABI "
    if [ -z "$LLVM_VERSION" ]; then
        builder_set_binprefix "$BINPREFIX"
    else
        builder_set_binprefix_llvm "$BINPREFIX"
        case $ABI in
            armeabi)
                LLVM_TRIPLE=armv5te-none-linux-androideabi
                ;;
            armeabi-v7a|armeabi-v7a-hard)
                LLVM_TRIPLE=armv7-none-linux-androideabi
                ;;
            arm64-v8a)
                LLVM_TRIPLE=aarch64-none-linux-android
                ;;
            x86)
                LLVM_TRIPLE=i686-none-linux-android
                ;;
            x86_64)
                LLVM_TRIPLE=x86_64-none-linux-android
                ;;
            mips)
                LLVM_TRIPLE=mipsel-none-linux-android
                ;;
            mips64)
                LLVM_TRIPLE=mips64el-none-linux-android
                ;;
            *)
                LLVM_TRIPLE=le32-none-ndk
                GCC_TOOLCHAIN=
                FLAGS=-emit-llvm
                ;;
        esac
        builder_cflags "-target $LLVM_TRIPLE $FLAGS"
        builder_ldflags "-target $LLVM_TRIPLE $FLAGS"
        if [ ! -z $GCC_TOOLCHAIN ]; then
            builder_cflags "-gcc-toolchain $GCC_TOOLCHAIN"
            builder_ldflags "-gcc-toolchain $GCC_TOOLCHAIN"
        fi
    fi

    builder_cflags "--sysroot=$SYSROOT"
    builder_cxxflags "--sysroot=$SYSROOT"
    builder_ldflags "--sysroot=$SYSROOT"

    case $ABI in
        armeabi)
            if [ -z "$LLVM_VERSION" ]; then
                # add -minline-thumb1-jumptable such that gabi++/stlport/libc++ can be linked
                # with compiler-rt where helpers __gnu_thumb1_case_* (in libgcc.a) don't exist
                builder_cflags "-minline-thumb1-jumptable"
            else
                builder_cflags ""
            fi
            ;;
        armeabi-v7a|armeabi-v7a-hard)
            builder_cflags "-march=armv7-a -mfpu=vfpv3-d16"
            builder_ldflags "-march=armv7-a -Wl,--fix-cortex-a8"
            if [ "$ABI" != "armeabi-v7a-hard" ]; then
                builder_cflags "-mfloat-abi=softfp"
            else
                builder_cflags "-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
                builder_ldflags "-Wl,--no-warn-mismatch -lm_hard"
            fi
            ;;
    esac
}

# $1: Build directory
# $2: Optional Makefile name
builder_begin_host ()
{
    prepare_host_build
    builder_begin "$1" "$2"
    builder_set_prefix "$HOST_TAG "
}