refactor: use cmake macros to do objcopy, install and tests
All checks were successful
Build nix packages / build-matrix (am-kernels) (push) Successful in 2m26s
Build nix packages / build-matrix (rv32Cross.am-kernels-nemu) (push) Successful in 2m23s
Build nix packages / build-matrix (rv32Cross.am-kernels-npc) (push) Successful in 2m21s

This commit is contained in:
xinyangli 2024-08-15 17:52:04 +08:00
parent f6c3a13e7f
commit 8630fe7667
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
12 changed files with 64 additions and 54 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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";

View file

@ -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"
},

View file

@ -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)

View file

@ -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;
}

View file

@ -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)

View file

@ -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)

View file

@ -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!");
}

View file

@ -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)

View file

@ -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()

View file

@ -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;
}