diff options
-rw-r--r-- | .github/workflows/build.yml | 2 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 64 | ||||
-rwxr-xr-x | run | 83 |
4 files changed, 85 insertions, 65 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 380f9fc..cfa6f20 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Install - run: sudo apt-get update && sudo apt-get install -y build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev texinfo libcloog-isl-dev libisl-0.18-dev ccache curl nasm grub-common qemu qemu-kvm mtools + run: sudo apt-get update && sudo apt-get install -y build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev texinfo libcloog-isl-dev libisl-0.18-dev ccache curl nasm grub-common qemu qemu-kvm mtools cmake - name: Get cross compiler id: cache-cross uses: actions/cache@v1 @@ -1,6 +1,5 @@ Melvix.iml tags -CMakeLists.txt compile_commands.json .idea/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3612ddb --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,64 @@ +cmake_minimum_required(VERSION 3.0) +project(Melvix) +enable_language(ASM) +# set(CMAKE_VERBOSE_MAKEFILE on) +set(CMAKE_COLOR_MAKEFILE on) +set(CMAKE_EXPORT_COMPILE_COMMANDS on) + +find_program(CCACHE_FOUND ccache) +if(CCACHE_FOUND) + set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) + set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) +endif(CCACHE_FOUND) + +# Melvix variables +set(NETWORK "rtl8139") + +# Compiler and linker +set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS " ") +set(CMAKE_C_COMPILER "${CMAKE_CURRENT_SOURCE_DIR}/cross/opt/bin/i686-elf-gcc") +set(CMAKE_ASM_COMPILER "nasm") +set(CMAKE_LINKER "${CMAKE_CURRENT_SOURCE_DIR}/cross/opt/bin/i686-elf-gcc") +set(CMAKE_ASM_LINKER_PREFERENCE ${CMAKE_LINKER}) +set(CMAKE_C_LINKER_PREFERENCE ${CMAKE_LINKER}) + +# Compiler and linker flags +set(CMAKE_C_FLAGS "-Wall -Wno-int-conversion -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdlib -ffreestanding -std=gnu99 -g -ggdb -c") +set(CMAKE_EXE_LINKER_FLAGS "-ffreestanding -O2 -nostdlib -g -ggdb") +set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <SOURCE> -o <OBJECT> -f elf32 -O0") +set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> ${CMAKE_EXE_LINKER_FLAGS} <OBJECTS> -o <TARGET> <LINK_LIBRARIES>") +set(CMAKE_ASM_LINK_EXECUTABLE "<CMAKE_LINKER> ${CMAKE_EXE_LINKER_FLAGS} <OBJECTS> -o <TARGET> <LINK_LIBRARIES>") + +# Recursive sources +file(GLOB_RECURSE kernel_sources src/kernel/*.c src/kernel/*.asm) +file(GLOB_RECURSE resources_sources src/resources/*.c) +file(GLOB_RECURSE user_sources src/userspace/*.c src/userspace/*.asm) + +# KERNEL +add_executable(kernel ${kernel_sources}) +target_include_directories(kernel PRIVATE "src") +set_target_properties(kernel PROPERTIES OUTPUT_NAME "${CMAKE_CURRENT_SOURCE_DIR}/build/kernel.bin") +target_compile_options(kernel PRIVATE "-D ${NETWORK}") +target_link_libraries(kernel PRIVATE "-T ${CMAKE_CURRENT_SOURCE_DIR}/src/kernel/linker.ld") + +# RESOURCES +add_executable(resources ${resources_sources}) +set_target_properties(resources PROPERTIES OUTPUT_NAME "${CMAKE_CURRENT_SOURCE_DIR}/build/font.o") +add_custom_command( + TARGET resources POST_BUILD + COMMAND cross/opt/bin/i686-elf-objcopy -O binary build/font.o iso/font.bin + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +# USERSPACE +add_executable(user ${user_sources}) +target_include_directories(user PUBLIC "src/userspace/") +set_target_properties(user PROPERTIES OUTPUT_NAME "${CMAKE_CURRENT_SOURCE_DIR}/build/user.o") +add_custom_command( + TARGET user POST_BUILD + COMMAND cross/opt/bin/i686-elf-objcopy -O binary build/user.o iso/user.bin + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +# Dependencies +add_dependencies(kernel resources user) @@ -10,19 +10,10 @@ qemu_with_flags() { # TODO: Find out why kvm install is incredibly slow SDL_VIDEO_X11_DGAMOUSE=0 qemu-system-i386 -no-reboot -vga std -smp "$(nproc)" -serial mon:stdio -rtc base=localtime -m 256M -net nic,model=${network},macaddr=42:42:42:42:42:42 -net user "$@" else - SDL_VIDEO_X11_DGAMOUSE=0 qemu-system-i386 -enable-kvm -cpu host -no-reboot -vga std -smp "$(nproc)" -serial stdio -rtc base=localtime -m 256M -net nic,model=${network},macaddr=42:42:42:42:42:42 -net user "$@" + SDL_VIDEO_X11_DGAMOUSE=0 qemu-system-i386 -no-reboot -vga std -serial stdio -rtc base=localtime -m 256M -net nic,model=${network},macaddr=42:42:42:42:42:42 -net user "$@" fi } -compile_with_flags() { - if [ "${mode}" = "image" ] || [ "${mode}" = "image_debug" ]; then - GCC_COLORS=1 ccache i686-elf-gcc -std=gnu99 -ffreestanding -nostdlib -Wall -Wextra -Wno-unused-parameter -D INSTALL_MELVIX "$@" || exit 1 - else - GCC_COLORS=1 ccache i686-elf-gcc -std=gnu99 -ffreestanding -nostdlib -Wall -Wextra -Wno-unused-parameter "$@" || exit 1 - fi - -} - make_cross() { if [ ! -d "./cross/" ]; then # Create directory @@ -69,59 +60,17 @@ make_cross() { } make_build() { - echo "Building..." - mkdir -p ./build/kernel && mkdir -p ./build/userspace - - # Assemble ASM files - find ./src/kernel/ -name \*.asm >./build/tmp - while read -r line; do - stripped=$(echo "${line}" | sed -r 's/\//_/g') - stripped=${stripped#??????} - stripped=${stripped%%???}o - nasm -f elf ./"${line}" -o ./build/kernel/asm_"${stripped}" || exit 1 - done <./build/tmp - rm ./build/tmp - - # Make all kernel C files - find ./src/kernel/ -name \*.c >./build/tmp - while read -r line; do - stripped=$(echo "${line}" | sed -r 's/\//_/g') - stripped=${stripped#??????} - stripped=${stripped%%?}o - compile_with_flags -s -c ./"${line}" -I ./src -D ${network} -o ./build/kernel/"${stripped}" - done <./build/tmp - rm ./build/tmp - - # Link kernel ASM and C objects - compile_with_flags -s ./build/kernel/*.o -T ./src/kernel/linker.ld -I ./src -o ./build/melvix.bin - - # Modules - # TODO: Find out why no font optimizations cause strange glitches - compile_with_flags -Os -c ./src/resources/font.c -o ./build/font.o - i686-elf-objcopy -O binary ./build/font.o ./build/font.bin - rm ./build/font.o - - # Userspace - # TODO: Find out why userspace optimizations (-Os) cause fatal errors - find ./src/userspace/ -name \*.c >./build/tmp - while read -r line; do - stripped=$(echo "${line}" | sed -r 's/\//_/g') - stripped=${stripped#??????} - stripped=${stripped%%?}o - compile_with_flags -O2 -c ./"${line}" -I ./src/userspace -o ./build/userspace/"${stripped}" - done <./build/tmp - rm ./build/tmp - compile_with_flags -emain -O2 ./build/userspace/*.o -I ./src/userspace -o ./build/user.bin + mkdir -p iso/ - # Create ISO - mkdir -p ./iso/boot/grub/ - cp ./build/melvix.bin ./iso/boot/kernel.bin - cp ./src/bootloader/grub.cfg ./iso/boot/grub/ - cp ./build/user.bin ./iso/user.bin - cp ./build/font.bin ./iso/font.bin - grub-mkrescue -o ./build/melvix.iso ./iso/ || exit 1 - - mke2fs -b 4096 -N 4096 ./build/disk.img 65536 || exit 1 + echo "Building..." + mkdir -p build + cd build || exit 1 + cmake .. >/dev/null || exit 1 + make || exit 1 + cd .. + + # Create disk image + mke2fs -b 1024 -N 1024 ./build/disk.img 65536 || exit 1 mkdir ./mnt/ || exit 1 sudo mount ./build/disk.img ./mnt/ || exit 1 sudo mkdir -p ./mnt/abc/def/ @@ -130,11 +79,17 @@ make_build() { sync && sudo umount mnt || exit 1 rm -r mnt/ + # Create ISO + mkdir -p ./iso/boot/grub/ + cp ./kernel ./iso/boot/kernel.bin + cp ./src/bootloader/grub.cfg ./iso/boot/grub/ + grub-mkrescue -o ./build/melvix.iso ./iso/ || exit 1 + printf "Build finshed successfully!\n\n" } make_test() { - qemu_with_flags -cdrom ./build/melvix.iso -hda ./build/disk.img -boot d + qemu_with_flags -cdrom ./build/melvix.iso -hda ./build/disk.img } make_debug() { @@ -197,6 +152,8 @@ elif [ "${mode}" = "test" ]; then make_clean make_build make_test +elif [ "${mode}" = "again" ]; then + make_test elif [ "${mode}" = "debug" ]; then make_debug elif [ "${mode}" = "image_debug" ]; then |