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

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

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

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

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

allargs=()
sources=()
objects=()
etc=()
libdirs=("/usr/lib")
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
   for libdir in "${libdirs[@]}"
   do
      staticlib="$libdir/lib$lib.a"
      if [ -e "$staticlib" ]
      then
         staticlibs+=("$staticlib")
         break
      fi
   done
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 gcc "${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 | ar -M
   [ "$?" = 0 ] || runlog exit $?

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

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