nrelease: Minor style improvements to the Makefile
[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 SRCDIR=         ${.CURDIR}/..
15
16 DPORTS_PATH?=   /usr/dports
17 ISODIR?=        /usr/obj/release
18 ISOROOT?=       ${ISODIR}/root
19 GITURL_SRC?=    git://git.dragonflybsd.org/dragonfly.git
20 GITURL_DPORTS?= git://mirror-master.dragonflybsd.org/dports.git
21 MAKE_JOBS?=     $$(sysctl -n hw.ncpu)
22 .if !defined(NOFSCHG)
23 MTREE_FSCHG=    -i
24 .endif
25
26 GITREV!=        sh ${SRCDIR}/tools/gitrev.sh
27
28 # The label/name of the disklabel64(5) slice in the ".img" file.  This is
29 # used to identify the USB device with DragonFly installation image, avoiding
30 # hardcoding the USB device name (e.g., "da8").
31 #
32 # This label is also used as the volume ID of the ".iso" file.
33 #
34 LABEL?=         DragonFly_${GITREV}
35
36 CHROOT_CMD?=    /usr/sbin/chroot ${ISOROOT} sh -c
37
38 ISOFILE?=       ${ISODIR}/dfly.iso
39 IMGFILE?=       ${ISODIR}/dfly.img
40 IMGMNT?=        ${ISODIR}/mnt
41
42 .if !make(nopkgs)
43 # User may specify extra packages in addition to the defaults
44 #
45 DPORTS_EXTRA_PACKAGES?=
46
47 # dports packages to be built and installed on the release ISO
48 #
49 DPORTS_PACKAGES?=       security/ca_root_nss \
50                         devel/git-lite \
51                         dns/bind-tools \
52                         net/isc-dhcp44-server \
53                         sysutils/cdrtools \
54                         security/wpa_supplicant \
55                         ${DPORTS_EXTRA_PACKAGES}
56
57 # dports options to use when building packages
58 #
59 DPORTS_OPTIONS+=        -DBATCH
60 DPORTS_OPTIONS+=        dns_bind-tools_UNSET=PYTHON
61 .endif
62
63 # Specify which root skeletons are required, and let the user include
64 # their own.  They are copied into ISODIR during the `customizeiso'
65 # target; each overwrites the last.
66 #
67 REQ_ROOTSKELS=  ${.CURDIR}/root
68 ROOTSKELS?=     ${REQ_ROOTSKELS}
69
70 .if make(gui)
71 ISOFILE?=               ${ISODIR}/dfly-gui.iso
72 IMGFILE?=               ${ISODIR}/dfly-gui.img
73
74 .if !make(nopkgs)
75 # NOTE: order important, do not sort package list
76 #
77 DPORTS_PACKAGES+=       x11/xorg \
78                         x11-wm/fluxbox \
79                         x11-wm/fvwm \
80                         www/firefox \
81                         graphics/xpdf \
82                         shells/zsh \
83                         editors/emacs \
84                         editors/vim \
85                         irc/irssi \
86                         lang/perl5 \
87                         editors/nano \
88                         shells/bash \
89                         devel/ctags \
90                         archivers/zip \
91                         security/sudo \
92                         www/links \
93                         ftp/wget \
94                         x11-fonts/terminus-font \
95                         net/rsync \
96                         x11-clocks/asclock \
97                         sysutils/screen
98 .endif
99
100 ROOTSKELS+=             ${.CURDIR}/gui
101 .endif  # make(gui)
102
103 #########################################################################
104 #                               BASE ISO TARGETS                        #
105 #########################################################################
106
107 release:        check clean buildworld1 buildkernel1 \
108                 buildiso customizeiso pkgs srcs mkiso mkimg
109
110 quickrel:       check clean buildworld2 buildkernel2 \
111                 buildiso customizeiso pkgs srcs mkiso mkimg
112
113 realquickrel:   check clean \
114                 buildiso customizeiso pkgs srcs mkiso mkimg
115
116 restartpkgs:    check pkgs srcs mkiso mkimg
117
118 quick:          quickrel
119
120 realquick:      realquickrel
121
122 #########################################################################
123 #                       CORE SUPPORT TARGETS                            #
124 #########################################################################
125
126 check:
127 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
128         @echo
129         @echo "Your machine does not have cdrtools installed.  You can install it with:"
130         @echo "    pkg install cdrtools"
131 .endif
132         @if [ ! -d ${DPORTS_PATH} ]; then \
133                 echo "${DPORTS_PATH} does not exist."; \
134                 echo ""; \
135                 echo "Please set DPORTS_PATH to the dports tree that shall be used for"; \
136                 echo "package building. The default is /usr/dports. See the Makefile in"; \
137                 echo "/usr if you are unfamiliar with dports."; \
138                 /usr/bin/false; \
139         fi
140 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
141         @/usr/bin/false
142 .endif
143         @echo "Using ${DPORTS_PATH} as the dports tree."
144
145 buildworld1 buildworld2:
146         ( cd ${SRCDIR}; \
147                 ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
148                         make -j ${MAKE_JOBS} -DWANT_INSTALLER \
149                         ${.TARGET:C/build(.*)2/quick\1/:C/1//} )
150
151 buildkernel1 buildkernel2:
152         ( cd ${SRCDIR}; \
153                 ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
154                         make -j ${MAKE_JOBS} \
155                         ${.TARGET:C/build(.*)2/quick\1/:C/1//} )
156
157 # Unconditionally clean out ${ISOROOT} so a previous img build
158 # does not blow up a future quick iso build
159 #
160 buildiso:
161         -chflags -R noschg ${ISOROOT}
162         rm -rf ${ISOROOT}
163         mkdir -p ${ISOROOT}
164         ( cd ${SRCDIR}; \
165                 make -DWANT_INSTALLER DESTDIR=${ISOROOT} installworld )
166         # Do not mess with any /usr/obj directories not related to
167         # buildworld, buildkernel, or nrelease.
168         ( cd ${SRCDIR}/etc && \
169                 tmpdir=`mktemp -d -t nrelease` && \
170                 MAKEOBJDIRPREFIX=$${tmpdir} \
171                         make -m ${SRCDIR}/share/mk \
172                         DESTDIR=${ISOROOT} distribution && \
173                 rm -rf $${tmpdir} )
174         cpdup ${ISOROOT}/etc ${ISOROOT}/etc.hdd
175         ( cd ${SRCDIR}; make DESTDIR=${ISOROOT} reinstallkernel )
176         rm -rf ${ISOROOT}/boot/kernel.old
177         ln -sf kernel ${ISOROOT}/boot/kernel/kernel.BOOTP
178         mtree ${MTREE_FSCHG} -deU -f ${SRCDIR}/etc/mtree/BSD.var.dist \
179                 -p ${ISOROOT}/var
180         dev_mkdb -f ${ISOROOT}/var/run/dev.db ${ISOROOT}/dev
181         ${CHROOT_CMD} "rcrestart ldconfig"
182
183 # The GUI build includes the full system source (~500 MB) and the full
184 # dports tree (~250 MB).  The nominal release build only includes the
185 # kernel source (~30 MB).
186 #
187 srcs:
188         rm -rf ${ISOROOT}/usr/dports
189         rm -f ${ISOROOT}/usr/src-sys.tar.bz2
190 .if !defined(WITHOUT_SRCS)
191 .if make(gui)
192         ( cd ${ISOROOT}/usr && \
193                 make dports-create-shallow GITURL_DPORTS=${GITURL_DPORTS} )
194         ( cd ${ISOROOT}/usr && \
195                 make src-create-shallow GITURL_SRC=${GITURL_SRC} )
196 .else
197         ( cd ${SRCDIR} && \
198                 tar --exclude .git -s '/^\./src/' -cf - \
199                         ./Makefile ./Makefile.inc1 ./sys | \
200                         bzip2 -9 > ${ISOROOT}/usr/src-sys.tar.bz2 )
201 .endif
202 .endif
203
204 # Customize the ISO by copying rootskels in reverse priority order.
205 #
206 customizeiso:
207         # Copy the rootskels.  Allow sources to be owned by someone other
208         # than root (as is common when checked out via git).
209         #
210 .for ROOTSKEL in ${ROOTSKELS}
211         cpdup -X cpignore -o ${ROOTSKEL} ${ISOROOT}
212         @test -O ${.CURDIR} || echo "chowning copied files to root:wheel"
213         @test -O ${.CURDIR} || ((cd ${ROOTSKEL} && find .) | fgrep -v cpignore | (cd ${ISOROOT} && xargs chown root:wheel))
214 .endfor
215         pwd_mkdb -p -d ${ISOROOT}/etc ${ISOROOT}/etc/master.passwd
216 .for UPGRADE_ITEM in Makefile                   \
217                      etc.${MACHINE_ARCH}        \
218                      rc.d/Makefile              \
219                      periodic/Makefile          \
220                      periodic/daily/Makefile    \
221                      periodic/security/Makefile \
222                      periodic/weekly/Makefile   \
223                      periodic/monthly/Makefile
224         cp -R ${SRCDIR}/etc/${UPGRADE_ITEM} ${ISOROOT}/etc/${UPGRADE_ITEM}
225 .endfor
226
227 # Install packages by building from dports.
228 #
229 pkgs:
230 .if !empty(DPORTS_PACKAGES)
231         cp /etc/resolv.conf ${ISOROOT}/etc
232         -@umount ${ISOROOT}/dev
233         mount_null /dev ${ISOROOT}/dev
234
235         -@umount ${ISOROOT}/usr/distfiles
236         -@umount ${ISOROOT}/usr/dports
237         rm -rf ${ISOROOT}/usr/obj/dports
238
239         mkdir -p ${ISOROOT}/usr/dports
240         mkdir -p ${ISOROOT}/usr/distfiles
241
242         # Mount /usr/dports read-only for safety, else a failed umount
243         # and our rm -rf will do bad things.
244         mount_null -o ro ${DPORTS_PATH} ${ISOROOT}/usr/dports
245
246         # Make sure /usr/distfiles is writable
247         cp /etc/shells ${ISOROOT}/usr/distfiles/.test > /dev/null 2>&1 \
248             || mount_null ${ISODIR}/distfiles ${ISOROOT}/usr/distfiles
249
250 .for PKG in ${DPORTS_PACKAGES}
251         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} install"
252 .endfor
253 .for PKG in ${DPORTS_PACKAGES}
254         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} clean"
255 .endfor
256
257         ${CHROOT_CMD} "yes | pkg autoremove"
258
259         -umount ${ISOROOT}/usr/distfiles
260         umount ${ISOROOT}/usr/dports
261
262         rm -rf ${ISOROOT}/usr/dports
263         rm -rf ${ISOROOT}/usr/distfiles
264         rm -rf ${ISOROOT}/usr/obj/dports
265
266         # Update the locate(8) and whatis(1) databases, allow ISODIR
267         # to be on tmpfs (fails to create locate database then)
268         #
269         -${CHROOT_CMD} /etc/periodic/weekly/310.locate
270         -${CHROOT_CMD} /etc/periodic/weekly/320.whatis
271
272         umount ${ISOROOT}/dev
273         rm -f ${ISOROOT}/etc/resolv.conf
274
275         # Recopy files that dports may have updated in /etc into /etc.hdd
276         #
277         cpdup ${ISOROOT}/etc/shells ${ISOROOT}/etc.hdd/shells
278         cpdup ${ISOROOT}/etc/group ${ISOROOT}/etc.hdd/group
279 .endif
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 pkgs