Makefile avanzado que crea automáticamente la estructura de directorios

 Makefile avanzado que crea automáticamente la estructura de directorios y genera archivos iniciales dentro de ProyectoSO.

# Definición de directorios
DIRS := ProyectoSO ProyectoSO/bootloader ProyectoSO/bootloader/build ProyectoSO/bootloader/iso \
        ProyectoSO/kernel ProyectoSO/kernel/drivers ProyectoSO/kernel/memory ProyectoSO/kernel/scheduler \
        ProyectoSO/kernel/syscall ProyectoSO/kernel/src ProyectoSO/user ProyectoSO/user/build \
        ProyectoSO/tools ProyectoSO/docs

# Archivos iniciales
FILES := ProyectoSO/README.md ProyectoSO/bootloader/boot.asm ProyectoSO/bootloader/stage1.asm ProyectoSO/bootloader/stage2.asm \
         ProyectoSO/bootloader/linker.ld ProyectoSO/kernel/kernel_entry.asm ProyectoSO/kernel/kernel_main.c \
         ProyectoSO/kernel/drivers/keyboard.c ProyectoSO/kernel/drivers/screen.c ProyectoSO/kernel/memory/paging.c \
         ProyectoSO/kernel/memory/allocator.c ProyectoSO/kernel/scheduler/process.c ProyectoSO/kernel/syscall/syscalls.c \
         ProyectoSO/user/shell.c ProyectoSO/tools/geniso.sh ProyectoSO/tools/qemu_run.sh ProyectoSO/docs/arquitectura.md

.PHONY: setup clean

# Creación de estructura de directorios y archivos iniciales
setup:
    mkdir -p $(DIRS)
    touch $(FILES)
    echo "# ProyectoSO" > ProyectoSO/README.md
    echo "; Bootloader ASM" > ProyectoSO/bootloader/boot.asm
    echo "#include <stdio.h>\nint main() { return 0; }" > ProyectoSO/kernel/kernel_main.c
    echo "#include <stdio.h>\nint main() { return 0; }" > ProyectoSO/user/shell.c
    echo "#!/bin/bash\necho 'Generando ISO...'" > ProyectoSO/tools/geniso.sh
    chmod +x ProyectoSO/tools/geniso.sh

# Limpieza de la estructura generada
clean:
    rm -rf ProyectoSO

Características destacadas:

  • Automatiza la creación de la estructura de directorios. 
  • Genera archivos fuente iniciales para cada módulo. 
  • Agrega permisos ejecutables a scripts en tools/. 
  • Organiza el sistema operativo para facilitar el desarrollo.

Este Makefile proporciona una base sólida para comenzar con tu sistema operativo

 

Makefile avanzado para compilar, enlazar y generar la imagen ISO del sistema operativo ProyectoSO. Este Makefile manejará el bootloader, el kernel, las aplicaciones de usuario y finalmente empaquetará todo en un archivo ISO listo para ejecutarse en hardware real y pruebas en QEMU. 🚀

# Definición de directorios y archivos generados
BOOTLOADER_DIR := ProyectoSO/bootloader
KERNEL_DIR := ProyectoSO/kernel
USER_DIR := ProyectoSO/user
BUILD_DIR := ProyectoSO/build
ISO_DIR := ProyectoSO/iso

BOOTLOADER_SRC := $(BOOTLOADER_DIR)/boot.asm $(BOOTLOADER_DIR)/stage1.asm $(BOOTLOADER_DIR)/stage2.asm
KERNEL_SRC := $(KERNEL_DIR)/kernel_entry.asm $(KERNEL_DIR)/kernel_main.c
USER_SRC := $(USER_DIR)/shell.c

BOOTLOADER_OBJ := $(BOOTLOADER_DIR)/build/boot.o $(BOOTLOADER_DIR)/build/stage1.o $(BOOTLOADER_DIR)/build/stage2.o
KERNEL_OBJ := $(KERNEL_DIR)/build/kernel_entry.o $(KERNEL_DIR)/build/kernel_main.o
USER_OBJ := $(USER_DIR)/build/shell.o

BOOTLOADER_BIN := $(BOOTLOADER_DIR)/build/bootloader.bin
KERNEL_BIN := $(KERNEL_DIR)/build/kernel.bin
FINAL_ELF := $(BUILD_DIR)/bootload_x8664.elf
FINAL_BIN := $(BUILD_DIR)/bootload_x8664.bin
FINAL_ISO := $(BUILD_DIR)/bootload_x8664.iso

# Herramientas de compilación
ASM := nasm
CC := gcc
LD := ld
ISO_TOOL := xorriso

.PHONY: all compile link iso clean

all: compile link iso

# Compilación de archivos fuente
compile:
    $(ASM) -f elf64 $(BOOTLOADER_DIR)/boot.asm -o $(BOOTLOADER_DIR)/build/boot.o
    $(ASM) -f elf64 $(BOOTLOADER_DIR)/stage1.asm -o $(BOOTLOADER_DIR)/build/stage1.o
    $(ASM) -f elf64 $(BOOTLOADER_DIR)/stage2.asm -o $(BOOTLOADER_DIR)/build/stage2.o
    $(ASM) -f elf64 $(KERNEL_DIR)/kernel_entry.asm -o $(KERNEL_DIR)/build/kernel_entry.o
    $(CC) -c $(KERNEL_DIR)/kernel_main.c -o $(KERNEL_DIR)/build/kernel_main.o
    $(CC) -c $(USER_DIR)/shell.c -o $(USER_DIR)/build/shell.o

# Enlace de los binarios finales
link:
    $(LD) -T $(BOOTLOADER_DIR)/linker.ld -o $(BOOTLOADER_BIN) $(BOOTLOADER_OBJ)
    $(LD) -T $(KERNEL_DIR)/linker.ld -o $(KERNEL_BIN) $(KERNEL_OBJ)
    $(LD) -T ProyectoSO/linker.ld -o $(FINAL_ELF) $(BOOTLOADER_BIN) $(KERNEL_BIN) $(USER_OBJ)
    objcopy -O binary $(FINAL_ELF) $(FINAL_BIN)

# Generación de la imagen ISO para hardware real y pruebas en QEMU
iso:
    mkdir -p $(ISO_DIR)
    cp $(FINAL_BIN) $(ISO_DIR)/
    $(ISO_TOOL) -as mkisofs -o $(FINAL_ISO) -b boot.cat -no-emul-boot $(ISO_DIR)

# Limpieza de archivos generados
clean:
    rm -rf $(BOOTLOADER_DIR)/build $(KERNEL_DIR)/build $(USER_DIR)/build $(BUILD_DIR) $(ISO_DIR)

 

Características clave:

  • Compila código en ensamblador (nasm) y C (gcc). 
  • Enlaza correctamente el núcleo y el bootloader. 
  • Genera una imagen ISO funcional para hardware real y pruebas en QEMU. 
  • Organización modular y adaptable para futuras expansiones del sistema.

Este Makefile es eficiente, estructurado y escalable para la construcción de tu sistema operativo. 🚀 

 

 

 

Publicar un comentario

0 Comentarios