nrelease: Reorder and clean up the "srcs" target
[dragonfly.git] / nrelease / Makefile
1 #########################################################################
2 #                               ENHANCEMENTS                            #
3 #########################################################################
4
5 # Optional targets may be specified which set make variables which enhance
6 # the build in various ways.
7 #
8 gui nopkgs:
9
10 #########################################################################
11 #                               SETUP                                   #
12 #########################################################################
13
14 DPORTS_PATH?=   /usr/dports
15 ISODIR?=        /usr/obj/release
16 ISOROOT?=       ${ISODIR}/root
17 GITURL_SRC?=    git://git.dragonflybsd.org/dragonfly.git
18 GITURL_DPORTS?= git://mirror-master.dragonflybsd.org/dports.git
19 MAKE_JOBS?=     $$(sysctl -n hw.ncpu)
20 .if !defined(NOFSCHG)
21 MTREE_FSCHG=    -i
22 .endif
23
24 GITREV!=        sh ${.CURDIR}/../tools/gitrev.sh
25
26 # The label/name of the disklabel64(5) slice in the ".img" file.  This is
27 # used to identify the USB device with DragonFly installation image, avoiding
28 # hardcoding the USB device name (e.g., "da8").
29 #
30 # This label is also used as the volume ID of the ".iso" file.
31 #
32 LABEL?=         DragonFly_${GITREV}
33
34 CHROOT_CMD?=    /usr/sbin/chroot ${ISOROOT} sh -c
35
36 ISOFILE?=       ${ISODIR}/dfly.iso
37 IMGFILE?=       ${ISODIR}/dfly.img
38 IMGMNT?=        ${ISODIR}/mnt
39
40 .if !make(nopkgs)
41 # User may specify extra packages in addition to the defaults
42 #
43 DPORTS_EXTRA_PACKAGES?=
44
45 # dports packages to be built and installed on the release ISO
46 #
47 DPORTS_PACKAGES?=       security/ca_root_nss \
48                         devel/git-lite \
49                         dns/bind-tools \
50                         net/isc-dhcp44-server \
51                         sysutils/cdrtools \
52                         security/wpa_supplicant \
53                         ${DPORTS_EXTRA_PACKAGES}
54
55 # dports options to use when building packages
56 #
57 DPORTS_OPTIONS+=        -DBATCH
58 DPORTS_OPTIONS+=        dns_bind-tools_UNSET=PYTHON
59 .endif
60
61 # Specify which root skeletons are required, and let the user include
62 # their own.  They are copied into ISODIR during the `customizeiso'
63 # target; each overwrites the last.
64 #
65 REQ_ROOTSKELS=  ${.CURDIR}/root
66 ROOTSKELS?=     ${REQ_ROOTSKELS}
67
68 .if make(gui)
69 ISOFILE?=               ${ISODIR}/dfly-gui.iso
70 IMGFILE?=               ${ISODIR}/dfly-gui.img
71
72 .if !make(nopkgs)
73 # NOTE: order important, do not sort package list
74 #
75 DPORTS_PACKAGES+=       x11/xorg \
76                         x11-wm/fluxbox \
77                         x11-wm/fvwm \
78                         www/firefox \
79                         graphics/xpdf \
80                         shells/zsh \
81                         editors/emacs \
82                         editors/vim \
83                         irc/irssi \
84                         lang/perl5 \
85                         editors/nano \
86                         shells/bash \
87                         devel/ctags \
88                         archivers/zip \
89                         security/sudo \
90                         www/links \
91                         ftp/wget \
92                         x11-fonts/terminus-font \
93                         net/rsync \
94                         x11-clocks/asclock \
95                         sysutils/screen
96 .endif
97
98 ROOTSKELS+=             ${.CURDIR}/gui
99 .endif
100
101 #########################################################################
102 #                               BASE ISO TARGETS                        #
103 #########################################################################
104
105 release:        check clean buildworld1 buildkernel1 \
106                 buildiso customizeiso srcs mkiso mkimg
107
108 quickrel:       check clean buildworld2 buildkernel2 \
109                 buildiso customizeiso srcs mkiso mkimg
110
111 realquickrel:   check clean \
112                 buildiso customizeiso srcs mkiso mkimg
113
114 restartpkgs:    check customizeiso srcs mkiso mkimg
115
116 quick:          quickrel
117
118 realquick:      realquickrel
119
120 #########################################################################
121 #                       CORE SUPPORT TARGETS                            #
122 #########################################################################
123
124 check:
125 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
126         @echo
127         @echo "Your machine does not have cdrtools installed.  You can install it with:"
128         @echo "    pkg install cdrtools"
129 .endif
130         @if [ ! -d ${DPORTS_PATH} ]; then \
131                 echo "${DPORTS_PATH} does not exist."; \
132                 echo ""; \
133                 echo "Please set DPORTS_PATH to the dports tree that shall be used for"; \
134                 echo "package building. The default is /usr/dports. See the Makefile in"; \
135                 echo "/usr if you are unfamiliar with dports."; \
136                 /usr/bin/false; \
137         fi
138 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
139         @/usr/bin/false
140 .endif
141         @echo "Using ${DPORTS_PATH} as the dports tree."
142
143 buildworld1 buildworld2:
144         cd ${.CURDIR}/..; \
145         ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
146                 make -j ${MAKE_JOBS} -DWANT_INSTALLER ${.TARGET:C/build(.*)2/quick\1/:C/1//}
147
148 buildkernel1 buildkernel2:
149         cd ${.CURDIR}/..; \
150         ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
151                 make -j ${MAKE_JOBS} ${.TARGET:C/build(.*)2/quick\1/:C/1//}
152
153 # Unconditionally clean out ${ISOROOT} so a previous img build
154 # does not blow up a future quick iso build
155 #
156 # We install with INSTALLSTRIPPEDMODULES to make things fit into the ISO.
157 # We leave the kernel's debug variables intact.
158 #
159 buildiso:
160         -chflags -R noschg ${ISOROOT}
161         rm -rf ${ISOROOT}
162         mkdir -p ${ISOROOT}
163         ( cd ${.CURDIR}/..; make -DWANT_INSTALLER DESTDIR=${ISOROOT} installworld )
164         # Do not mess with any /usr/obj directories not related to
165         # buildworld, buildkernel, or nrelease.
166         ( cd ${.CURDIR}/../etc && \
167                 tmpdir=`mktemp -d -t nrelease` && \
168                 MAKEOBJDIRPREFIX=$${tmpdir} \
169                         make -m ${.CURDIR}/../share/mk \
170                         DESTDIR=${ISOROOT} distribution && \
171                 rm -rf $${tmpdir} )
172         cpdup ${ISOROOT}/etc ${ISOROOT}/etc.hdd
173         ( cd ${.CURDIR}/..; make DESTDIR=${ISOROOT} reinstallkernel )
174         rm -rf ${ISOROOT}/boot/kernel.old
175         ln -sf kernel ${ISOROOT}/boot/kernel/kernel.BOOTP
176         mtree ${MTREE_FSCHG} -deU -f ${.CURDIR}/../etc/mtree/BSD.var.dist \
177                 -p ${ISOROOT}/var
178         dev_mkdb -f ${ISOROOT}/var/run/dev.db ${ISOROOT}/dev
179
180 # The GUI build includes the full system source (~500 MB) and the full
181 # dports tree (~250 MB).  The nominal release build only includes the
182 # kernel source (~30 MB).
183 #
184 srcs:
185         rm -rf ${ISOROOT}/usr/dports
186         rm -f ${ISOROOT}/usr/src-sys.tar.bz2
187 .if !defined(WITHOUT_SRCS)
188 .if make(gui)
189         ( cd ${ISOROOT}/usr && \
190                 make dports-create-shallow GITURL_DPORTS=${GITURL_DPORTS} )
191         ( cd ${ISOROOT}/usr && \
192                 make src-create-shallow GITURL_SRC=${GITURL_SRC} )
193 .else
194         ( cd ${.CURDIR}/.. && \
195                 tar --exclude .git -s '/^\./src/' -cf - \
196                         ./Makefile ./Makefile.inc1 ./sys | \
197                         bzip2 -9 > ${ISOROOT}/usr/src-sys.tar.bz2 )
198 .endif
199 .endif
200
201 # Customize the ISO by copying rootskels in reverse priority order,
202 # building packages, and doing other post-install tasks.
203 #
204 customizeiso:
205         # Copy the rootskels.  Allow sources to be owned by someone other
206         # than root (as is common when checked out via git).
207         #
208 .for ROOTSKEL in ${ROOTSKELS}
209         cpdup -X cpignore -o ${ROOTSKEL} ${ISOROOT}
210         @test -O ${.CURDIR} || echo "chowning copied files to root:wheel"
211         @test -O ${.CURDIR} || ((cd ${ROOTSKEL} && find .) | fgrep -v cpignore | (cd ${ISOROOT} && xargs chown root:wheel))
212 .endfor
213         pwd_mkdb -p -d ${ISOROOT}/etc ${ISOROOT}/etc/master.passwd
214 .for UPGRADE_ITEM in Makefile                   \
215                      etc.${MACHINE_ARCH}        \
216                      rc.d/Makefile              \
217                      periodic/Makefile          \
218                      periodic/daily/Makefile    \
219                      periodic/security/Makefile \
220                      periodic/weekly/Makefile   \
221                      periodic/monthly/Makefile
222         cp -R ${.CURDIR}/../etc/${UPGRADE_ITEM} ${ISOROOT}/etc/${UPGRADE_ITEM}
223 .endfor
224         #
225         # Setup some things & mount dports tree.  Use defensive umounts and
226         # rm -rf's to allow restarts.  Allow /usr/dports to be read-only.
227         #
228         -@umount ${ISOROOT}/dev
229         mount_null /dev ${ISOROOT}/dev
230 .if !empty(DPORTS_PACKAGES)
231         cp /etc/resolv.conf ${ISOROOT}/etc
232         ${CHROOT_CMD} "ldconfig -elf /lib /usr/lib /usr/lib/gcc*"
233         -@umount ${ISOROOT}/usr/distfiles
234         -@umount ${ISOROOT}/usr/dports
235         rm -rf ${ISOROOT}/usr/obj/dports
236         #
237         # Mount /usr/dports, make sure /usr/distfiles is writable.
238         # Make /usr/dports read-only for safety, else a failed umount and
239         # our rm -rf will do bad things.
240         #
241         mkdir -p ${ISOROOT}/usr/dports
242         mkdir -p ${ISOROOT}/usr/distfiles
243         mount_null -o ro ${DPORTS_PATH} ${ISOROOT}/usr/dports
244         cp /etc/shells ${ISOROOT}/usr/distfiles/.test > /dev/null 2>&1 \
245             || mount_null ${ISODIR}/distfiles ${ISOROOT}/usr/distfiles
246         #
247         # Build and install packages, skip packages already installed
248         #
249 .for PKG in ${DPORTS_PACKAGES}
250         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} install"
251 .endfor
252 .for PKG in ${DPORTS_PACKAGES}
253         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} clean"
254 .endfor
255         #
256         # Remove packages which nothing depends on, add a whatis database
257         # for dports manual pages, create an initial locate database and
258         # clean up
259         #
260         ${CHROOT_CMD} "yes | pkg autoremove"
261         ${CHROOT_CMD} "makewhatis /usr/local/man"
262         -umount ${ISOROOT}/usr/distfiles
263         umount ${ISOROOT}/usr/dports
264         rm -rf ${ISOROOT}/usr/dports
265         rm -rf ${ISOROOT}/usr/obj/dports
266         rm -rf ${ISOROOT}/usr/distfiles
267         rm -f ${ISOROOT}/etc/resolv.conf
268 .endif
269         #
270         # Finally, update the locate(8) database, allow ISODIR
271         # to be on tmpfs (fails to create locate database then)
272         #
273         -${CHROOT_CMD} /etc/periodic/weekly/310.locate
274         umount ${ISOROOT}/dev
275         #
276         # Recopy files that dports may have updated in /etc into /etc.hdd
277         #
278         cpdup ${ISOROOT}/etc/shells ${ISOROOT}/etc.hdd/shells
279         cpdup ${ISOROOT}/etc/group ${ISOROOT}/etc.hdd/group
280
281 mkiso:
282         if [ ! -d ${IMGMNT} ]; then mkdir -p ${IMGMNT}; fi
283         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
284             vnconfig -u `cat ${ISODIR}/vn.which`; fi
285         newfs_msdos -C 400k -F 12 -L EFI -m 0xf8 ${ISOROOT}/boot/efiboot.img
286         vnconfig -l | grep "not in use" | head -n 1 |                   \
287             cut -f 1 -d: > ${ISODIR}/vn.which
288         vnconfig -e -s labels `cat ${ISODIR}/vn.which` ${ISOROOT}/boot/efiboot.img
289         mount_msdos /dev/`cat ${ISODIR}/vn.which` ${IMGMNT}
290         mkdir -p ${IMGMNT}/EFI/BOOT
291         cp ${ISOROOT}/boot/loader.efi ${IMGMNT}/EFI/BOOT/BOOTX64.EFI
292         umount ${IMGMNT}
293         ( cd ${ISOROOT}; mkisofs -R -J -o ${ISOFILE} \
294             -b boot/cdboot -no-emul-boot \
295             -eltorito-alt-boot -eltorito-platform efi \
296             -eltorito-boot boot/efiboot.img -no-emul-boot \
297             -V ${LABEL:C/(^.{32}).*/\1/} . )  # cut label to <= 32 characters
298         rm -f ${ISOROOT}/boot/efiboot.img
299
300 mkimg:
301         if [ ! -d ${IMGMNT} ]; then mkdir -p ${IMGMNT}; fi
302         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
303             vnconfig -u `cat ${ISODIR}/vn.which`; fi
304 .ifdef IMGSIZE
305         @echo "STEP: use an image size of ${IMGSIZE} 512-byte sectors"
306         rm -f ${IMGFILE}
307         sz=`bc -e "((${IMGSIZE}) * 512)" -equit`; truncate -s $${sz} ${IMGFILE}
308 .else
309         @echo "STEP: Determine required image size in 1GB steps"
310         @echo "      Leave ~600MB of unused space"
311         sz=`du -ck ${ISOROOT} | tail -n 1 | cut -f 1`;                  \
312             sz=`bc -e "(($${sz}) * 1.15 + 999999 + 600000) / 1000000" -equit | \
313             cut -f1 -d.`;                                               \
314             sz=`bc -e "(($${sz}) * 953)" -equit | cut -f1 -d.`;         \
315             rm -f ${IMGFILE};                                           \
316             truncate -s $${sz}M ${IMGFILE};
317 .endif
318         @echo "STEP: determine free vn device"
319         vnconfig -l | grep "not in use" | head -n 1 |                   \
320             cut -f 1 -d: > ${ISODIR}/vn.which
321         vnconfig -e -s labels `cat ${ISODIR}/vn.which` ${IMGFILE}
322         @echo "STEP: set up legacy MBR"
323         fdisk -b ${ISOROOT}/boot/mbr -IB `cat ${ISODIR}/vn.which`
324         fdisk -s `cat ${ISODIR}/vn.which` >${ISODIR}/fdisk.dat
325         awk '(NR==1){printf("g c%s h%s s%s\n", $$2, $$4, $$6);}' \
326             ${ISODIR}/fdisk.dat >${ISODIR}/fdisk.conf
327         echo "p 1 239 63 257985" >>${ISODIR}/fdisk.conf
328         awk '($$1=="1:"){printf("p 2 165 258048 %lu\n", $$3 - 258528);}' \
329             ${ISODIR}/fdisk.dat >>${ISODIR}/fdisk.conf
330         echo "a 2" >>${ISODIR}/fdisk.conf
331         fdisk -iv -f ${ISODIR}/fdisk.conf `cat ${ISODIR}/vn.which`
332         rm ${ISODIR}/fdisk.conf ${ISODIR}/fdisk.dat
333         newfs_msdos -F 32 -c 2 -L EFI -m 0xf8 `cat ${ISODIR}/vn.which`s1
334         mount_msdos /dev/`cat ${ISODIR}/vn.which`s1 ${IMGMNT}
335         mkdir -p ${IMGMNT}/EFI/BOOT
336         cp ${ISOROOT}/boot/boot1.efi ${IMGMNT}/EFI/BOOT/BOOTX64.EFI
337         umount ${IMGMNT}
338         @echo "STEP: write standard disklabel"
339         disklabel -w -r `cat ${ISODIR}/vn.which`s2 auto
340         @echo "STEP: read disklabel back"
341         disklabel -r `cat ${ISODIR}/vn.which`s2 > ${IMGFILE}.label
342         @echo "STEP: set disklabel name"
343         echo "label: ${LABEL}" >> ${IMGFILE}.label
344         @echo "STEP: add slice parition"
345         echo "a: * * 4.2BSD" >> ${IMGFILE}.label;
346         @echo "STEP: write modified disklabel back"
347         disklabel -R -r `cat ${ISODIR}/vn.which`s2 ${IMGFILE}.label
348         rm ${IMGFILE}.label
349         disklabel -B -b ${ISOROOT}/boot/boot1_64 -s ${ISOROOT}/boot/boot2_64 \
350             `cat ${ISODIR}/vn.which`s2
351         newfs /dev/`cat ${ISODIR}/vn.which`s2a
352         mount /dev/`cat ${ISODIR}/vn.which`s2a ${IMGMNT}
353         cpdup ${ISOROOT} ${IMGMNT}
354         @echo "STEP: fixup ${IMGMNT}/etc/fstab"
355         echo "/dev/part-by-label/${LABEL}.a / ufs rw,noatime 0 1" > ${IMGMNT}/etc/fstab
356         echo "dummy /tmp tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
357         echo "dummy /var/tmp tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
358         echo "dummy /var/run tmpfs rw,-C 0 0" >> ${IMGMNT}/etc/fstab
359         echo "dummy /usr/obj tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
360         echo "proc /proc procfs rw 0 0" >> ${IMGMNT}/etc/fstab
361         @echo "STEP: fixup ${IMGMNT}/boot/loader.conf"
362         -fgrep -v kernel_options ${IMGMNT}/boot/loader.conf > ${IMGMNT}/boot/loader.conf.new
363         echo 'vfs.root.mountfrom="ufs:part-by-label/${LABEL}.a"' >> ${IMGMNT}/boot/loader.conf.new
364         mv ${IMGMNT}/boot/loader.conf.new ${IMGMNT}/boot/loader.conf
365         @echo "STEP: cleanup"
366         df ${IMGMNT}
367         sync
368         sleep 1
369         umount ${IMGMNT}
370         vnconfig -u `cat ${ISODIR}/vn.which`
371         rm -f ${ISODIR}/vn.which
372         rmdir ${IMGMNT}
373         @echo "STEP: done, image files are in ${ISODIR}"
374
375 clean:
376         sync
377         sleep 1
378         -umount ${ISOROOT}/usr/distfiles > /dev/null 2>&1
379         -umount ${ISOROOT}/usr/dports > /dev/null 2>&1
380         -umount ${ISOROOT}/dev > /dev/null 2>&1
381         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
382             vnconfig -u `cat ${ISODIR}/vn.which`; fi
383         if [ -d ${ISOROOT} ]; then chflags -R noschg ${ISOROOT}; fi
384         rm -rf ${ISOROOT}
385         rm -f ${ISODIR}/.didbootstrap ${ISODIR}/vn.which
386
387 realclean:      clean
388         rm -rf ${ISODIR}/packages
389         rm -rf ${ISODIR}/distfiles
390
391 help all:
392         @echo "Targets:"
393         @echo "  release     - full build from scratch"
394         @echo "  quick       - attempt to do an incremental rebuild"
395         @echo "  realquick   - attempt to restart after world & kernel"
396         @echo "  restartpkgs - attempt to restart at the pkg building stage"
397         @echo ""
398         @echo "Optional targets:"
399         @echo "  nopkgs      - do not install any packages"
400         @echo "  gui         - do a GUI release"
401         @echo ""
402         @echo "Variables:"
403         @echo "  DPORTS_EXTRA_PACKAGES: add additional packages"
404         @echo "  GITURL_SRC: override the Git URL to source repository"
405         @echo "  GITURL_DPORTS: override the Git URL to dports repository"
406         @echo "  IMGSIZE: override the size of .img (in 512-byte sectors)"
407         @echo "  MAKE_JOBS: override the default value (sysctl hw.ncpu)"
408         @echo ""
409
410 .PHONY: release quickrel realquickrel
411 .PHONY: quick realquick
412 .PHONY: check buildworld1 buildworld2
413 .PHONY: buildkernel1 buildkernel2 buildiso customizeiso mkiso mkimg
414 .PHONY: clean realclean help all srcs