[Posix] Optimize string marshal code by using unsafe code (#4964)
[mono.git] / nacl / common.sh
1 # Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that be
3 # found in the LICENSE file.
4 #
5
6 set -o nounset
7 set -o errexit
8
9 # scripts that source this file must be run from within packages tree
10 readonly SAVE_PWD=$(pwd)
11
12 # Pick platform directory for compiler.
13 readonly OS_NAME=$(uname -s)
14 if [ $OS_NAME = "Darwin" ]; then
15   readonly OS_SUBDIR="mac"
16   readonly OS_SUBDIR_SHORT="mac"
17 elif [ $OS_NAME = "Linux" ]; then
18   readonly OS_SUBDIR="linux"
19   readonly OS_SUBDIR_SHORT="linux"
20 else
21   readonly OS_SUBDIR="windows"
22   readonly OS_SUBDIR_SHORT="win"
23 fi
24
25 readonly MACHINE=$(uname -m)
26 if [ $MACHINE = "x86_64" ]; then
27   readonly TARGET_BITSIZE=${TARGET_BITSIZE:-"64"}
28   readonly HOST_BITSIZE=${HOST_BITSIZE:-"64"}
29 else
30   # uname -m reports i686 on Linux and i386 on Mac
31   readonly TARGET_BITSIZE=${TARGET_BITSIZE:-"32"}
32   readonly HOST_BITSIZE=${HOST_BITSIZE:-"32"}
33 fi
34
35 if [ $TARGET_BITSIZE == "64" ]; then
36   readonly TARGET_BIT_PREFIX="64"
37   readonly CROSS_ID=x86_64
38 else
39   readonly TARGET_BIT_PREFIX=""
40   readonly CROSS_ID=i686
41 fi
42 # we might want to override the detected host platform (e.g. on OSX 10.6)
43 if [ $HOST_BITSIZE == "64" ]; then
44   readonly HOST_BIT_PREFIX="64"
45 else
46   readonly HOST_BIT_PREFIX=""
47 fi
48
49 export NACL_CROSS_PREFIX=${CROSS_ID}-nacl
50 export NACL_CROSS_PREFIX_DASH=${NACL_CROSS_PREFIX}-
51
52 readonly NACL_NEWLIB=${NACL_NEWLIB:-"0"}
53
54 if [ $NACL_NEWLIB = "1" ]; then
55   readonly NACL_SDK_BASE=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86_newlib
56 else
57 case "${NACL_SDK_ROOT}" in
58 *pepper_15* | *pepper_16* | *pepper_17*)
59   readonly NACL_SDK_BASE=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86
60   ;;
61 *)
62   readonly NACL_SDK_BASE=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86_glibc
63   ;;
64 esac
65 fi
66
67 readonly NACL_BIN_PATH=${NACL_SDK_BASE}/bin
68 export NACLCC=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}gcc
69 export NACLCXX=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}g++
70 export NACLAR=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ar
71 export NACLRANLIB=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ranlib
72 export NACLLD=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ld
73 export NACLAS=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}as
74
75 # NACL_SDK_GCC_SPECS_PATH is where nacl-gcc 'specs' file will be installed
76 readonly NACL_SDK_GCC_SPECS_PATH=${NACL_SDK_BASE}/lib/gcc/x86_64-nacl/4.4.3
77
78 # NACL_SDK_USR is where the headers, libraries, etc. will be installed
79 readonly NACL_SDK_USR=${NACL_SDK_BASE}/${NACL_CROSS_PREFIX}/usr
80 readonly NACL_SDK_USR_INCLUDE=${NACL_SDK_USR}/include
81 readonly NACL_SDK_USR_LIB=${NACL_SDK_USR}/lib
82
83
84 ######################################################################
85 # Helper functions
86 ######################################################################
87
88 Banner() {
89   echo "######################################################################"
90   echo $*
91   echo "######################################################################"
92 }
93
94
95 VerifyPath() {
96   # make sure path isn't all slashes (possibly from an unset variable)
97   local PATH=$1
98   local TRIM=${PATH##/}
99   if [ ${#TRIM} -ne 0 ]; then
100     return 0
101   else
102     return 1
103   fi
104 }
105
106
107 ChangeDir() {
108   local NAME=$1
109   if VerifyPath ${NAME}; then
110     cd ${NAME}
111   else
112     echo "ChangeDir called with bad path."
113     exit -1
114   fi
115 }
116
117
118 Remove() {
119   local NAME=$1
120   if VerifyPath ${NAME}; then
121     rm -rf ${NAME}
122   else
123     echo "Remove called with bad path."
124     exit -1
125   fi
126 }
127
128
129 MakeDir() {
130   local NAME=$1
131   if VerifyPath ${NAME}; then
132     mkdir -p ${NAME}
133   else
134     echo "MakeDir called with bad path."
135     exit -1
136   fi
137 }
138
139
140 PatchSpecFile() {
141   # fix up spaces so gcc sees entire path
142   local SED_SAFE_SPACES_USR_INCLUDE=${NACL_SDK_USR_INCLUDE/ /\ /}
143   local SED_SAFE_SPACES_USR_LIB=${NACL_SDK_USR_LIB/ /\ /}
144   # have nacl-gcc dump specs file & add include & lib search paths
145   ${NACL_SDK_BASE}/bin/x86_64-nacl-gcc -dumpspecs |\
146     sed "/*cpp:/{
147       N
148       s|$| -I${SED_SAFE_SPACES_USR_INCLUDE}|
149     }" |\
150     sed "/*link_libgcc:/{
151       N
152       s|$| -L${SED_SAFE_SPACES_USR_LIB}|
153     }" >${NACL_SDK_GCC_SPECS_PATH}/specs
154 }
155
156
157 DefaultConfigureStep() {
158   Banner "Configuring ${PACKAGE_NAME}"
159   # export the nacl tools
160   export CC=${NACLCC}
161   export CXX=${NACLCXX}
162   export AR=${NACLAR}
163   export RANLIB=${NACLRANLIB}
164   export PKG_CONFIG_PATH=${NACL_SDK_USR_LIB}/pkgconfig
165   export PKG_CONFIG_LIBDIR=${NACL_SDK_USR_LIB}
166   export PATH=${NACL_BIN_PATH}:${PATH};
167   ChangeDir ${NACL_PACKAGES_REPOSITORY}/${PACKAGE_NAME}
168   Remove ${PACKAGE_NAME}-build
169   MakeDir ${PACKAGE_NAME}-build
170   cd ${PACKAGE_NAME}-build
171   ../configure \
172     --host=nacl \
173     --disable-shared \
174     --prefix=${NACL_SDK_USR} \
175     --exec-prefix=${NACL_SDK_USR} \
176     --libdir=${NACL_SDK_USR_LIB} \
177     --oldincludedir=${NACL_SDK_USR_INCLUDE} \
178     --with-http=off \
179     --with-html=off \
180     --with-ftp=off \
181     --with-x=no
182 }
183
184
185 DefaultBuildStep() {
186   # assumes pwd has makefile
187   make clean
188 if [ $TARGET_BITSIZE == "64" ]; then
189   make -j8
190 else
191   make
192 fi
193 }
194
195
196 DefaultInstallStep() {
197   # assumes pwd has makefile
198   make install
199 }
200
201
202 DefaultCleanUpStep() {
203   PatchSpecFile
204   ChangeDir ${SAVE_PWD}
205 }
206
207
208 DefaultPackageInstall() {
209   DefaultPreInstallStep
210   DefaultDownloadStep
211   DefaultExtractStep
212   DefaultPatchStep
213   DefaultConfigureStep
214   DefaultBuildStep
215   DefaultInstallStep
216   DefaultCleanUpStep
217 }