Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
new: Scripts pour make *.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainTHD committed Oct 4, 2020
1 parent 7ad4c5c commit 675a56d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
cmake-*
cmake*
bin/part
obj
78 changes: 41 additions & 37 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@ MAKEFLAGS += --silent
GCC_PATH = "$$HOME"/opt/cross/bin/x86_64-elf-g++
LINKER_PATH = "$$HOME"/opt/cross/bin/x86_64-elf-ld

SRCDIR := ./src
OBJDIR := ./obj
DEPDIR := dep
SRCS := $(shell find $(SRCDIR) -name "*.cpp")
OBJS := $(SRCS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
DEPS := $(SRCS:$(SRCDIR)/%.cpp=$(DEPDIR)/%.d)
TREE := $(patsubst %/,%,$(dir $(OBJS)))
CPPFLAGS = -Ttext 0x8000 -ffreestanding -mno-red-zone -m64 -save-temps=obj -Wall -Wextra -pedantic -Wno-write-strings -std=c++1z -I ./src -I ./src/std
# -Ttext 0x8000 Set la section .text à 0x8000, pour situer le main
# -ffreestanding La lib standard n'existe pas, et main n'est pas le point d'entrée
# -mno-red-zone Désactive la red-zone, 128 bytes sous le stack utilisés librement par gcc
# -m64 Génère du code pour 64 bits
# -save-temps=obj Exporte les .cpp en assembleur dans le même dossier que les .o
# -Wall Toutes les erreurs
# -Wextra Extra erreurs
# -pedantic Erreurs en plus
# -Wno-write-strings Disable le warning pour l'utilisation de char* au lieu de std::string (std::string n'existe pas...)
# -std=c++1z C++ 17
# -I ./src Dossier d'include pour les .hpp
# -I ./src/std Dossier d'include pour les .hpp : std

all: clean build run

build: compileAsm compileCPP link merge calcNbSeg

clean:
rm -r bin/part
mkdir bin/part
rm -r ./obj
mkdir ./obj
rm -r ./bin/part
mkdir ./bin/part

compileAsm:
echo "Compilation bootloader..."
Expand All @@ -18,47 +41,28 @@ compileAsm:

echo "Compilation secteurs suivants..."
cd ./src/kernel/secondSector && \
nasm ./extendedProgram.asm -f elf64 -i ../bootSector/ -o ../../../bin/part/extendedProgram.o -w+orphan-labels && \
nasm ./binaries.asm -f elf64 -o ../../../bin/part/binaries.o -w+orphan-labels
nasm ./extendedProgram.asm -f elf64 -i ../bootSector/ -o ../../../obj/extendedProgram.o -w+orphan-labels && \
nasm ./binaries.asm -f elf64 -o ../../../obj/binaries.o -w+orphan-labels

compileCPP: _beforeCompilationCPP $(OBJS) _afterCompilationCPP

compileCPP:
_beforeCompilationCPP:
echo "Compilation C/C++..."

cd ./src && $(GCC_PATH) \
-Ttext 0x8000 \
-ffreestanding \
-mno-red-zone \
-m64 \
-save-temps=obj \
-Wall \
-Wextra \
-pedantic \
-Wno-write-strings \
-std=c++1z \
-I . \
-I ./std \
-c kernel/kernel.cpp \
-o ../bin/part/kernel.o

# -Ttext 0x8000 Set la section .text à 0x8000, pour situer le main
# -ffreestanding La lib standard n'existe pas, et main n'est pas le point d'entrée
# -mno-red-zone Désactive la red-zone, 128 bytes sous le stack utilisés librement par gcc
# -m64 Génère du code pour 64 bits
# -save-temps=obj Exporte les .cpp en assembleur dans le même dossier que les .o
# -Wall Toutes les erreurs
# -Wextra Extra erreurs
# -pedantic Erreurs en plus
# -Wno-write-strings Disable le warning pour l'utilisation de char* au lieu de std::string (std::string n'existe pas...)
# -std=c++1z C++ 17
# -I . Dossier d'include pour les .hpp
# -I ./std Dossier d'include pour les .hpp : std
# -c kernel/kernel.cpp Source
# -o ../bin/part/kernel.o Output
.SECONDEXPANSION:
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $$(@D)
$(GCC_PATH) $(CPPFLAGS) -o $@ -c $<

$(TREE): %:
mkdir -p $@
mkdir -p $(@:$(OBJDIR)%=$(DEPDIR)%)

_afterCompilationCPP:
python3 ./scripts/editLinker.py

link:
echo "Linker..."
cd ./bin/part && \
$(LINKER_PATH) -T "../../linker.ld"
$(LINKER_PATH) -T "./scripts/linker.ld"

merge:
echo "Fusion des fichiers binaires..."
Expand Down
42 changes: 42 additions & 0 deletions scripts/editLinker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os

if os.getcwd().replace('\\', '/').endswith("/scripts") or os.getcwd().replace('\\', '/').endswith("/scripts/"):
os.chdir("..")

obj = []

for root, subdirs, files in os.walk("./obj"):
for f in files:
if f.endswith(".o"):
obj.append(root.replace('\\', '/') + '/' + f)

f = open("./scripts/linker.ld", 'r')
linkerStr = f.read()
f.close()

f = open("./scripts/linker.ld.bak", 'w')
f.write(linkerStr)
f.close()

linkerArr = []

split_1 = linkerStr.split(sep="INPUT")

linkerArr.append(split_1[0])

split_2 = split_1[1].split(sep="OUTPUT")

linkerArr.append("OUTPUT" + split_2[0 if len(split_2) == 1 else 1])

linkerInput = "INPUT (\n"

for file in obj:
linkerInput += ' '*4 + file + "\n"

linkerInput += ")\n\n"

linker = linkerInput.join(linkerArr)

f = open("./scripts/linker.ld", 'w')
f.write(linker)
f.close()
8 changes: 4 additions & 4 deletions linker.ld → scripts/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ OUTPUT_FORMAT(binary)
ENTRY(_start)

INPUT (
extendedProgram.o
kernel.o
binaries.o
./obj/binaries.o
./obj/extendedProgram.o
./obj/kernel/kernel.o
)

OUTPUT (
kernel.bin
./bin/part/kernel.bin
)

SECTIONS {
Expand Down
45 changes: 45 additions & 0 deletions scripts/linker.ld.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
OUTPUT_FORMAT(binary)

ENTRY(_start)

INPUT (
./obj/binaries.o
./obj/extendedProgram.o
./obj/test.o
./obj/kernel/kernel.o
)

OUTPUT (
./bin/part/kernel.bin
)

SECTIONS {
/* Début des sections */
. = 0x8000;

.text : ALIGN(0x1000) {
*(.text)
*(.rodata*)
}

/* Pour les interruptions */
.idt BLOCK(0x1000) : ALIGN(0x1000) {
_idt = .;
/* On réserve au moins 4096 bytes */
. = . + 0x1000;
}

/* Read-only data */
.rodata : ALIGN(0x1000) {}

/* Read-write initialized data */
.data : ALIGN(0x1000) {
*(.data)
}

/* Read-write uninitialized data + stack */
.bss : ALIGN(0x1000) {
*(COMMON)
*(.bss)
}
}

0 comments on commit 675a56d

Please sign in to comment.