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