• 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# Check if mingw compiler has dlfcn.h
110# $1: mignw compiler
111#
112mingw_has_dlfcn_h ()
113{
114   local CC="$1"
115
116   if [ ! -f "$CC" ]; then
117       # compiler not found
118       return 1
119   fi
120   "$CC" -xc /dev/null -dM -E | grep -q MINGW
121   if [ $? != 0 ]; then
122       # not a mingw compiler
123       return 1
124   fi
125
126   "$CC" -xc -c /dev/null -include dlfcn.h -o /dev/null > /dev/null 2>&1
127}
128
129# Generate a small wrapper program
130#
131# $1: program name, without any prefix (e.g. gcc, g++, ar, etc..)
132# $2: source prefix (e.g. 'i586-mingw32msvc-')
133# $3: destination prefix (e.g. 'i586-px-mingw32msvc-')
134# $4: destination directory for the generate program
135#
136gen_wrapper_program ()
137{
138    local PROG="$1"
139    local SRC_PREFIX="$2"
140    local DST_PREFIX="$3"
141    local DST_FILE="$4/${SRC_PREFIX}$PROG"
142    local FLAGS=""
143    local LDFLAGS=""
144
145    case $PROG in
146      cc|gcc|cpp)
147          FLAGS=$FLAGS" $EXTRA_CFLAGS"
148          if mingw_has_dlfcn_h ${DST_PREFIX}$PROG; then
149              LDFLAGS="-ldl"
150          fi
151          ;;
152      c++|g++)
153          FLAGS=$FLAGS" $EXTRA_CXXFLAGS"
154          if mingw_has_dlfcn_h ${DST_PREFIX}$PROG; then
155              LDFLAGS="-ldl"
156          fi
157          ;;
158      ar) FLAGS=$FLAGS" $EXTRA_ARFLAGS";;
159      as) FLAGS=$FLAGS" $EXTRA_ASFLAGS";;
160      ld|ld.bfd|ld.gold) FLAGS=$FLAGS" $EXTRA_LDFLAGS";;
161    esac
162
163    if [ -n "$CCACHE" ]; then
164        DST_PREFIX=$CCACHE" "$DST_PREFIX
165    fi
166
167    cat > "$DST_FILE" << EOF
168#!/bin/sh
169# Auto-generated, do not edit
170${DST_PREFIX}$PROG $FLAGS "\$@" $LDFLAGS
171EOF
172    chmod +x "$DST_FILE"
173    log "Generating: ${SRC_PREFIX}$PROG"
174}
175
176log "Generating toolchain wrappers in: $DST_DIR"
177
178for PROG in $PROGRAMS; do
179  gen_wrapper_program $PROG "$SRC_PREFIX" "$DST_PREFIX" "$DST_DIR"
180done
181
182log "Done!"
183