From 8630fe7667967009757721bece55235dca5b7614 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Thu, 15 Aug 2024 17:52:04 +0800 Subject: [PATCH] refactor: use cmake macros to do objcopy, install and tests --- CMakeLists.txt | 25 ++++++++++++++++++++++++ benchmarks/microbench/src/CMakeLists.txt | 9 ++------- default.nix | 2 ++ flake.lock | 8 ++++---- kernels/demo/src/CMakeLists.txt | 8 ++------ kernels/demo/src/main.c | 8 ++++---- kernels/hello/CMakeLists.txt | 9 ++------- kernels/yield-os/CMakeLists.txt | 4 ++-- kernels/yield-os/yield-os.c | 14 ++++++++----- tests/am-tests/src/CMakeLists.txt | 8 ++------ tests/cpu-tests/tests/CMakeLists.txt | 8 ++------ tests/cpu-tests/tests/hello-str.c | 15 +++++++------- 12 files changed, 64 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ad3e5c6..59293ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,36 @@ enable_language(C ASM) include(CheckPIESupported) include(GNUInstallDirs) +if(${ARCH} STREQUAL "native") + include(CTest) +endif() + find_package(am-${ARCH}) find_package(klib) check_pie_supported() +# -- Helper functions +macro(create_binary target_name) + if(NOT ${ARCH} STREQUAL "native") + add_custom_command( + TARGET ${target_name} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -S --set-section-flags .bss=alloc,contents -O binary ${target_name} ${target_name}.bin + COMMENT "Creating binary file ${target_name}.bin" + ) + endif() +endmacro(create_binary) + +macro(install_target_and_binary target_name) + if(NOT ${ARCH} STREQUAL "native") + install(TARGETS ${target_name} RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) + else() + install(TARGETS ${target_name}) + add_test(NAME ${target_name}_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${target_name}) + endif() +endmacro(install_target_and_binary) + add_subdirectory(tests/cpu-tests) # add_subdirectory(tests/alu-tests) add_subdirectory(tests/am-tests) diff --git a/benchmarks/microbench/src/CMakeLists.txt b/benchmarks/microbench/src/CMakeLists.txt index 7fd36c7..60b1f0f 100644 --- a/benchmarks/microbench/src/CMakeLists.txt +++ b/benchmarks/microbench/src/CMakeLists.txt @@ -14,10 +14,5 @@ add_executable(bench ) target_link_libraries(bench am-${ARCH} klib) - -# -- Extract binary file from ELF -add_custom_command(TARGET bench - COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary bench bench.bin) - -install(TARGETS bench RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/bench.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) +create_binary(bench) +install_target_and_binary(bench) diff --git a/default.nix b/default.nix index b034704..72a3e96 100644 --- a/default.nix +++ b/default.nix @@ -26,6 +26,8 @@ stdenv.mkDerivation rec { cmakeBuildType = "Debug"; dontStrip = true; + doCheck = true; + meta = with lib; { description = "AbstractMachine kernels"; homepage = "https://github.com/NJU-ProjectN/am-kernels.git"; diff --git a/flake.lock b/flake.lock index dc497e7..1e9e208 100644 --- a/flake.lock +++ b/flake.lock @@ -941,11 +941,11 @@ "pre-commit-hooks": "pre-commit-hooks_5" }, "locked": { - "lastModified": 1723711136, - "narHash": "sha256-wwVG49IBLtyb2mZ9kNGojuJSYa4evf/2IEcdx8HZxKA=", + "lastModified": 1723714692, + "narHash": "sha256-EU6HE8DfSorD2Wx7yjBicSSLxGKxKbDD/3XI+RUB7Gs=", "ref": "refs/heads/master", - "rev": "3d64dbd200ab3e944b99df76ba884abb2cdbbef6", - "revCount": 125, + "rev": "0e408882b21b17fa19158b2044498ddad8e691e7", + "revCount": 126, "type": "git", "url": "https://git.xinyang.life/xin/ysyx-workbench" }, diff --git a/kernels/demo/src/CMakeLists.txt b/kernels/demo/src/CMakeLists.txt index adce99d..1c97f14 100644 --- a/kernels/demo/src/CMakeLists.txt +++ b/kernels/demo/src/CMakeLists.txt @@ -12,9 +12,5 @@ add_executable(demo target_include_directories(demo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) target_link_libraries(demo PRIVATE am-${ARCH} klib) -# -- Extract binary file from ELF -add_custom_command(TARGET demo - COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary demo demo.bin) - -install(TARGETS demo RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/demo.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) +create_binary(demo) +install_target_and_binary(demo) diff --git a/kernels/demo/src/main.c b/kernels/demo/src/main.c index 9457fa5..e1b81d2 100644 --- a/kernels/demo/src/main.c +++ b/kernels/demo/src/main.c @@ -36,9 +36,9 @@ int main(const char *args) { } printf("Press Q to Exit\n"); - while (1) { - AM_INPUT_KEYBRD_T ev = io_read(AM_INPUT_KEYBRD); - if (ev.keydown && ev.keycode == AM_KEY_Q) break; - } + // while (1) { + // AM_INPUT_KEYBRD_T ev = io_read(AM_INPUT_KEYBRD); + // if (ev.keydown && ev.keycode == AM_KEY_Q) break; + // } return 0; } diff --git a/kernels/hello/CMakeLists.txt b/kernels/hello/CMakeLists.txt index e307f4f..1571a4f 100644 --- a/kernels/hello/CMakeLists.txt +++ b/kernels/hello/CMakeLists.txt @@ -4,10 +4,5 @@ add_executable(hello target_include_directories(hello PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) target_link_libraries(hello PRIVATE am-${ARCH} klib) - -# -- Extract binary file from ELF -add_custom_command(TARGET hello - COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary hello hello.bin) - -install(TARGETS hello RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/hello.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) +create_binary(hello) +install_target_and_binary(hello) diff --git a/kernels/yield-os/CMakeLists.txt b/kernels/yield-os/CMakeLists.txt index 2db7c9c..7684719 100644 --- a/kernels/yield-os/CMakeLists.txt +++ b/kernels/yield-os/CMakeLists.txt @@ -7,5 +7,5 @@ target_link_libraries(yield-os PRIVATE am-${ARCH} klib) add_custom_command(TARGET yield-os COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary yield-os yield-os.bin) -install(TARGETS yield-os RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/yield-os.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) +create_binary(yield-os) +install_target_and_binary(yield-os) diff --git a/kernels/yield-os/yield-os.c b/kernels/yield-os/yield-os.c index 90eb3ff..caf4e84 100644 --- a/kernels/yield-os/yield-os.c +++ b/kernels/yield-os/yield-os.c @@ -4,16 +4,20 @@ #define STACK_SIZE (4096 * 8) typedef union { uint8_t stack[STACK_SIZE]; - struct { Context *cp; }; + struct { + Context *cp; + }; } PCB; static PCB pcb[2], pcb_boot, *current = &pcb_boot; static void f(void *arg) { - while (1) { + for (int i = 0; i < 100; i++) { putch("?AB"[(uintptr_t)arg > 2 ? 0 : (uintptr_t)arg]); - for (int volatile i = 0; i < 100000; i++) ; + for (int volatile i = 0; i < 100000; i++) + ; yield(); } + halt(0); } static Context *schedule(Event ev, Context *prev) { @@ -24,8 +28,8 @@ static Context *schedule(Event ev, Context *prev) { int main() { cte_init(schedule); - pcb[0].cp = kcontext((Area) { pcb[0].stack, &pcb[0] + 1 }, f, (void *)1L); - pcb[1].cp = kcontext((Area) { pcb[1].stack, &pcb[1] + 1 }, f, (void *)2L); + pcb[0].cp = kcontext((Area){pcb[0].stack, &pcb[0] + 1}, f, (void *)1L); + pcb[1].cp = kcontext((Area){pcb[1].stack, &pcb[1] + 1}, f, (void *)2L); yield(); panic("Should not reach here!"); } diff --git a/tests/am-tests/src/CMakeLists.txt b/tests/am-tests/src/CMakeLists.txt index 2859620..5115a71 100644 --- a/tests/am-tests/src/CMakeLists.txt +++ b/tests/am-tests/src/CMakeLists.txt @@ -16,9 +16,5 @@ add_executable(am-tests target_include_directories(am-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include) target_link_libraries(am-tests PRIVATE am-${ARCH} klib) -# -- Extract binary file from ELF -add_custom_command(TARGET am-tests - COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary am-tests am-tests.bin) - -install(TARGETS am-tests RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/am-tests.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) +create_binary(am-tests) +install_target_and_binary(am-tests) diff --git a/tests/cpu-tests/tests/CMakeLists.txt b/tests/cpu-tests/tests/CMakeLists.txt index c03254b..03e071d 100644 --- a/tests/cpu-tests/tests/CMakeLists.txt +++ b/tests/cpu-tests/tests/CMakeLists.txt @@ -41,10 +41,6 @@ foreach(SOURCE IN LISTS SOURCES) ${SOURCE}) target_link_libraries(${SOURCE_NAME} PRIVATE am-${ARCH} klib) - # -- Extract binary file from ELF - add_custom_command(TARGET ${SOURCE_NAME} - COMMAND ${CMAKE_OBJCOPY} ARGS -S --set-section-flags .bss=alloc,contents -O binary ${SOURCE_NAME} ${SOURCE_NAME}.bin) - - install(TARGETS ${SOURCE_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/am-kernels) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_NAME}.bin DESTINATION ${CMAKE_INSTALL_DATADIR}/am-kernels) + create_binary(${SOURCE_NAME}) + install_target_and_binary(${SOURCE_NAME}) endforeach() diff --git a/tests/cpu-tests/tests/hello-str.c b/tests/cpu-tests/tests/hello-str.c index 90e5b1a..c4376b4 100644 --- a/tests/cpu-tests/tests/hello-str.c +++ b/tests/cpu-tests/tests/hello-str.c @@ -3,14 +3,15 @@ char buf[128]; int main() { - sprintf(buf, "%s", "Hello world!\n"); - check(strcmp(buf, "Hello world!\n") == 0); + sprintf(buf, "%s", "Hello world!\n"); + check(strcmp(buf, "Hello world!\n") == 0); - sprintf(buf, "%d + %d = %d\n", 1, 1, 2); - check(strcmp(buf, "1 + 1 = 2\n") == 0); + sprintf(buf, "%d + %d = %d\n", 1, 1, 2); + check(strcmp(buf, "1 + 1 = 2\n") == 0); - sprintf(buf, "%d + %d = %d\n", 2, 10, 12); - check(strcmp(buf, "2 + 10 = 12\n") == 0); + sprintf(buf, "%d + %d = %d\n", 2, 10, 12); + printf(buf); + check(strcmp(buf, "2 + 10 = 12\n") == 0); - return 0; + return 0; }