aboutsummaryrefslogtreecommitdiff
path: root/binary/static-gcc
blob: 1e4ad0eb1d12c978661df5598aa84be50a12a11a (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
#!/usr/bin/env bash

STATIC_GCC_AR=${STATIC_GCC_AR:-ar}
STATIC_GCC_RANLIB=${STATIC_GCC_RANLIB:-ranlib}
STATIC_GCC_CC=${STATIC_GCC_CC:-gcc}

DIR="$( cd "$( dirname "$0" )" && pwd )"

function log() { echo -- "$@" >> $DIR/log.txt; }

function runlog() { log "$@"; "$@"; }

log "---------------------------"
log INP "$@"

allargs=()
sources=()
objects=()
etc=()
libdirs=($("$STATIC_GCC_CC" -print-search-dirs | grep libraries | cut -d= -f2 | tr ':' '\n'))
incdirs=()

linking=0

while [ "$1" ]
do
   allargs+=("$1")
   if [ "$next_libdir" = "1" ]
   then
      libdirs+=("$1")
      next_libdir=0
   elif [ "$next_incdir" = "1" ]
   then
      incdirs+=("-I$1")
      next_incdir=0
   elif [ "$next_lib" = "1" ]
   then
      libs+=("$1")
      next_lib=0
   elif [ "$next_output" = "1" ]
   then
      output="$1"
      next_output=0
   else
      case "$1" in
      -*)
         case "$1" in
         -shared)
            linking=1
         ;;
         -static)
            linking=1
         ;;
         -o)
            next_output=1
         ;;
         -c)
            object=1
            etc+=("$1")
         ;;
         -L)
            next_libdir=1
         ;;
         -L*)
            libdirs+=("${1:2}")
         ;;
         -I)
            next_incdir=1
         ;;
         -I*)
            incdirs+=("$1")
         ;;
         -l)
            next_lib=1
         ;;
         -l*)
            libs+=("${1:2}")
         ;;
         *)
            etc+=("$1")
         ;;
         esac
      ;;
      *.c)
         sources+=("$1")
      ;;
      *.o)
         objects+=("$1")
      ;;
      *)
         etc+=("$1")
      ;;
      esac
   fi
   shift
done

staticlibs=()
for lib in "${libs[@]}"
do
   found=0
   for libdir in "${libdirs[@]}"
   do
      staticlib="$libdir/lib$lib.a"
      if [ -e "$staticlib" ]
      then
         staticlibs+=("$staticlib")
         found=1
         break
      fi
   done
   if [ "$found" = 0 ]
   then
      log "STATICLIB not found for $lib"
      runlog exit 1
   fi
done

oflag=()
if [ "$output" != "" ]
then
   oflag=("-o" "$output")
fi

if [ "$linking" = "1" ]
then
   log LINK
   if [ "${#sources[@]}" -gt 0 ]
   then
      for source in "${sources[@]}"
      do
         object="${source%.c}.o"
         runlog $STATIC_GCC_CC "${incdirs[@]}" "${etc[@]}" -c -o "$object" "$source"
         [ "$?" = 0 ] || runlog exit $?
         objects+=("$object")
      done
   fi

   # runlog ar rcu "${oflag[@]}" "${objects[@]}" "${staticlibs[@]}"
   echo "CREATE $output" > ar.script
   for o in "${objects[@]}"
   do
      echo "ADDMOD $o" >> ar.script
   done
   for o in "${staticlibs[@]}"
   do
      echo "ADDLIB $o" >> ar.script
   done
   echo "SAVE" >> ar.script
   echo "END" >> ar.script
   cat ar.script >> "$DIR/log.txt"
   cat ar.script | $STATIC_GCC_AR -M
   [ "$?" = 0 ] || runlog exit $?

   [ -e "$output" ] || {
      exit 1
   }

   runlog $STATIC_GCC_RANLIB "$output"
   runlog exit $?
elif [ "$object" = 1 ]
then
   log OBJECT
   runlog $STATIC_GCC_CC "${oflag[@]}" "${incdirs[@]}" "${etc[@]}" "${sources[@]}"
   runlog exit $?
else
   log EXECUTABLE
   runlog $STATIC_GCC_CC "${allargs[@]}"
   runlog exit $?
fi