• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Copyright (C) 2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#  This shell script is used to rebuild the gcc and toolchain binaries
18#  for the Android NDK.
19#
20
21PROGDIR=$(dirname "$0")
22. "$PROGDIR/prebuilt-common.sh"
23
24PROGRAM_PARAMETERS="<dst-dir>"
25PROGRAM_DESCRIPTION="\
26This script allows you to generate a 'wrapper toolchain', i.e. a set of
27simple scripts that act as toolchain binaries (e.g. my-cc, my-c++, my-ld,
28etc...) but call another installed toolchain instead, possibly with additional
29command-line options.
30
31For example, imagine we want a toolchain that generates 32-bit binaries while
32running on a 64-bit system, we could call this script as:
33
34   $PROGNAME --cflags="-m32" --cxxflags="-m32" --ldflags="-m32" /tmp/my-toolchain
35
36Then, this will create programs like:
37
38   /tmp/my-toolchain/my-cc
39   /tmp/my-toolchain/my-gcc
40   /tmp/my-toolchain/my-c++
41   /tmp/my-toolchain/my-g++
42   /tmp/my-toolchain/my-ld
43   ...
44
45Where the compilers and linkers will add the -m32 flag to the command-line before
46calling the host version of 'cc', 'gcc', etc...
47
48Generally speaking:
49
50  - The 'destination toolchain' is the one that will be called by the
51    generated wrapper script. It is identified by a 'destination prefix'
52    (e.g. 'x86_64-linux-gnu-', note the dash at the end).
53
54    If is empty by default, but can be changed with --dst-prefix=<prefix>
55
56  - The 'source prefix' is the prefix added to the generated toolchain scripts,
57    it is 'my-' by default, but can be changed with --src-prefix=<prefix>
58
59  - You can use --cflags, --cxxflags, --ldflags, etc... to add extra
60    command-line flags for the generated compiler, linker, etc.. scripts
61
62"
63
64DEFAULT_SRC_PREFIX="my-"
65DEFAULT_DST_PREFIX=""
66
67SRC_PREFIX=$DEFAULT_SRC_PREFIX
68register_var_option "--src-prefix=<prefix>" SRC_PREFIX "Set source toolchain prefix"
69
70DST_PREFIX=$DEFAULT_DST_PREFIX
71register_var_option "--dst-prefix=<prefix>" DST_PREFIX "Set destination toolchain prefix"
72
73EXTRA_CFLAGS=
74register_var_option "--cflags=<options>" EXTRA_CFLAGS "Add extra C compiler flags"
75
76EXTRA_CXXFLAGS=
77register_var_option "--cxxflags=<options>" EXTRA_CXXFLAGS "Add extra C++ compiler flags"
78
79EXTRA_LDFLAGS=
80register_var_option "--ldflags=<options>" EXTRA_LDFLAGS "Add extra linker flags"
81
82EXTRA_ASFLAGS=
83register_var_option "--asflags=<options>" EXTRA_ASFLAGS "Add extra assembler flags"
84
85EXTRA_ARFLAGS=
86register_var_option "--arflags=<options>" EXTRA_ARFLAGS "Add extra archiver flags"
87
88CCACHE=
89register_var_option "--ccache=<prefix>" CCACHE "Use ccache compiler driver"
90
91PROGRAMS="cc gcc c++ g++ cpp as ld ar ranlib strip strings nm objdump dlltool"
92register_var_option "--programs=<list>" PROGRAMS "List of programs to generate wrapper for"
93
94extract_parameters "$@"
95
96PROGRAMS=$(commas_to_spaces "$PROGRAMS")
97if [ -z "$PROGRAMS" ]; then
98    panic "Empty program list, nothing to do!"
99fi
100
101DST_DIR="$PARAMETERS"
102if [ -z "$DST_DIR" ]; then
103    panic "Please provide a destination directory as a parameter! See --help for details."
104fi
105
106mkdir -p "$DST_DIR"
107fail_panic "Could not create destination directory: $DST_DIR"
108
109# Generate a small wrapper program
110#
111# $1: program name, without any prefix (e.g. gcc, g++, ar, etc..)
112# $2: source prefix (e.g. 'i586-mingw32msvc-')
113# $3: destination prefix (e.g. 'i586-px-mingw32msvc-')
114# $4: destination directory for the generate program
115#
116gen_wrapper_program ()
117{
118    local PROG="$1"
119    local SRC_PREFIX="$2"
120    local DST_PREFIX="$3"
121    local DST_FILE="$4/${SRC_PREFIX}$PROG"
122    local FLAGS=""
123
124    case $PROG in
125      cc|gcc) FLAGS=$FLAGS" $EXTRA_CFLAGS";;
126      c++|g++) FLAGS=$FLAGS" $EXTRA_CXXFLAGS";;
127      ar) FLAGS=$FLAGS" $EXTRA_ARFLAGS";;
128      as) FLAGS=$FLAGS" $EXTRA_ASFLAGS";;
129      ld|ld.bfd|ld.gold) FLAGS=$FLAGS" $EXTRA_LDFLAGS";;
130    esac
131
132    if [ -n "$CCACHE" ]; then
133        DST_PREFIX=$CCACHE" "$DST_PREFIX
134    fi
135
136    cat > "$DST_FILE" << EOF
137#!/bin/sh
138# Auto-generated, do not edit
139${DST_PREFIX}$PROG $FLAGS "\$@"
140EOF
141    chmod +x "$DST_FILE"
142    log "Generating: ${SRC_PREFIX}$PROG"
143}
144
145log "Generating toolchain wrappers in: $DST_DIR"
146
147for PROG in $PROGRAMS; do
148  gen_wrapper_program $PROG "$SRC_PREFIX" "$DST_PREFIX" "$DST_DIR"
149done
150
151log "Done!"
152