-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
641 lines (612 loc) · 29.1 KB
/
Makefile
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#==--[ Makefile for MRainOS - Linux based ]--==#
# ---[ Makefile variable configuration ]--- #
# --[ Default version info ]-- #
VERSION := 0
PATCHLEVEL := 0
SUBLEVEL := 0
# Extraversion is declared after "-include .config.mk" so it can get values of src_dir_(conf & scripts)
RELEASE_TAG := unknown
# --[ Default escape sequence for colour values ]-- #
col_HEADING := \e[1;37;45m
col_SUBHEADING := \e[1;35;40m
col_INFOHEADING := \e[37;44m
col_INFO := \e[36m
col_TRUE := \e[32m
col_FALSE := \e[91m
col_DONE := \e[92m
col_ERROR := \e[1;91m
col_IMP := \e[1;37;41m
col_NORMAL := \e[0m
export col_HEADING col_SUBHEADING col_INFOHEADING col_INFO col_TRUE col_FALSE col_DONE col_ERROR col_IMP col_NORMAL
# --[ Others ]-- #
val_target := $(MAKECMDGOALS)# # Gets the target that the user invoked
val_mkfile_dir := $(shell pwd)# # Gets the current working director
val_unmain_sect := help# # Default sections to not include in global code
val_temp :=# # Temporary variable
S = $(Q)$(src_dir_scripts)/functions.sh
# --[ User's configuration (overrides vars with same name) ]-- #
-include .config.mk# # Include .config.mk
# Extraversion is here - read the comment in its previous location #
EXTRAVERSION := $(shell "$(src_dir_scripts)/get_var.sh" "latest_next" "$(src_dir_conf)/bcount.txt")
# ---[ Macros ]--- #
# --[ Definitions that do something ]-- #
define get_hash
shasum "$(strip $(1))" | cut -d ' ' -f 1
endef
define save_hash
@$(call heading, info, Saving hash of file '$(strip $(2))' as '$(strip $(1))=')
$(Q)"$(src_dir_scripts)/set_var.sh" "$(strip $(1))" `$(call get_hash, $(2))` "$(src_dir_conf)/hashes.txt"
endef
define get_hash_dir
find "$(strip $(1))" -type f -exec stat --format="%s %Y %n" {} + | sort | shasum | cut -d ' ' -f 1
endef
define save_hash_dir
@$(call heading, info, Saving hash of directory '$(strip $(2))' as '$(strip $(1))=')
$(Q)"$(src_dir_scripts)/set_var.sh" "$(strip $(1))" `$(call get_hash_dir, $(2))` "$(src_dir_conf)/hashes.txt"
endef
# --[ Exceptional message printers ]-- #
define stop
@echo "\n$(col_IMP)STOP:$(col_NORMAL)$(col_ERROR) $(strip $(1))$(col_NORMAL)\n" >&2; \
false
endef
define warn
@echo "$(col_ERROR) Warning:$(col_NORMAL)$(col_FALSE) $(strip $(1))$(col_NORMAL)" >&2
endef
define ok
@echo "$(col_DONE)$(1)$(col_NORMAL)"
endef
# --[ Common message printers ]-- #
define stat
@echo -n; \
val_temp='$(strip $(1))'; \
val_temp2=$${#val_temp}; \
if [ $$val_temp2 -gt 60 ] || [ $$((val_temp2 % 2)) -ne 0 ]; then \
$(subst @echo, echo, $(call warn, Total length of characters ($$val_temp2) supplied to definition \"$(0)\" is greater than 60 or is not even.)); \
else \
val_temp2=$$(((60 - (val_temp2)) / 2)); \
val_temp2=$$(printf '%*s' "$$val_temp2"); \
echo "$(col_INFO) !** $$val_temp2$$val_temp$$val_temp2 **! $(col_NORMAL)"; \
fi
endef
define true
@echo -n; \
val_temp='$(strip $(1))'; \
val_temp2=$${#val_temp}; \
val_temp3='$(strip $(2))'; \
val_temp4=$${#val_temp3}; \
val_temp2=$$((val_temp2 + val_temp4)); \
if [ $$val_temp2 -gt 42 ] || [ $$((val_temp2 % 2)) -ne 0 ]; then \
$(subst @echo, echo, $(call warn, Total length of characters ($$val_temp2) supplied to definition \"$(0)\" is greater than 42 or is not even.)); \
else \
val_temp2=$$(((60 - (val_temp2 + 4 + 14)) / 2)); \
val_temp2=$$(printf '%*s' "$$val_temp2"); \
echo "$(col_TRUE) !** $$val_temp2$$val_temp ($$val_temp3) is set to true$$val_temp2 **! $(col_NORMAL)"; \
fi
endef
define false
@echo -n; \
val_temp='$(strip $(1))'; \
val_temp2=$${#val_temp}; \
val_temp3='$(strip $(2))'; \
val_temp4=$${#val_temp3}; \
val_temp2=$$((val_temp2 + val_temp4)); \
if [ $$val_temp2 -gt 48 ] || [ $$((val_temp2 % 2)) -ne 0 ]; then \
$(subst @echo, echo, $(call warn, Total length of characters ($$val_temp2) supplied to definition \"$(0)\" is greater than 48 or is not even.)); \
else \
val_temp2=$$(((60 - (val_temp2 + 4 + 8)) / 2)); \
val_temp2=$$(printf '%*s' "$$val_temp2"); \
echo "$(col_FALSE) !** $$val_temp2$$val_temp ($$val_temp3) is false$$val_temp2 **! $(col_NORMAL)"; \
fi
endef
ifeq ($(bool_use_old_headings), y)
define heading
@echo -n; \
if [ "$(strip $(1))" = "imp" ]; then \
echo "\e[1;37;41m !! $(strip $(2)) !! \e[0m"; \
elif [ "$(strip $(1))" = "main" ]; then \
echo "\e[95m // $(strip $(2)) //\e[0m"; \
elif [ "$(strip $(1))" = "sub" ]; then \
echo "\e[38;5;206m / $(strip $(2)) /\e[0m"; \
elif [ "$(strip $(1))" = "sub2" ]; then \
echo "$(col_SUBHEADING) -+ $(strip $(2)) +- $(col_NORMAL)"; \
elif [ "$(strip $(1))" = "info" ]; then \
echo "\e[36m // $(strip $(2))\e[36m //\e[0m"; \
else \
$(subst @echo, echo, $(call warn, Definition \"$(0)\" doesn't know what '$(strip $(1))' means.)); \
fi
endef
else
define heading
@echo -n; \
if [ "$(strip $(1))" = "imp" ]; then \
echo "$(col_IMP) !! $(strip $(2)) !! $(col_NORMAL)"; \
elif [ "$(strip $(1))" = "main" ]; then \
echo "$(col_HEADING)---[ $(strip $(2)) ]---$(col_NORMAL)"; \
elif [ "$(strip $(1))" = "sub" ]; then \
echo "$(col_SUBHEADING) --+ $(strip $(2)) +-- $(col_NORMAL)"; \
elif [ "$(strip $(1))" = "sub2" ]; then \
echo "$(col_SUBHEADING) -+ $(strip $(2)) +- $(col_NORMAL)"; \
elif [ "$(strip $(1))" = "info" ]; then \
echo "$(col_INFOHEADING) ++ $(strip $(2))$(col_NORMAL)$(col_INFOHEADING) ++ $(col_NORMAL)"; \
else \
$(subst @echo, echo, $(call warn, Definition \"$(0)\" doesn't know what '$(strip $(1))' means.)); \
fi
endef
endif
# ---[ Global ]--- #
$(info $(shell echo "$(col_INFO) ++++++++++++++++++++++ MRain Operating System ++++++++++++++++++++++ $(col_NORMAL)"))
ifeq ($(bool_show_cmd), y)
$(info $(shell $(subst @echo, echo, $(call true, ShowCommand, bool_show_cmd))))
export Q :=
else
$(info $(shell $(subst @echo, echo, $(call false, ShowCommand, bool_show_cmd))))
export Q ?= @
endif
ifeq ($(bool_show_cmd_out), y)
$(info $(shell $(subst @echo, echo, $(call true, ShowAppOutput, bool_show_cmd_out))))
else
$(info $(shell $(subst @echo, echo, $(call false, ShowAppOutput, bool_show_cmd_out))))
ifeq ($(bool_show_cmd_out_err), y)
$(info $(shell $(subst @echo, echo, $(call true, ShowAppErrors, bool_show_cmd_out_err))))
export OUT := > /dev/null
else
$(info $(shell $(subst @echo, echo, $(call false, ShowAppErrors, bool_show_cmd_out_err))))
export OUT := > /dev/null 2>&1
endif
ifeq ($(findstring --no-print-directory,$(MAKEFLAGS)), )
MAKEFLAGS += --no-print-directory
endif
endif
ifeq ($(filter $(val_target),$(val_unmain_sect)),)
$(info $(shell $(subst @echo, echo, $(call stat, Checking variable 'EXTRAVERSION'))))
ifeq ($(shell echo "$(EXTRAVERSION)" | grep -Eq '^[0-9]+$$' && echo 1 || echo 0), 0)
$(info )
$(info $(shell $(subst @echo, echo, $(call warn, Variable 'EXTRAVERSION' is not a numeric value.))))
$(info + Contents of the variable at this time:)
$(info + $(EXTRAVERSION))
$(info )
$(info $(shell $(subst @echo, echo, $(call warn, Using value '0' instead - make sure bconfig.txt is correct.))))
$(eval EXTRAVERSION = 0)
endif
ifneq ($(and $(val_target), $(filter %config,$(val_target)), $(val_target)), )
# menuconfig
$(info $(shell $(subst @echo, echo, $(call stat, Preparing config manager))))
export srctree := $(if $(KBUILD_SRC),$(KBUILD_SRC),$(CURDIR))
export HOSTCC := gcc
include $(srctree)/make/Kbuild.include
else
# non-menuconfig
$(info $(shell $(subst @echo, echo, $(call heading, info, Sanitizing PATH))))
export PATH := $(shell echo $$PATH | tr -d '[:space:]')# Remove paths with spaces in variable PATH
ifneq ($(shell [ -f ".config" ] && echo y), y)
$(info $(shell $(call warn, .config file is not found. Settings in menuconfig may not reflect the current settings.)))
endif
ifeq ($(shell [ -f ".config.mk" ] && echo y), y)
$(info $(shell echo "$(col_TRUE) !** Configuration file found **! $(col_NORMAL)"))
else
$(info $(shell echo "$(col_FALSE) !** Configuration file isn't found **! $(col_NORMAL)"))
$(info $(shell $(call stop, .config.mk is not found. Run 'make menuconfig', save it & try again)))
endif
include $(src_dir_scripts)/scripts.mk# # Makefile scripts
export HOME=$(val_mkfile_dir)/$(bin_dir)/home# # Home dir replacement
export val_mkfile_dir # Current mkfile dir
endif
# Put stuff meant for menuconfig and non-menuconfig here
$(info $(shell $(subst @echo, echo, $(call heading, info, Exporting MRain System version))))
export MRAIN_VERSION := $(VERSION).$(PATCHLEVEL).$(SUBLEVEL).$(EXTRAVERSION)-$(RELEASE_TAG)
endif
# Go to 'all' if nothing is specified #
.DEFAULT_GOAL := all
# ---[ Menuconfig interface setup ]--- #
.PHONY: config
config:
$(Q)$(MAKE) $(build)=make/basic
$(Q)$(MAKE) $(build)=make/kconfig $@
%config:
$(Q)$(MAKE) $(build)=make/basic
$(Q)$(MAKE) $(build)=make/kconfig $@
# --- Default --- #
.PHONY: all
all: main prepare-iso final-touch
@echo ""
@width=$$(tput cols); \
for i in $$(seq 1 $$width); do \
printf "$(col_DONE)-"; \
done; \
printf "$(col_NORMAL)\n"
@echo ""
ifeq ($(ISO), y)
$(call ok, // The ISO is now ready. You can find it in '$(bin_dir_iso)' // )
@echo "$(col_FALSE) // 'make run' & 'make runs' do not have support for ISO image in '$(bin_dir_iso)' by default // $(col_NORMAL)"
else
$(call ok, // The temporary ISO image is now ready. You can find it as '$(bin_dir)/boot.iso' // )
$(call ok, // Use 'make run' if you want to run this ISO image now // )
$(call ok, // Use 'make runs' if you want to automatically open the ISO in $(util_vm) // )
@echo ""
$(call ok, // Run 'make' with parameter 'ISO=y' if you want to create a persistant ISO image in '$(bin_dir_iso)' // )
@echo "$(col_FALSE) // 'make run' & 'make runs' do not have support for ISO image in '$(bin_dir_iso)' by default // $(col_NORMAL)"
endif
@echo ""
# --- Main compiling procedure --- #
.PHONY: main
main:
# -- Update no. of times Make has been run -- #
$(Q)"$(src_dir_scripts)/base_count_increment.sh" "latest_next" "$(src_dir_conf)/mcount.txt"
# -- Compare MRain version -- #
$(call stat, Comparing MarkedRain version)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "ver_previous_mrain-sys" "$(src_dir_conf)/variables.txt"), $(MRAIN_VERSION))
$(call heading, info, Version change detected)
$(eval bool_ver_change := y)
endif
# -- Clean -- #
$(Q)$(call cleancode)
# -- Directories -- #
$(call heading, info, $(col_OK)Setting up temporary directories)
$(Q)mkdir -p "/dev/shm/mrain-bin"
$(Q)ln -s /dev/shm/mrain-bin "$(bin_dir)"
$(Q)mkdir -p "$(bin_dir_tmp)" "$(bin_dir_tmp_squashfs)" "$$HOME"
$(call heading, main, Creating system directories)
$(Q)mkdir -p "$(bin_dir_tmp)/boot" "$(bin_dir_tmp)/boot/grub"
$(Q)"$(src_dir_scripts)/mk_sys_dir.sh" "$(src_dir_conf)/dir.txt" "$(bin_dir_tmp_squashfs)"
# -- Buildroot -- #
@echo ""
$(call heading, main, Adding buildroot into the image)
# Delete .recombr if it exists
ifeq ($(shell [ -f ".recombr" ] && echo y), y)
$(Q)rm .recombr
endif
# Can't put BR make here and do the ifneq conditions because bash 'if' conditions
# doesn't seem to work with makefile, and can't make those ifeq and ifneq conditions into bash
# because it cannot do $(eval). Can't make these bash commands into Makefile because
# the 'ifeq' conditions doesn't seem to recognise changes to variables
$(Q)if [ "$(bool_ver_change)" = "y" ] && [ "$(strip $(val_remake_br_pack))" != "" ]; then \
val_temp="$(val_remake_br_pack)"; \
for val_temp2 in $$val_temp; do \
if [ "$$repeat" = "y" ]; then \
$(subst @echo, echo, $(call heading, sub, Re-making package: $$val_temp2)); \
fakeroot $(MAKE) -C "$(src_dir_buildroot)" "$${val_temp2}-reconfigure"; \
continue; \
fi; \
echo -n "$(col_FALSE)[Version changed] $(col_INFO)Do you want to re-compile package '$$val_temp2'? $(col_NORMAL)[$(col_DONE)[Y]es$(col_NORMAL)/$(col_FALSE)[N]o$(col_NORMAL)/$(col_INFO)[A]ll$(col_NORMAL)]"; \
while true; do \
read choice; \
if [ "$$choice" = "N" ] || [ "$$choice" = "n" ]; then \
break; \
elif [ "$$choice" = "Y" ] || [ "$$choice" = "y" ] || [ "$$choice" = "A" ] || [ "$$choice" = "a" ] ; then \
$(subst @echo, echo, $(call heading, sub, Re-making package: $$val_temp2)); \
fakeroot $(MAKE) -C "$(src_dir_buildroot)" "$${val_temp2}-reconfigure"; \
echo > .recombr; \
if [ "$$choice" = "A" ] || [ "$$choice" = "a" ] ; then \
repeat=y; \
fi; \
break; \
fi; \
echo -n "$(col_FALSE)Invalid option. Available options: $(col_NORMAL)[$(col_DONE)[Y]es$(col_NORMAL)/$(col_FALSE)[N]o$(col_NORMAL)/$(col_INFO)[A]ll$(col_NORMAL)]"; \
done; \
done; \
fi
ifneq ($(shell [ -f "$(src_dir_buildroot)/output/images/rootfs.tar" ] && echo y), y)
$(call heading, sub, rootfs.tar does not exist; making Buildroot)
$(Q)fakeroot $(MAKE) -C "$(src_dir_buildroot)" $(OUT) || exit 1
$(call save_hash, hash_buildroot, $(src_dir_buildroot)/.config)
$(eval bool_do_update_count := y)
$(eval val_changes += rootfs.tar did not exist\n)
else
$(call heading, sub, Comparing .config hash)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_buildroot" "$(src_dir_conf)/hashes.txt"),$(shell $(call get_hash, $(src_dir_buildroot)/.config)))
$(call heading, sub, Hashes didn't match; making Buildroot)
$(Q)fakeroot $(MAKE) -C "$(src_dir_buildroot)" $(OUT) || exit 1
$(call save_hash, hash_buildroot, $(src_dir_buildroot)/.config)
$(eval bool_do_update_count := y)
$(eval val_changes += Buildroot's configuration (.config) changed\n)
else
# Invoke BR make if version changed & all other checks are clear
ifeq ($(shell [ -f ".recombr" ] && echo y), y)
$(call heading, sub, Making Buildroot (version changed))
$(Q)fakeroot $(MAKE) -C "$(src_dir_buildroot)" $(OUT) || exit 1
endif
endif
endif
ifeq ($(shell [ -f ".recombr" ] && echo y), y)
$(Q)rm .recombr
endif
$(call heading, sub, Extracting rootfs archive to '$(bin_dir_tmp_squashfs)')
$(Q)pv -i 0.01 "$(src_dir_buildroot)/output/images/rootfs.tar" | tar -xf "-" -C "$(bin_dir_tmp_squashfs)"
# -- Applications -- #
@echo ""
$(call applications)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_apps" "$(src_dir_conf)/hashes.txt"), $(shell $(call get_hash_dir, $(src_dir_apps))))
$(call save_hash_dir, hash_apps, $(src_dir_apps))
$(eval bool_do_update_count := y)
$(eval val_changes += Applications directory changed\n)
endif
# -- Kernel -- #
@echo ""
$(call heading, main, Adding the linux kernel)
ifneq ($(shell [ -f "$(src_dir_linux)" ] && echo y), y)
$(call stop, Kernel file doesn't exist in $(src_dir_linux). Ensure you gave the correct path to it by running menuconfig.)
else
$(call heading, sub, Comparing kernel hash)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_kernel" "$(src_dir_conf)/hashes.txt"), $(shell $(call get_hash, $(src_dir_linux))))
$(call save_hash, hash_kernel, $(src_dir_linux))
$(eval bool_do_update_count := y)
$(eval val_changes += Kernel file changed\n)
endif
endif
$(call heading, sub, Copying kernel as '$(bin_dir_tmp)$(sys_dir_linux)')
$(Q)cp "$(src_dir_linux)" "$(bin_dir_tmp)$(sys_dir_linux)" $(OUT)
$(call heading, sub, Copying kernel as '$(bin_dir_tmp_squashfs)$(sys_dir_linux)')
$(Q)cp "$(src_dir_linux)" "$(bin_dir_tmp_squashfs)$(sys_dir_linux)" $(OUT)
# -- Modules -- #
@echo ""
$(call heading, main, Adding modules)
$(Q)cp -r "$(src_dir_modules)" "$(bin_dir_tmp_squashfs)/usr/lib"
# -- Text files -- #
@echo ""
$(call heading, main, Generating/Appending to text files in the SquashFS/System image)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_mktext_root" "$(src_dir_conf)/hashes.txt"), $(shell $(call get_hash_dir, $(src_dir_mktext)/root)))
$(call save_hash_dir, hash_mktext_root, $(src_dir_mktext)/root)
$(eval bool_do_update_count := y)
$(eval val_changes += Text files created in [SquashFS/System archive] changed\n)
endif
$(Q)"$(src_dir_scripts)/make_text_files.sh" "$(src_dir_mktext)/root" "$(bin_dir_tmp_squashfs)"
@echo ""
$(call heading, main, Generating/Appending to text files in the InitRamFS archive)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_mktext_init" "$(src_dir_conf)/hashes.txt"), $(shell $(call get_hash_dir, $(src_dir_mktext)/init)))
$(call save_hash_dir, hash_mktext_init, $(src_dir_mktext)/init)
$(eval bool_do_update_count := y)
$(eval val_changes += Text files created in [init archive] changed\n)
endif
$(Q)export sys_dir_squashfs="$(sys_dir_squashfs)"; \
"$(src_dir_scripts)/make_text_files.sh" "$(src_dir_mktext)/init" "$(src_dir_initramfs)/source"
@echo ""
$(call heading, main, Generating/Appending to text files in the final image)
ifneq ($(shell "$(src_dir_scripts)/get_var.sh" "hash_mktext_image" "$(src_dir_conf)/hashes.txt"), $(shell $(call get_hash_dir, $(src_dir_mktext)/image)))
$(call save_hash_dir, hash_mktext_image, $(src_dir_mktext)/image)
$(eval bool_do_update_count := y)
$(eval val_changes += Text files created in [Root of image] changed\n)
endif
$(Q)export boot_resolution="$(val_grub-boot_resolution)"; \
export boot_default="$(val_grub-boot_default)"; \
export boot_timeout="$(val_grub-boot_timeout)"; \
export entry_name="$(val_grub-entry-one_name)"; \
export sys_dir_linux="$(sys_dir_linux)"; \
export linux_params=$(val_grub-entry-one_li_params); \
export sys_dir_initramfs="$(sys_dir_initramfs)"; \
"$(src_dir_scripts)/make_text_files.sh" "$(src_dir_mktext)/image" "$(bin_dir_tmp)"
# -- Initramfs -- #
@echo ""
$(call heading, main, Adding initramfs)
$(call heading, sub, Making 'init' executable)
$(Q)chmod +x "$(src_dir_initramfs)/source/init"
$(call heading, sub, Creating init archive)
$(Q)cd "$(src_dir_initramfs)/source"; \
find . | cpio -o -H newc --quiet | pv -i 0.01 -s $$(du -sb . | awk '{print $$1}') | zstd --force --quiet -o "$(val_mkfile_dir)/$(bin_dir_tmp)$(sys_dir_initramfs)"
# -- Finalization -- #
@echo ""
$(call heading, main, Doing finalization procedures)
# - Branding - #
$(call heading, sub, Adding branding image)
$(Q)cp "$(src_dir_media)/branding/flat.png" "$(bin_dir_tmp_squashfs)/usr/share/branding/mrain-flat.png"
# - Convenient aliases & Functions - #
ifeq ($(bool_include_aliases), y)
$(call heading, sub, Convenient aliases & functions)
$(Q)"$(src_dir_mktext)/aliases.sh" >> "$(bin_dir_tmp_squashfs)/etc/profile"
endif
# - Drive letters - #
ifeq ($(bool_use_drive_letters), y)
$(call heading, sub, Drive letters)
$(Q)export val_disks_path="$(val_disks_path)"; \
"$(src_dir_mktext)/drive_letters.sh" >> "$(bin_dir_tmp_squashfs)/etc/zprofile"
endif
# - OpenRC Getty tty2,3,4,5,6 symlinks - #
ifeq ($(bool_tty_expansion), y)
$(call heading, sub, OpenRC getty on tty2 - tty6 symlinks)
$(Q)cd "$(bin_dir_tmp_squashfs)"; \
ln -s "/etc/init.d/agetty" "etc/init.d/agetty.tty2"; \
ln -s "/etc/init.d/agetty" "etc/init.d/agetty.tty3"; \
ln -s "/etc/init.d/agetty" "etc/init.d/agetty.tty4"; \
ln -s "/etc/init.d/agetty" "etc/init.d/agetty.tty5"; \
ln -s "/etc/init.d/agetty" "etc/init.d/agetty.tty6"; \
ln -s "/etc/init.d/agetty.tty2" "etc/runlevels/default/agetty.tty2"; \
ln -s "/etc/init.d/agetty.tty3" "etc/runlevels/default/agetty.tty3"; \
ln -s "/etc/init.d/agetty.tty4" "etc/runlevels/default/agetty.tty4"; \
ln -s "/etc/init.d/agetty.tty5" "etc/runlevels/default/agetty.tty5"; \
ln -s "/etc/init.d/agetty.tty6" "etc/runlevels/default/agetty.tty6"
endif
# - Watchdog daemon startup script fix - #
$(call heading, sub, Watchdog daemon script (if exist) timer delay)
$(Q)file="bin/squashfs/etc/init.d/S15watchdog"; \
if [ -f "$$file" ]; then \
delay_time=$$(awk '/^[[:space:]]*watchdog/ && /-t [0-9]+/ { for (i=1; i<=NF; i++) if ($$i == "-t") print $$(i+1) }' "$$file"); \
if [ -n "$$delay_time" ]; then \
sed -i 's/ -t [0-9]*//g' "$$file"; \
fi; \
fi
# --- Final touches --- #
.PHONY: final-touch
final-touch:
# -- Version updation -- #
# Makefile's 'ifeq' conditions ain't working here for some reason
$(Q)if [ "$(bool_ver_change)" = "y" ]; then \
$(subst @echo, echo, $(call heading, info, Saving version: $(MRAIN_VERSION))); \
"$(src_dir_scripts)/set_var.sh" "ver_previous_mrain-sys" "$(MRAIN_VERSION)" "$(src_dir_conf)/variables.txt"; \
fi
$(Q)if [ "$(bool_do_update_count)" = "y" ] || [ "$(update)" = "true" ]; then \
echo "$(col_TRUE)Summary of items changed"; \
echo "$(col_TRUE)--------------------------$(col_NORMAL)"; \
echo -n " $(val_changes)"; \
if [ "$(update)" = "true" ]; then \
echo "$(col_INFO)Variable 'update' is set to true."; \
fi; \
"$(src_dir_scripts)/count_increment.sh" "latest_next" "$(src_dir_conf)/bcount.txt"; \
fi
# -- Automatic cleaning -- #
ifeq ($(bool_clean_dir_tmp), y)
$(call true, Wipe temporary directory, bool_clean_dir_tmp)
$(call heading, info, $(col_FALSE)Cleaning temporary files)
$(Q)rm -rf "$(bin_dir_tmp)"/* "$(bin_dir_tmp_squashfs)"
else
$(call false, Wipe temporary directory, bool_clean_dir_tmp)
endif
# --- ISO Image --- #
.PHONY: iso prepare-iso
iso: all
prepare-iso:
# -- Create SquashFS image -- #
@echo ""
$(call heading, main, Generating compressed SquashFS image)
$(Q)mksquashfs "$(bin_dir_tmp_squashfs)" "$(bin_dir_tmp)$(sys_dir_squashfs)" -noappend -quiet -comp zstd
# -- Generate disc image using GRUB -- #
@echo ""
$(call heading, main, Creating new disc image with GRUB)
ifeq ($(ISO), y)
$(Q)grub-mkrescue -o "$(bin_dir_iso)" "$(bin_dir_tmp)" $(OUT)
else
$(Q)grub-mkrescue -o "$(bin_dir)/boot.iso" "$(bin_dir_tmp)" $(OUT)
endif
# --- Disk Image --- #
.PHONY: image prepare-image
image: main prepare-image final-touch
@echo ""
@width=$$(tput cols); \
for i in $$(seq 1 $$width); do \
printf "$(col_DONE)-"; \
done; \
printf "$(col_NORMAL)\n"
@echo ""
ifeq ($(ISO), y)
$(call ok, // The Disk Image is now ready. You can find it in '$(bin_dir_iso)' // )
@echo "$(col_FALSE) // 'make run' & 'make runs' do not have support for ISO image in '$(bin_dir_iso)' by default // $(col_NORMAL)"
else
$(call ok, // The temporary Disk Image image is now ready. You can find it as '$(bin_dir)/boot.iso' // )
$(call ok, // Use 'make ir' or 'make imagerun' if you want to run this ISO image now // )
$(call ok, // Use 'make irs' or 'make imageruns' if you want to automatically open the Disk Image in $(util_vm) // )
@echo ""
$(call ok, // Run 'make' with parameter 'ISO=y' if you want to create a persistant ISO image in '$(bin_dir_iso)' // )
@echo "$(col_FALSE) // 'make run' & 'make runs' do not have support for ISO image in '$(bin_dir_iso)' by default // $(col_NORMAL)"
endif
@echo ""
prepare-image:
# -- Move root files into image dir -- #
$(call heading, main, Moving contents of $(bin_dir_tmp) into $(bin_dir_tmp_squashfs))
$(Q)rsync -a --ignore-existing "$(bin_dir_tmp)/" "$(bin_dir_tmp_squashfs)/"
# -- Make writable image -- #
$(call heading, main, Creating the writable & portable image)
$(call heading, sub, Creating an empty file ($(val_portable_size)MB))
$(Q)truncate -s $$(expr $$(sudo du -sB1 "$(bin_dir_tmp_squashfs)" | awk '{print int($$1 / 1024 / 1024)}') + 1 + $(val_portable_size_extension))M "$(bin_dir)/boot.iso" $(OUT)
$(call heading, imp, Please provide password to perform operations on this image)
$(Q)sudo losetup -fP --show "$(bin_dir)/boot.iso" > "$(bin_dir)/.tmp"
$(call heading, sub, Creating partition table)
$(Q)echo ',,83,*' | sudo sfdisk "$$(cat $(bin_dir)/.tmp)"
$(Q)sudo partprobe "$$(cat $(bin_dir)/.tmp)"
$(call heading, sub, Formatting using the ext4 filesystem)
$(Q)sudo mkfs.ext4 "$$(cat $(bin_dir)/.tmp)p1" | grep -oP 'UUID: \K[\w-]+' > "$(bin_dir)/.uuid"
$(call heading, sub, Mounting partition)
$(Q)sudo mount "$$(cat $(bin_dir)/.tmp)p1" "$(bin_dir_tmp)"
$(call heading, sub, Installing GRUB Bootloader)
$(Q)sudo grub-install --target=i386-pc --boot-directory="$(bin_dir_tmp)/boot" "$$(cat $(bin_dir)/.tmp)"
$(call heading, sub2, Updating root= value)
$(Q)sed -i "s/root=auto_cd/root=UUID=$$(cat '$(bin_dir)/.uuid')/g" "$(bin_dir_tmp_squashfs)/boot/grub/grub.cfg"
$(call heading, sub, Copying system into the image)
$(Q)sudo cp -r "$(bin_dir_tmp_squashfs)/"* "$(bin_dir_tmp)"
$(call heading, sub, Unmounting image)
$(Q)sudo umount "$(bin_dir_tmp)"
$(call heading, sub, Deleting loopback device and temporary files)
$(Q)sudo losetup -d "$$(cat $(bin_dir)/.tmp)"
$(Q)rm "$(bin_dir)/.tmp" "$(bin_dir)/.uuid"
ifeq ($(ISO), y)
$(call heading, main, Creating persistant image)
$(Q)cp "$(bin_dir)/boot.iso" "$(bin_dir_iso)"
endif
# --- Run --- #
.PHONY: run runs imagerun imageruns ir irs
# -- ISO9660 -- #
run:
ifeq ($(bool_use_qemu_kvm), y)
$(eval util_vm_params += -enable-kvm -cpu host)
endif
$(eval util_vm_params += -cdrom $(bin_dir)/boot.iso)
$(Q)if [ -f "$(bin_dir)/boot.iso" ]; then \
echo ""; \
$(subst @echo, echo, $(call ok, // Now running CD image "$(bin_dir)/boot.iso" using "$(util_vm)" and parameters "$(util_vm_params)" // )); \
"$(util_vm)" $(shell echo -n $(util_vm_params)); \
else \
$(subst @echo, echo, $(call stop, Supplied file "$(bin_dir)/boot.iso" doesn't exist. Make sure you ran "make"; and try again.)); \
fi
@echo ""
runs: main prepare-iso final-touch run
# -- ext4 img -- #
imagerun:
ifeq ($(bool_use_qemu_kvm), y)
$(eval util_vm_params += -enable-kvm -cpu host)
endif
$(eval util_vm_params += -hda $(bin_dir)/boot.iso)
$(Q)if [ -f "$(bin_dir)/boot.iso" ]; then \
echo ""; \
$(subst @echo, echo, $(call ok, // Now running hdd image "$(bin_dir)/boot.iso" using "$(util_vm)" and parameters "$(util_vm_params)" // )); \
"$(util_vm)" $(shell echo -n $(util_vm_params)); \
else \
$(subst @echo, echo, $(call stop, Supplied file "$(bin_dir)/boot.iso" doesn't exist. Make sure you ran "make"; and try again.)); \
fi
@echo ""
ir: imagerun
imageruns: main prepare-image final-touch imagerun
irs: imageruns
# --- Clean --- #
.PHONY: clean
clean:
@$(call cleancode)
define cleancode
if mountpoint -q "$(bin_dir_tmp)"; then \
$(subst @echo, echo, $(call heading, info, $(col_FALSE)Unmounting image)); \
sudo umount "$(bin_dir_tmp)"; \
fi; \
$(subst @echo, echo, $(call heading, info, $(col_FALSE)Deleting directories and image)); \
rm -rf "$(bin_dir_tmp)" "$(bin_dir)/boot.iso" "$(bin_dir)"/* "$(bin_dir)"
endef
# --- Clean all stuff --- #
.PHONY: cleanall
cleanall:
@echo ""
$(call warn, Doing 'cleanall' will run clean on ALL of the source files and may take longer to compile.)
@echo ""
@echo "$(col_INFO) Press "Y" and enter to continue, any other key will terminate.$(col_NORMAL)"
$(Q)read choice; \
if [ "$$choice" = "Y" ] || [ "$$choice" = "y" ]; then \
$(call cleancode); \
$(subst @echo, echo, $(call heading, info, $(col_FALSE)Cleaning buildroot)); \
$(MAKE) -C "$(src_dir_buildroot)" clean $(OUT) || exit 1; \
echo ""; \
$(subst @echo, echo, $(call ok, Done. Run 'make' to re-compile. Be prepared to wait a long time. )); \
echo ""; \
else \
$(subst @echo, echo, $(call ok, Cancelled. )); \
echo ""; \
fi
# --- Help --- #
.PHONY: help
help:
help:
@echo "Build targets for ISO9660 iso:"
@echo " (default) - Creates a temporary ISO image that loads into memory. All changes are lost after poweroff."
@echo " run - Runs the pre-made ISO image from the <bin> directory."
@echo " runs - Auto-runs the iso image after building."
@echo ""
@echo "Build targets for ext4 iso:"
@echo " image - Creates a temporary disk image that saves user changes."
@echo " ir/imagerun - Runs the pre-made disk image from the <bin> directory."
@echo " irs/imageruns - Auto-runs the disk image after building."
@echo ""
@echo "Variables:"
@echo " ISO=[y] - Creates a persistant ISO image in <bin_dir_iso>."
@echo ""
@echo "Configuration targets:"
@echo " menuconfig - Opens the configuration menu."
@echo ""
@echo "Cleaning targets:"
@echo " clean - Clears the temporary directory and removes the ISO image."
@echo " cleanall - Clears all generated output, similar to Buildroot cleanup."
@echo ""
@echo "Others:"
@echo " help - Displays this help text"