aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 65c81df3385a8211145a89706c4592a112f1be8a (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
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
.PHONY: all clean run docker-build docker-run native-build build-grub

# ═══════════════════════════════════════════════════════════════════════════
# Platform Detection
# ═══════════════════════════════════════════════════════════════════════════

ifdef DOCKER_BUILD
    BUILD_MODE = native
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        IS_ARCH := $(shell test -f /etc/arch-release && echo yes || echo no)
        ifeq ($(IS_ARCH),yes)
            BUILD_MODE = native
        else
            BUILD_MODE = docker
        endif
    else
        BUILD_MODE = docker
    endif
endif

# ═══════════════════════════════════════════════════════════════════════════
# Configuration
# ═══════════════════════════════════════════════════════════════════════════

GRUB_VERSION = 2.12
GRUB_URL = https://ftp.gnu.org/gnu/grub/grub-$(GRUB_VERSION).tar.xz
GRUB_SRC = toolchain/grub-afs/grub-$(GRUB_VERSION)
GRUB_BUILD = toolchain/grub-afs/build

GRUB_CONFIG = boot/grub/grub.cfg
GRUB_THEME_DIR = boot/grub/themes
DISK_IMAGE = iso/akiba.img
FS_ROOT = iso/akiba
BUILD_DIR = iso/build
SYSTEM_DIRS = resources/fonts resources/test
BINARIES_DIR = binaries

CONTENT_SIZE_KB := $(shell du -sk $(FS_ROOT) 2>/dev/null | awk '{sum += $$1} END {print sum + 10000}')
DISK_SIZE_MB := $(shell echo "$$(($(CONTENT_SIZE_KB) / 1024 + 50))")1

# ═══════════════════════════════════════════════════════════════════════════
# Docker Build Mode
# ═══════════════════════════════════════════════════════════════════════════

ifeq ($(BUILD_MODE),docker)

all: docker-build

docker-build:
	@echo "═══════════════════════════════════════════════════════════"
	@echo " Building Akiba OS (Docker mode)"
	@echo "═══════════════════════════════════════════════════════════"
	@echo "Setting Docker time to: $(shell date -u)"
	@docker build -t akiba-builder ./toolchain
	@docker run --rm -v $(PWD):/akiba -e BUILD_DATE="$(shell date -u)" -e DOCKER_BUILD=1 akiba-builder sh -c 'echo "Docker received date: $$BUILD_DATE" && date -s "$$BUILD_DATE" >/dev/null 2>&1 || true && echo "Docker current time: $$(date -u)" && make all'

docker-run:
	@./scripts/run.sh

clean:
	@docker run --rm -v $(PWD):/akiba -e DOCKER_BUILD=1 akiba-builder make clean 2>/dev/null || rm -rf iso/ zig-out/ zig-cache/ .zig-cache/

clean-grub:
	@docker run --rm -v $(PWD):/akiba -e DOCKER_BUILD=1 akiba-builder make clean-grub 2>/dev/null || rm -rf toolchain/grub-afs/grub-* toolchain/grub-afs/build

clean-all: clean clean-grub

run: all docker-run

# ═══════════════════════════════════════════════════════════════════════════
# Native Build Mode
# ═══════════════════════════════════════════════════════════════════════════

else

all: native-build

native-build: build-grub $(DISK_IMAGE)

clean:
	@rm -rf iso/ zig-out/ zig-cache/ .zig-cache/

clean-grub:
	@rm -rf toolchain/grub-afs/grub-* toolchain/grub-afs/build

clean-all: clean clean-grub

run: all
	@./scripts/run.sh

endif

# ═══════════════════════════════════════════════════════════════════════════
# GRUB with AFS Built-in
# ═══════════════════════════════════════════════════════════════════════════

build-grub: $(GRUB_BUILD)/bin/grub-mkstandalone

$(GRUB_SRC)/grub-core/fs/afs.c: toolchain/grub-afs/afs.c
	@echo "→ Downloading GRUB source..."
	@mkdir -p toolchain/grub-afs
	@cd toolchain/grub-afs && curl -L $(GRUB_URL) -o grub.tar.xz
	@cd toolchain/grub-afs && tar -xf grub.tar.xz
	@rm -f toolchain/grub-afs/grub.tar.xz
	@echo "→ Integrating AFS into GRUB source..."
	@cp toolchain/grub-afs/afs.c $(GRUB_SRC)/grub-core/fs/
	@echo "" >> $(GRUB_SRC)/grub-core/Makefile.core.def
	@echo "module = {" >> $(GRUB_SRC)/grub-core/Makefile.core.def
	@echo "  name = afs;" >> $(GRUB_SRC)/grub-core/Makefile.core.def
	@echo "  common = fs/afs.c;" >> $(GRUB_SRC)/grub-core/Makefile.core.def
	@echo "};" >> $(GRUB_SRC)/grub-core/Makefile.core.def
	@echo "→ Creating missing build files..."
	@touch $(GRUB_SRC)/grub-core/extra_deps.lst
	@sleep 2
	@touch $(GRUB_SRC)/grub-core/Makefile.core.am
	@touch $(GRUB_SRC)/grub-core/Makefile.in
	@touch $(GRUB_SRC)/configure
	@touch $(GRUB_SRC)/grub-core/fs/afs.c

$(GRUB_BUILD)/bin/grub-mkstandalone: $(GRUB_SRC)/grub-core/fs/afs.c
	@echo "→ Building GRUB with AFS support (this takes a few minutes)..."
	@mkdir -p $(GRUB_BUILD)
	cd $(GRUB_BUILD) && ../grub-$(GRUB_VERSION)/configure \
		--prefix=$$(pwd) \
		--target=x86_64 \
		--with-platform=efi \
		--disable-werror
	cd $(GRUB_BUILD) && $(MAKE) -j$$(nproc)
	cd $(GRUB_BUILD) && $(MAKE) install
	@echo "  ✓ GRUB with AFS built successfully"

# ═══════════════════════════════════════════════════════════════════════════
# Filesystem Preparation
# ═══════════════════════════════════════════════════════════════════════════

prepare-filesystem: build-grub
	@echo "→ Preparing filesystem structure..."
	@mkdir -p $(FS_ROOT)/boot/grub
	@mkdir -p $(FS_ROOT)/system/akiba
	@mkdir -p $(FS_ROOT)/system/libraries
	@mkdir -p $(FS_ROOT)/binaries
	@mkdir -p $(FS_ROOT)/EFI/BOOT
	@mkdir -p $(BUILD_DIR)
	
	@echo "→ Building kernel..."
	@zig build --cache-dir $(BUILD_DIR)/cache --prefix $(BUILD_DIR)
	@mv $(BUILD_DIR)/bin/mirai.akibakernel $(FS_ROOT)/system/akiba/
	
	@echo "→ Building libraries..."
	@mkdir -p $(BUILD_DIR)/lib $(FS_ROOT)/system/libraries
	@for libdir in system/libraries/*/; do \
		if [ -d "$$libdir" ]; then \
			libname=$$(basename $$libdir); \
			if [ -f "$$libdir$$libname.zig" ]; then \
				zig build-obj \
					-target x86_64-freestanding-none \
					-O ReleaseSmall \
					"$$libdir$$libname.zig" \
					-femit-bin="$(BUILD_DIR)/lib/$$libname.o" 2>/dev/null && \
				ar rcs "$(BUILD_DIR)/lib/lib$$libname.a" "$(BUILD_DIR)/lib/$$libname.o" && \
				cp "$(BUILD_DIR)/lib/lib$$libname.a" "$(FS_ROOT)/system/libraries/$$libname.arx" && \
				echo "  ✓ $$libname.arx"; \
			fi; \
		fi; \
	done
	
	@echo "→ Building akibacompile tool..."
	@cd toolchain/akibacompile && zig build --prefix ../../$(BUILD_DIR)
	
	@echo "→ Building akibabuilder tool..."
	@cd toolchain/akibabuilder && zig build --prefix ../../$(BUILD_DIR)
	
	@echo "→ Building system binaries..."
	@mkdir -p $(BUILD_DIR)/system
	@for sysdir in system/*/; do \
		if [ -d "$$sysdir" ]; then \
			sysname=$$(basename $$sysdir); \
			if [ "$$sysname" = "libraries" ]; then continue; fi; \
			if [ -f "$$sysdir$$sysname.zig" ]; then \
				echo "  Compiling $$sysname..."; \
				$(BUILD_DIR)/bin/akibacompile "$$sysdir" "$(BUILD_DIR)/system/$$sysname" "system/libraries" && \
				if [ "$$sysname" = "pulse" ]; then \
					echo "  Creating pulse.akibainit..."; \
					$(BUILD_DIR)/bin/akibabuilder "$(BUILD_DIR)/system/$$sysname" "$(FS_ROOT)/system/akiba/pulse.akibainit" init && \
					echo "  ✓ pulse.akibainit"; \
				else \
					echo "  Wrapping $$sysname.akiba..."; \
					mkdir -p "$(FS_ROOT)/system/$$sysname" && \
					$(BUILD_DIR)/bin/akibabuilder "$(BUILD_DIR)/system/$$sysname" "$(FS_ROOT)/system/$$sysname/$$sysname.akiba" cli && \
					echo "  ✓ system/$$sysname/$$sysname.akiba"; \
				fi; \
			fi; \
		fi; \
	done
	
	@echo "→ Compiling binaries..."
	@mkdir -p $(BUILD_DIR)/binaries
	@for bindir in binaries/*/; do \
		if [ -d "$$bindir" ]; then \
			binname=$$(basename $$bindir); \
			if [ -f "$$bindir$$binname.zig" ]; then \
				echo "  Compiling $$binname..."; \
				$(BUILD_DIR)/bin/akibacompile "$$bindir" "$(BUILD_DIR)/binaries/$$binname" "system/libraries" && \
				echo "  Wrapping $$binname.akiba..." && \
				$(BUILD_DIR)/bin/akibabuilder "$(BUILD_DIR)/binaries/$$binname" "$(FS_ROOT)/binaries/$$binname.akiba" cli && \
				echo "  ✓ $$binname.akiba"; \
			fi; \
		fi; \
	done
	
	@echo "→ Copying system resources..."
	@for dir in $(SYSTEM_DIRS); do \
		if [ -d $$dir ]; then \
			target_name=$$(basename $$dir); \
			mkdir -p $(FS_ROOT)/system/$$target_name; \
			cp -R $$dir/* $(FS_ROOT)/system/$$target_name/; \
		fi; \
	done
	
	@cp $(GRUB_CONFIG) $(FS_ROOT)/boot/grub/
	@cp -R $(GRUB_THEME_DIR) $(FS_ROOT)/boot/grub/
	
	@echo "→ Creating GRUB bootloader with AFS support..."
	@$(GRUB_BUILD)/bin/grub-mkstandalone \
		--format=x86_64-efi \
		--output=$(FS_ROOT)/EFI/BOOT/BOOTX64.EFI \
		--directory=$(GRUB_BUILD)/lib/grub/x86_64-efi \
		--locales="" \
		--fonts="unicode" \
		--modules="part_gpt part_msdos fat afs iso9660 multiboot2 normal all_video gfxterm png gfxmenu" \
		"boot/grub/grub.cfg=$(GRUB_CONFIG)"

	@echo "→ Copying AFS module to ESP structure..."
	@mkdir -p $(FS_ROOT)/boot/grub/modules
	@cp $(GRUB_BUILD)/lib/grub/x86_64-efi/afs.mod $(FS_ROOT)/boot/grub/modules/
	
	@echo "✓ Build completed successfully"
	@echo ""
	@echo "Built artifacts:"
	@if [ -d "$(FS_ROOT)/system/libraries" ]; then \
		echo "  Libraries (.arx):"; \
		ls -lh $(FS_ROOT)/system/libraries/*.arx 2>/dev/null || echo "    (none)"; \
	fi
	@if [ -d "$(FS_ROOT)/binaries" ]; then \
		echo "  Binaries (.akiba):"; \
		ls -lh $(FS_ROOT)/binaries/*.akiba 2>/dev/null || echo "    (none)"; \
	fi
	
# ═══════════════════════════════════════════════════════════════════════════
# Disk Image Creation
# ═══════════════════════════════════════════════════════════════════════════

$(DISK_IMAGE): prepare-filesystem
	@echo "→ Building mkafsdisk tool..."
	@cd toolchain/mkafsdisk && zig build --prefix ../../iso/build
	
	@echo "→ Creating bootable disk image..."
	@iso/build/bin/mkafsdisk $(FS_ROOT) $(DISK_IMAGE) $(DISK_SIZE_MB)
	
	@echo "✓ Akiba OS disk image ready: $(DISK_IMAGE)"