aboutsummaryrefslogtreecommitdiffstats
path: root/kernel-build/kbuild
blob: 8d108433279e20d0f6f76f43dbbe26993155554b (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
#!/bin/bash
#
# kbuild -- covenience script for build kernels
#
# This script will build a kernel using an external build directory.
# To use it, create the directory ".git/kbuild" in the top-level of
# the kernel sources, and create a file .git/kbuild/config with the
# following contents:
#
# BLD_DIR=/build/ext4
# KERN_ARCH=x86_64
#
# This will deposit the object files into the /build/ext4 directory,
# and create the modules.tar.gz directory for use with kvm-xfstests
# and gce-xfstests.

# N=$(($(getconf _NPROCESSORS_ONLN) / 2))
N=$(($(getconf _NPROCESSORS_ONLN) * 1))

if test ! -f MAINTAINERS ; then
    echo "Not at top-level of kernel source tree?" 1>&2
    exit 1
fi

mkdir -p .git/kbuild
if test -f .git/kbuild/config ; then
    . .git/kbuild/config
elif test -f .git/kbuild.conf ; then
    . .git/kbuild.conf
    mv .git/kbuild.conf .git/kbuild/config
fi

if test "$1" == "--32" -o "$1" == "-32" ; then
    shift
    if test -n "$BLD_DIR_32" ; then
	BLD_DIR=$BLD_DIR_32
    fi
    if test -n "$KERN_ARCH_32" ; then
	KERN_ARCH=$KERN_ARCH_32
    else
	KERN_ARCH=i386
    fi
fi

if test "$1" == "--get-build-dir" ; then
    if test -z "$BLD_DIR" ; then
	BLD_DIR=.
    fi
    echo $BLD_DIR
    exit 0
fi

if test -n "$BLD_DIR" -a ! -d "$BLD_DIR" ; then
    mkdir -p "$BLD_DIR"
    if test -f .git/kbuild/kernel-config ; then
	cp .git/kbuild/kernel-config "$BLD_DIR/.config"
    fi
    for i in x509.genkey signing_key.pem signing_key.x509
    do
	if test -f ".git/kbuild/$i" ; then
	    mkdir -p "$BLD_DIR/certs"
	    cp ".git/kbuild/$i" "$BLD_DIR/certs"
	fi
    done
fi

MAKE_ARGS=("ARCH=${KERN_ARCH:-x86_64}" "-j$N")
if test -n "$BLD_DIR" ; then
    MAKE_ARGS+=("O=$BLD_DIR")
else
    BLD_DIR="."
fi

if grep -q CONFIG_CC_IS_CLANG=y "$BLD_DIR/.config" 2>/dev/null ; then
    MAKE_ARGS+=("CC=clang")
fi

if test "$1" == "--dpkg" ; then
    shift
    DO_DPKG=yes
    DPKG_EXPLICIT=yes
elif test "$1" == "--no-dpkg" ; then
    shift
    DO_DPKG=
    DPKG_EXPLICIT=yes
fi

rm -f "$BLD_DIR/linux-image.deb" "$BLD_DIR/linux-image-dbg.deb" \
   "$BLD_DIR/linux-headers.deb" "$BLD_DIR/linux-libc-dev.deb"

if test -n "$1" -a -z "$DPKG_EXPLICIT" ; then
    DO_DPKG=
fi

if test -n "$DO_DPKG" ; then
    make "${MAKE_ARGS[@]}" prepare
    REL=$(make "${MAKE_ARGS[@]}" kernelrelease | grep -v ^make)
    time nice make "KDEB_SOURCENAME=linux-${REL}" "${MAKE_ARGS[@]}" \
	 bindeb-pkg "$@"
    err=$?
    d="$BLD_DIR/.."
    if test -f "$BLD_DIR/debian/arch" ; then
	arch=$(cat $BLD_DIR/debian/arch)
    else
	arch=$(dpkg-architecture -q DEB_TARGET_ARCH)
    fi
    NUM=$(cd $d ; /bin/ls -t linux-${REL}_${REL}*.changes | head -1 | \
	       sed -e "s/linux-${REL}_${REL}-//" -e "s/_${arch}.changes//")
    v="${REL}-${NUM}_${arch}"
    if test -f "$d/linux-image-${REL}_${v}.deb" ; then
	mv "$d/linux-image-${REL}_${v}.deb" "$BLD_DIR/linux-image.deb"
    fi
    if test -f "$d/linux-image-${REL}-dbg_${v}.deb" ; then
	mv "$d/linux-image-${REL}-dbg_${v}.deb" "$BLD_DIR/linux-image-dbg.deb"
    fi
    if test -f "$d/linux-headers-${REL}_${v}.deb" ; then
	mv "$d/linux-headers-${REL}_${v}.deb" "$BLD_DIR/linux-headers.deb"
    fi
    if test -f "$d/linux-libc-dev_${v}.deb" ; then
	mv "$d/linux-libc-dev_${v}.deb" "$BLD_DIR/linux-libc-dev.deb"
    fi
    rm -f "$d/linux-${REL}_${v}.buildinfo" "$d/linux-${REL}_${v}.changes"
else
    time nice make "${MAKE_ARGS[@]}" "$@"
    err=$?
fi

if test -z "$*" -a "$err" == 0 && \
	grep -q CONFIG_MODULES=y $BLD_DIR/.config ; then
    TMPMODDIR=$(mktemp --tmpdir -d kbuild-modules.XXXXXXXX)
    echo "Generating $BLD_DIR/modules.tar.xz"
    make "${MAKE_ARGS[@]}" modules_install INSTALL_MOD_PATH="$TMPMODDIR" > /dev/null
    tar -C "$TMPMODDIR" -c --owner=0 --group=0 --numeric-owner \
	--mode=go+u-w -f - lib/modules | xz > "$BLD_DIR/modules.tar.xz"
    rm -rf "$TMPMODDIR"
else
    rm -f "$BLD_DIR/modules.tar.xz"
fi

cp "$BLD_DIR/.config" .git/kbuild/kernel-config
for i in x509.genkey signing_key.pem signing_key.x509
do
    if test -f "$BLD_DIR/certs/$i" ; then
	cp "$BLD_DIR/certs/$i" .git/kbuild
    fi
done

exit $err