summaryrefslogtreecommitdiffstats
path: root/sanity/kernel_compile.sh
blob: f010e3d385de3629604d56ac43cf24822375be4d (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#! /bin/bash
## vi: ai si et ts=4 sw=4 sts=4 bs=2 sta sr

ENV_me=$(basename $0);
ENV_medir="$(dirname $0)";
[ "$ENV_medir" = "." ] && ENV_medir="$(pwd)";
ENV_retval=0;

ENV_short_options='cs:m:V';
ENV_long_options='continue,site:,mainline:,verbose,quiet,help,version';

ENV_timestamp_in_log=0;     # 0 = Disabled, 1 = Enabled
ENV_log="/dev/stderr";
ENV_verbosity=2;
ENV_debug=1;
ENV_tmpdir="";              ## Temporary directory. Defined later

ENV_version="0.0";
ENV_copyright_years="2008";

def_mainline="2.6";
def_site="http://www.kernel.org";

function print_help () {
    cat <<-_HERE_
Usage: $ENV_me [OPTION]... [FILE]...
<DESCRIPTION HERE>.

Mandatory arguments to long options are mandatory for short options too.
      --help    display this help and exit
      --version output version information and exit
_HERE_
}

function timestamp () {
    echo "$(date +%F\ %T)";
}

function log () {
    if [ "$ENV_timestamp_in_log" = "1" ]; then
        echo "$ENV_me: $(timestamp): $@" | tee -a $log;
    else
        echo "$ENV_me: $(timestamp): $@";
    fi
}

function debug () {
    if [ "$ENV_debug" = 1 -o "$ENV_verbosity" = 3]; then
        echo "$@";
    fi
}

function error () {
    if [ "$ENV_verbosity" -ge 1 ]; then
        echo "$ENV_me: $@" 1>&2;
        quit 1;
    fi
}

function warn () {
    if [ "$ENV_verbosity" -ge 1 ]; then
        echo "$ENV_me: $@" 1>&2;
    fi
}

function outn () {
    if [ "$ENV_verbosity" -ge 2 ]; then
        echo -n "$@";
    fi
}

function out () {
    if [ "$ENV_verbosity" -ge 2 ]; then
        echo "$@";
    fi
}

function outln () {
    if [ "$ENV_verbosity" -ge 2 ]; then
        echo -n "$@" | tee -a $ENV_log;
    fi
}

function outl () {
    if [ "$ENV_verbosity" -ge 2 ]; then
        echo "$@" | tee -a $ENV_log;
    fi
}

function quit () {
    [ -d "$ENV_tmpdir" ] && rm -rf "$ENV_tmpdir";
    exit $1;
}

function print_version () {
    if [ "$ENV_package" != "" ]; then
        version_str="$ENV_me ($ENV_package) $ENV_version";
    else
        version_str="$ENV_me $ENV_version";
    fi

    cat <<-_version_
		$version_str
		Copyright (C) ${ENV_copyright_years:-$(date +%Y)} Z Research, Inc.
_version_
}

function print_usage () {
    warn "Try \`$ENV_me --help' for more information.";
    if [ "$1" != "0" -a "$1" != "" ]; then
        exit "$1";
    fi
}

function parseargs () {
    unset ${!OPT_@};
    ARGV=($(getopt --shell bash --name $ENV_me \
    --option "$ENV_short_options" \
    --longoptions "$ENV_long_options" \
    -- "$@")) || print_usage 1;

    local index=0;
    while [ "${ARGV[$index]}" != "--" ]; do
        local opt="$(echo ${ARGV[$index]//-/_} | sed 's/^__\?//')";
        eval local arg=${ARGV[$index+1]};   ## eval to get rid of 's

        if [[ "${ARGV[$index+1]}" =~ ^$'\''.* ]]; then # To
##             debug "Setting OPT_$opt = $arg";
            eval OPT_${opt}=$arg;
            index=$((index+1));
        else
##             debug "Incrementing OPT_$opt";
            eval OPT_$opt=$((OPT_$opt+1));
        fi
        index=$((index+1));
    done
    eval ARGV=("${ARGV[@]:$index+1}");
}

function make_tmp_dir () {
    local dirname="$ENV_me.$RANDOM.$RANDOM.$RANDOM.$$.XXXX";

    umask 077;
    ENV_tmpdir="$(mktemp -d ${tmpdir:-/tmp}/$dirname)";

    [ -z "$ENV_tmpdir" ] || [ ! -d "$ENV_tmpdir" ] && {
        warn "Unable to create temporary directory. Exiting ..";
        return 1;
    }
    return 0;
}

function get_tarball () {
    url="$1";
    filename=$(basename $url);
    basedir=$(pwd);

    [ -f "$filename" ] && {
        [ "$OPT_continue" != "1" ] && error "$filename: file exists.";
    }
    wget -c $url;
## out "Updating Timestamps ..";
##   $UPDATE_TIMESTAMP "$filename";

    return 0;
}


function get_latest_tarball () {
    basedir=$(pwd);
    cd $ENV_tmpdir;

    echo -n "Retreiving listing [ $ENV_site ] ... ";
    wget -q "$ENV_site";
    echo "Done";

    latest=$(sed -n '/The latest stable version of the Linux kernel is/,/<\/tr>/p' index.html | \
        sed -n 's/.*"\(.*\)">F<.*/\1/p');
    [ "$latest" = "" ] && error "$ENV_site: No tarball found";

    cd $basedir;
    url="$ENV_site$latest";
    get_tarball "$url";

    return 0;
}

function init () {
    parseargs "$@";
    set -e;

    [ "$OPT_help" = "1" ] && print_help && quit 0;
    [ "$OPT_version" = "1" ] && print_version && quit 0;
    trap 'quit 255' 1 2 3 6 13 15;
    make_tmp_dir || quit 1;    

    OPT_verbosity=$((${OPT_verbose:-0} + ${OPT_v:-0}));

    ENV_mainline=${OPT_mainline:-${OPT_m:-$def_mainline}};
    ENV_site=${OPT_site:-${OPT_s:-$def_site}};

    return 0;
}

function main () {
    filename=;

    if [ ${#ARGV[@]} = 0 ]; then
        out "No input specified. Fetching latest kernel tarball ..";
        get_latest_tarball;
    elif [[ "${ARGV[0]}" =~ ^http|ftp ]]; then
        out "URL detected. Fetching from URL ..";
        get_tarball "$@";
    elif [ -f "${ARGV[0]}" ]; then
        filename=$(basename ${ARGV[0]});
        if [[ ("${ARGV[0]}" =~ /) && ("${ARGV[0]::2}" != "./") ]]; then
            out "File detected. Copying from location ..";
            cp "${ARGV[0]}" .;
        fi
    else
        error "${ARGV[0]}: Unknown input method";
    fi

    out "Extracting Tarball ..";
    tar -jxf $filename;
    cd ${filename%.tar.bz2};
    find . -exec touch '{}' \;
    out "Making allmodconfig";
    make allmodconfig;
    out "Making depmod ..";
    #make depmod;
    out "Making bzImage ..";
    make bzImage;
    out "Making modules ..";
    make modules;

    quit 0;
}

init "$@" && main "$@";
quit $ENV_retval;