Skip to content

Reorganize project structure #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
*.o
*.o.d
*.sw*
semu
build/

# Linux-specific
*.dtb
Image
ext4.img
rootfs.cpio
Expand Down
22 changes: 11 additions & 11 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[submodule "mini-gdbstub"]
path = mini-gdbstub
url = https://github.com/RinHizakura/mini-gdbstub
shallow = true
[submodule "minislirp"]
path = minislirp
[submodule "3rdparty/mini-gdbstub"]
path = 3rdparty/mini-gdbstub
url = https://github.com/RinHizakura/mini-gdbstub
shallow = true
[submodule "3rdparty/minislirp"]
path = 3rdparty/minislirp
url = https://github.com/edubart/minislirp
shallow = true
[submodule "portaudio"]
path = portaudio
url = https://github.com/PortAudio/portaudio
shallow = true
shallow = true
[submodule "3rdparty/portaudio"]
path = 3rdparty/portaudio
url = https://github.com/PortAudio/portaudio
shallow = true
1 change: 1 addition & 0 deletions 3rdparty/mini-gdbstub
Submodule mini-gdbstub added at 39020f
1 change: 1 addition & 0 deletions 3rdparty/portaudio
Submodule portaudio added at c3f922
96 changes: 56 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
include mk/common.mk
include mk/check-libs.mk

THIRDPARTY_DIR := 3rdparty
INCLUDE_DIR := include
BUILD_DIR := build
SRC_DIR := src

CC ?= gcc
CFLAGS := -O2 -g -Wall -Wextra
CFLAGS += -include common.h
CFLAGS += -I$(INCLUDE_DIR) -I$(THIRDPARTY_DIR) -include $(INCLUDE_DIR)/common.h

# clock frequency
CLOCK_FREQ ?= 65000000
DT_CFLAGS := -D CLOCK_FREQ=$(CLOCK_FREQ)
CFLAGS += $(DT_CFLAGS)

OBJS_EXTRA :=
LIBS_EXTRA :=

# command line option
OPTS :=

LDFLAGS :=

SHELL_HACK := $(shell mkdir -p $(BUILD_DIR))

# virtio-blk
ENABLE_VIRTIOBLK ?= 1
$(call set-feature, VIRTIOBLK)
Expand All @@ -24,7 +33,7 @@ MKFS_EXT4 ?= mkfs.ext4
ifeq ($(call has, VIRTIOBLK), 1)
OBJS_EXTRA += virtio-blk.o
DISKIMG_FILE := ext4.img
OPTS += -d $(DISKIMG_FILE)
OPTS += -d $(BUILD_DIR)/$(DISKIMG_FILE)
MKFS_EXT4 := $(shell which $(MKFS_EXT4))
ifndef MKFS_EXT4
MKFS_EXT4 := $(shell which $$(brew --prefix e2fsprogs)/sbin/mkfs.ext4)
Expand Down Expand Up @@ -77,8 +86,9 @@ $(call set-feature, VIRTIOSND)
ifeq ($(call has, VIRTIOSND), 1)
OBJS_EXTRA += virtio-snd.o

PA_LIB := portaudio/lib/.libs/libportaudio.a
PA_CFLAGS := -Iportaudio/include
PORTAUDIO_DIR := $(THIRDPARTY_DIR)/portaudio
PA_LIB := $(PORTAUDIO_DIR)/lib/.libs/libportaudio.a
PA_CFLAGS := -I$(PORTAUDIO_DIR)/include
PA_CONFIG_PARAMS :=
LDFLAGS += $(PA_LIB)
CFLAGS += $(PA_CFLAGS)
Expand All @@ -105,9 +115,10 @@ ifeq ($(call has, VIRTIOSND), 1)
# -lm separately.
LDFLAGS += -lpthread

portaudio/Makefile:
git submodule update --init portaudio
$(PA_LIB): portaudio/Makefile
$(PORTAUDIO_DIR)/Makefile:
git submodule update --init $(PORTAUDIO_DIR)

$(PA_LIB): $(PORTAUDIO_DIR)/Makefile
cd $(dir $<) && git clean -fdx && git reset --hard HEAD
cd $(dir $<) && ./configure \
--enable-static \
Expand All @@ -119,10 +130,10 @@ $(PA_LIB): portaudio/Makefile
--disable-dependency-tracking \
$(PA_CONFIG_PARAMS)
$(MAKE) -C $(dir $<)
main.o: $(PA_LIB)
virtio-snd.o: $(PA_LIB)
$(BUILD_DIR)/main.o: $(PA_LIB)
$(BUILD_DIR)/virtio-snd.o: $(PA_LIB)
# suppress warning when compiling PortAudio
virtio-snd.o: CFLAGS += -Wno-unused-parameter
$(BUILD_DIR)/virtio-snd.o: CFLAGS += -Wno-unused-parameter
endif

# Set libm as the last dependency so that no need to set -lm seperately.
Expand All @@ -132,8 +143,9 @@ LDFLAGS += -lm
# after git submodule.
.DEFAULT_GOAL := all

BIN = semu
all: $(BIN) minimal.dtb
BIN = $(BUILD_DIR)/semu

all: $(BIN) $(BUILD_DIR)/minimal.dtb

OBJS := \
riscv.o \
Expand All @@ -145,34 +157,36 @@ OBJS := \
aclint.o \
$(OBJS_EXTRA)

deps := $(OBJS:%.o=.%.o.d)
objs := $(foreach obj,$(OBJS),$(BUILD_DIR)/$(obj))
deps := $(patsubst %.o,$(BUILD_DIR)/%.o.d,$(OBJS))

GDBSTUB_LIB := mini-gdbstub/build/libgdbstub.a
LDFLAGS += $(GDBSTUB_LIB)
mini-gdbstub/Makefile:
MINI_GDBSTUB_DIR := $(THIRDPARTY_DIR)/mini-gdbstub
MINI_GDBSTUB_LIB := $(MINI_GDBSTUB_DIR)/build/libgdbstub.a
LDFLAGS += $(MINI_GDBSTUB_LIB)
$(THIRDPARTY_DIR)/mini-gdbstub/Makefile:
git submodule update --init $(dir $@)
$(GDBSTUB_LIB): mini-gdbstub/Makefile
$(MINI_GDBSTUB_LIB): $(MINI_GDBSTUB_DIR)/Makefile
$(MAKE) -C $(dir $<)
$(OBJS): $(GDBSTUB_LIB)
LIBS_EXTRA += $(MINI_GDBSTUB_LIB)

ifeq ($(call has, VIRTIONET), 1)
MINISLIRP_DIR := minislirp
MINISLIRP_LIB := minislirp/src/libslirp.a
MINISLIRP_DIR := $(THIRDPARTY_DIR)/minislirp
MINISLIRP_LIB := $(MINISLIRP_DIR)/src/libslirp.a
LDFLAGS += $(MINISLIRP_LIB)
$(MINISLIRP_DIR)/src/Makefile:
git submodule update --init $(MINISLIRP_DIR)
$(MINISLIRP_LIB): $(MINISLIRP_DIR)/src/Makefile
$(MAKE) -C $(dir $<)
$(OBJS): $(MINISLIRP_LIB)
LIBS_EXTRA += $(MINISLIRP_LIB)
endif

$(BIN): $(OBJS)
$(BIN): $(objs)
$(VECHO) " LD\t$@\n"
$(Q)$(CC) -o $@ $^ $(LDFLAGS)

%.o: %.c
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(LIBS_EXTRA)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF .[email protected] $<
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF [email protected] $<

DTC ?= dtc

Expand All @@ -196,40 +210,42 @@ S := $E $E
CFLAGS += -D SEMU_BOOT_TARGET_TIME=10

SMP ?= 1
.PHONY: riscv-harts.dtsi
riscv-harts.dtsi:
.PHONY: $(INCLUDE_DIR)/riscv-harts.dtsi
$(INCLUDE_DIR)/riscv-harts.dtsi:
$(Q)python3 scripts/gen-hart-dts.py $@ $(SMP) $(CLOCK_FREQ)

minimal.dtb: minimal.dts riscv-harts.dtsi
$(BUILD_DIR)/minimal.dtb: $(SRC_DIR)/minimal.dts $(INCLUDE_DIR)/riscv-harts.dtsi
$(VECHO) " DTC\t$@\n"
$(Q)$(CC) -nostdinc -E -P -x assembler-with-cpp -undef \
-I$(INCLUDE_DIR) \
$(DT_CFLAGS) \
$(subst ^,$S,$(filter -D^SEMU_FEATURE_%, $(subst -D$(S)SEMU_FEATURE,-D^SEMU_FEATURE,$(CFLAGS)))) $< \
| $(DTC) - > $@

# Rules for downloading prebuilt Linux kernel image
include mk/external.mk

ext4.img:
$(BUILD_DIR)/$(DISKIMG_FILE):
$(Q)dd if=/dev/zero of=$@ bs=4k count=600
$(Q)$(MKFS_EXT4) -F $@

check: $(BIN) minimal.dtb $(KERNEL_DATA) $(INITRD_DATA) $(DISKIMG_FILE)
.PHONY: download-artifacts
download-artifacts:
$(Q)scripts/download-artifacts.sh Image $(BUILD_DIR)
$(Q)scripts/download-artifacts.sh rootfs $(BUILD_DIR)

check: $(BIN) $(BUILD_DIR)/minimal.dtb $(BUILD_DIR)/$(DISKIMG_FILE) download-artifacts
@$(call notice, Ready to launch Linux kernel. Please be patient.)
$(Q)./$(BIN) -k $(KERNEL_DATA) -c $(SMP) -b minimal.dtb -i $(INITRD_DATA) -n $(NETDEV) $(OPTS)
$(Q)$(BIN) -k $(BUILD_DIR)/Image -c $(SMP) -b $(BUILD_DIR)/minimal.dtb -i $(BUILD_DIR)/rootfs.cpio -n $(NETDEV) $(OPTS)

build-image:
scripts/build-image.sh

clean:
$(Q)$(RM) $(BIN) $(OBJS) $(deps)
$(Q)$(MAKE) -C mini-gdbstub clean
$(Q)$(MAKE) -C minislirp/src clean
$(Q)$(RM) $(BIN) $(objs) $(deps)
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/mini-gdbstub clean
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/minislirp/src clean
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/portaudio clean

distclean: clean
$(Q)$(RM) riscv-harts.dtsi
$(Q)$(RM) minimal.dtb
$(Q)$(RM) Image rootfs.cpio
$(Q)$(RM) ext4.img
$(Q)$(RM) $(INCLUDE_DIR)/riscv-harts.dtsi
$(Q)$(RM) $(BUILD_DIR)/*

-include $(deps)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion mini-gdbstub
Submodule mini-gdbstub deleted from 18a374
27 changes: 0 additions & 27 deletions mk/external.mk

This file was deleted.

1 change: 0 additions & 1 deletion portaudio
Submodule portaudio deleted from e97eff
88 changes: 88 additions & 0 deletions scripts/download-artifacts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/sh

BASE_URL="https://github.com/sysprog21/semu/raw/blob"

IMAGE_SHA1="72a8454575508d09210f067b25239cd51b929d27"
ROOTFS_SHA1="18bd639c17fc299c317f8fed1c845e7dda296748"

DEST_DIR="./build"

if [ -z "$1" ]; then
echo "No file name provided. Please provide a target as the first argument."
exit 1
fi

case "$1" in
"Image")
FULL_URL="${BASE_URL}/Image.bz2"
LOCAL_FILE_PATH="${DEST_DIR}/Image"
FILE_SHA1="${IMAGE_SHA1}"
;;
"rootfs")
FULL_URL="${BASE_URL}/rootfs.cpio.bz2"
LOCAL_FILE_PATH="${DEST_DIR}/rootfs.cpio"
FILE_SHA1="${ROOTFS_SHA1}"
;;
*)
echo "Invalid target: \"$1\""
echo "Available keywords are: Image, rootfs"
exit 1
;;
esac

if [ ! -z "$2" ]; then
DEST_DIR="$2"
else
echo "No destination directory provided. Using the default one: \"$DEST_DIR\""
fi

if [ ! -d "$DEST_DIR" ]; then
mkdir -p "$DEST_DIR"
fi

if [ ! -f "${LOCAL_FILE_PATH}.bz2" ]; then
NEED_REDOWNLOAD="1"

echo "The file \"${LOCAL_FILE_PATH}.bz2\" was not found. Downloading..."
else
SHA1_CHECK_RES=$(echo "${FILE_SHA1} ${LOCAL_FILE_PATH}.bz2" | sha1sum -c - >/dev/null 2>&1 || echo "failed")

if [ "$SHA1_CHECK_RES" = "failed" ]; then
NEED_REDOWNLOAD="1"

echo "Local file \"${LOCAL_FILE_PATH}.bz2\" already exists. Checking SHA1... Failed"

# if the existing file is larger, the extra part would be ignored by "curl -C".
rm "${LOCAL_FILE_PATH}.bz2"

echo "Re-downloading file..."
else
echo "Local file \"${LOCAL_FILE_PATH}.bz2\" already exists. Checking SHA1... Passed"
echo "File is up-to-date. Skipping download."
fi
fi

if [ ! -z "$NEED_REDOWNLOAD" ]; then
curl --progress-bar -L -C - "$FULL_URL" -o "${LOCAL_FILE_PATH}.bz2"

SHA1_CHECK_RES=$(echo "${FILE_SHA1} ${LOCAL_FILE_PATH}.bz2" | sha1sum -c - >/dev/null 2>&1 || echo "failed")

if [ "$SHA1_CHECK_RES" = "failed" ]; then
echo "Checking SHA1... Failed"
echo "Error: The SHA1 might be outdated. Please update your local copy and try again."
exit 1
else
echo "Checking SHA1... Passed"
fi
fi

echo "Extracting from \"${LOCAL_FILE_PATH}.bz2\""

bunzip2 --keep "${LOCAL_FILE_PATH}.bz2" >/dev/null 2>&1

if [ ! -f "$LOCAL_FILE_PATH" ]; then
echo "Something went wrong! The file \"$LOCAL_FILE_PATH\" was not found."
exit 1
else
echo "Completed!"
fi
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion riscv.c → src/riscv.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdio.h>

#include "common.h"
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion virtio-blk.c → src/virtio-blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <sys/stat.h>
#include <unistd.h>

#include "common.h"
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
Expand Down
1 change: 0 additions & 1 deletion virtio-net.c → src/virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <sys/ioctl.h>
#include <sys/uio.h>

#include "common.h"
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
Expand Down
1 change: 0 additions & 1 deletion virtio-rng.c → src/virtio-rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string.h>
#include <unistd.h>

#include "common.h"
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
Expand Down
File renamed without changes.