Skip to content

Commit c672298

Browse files
committed
Add Makefile and build script for custom Linux distro
1 parent 1b62554 commit c672298

2 files changed

Lines changed: 577 additions & 0 deletions

File tree

Makefile

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# =============================================================================
2+
# Makefile for Custom Linux Distribution
3+
# =============================================================================
4+
5+
.PHONY: all build clean test run extract help docker-build docker-run \
6+
docker-clean iso kernel initramfs boot-img qemu qemu-nographic
7+
8+
# Configuration
9+
IMAGE_NAME := handbuilt-linux
10+
DOCKER_TAG := latest
11+
ISO_OUTPUT := output.iso
12+
BOOT_IMG := boot.img
13+
QEMU_MEMORY := 512M
14+
15+
# Colors for output
16+
COLOR_RESET := \033[0m
17+
COLOR_BOLD := \033[1m
18+
COLOR_GREEN := \033[32m
19+
COLOR_BLUE := \033[34m
20+
COLOR_YELLOW := \033[33m
21+
22+
# Default target
23+
all: help
24+
25+
# -----------------------------------------------------------------------------
26+
# Help
27+
# -----------------------------------------------------------------------------
28+
help:
29+
@echo "$(COLOR_BOLD)handbuilt-linux Build System$(COLOR_RESET)"
30+
@echo ""
31+
@echo "$(COLOR_BOLD)Available targets:$(COLOR_RESET)"
32+
@echo " $(COLOR_GREEN)build$(COLOR_RESET) - Build Docker image"
33+
@echo " $(COLOR_GREEN)extract$(COLOR_RESET) - Extract build artifacts from Docker"
34+
@echo " $(COLOR_GREEN)iso$(COLOR_RESET) - Extract ISO image only"
35+
@echo " $(COLOR_GREEN)kernel$(COLOR_RESET) - Extract kernel image only"
36+
@echo " $(COLOR_GREEN)initramfs$(COLOR_RESET) - Extract initramfs only"
37+
@echo " $(COLOR_GREEN)boot-img$(COLOR_RESET) - Create bootable disk image"
38+
@echo " $(COLOR_GREEN)run$(COLOR_RESET) - Run Docker container interactively"
39+
@echo " $(COLOR_GREEN)test$(COLOR_RESET) - Run tests"
40+
@echo " $(COLOR_GREEN)qemu$(COLOR_RESET) - Run ISO in QEMU"
41+
@echo " $(COLOR_GREEN)qemu-nographic$(COLOR_RESET) - Run ISO in QEMU (no graphics)"
42+
@echo " $(COLOR_GREEN)clean$(COLOR_RESET) - Remove build artifacts"
43+
@echo " $(COLOR_GREEN)docker-clean$(COLOR_RESET) - Remove Docker images and containers"
44+
@echo " $(COLOR_GREEN)help$(COLOR_RESET) - Show this help message"
45+
@echo ""
46+
@echo "$(COLOR_BOLD)Examples:$(COLOR_RESET)"
47+
@echo " make build # Build the Docker image"
48+
@echo " make extract # Extract all artifacts"
49+
@echo " make qemu # Test with QEMU"
50+
@echo " make clean build # Clean and rebuild"
51+
@echo ""
52+
53+
# -----------------------------------------------------------------------------
54+
# Docker Build
55+
# -----------------------------------------------------------------------------
56+
build: docker-build
57+
58+
docker-build:
59+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building Docker image...$(COLOR_RESET)"
60+
docker build -t $(IMAGE_NAME):$(DOCKER_TAG) .
61+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Docker image built successfully$(COLOR_RESET)"
62+
63+
# Build specific stages
64+
docker-build-kernel:
65+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building kernel stage...$(COLOR_RESET)"
66+
docker build --target kernel-builder -t $(IMAGE_NAME):kernel .
67+
68+
docker-build-busybox:
69+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building BusyBox stage...$(COLOR_RESET)"
70+
docker build --target busybox-builder -t $(IMAGE_NAME):busybox .
71+
72+
docker-build-iso:
73+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Building ISO stage...$(COLOR_RESET)"
74+
docker build --target iso-builder -t $(IMAGE_NAME):iso .
75+
76+
# -----------------------------------------------------------------------------
77+
# Extract Artifacts
78+
# -----------------------------------------------------------------------------
79+
extract: iso kernel initramfs
80+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ All artifacts extracted$(COLOR_RESET)"
81+
82+
iso: build
83+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Extracting ISO image...$(COLOR_RESET)"
84+
@docker run --rm $(IMAGE_NAME):$(DOCKER_TAG) cat /distro/output.iso > $(ISO_OUTPUT)
85+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ ISO extracted: $(ISO_OUTPUT)$(COLOR_RESET)"
86+
87+
kernel: build
88+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Extracting kernel...$(COLOR_RESET)"
89+
@docker run --rm $(IMAGE_NAME):$(DOCKER_TAG) cat /distro/bzImage > bzImage
90+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Kernel extracted: bzImage$(COLOR_RESET)"
91+
92+
initramfs: build
93+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Extracting initramfs...$(COLOR_RESET)"
94+
@docker run --rm $(IMAGE_NAME):$(DOCKER_TAG) cat /distro/initramfs > initramfs
95+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Initramfs extracted: initramfs$(COLOR_RESET)"
96+
97+
# -----------------------------------------------------------------------------
98+
# Boot Image
99+
# -----------------------------------------------------------------------------
100+
boot-img: extract
101+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Creating bootable disk image...$(COLOR_RESET)"
102+
@./build.sh
103+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Boot image created: $(BOOT_IMG)$(COLOR_RESET)"
104+
105+
# -----------------------------------------------------------------------------
106+
# Docker Run
107+
# -----------------------------------------------------------------------------
108+
run: docker-run
109+
110+
docker-run: build
111+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Running Docker container...$(COLOR_RESET)"
112+
docker run --rm -it $(IMAGE_NAME):$(DOCKER_TAG)
113+
114+
# Run with mounted volume
115+
docker-run-mount: build
116+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Running Docker container with mounted volume...$(COLOR_RESET)"
117+
docker run --rm -it -v $(PWD)/output:/output $(IMAGE_NAME):$(DOCKER_TAG)
118+
119+
# -----------------------------------------------------------------------------
120+
# Testing
121+
# -----------------------------------------------------------------------------
122+
test: build
123+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Running tests...$(COLOR_RESET)"
124+
@if [ -f scripts/test.sh ]; then \
125+
./scripts/test.sh; \
126+
else \
127+
echo "$(COLOR_YELLOW)No test script found$(COLOR_RESET)"; \
128+
fi
129+
130+
# Test with QEMU
131+
test-qemu: qemu-nographic
132+
133+
# -----------------------------------------------------------------------------
134+
# QEMU
135+
# -----------------------------------------------------------------------------
136+
qemu: iso
137+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting QEMU...$(COLOR_RESET)"
138+
@echo "$(COLOR_YELLOW)Press Ctrl+Alt+2 for QEMU console, Ctrl+Alt+1 to return$(COLOR_RESET)"
139+
qemu-system-x86_64 -cdrom $(ISO_OUTPUT) -m $(QEMU_MEMORY)
140+
141+
qemu-nographic: iso
142+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting QEMU (no graphics)...$(COLOR_RESET)"
143+
@echo "$(COLOR_YELLOW)Press Ctrl+A then X to exit$(COLOR_RESET)"
144+
qemu-system-x86_64 -cdrom $(ISO_OUTPUT) -m $(QEMU_MEMORY) -nographic
145+
146+
qemu-kernel: kernel initramfs
147+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting QEMU with kernel and initramfs...$(COLOR_RESET)"
148+
qemu-system-x86_64 \
149+
-kernel bzImage \
150+
-initrd initramfs \
151+
-append "console=ttyS0" \
152+
-m $(QEMU_MEMORY) \
153+
-nographic
154+
155+
qemu-boot-img: boot-img
156+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting QEMU with boot image...$(COLOR_RESET)"
157+
qemu-system-x86_64 $(BOOT_IMG) -m $(QEMU_MEMORY)
158+
159+
# QEMU with network
160+
qemu-network: iso
161+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting QEMU with network...$(COLOR_RESET)"
162+
qemu-system-x86_64 \
163+
-cdrom $(ISO_OUTPUT) \
164+
-m $(QEMU_MEMORY) \
165+
-netdev user,id=net0 \
166+
-device e1000,netdev=net0
167+
168+
# -----------------------------------------------------------------------------
169+
# Cleanup
170+
# -----------------------------------------------------------------------------
171+
clean:
172+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Cleaning build artifacts...$(COLOR_RESET)"
173+
@rm -f $(ISO_OUTPUT) $(BOOT_IMG) bzImage initramfs initramfs.cpio.gz
174+
@rm -rf mnt/ myiso/ initramfs/ build/ output/
175+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Cleaned$(COLOR_RESET)"
176+
177+
docker-clean:
178+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Cleaning Docker images...$(COLOR_RESET)"
179+
@docker rmi $(IMAGE_NAME):$(DOCKER_TAG) 2>/dev/null || true
180+
@docker rmi $(IMAGE_NAME):kernel 2>/dev/null || true
181+
@docker rmi $(IMAGE_NAME):busybox 2>/dev/null || true
182+
@docker rmi $(IMAGE_NAME):iso 2>/dev/null || true
183+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Docker images removed$(COLOR_RESET)"
184+
185+
clean-all: clean docker-clean
186+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ Full cleanup complete$(COLOR_RESET)"
187+
188+
# -----------------------------------------------------------------------------
189+
# Development
190+
# -----------------------------------------------------------------------------
191+
shell: docker-run
192+
193+
# Enter build container for debugging
194+
debug: build
195+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Starting debug shell...$(COLOR_RESET)"
196+
docker run --rm -it --entrypoint /bin/bash $(IMAGE_NAME):$(DOCKER_TAG)
197+
198+
# Show build logs
199+
logs:
200+
@docker logs $(shell docker ps -lq)
201+
202+
# Show Docker image size
203+
size: build
204+
@echo "$(COLOR_BOLD)Image sizes:$(COLOR_RESET)"
205+
@docker images $(IMAGE_NAME):$(DOCKER_TAG) --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
206+
207+
# -----------------------------------------------------------------------------
208+
# Configuration
209+
# -----------------------------------------------------------------------------
210+
menuconfig-kernel:
211+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Configuring kernel...$(COLOR_RESET)"
212+
docker run --rm -it -v $(PWD):/work $(IMAGE_NAME):$(DOCKER_TAG) bash -c \
213+
"cd /opt/mydistro/linux && make menuconfig && cp .config /work/linux.config"
214+
215+
menuconfig-busybox:
216+
@echo "$(COLOR_BOLD)$(COLOR_BLUE)Configuring BusyBox...$(COLOR_RESET)"
217+
docker run --rm -it -v $(PWD):/work $(IMAGE_NAME):$(DOCKER_TAG) bash -c \
218+
"cd /opt/mydistro/busybox && make menuconfig && cp .config /work/busybox.config"
219+
220+
# -----------------------------------------------------------------------------
221+
# CI/CD
222+
# -----------------------------------------------------------------------------
223+
ci: clean build test
224+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ CI build complete$(COLOR_RESET)"
225+
226+
# Check for required tools
227+
check-deps:
228+
@echo "$(COLOR_BOLD)Checking dependencies...$(COLOR_RESET)"
229+
@command -v docker >/dev/null 2>&1 || { echo "$(COLOR_RED)✗ docker not found$(COLOR_RESET)"; exit 1; }
230+
@command -v qemu-system-x86_64 >/dev/null 2>&1 || echo "$(COLOR_YELLOW)! qemu-system-x86_64 not found (optional)$(COLOR_RESET)"
231+
@echo "$(COLOR_BOLD)$(COLOR_GREEN)✓ All required dependencies found$(COLOR_RESET)"

0 commit comments

Comments
 (0)