Ticket #28288: c-wrapper-template

File c-wrapper-template, 2.5 KB (added by Veence (Vincent), 13 years ago)

A c/c++ "wrapper" to allows two-way universal builds

Line 
1#!/bin/sh
2COMPILER='@@@'
3SUFFIX='&&&'
4OUTPUT_O='NO'
5OUTPUT=''
6NAMED_OUTPUT=''
7LASTFILE=''
8INTEL='NO'
9SIZE32='NO'
10SIZE64='NO'
11NEWARGS=''
12
13SKIP='NO'
14
15for arg in $@
16do
17        if [ $SKIP = 'ARCH' ]; then
18                # intercept -arch option and set SIZEXX
19                SKIP='NO'
20                if [ $arg = 'x86_64' ] || [ $arg = 'ppc64' ]; then
21                        SIZE64='YES'
22                else
23                        SIZE32='YES'
24                fi
25               
26                # which architecture are we compiling for?
27                if [ $arg = 'x86_64' ] || [ $arg = 'i386' ]; then
28                        INTEL='YES'
29                fi
30               
31        elif [ $arg = '-arch' ]; then
32                SKIP='ARCH'
33               
34        elif [ $arg = '--version' ]; then
35                ${COMPILER} --version
36                exit 0
37               
38        else
39                NEWARGS+="$arg "
40               
41                # if the -c option is given, the output is .o
42                if [ $arg = '-c' ]; then
43                        OUTPUT_O='YES'
44                fi
45
46                # if the output file is given by a -o option, record it
47                if [ $SKIP = 'O' ]; then
48                        SKIP='NO'
49                        NAMED_OUTPUT=$arg
50                fi
51               
52                if [ $arg = '-o' ]; then
53                        SKIP='O'
54                fi
55               
56                # Note each file ending by ${SUFFIX} and remember the last one
57                # Transform them in .o
58                if `echo $arg | grep -q "${SUFFIX}$"`; then
59                        LASTFILE=$arg
60                        OUTPUT+=`echo $arg | sed "s/${SUFFIX}/\.o/"`
61                        OUTPUT+=' '
62                fi
63        fi
64done
65
66# What is the output?
67
68if [ ${NAMED_OUTPUT}"X" != "X" ]; then
69        OUTPUT=$NAMED_OUTPUT
70
71elif [ $OUTPUT_O = 'NO' ]; then
72        # It is an executable whose is name is the LASTFILE without suffix
73        OUTPUT=`echo ${LASTFILE} | sed "s/${SUFFIX}//"`
74
75else
76        # Othewise, the output is just the ${OUTPUT} variable as computed before
77        # without path
78        OUTPUT=`echo ${OUTPUT} | sed -E "s|(.*)/([^/]*)$|\2|"`
79fi
80
81# Now, compile
82
83if [ $SIZE32 = 'NO' ] && [ $SIZE64 = 'NO' ]; then
84        # No size indication given, just proceed with default
85        if `${COMPILER} $NEWARGS`; then
86                exit 0
87        else
88                exit 1
89        fi
90
91elif [ $SIZE32 = 'YES' ] && [ $SIZE64 = 'NO' ]; then
92        # 32-bit
93        if `${COMPILER} -m32 $NEWARGS`; then
94                exit 0
95        else
96                exit 1
97        fi
98       
99elif [ $SIZE32 = 'NO' ] && [ $SIZE64 = 'YES' ]; then
100        # 64-bit
101        if `${COMPILER} -m64 $NEWARGS`; then
102                exit 0
103        else
104                exit 1
105        fi
106
107else
108        # Universal case
109        if `${COMPILER} -m32 $NEWARGS`; then
110                for filename in ${OUTPUT}
111                do
112                        mv ${filename} ${filename}.32
113                done
114       
115                if `${COMPILER} -m64 $NEWARGS`; then
116                        for filename in ${OUTPUT}
117                        do
118                                mv ${filename} ${filename}.64
119                                if [ $INTEL = 'YES' ]; then
120                                        lipo -create -arch x86_64 ${filename}.64 \
121                                                 -arch i386 ${filename}.32 \
122                                                 -output ${filename}
123                                else
124                                        lipo -create -arch ppc64 ${filename}.64 \
125                                                 -arch ppc ${filename}.32 \
126                                                 -output ${filename}
127                                fi
128                       
129                                rm -f ${filename}.32 ${filename}.64
130                        done
131                else
132                        exit 1
133                fi
134        else
135                exit 1
136        fi
137fi
138exit 0