Skip to content

Commit

Permalink
feat(kmod): separete build for kunit (#468)
Browse files Browse the repository at this point in the history
Signed-off-by: veqcc <ryuta.kambe@tier4.jp>
  • Loading branch information
veqcc authored Mar 4, 2025
1 parent b76a16d commit 8c9d4b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
1 change: 0 additions & 1 deletion agnocast_kmod/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
*.gcno
Module.symvers
modules.order
agnocast_tmp_copied.c
66 changes: 30 additions & 36 deletions agnocast_kmod/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
obj-m := agnocast.o
CFLAGS_agnocast.o := -Wall -Werror
CFLAGS_agnocast_kunit.o := -Wall -Werror -DKUNIT_BUILD
ccflags-y += -Wall -Werror

ifneq ($(KUNIT_BUILD),y)
obj-m := agnocast.o
else
ifeq ($(CONFIG_KUNIT)$(CONFIG_GCOV_KERNEL),yy)
obj-m := agnocast_kunit.o
agnocast_kunit-m := agnocast_kunit_main.o agnocast.o \
agnocast_kunit/agnocast_kunit_subscriber_add.o \
agnocast_kunit/agnocast_kunit_publisher_add.o \
agnocast_kunit/agnocast_kunit_increment_rc.o \
agnocast_kunit/agnocast_kunit_decrement_rc.o \
agnocast_kunit/agnocast_kunit_receive_msg.o \
agnocast_kunit/agnocast_kunit_publish_msg.o \
agnocast_kunit/agnocast_kunit_take_msg.o \
agnocast_kunit/agnocast_kunit_new_shm.o \
agnocast_kunit/agnocast_kunit_get_subscriber_num.o \
agnocast_kunit/agnocast_kunit_get_topic_list.o
ccflags-y += -DKUNIT_BUILD -fprofile-arcs -ftest-coverage
else
$(warning "CONFIG_KUNIT or CONFIG_GCOV_KERNEL is not set")
endif
endif

KERNEL_VERSION := $(shell uname -r)
KERNEL_MAJOR := $(shell echo $(KERNEL_VERSION) | cut -d '.' -f 1)
Expand All @@ -15,50 +35,24 @@ KERNEL_MINOR := $(shell echo $(KERNEL_VERSION) | cut -d '.' -f 2)
# }
# Therefore, add -std=gnu11 to suppress this error.
ifeq ($(shell expr $(KERNEL_MAJOR) \< 5 \| $(KERNEL_MAJOR) = 5 \& $(KERNEL_MINOR) \<= 17), 1)
CFLAGS_agnocast.o += -std=gnu11
CFLAGS_agnocast_tmp_copied.o += -std=gnu11
CFLAGS_agnocast_kunit.o += -std=gnu11
ccflags-y += -std=gnu11
endif


# In Linux kernel versions v6.4 and earlier, the top-level Makefile includes
# -Wdeclaration-after-statement, which requires all variables used within
# a function to be declared at the beginning of the function. To suppress
# this, we should add -Wno-declaration-after-statement.
ifeq ($(shell expr $(KERNEL_MAJOR) \< 6 \| $(KERNEL_MAJOR) = 6 \& $(KERNEL_MINOR) \<= 4), 1)
CFLAGS_agnocast.o += -Wno-declaration-after-statement
CFLAGS_agnocast_tmp_copied.o += -Wno-declaration-after-statement
CFLAGS_agnocast_kunit.o += -Wno-declaration-after-statement
endif

ifeq ($(CONFIG_KUNIT),y)
obj-m += agnocast_kunit.o
agnocast_kunit-m := agnocast_kunit_main.o agnocast_tmp_copied.o \
agnocast_kunit/agnocast_kunit_subscriber_add.o \
agnocast_kunit/agnocast_kunit_publisher_add.o \
agnocast_kunit/agnocast_kunit_increment_rc.o \
agnocast_kunit/agnocast_kunit_decrement_rc.o \
agnocast_kunit/agnocast_kunit_receive_msg.o \
agnocast_kunit/agnocast_kunit_publish_msg.o \
agnocast_kunit/agnocast_kunit_take_msg.o \
agnocast_kunit/agnocast_kunit_new_shm.o \
agnocast_kunit/agnocast_kunit_get_subscriber_num.o \
agnocast_kunit/agnocast_kunit_get_topic_list.o
$(foreach obj,$(agnocast_kunit-m),$(eval CFLAGS_$(obj) += -DKUNIT_BUILD))
endif

ifeq ($(CONFIG_GCOV_KERNEL),y)
CFLAGS_agnocast.o += -fprofile-arcs -ftest-coverage
CFLAGS_agnocast_tmp_copied.o += -fprofile-arcs -ftest-coverage
CFLAGS_agnocast_kunit.o += -fprofile-arcs -ftest-coverage
ccflags-y += -Wno-declaration-after-statement
endif

all:
# In order to build with different build options (KUNIT_BUILD),
# we need to temporarily copy the kernel module source.
cp agnocast.c agnocast_tmp_copied.c
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

test:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) KUNIT_BUILD=y modules

clean:
rm -f agnocast_tmp_copied.c
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

.PHONY: all test clean
4 changes: 2 additions & 2 deletions scripts/run_kunit
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi

### Check if agnocast.ko is not already loaded.
### Since agnocast_kunit.ko contains symbols from agnocast.ko,
### symbol conflicts will ocurr if it is already loaded.
### symbol conflicts will occur if it is already loaded.
if lsmod | grep -q "agnocast"; then
echo "Kernel module 'agnocast' is already loaded. Unload it before kunit test."
exit 1
Expand All @@ -35,7 +35,7 @@ fi

cd $AGNOCAST_KMOD_PATH
make clean
make
make test
sudo insmod $AGNOCAST_KMOD_PATH/agnocast_kunit.ko
sleep 3 # HACK
TOTALS_LINE=$(sudo dmesg | tail -n 10 | grep -E "# Totals: pass:[0-9]+ fail:[0-9]+")
Expand Down

0 comments on commit 8c9d4b8

Please sign in to comment.