build: build am for multiple platform in parallel

This commit is contained in:
xinyangli 2024-04-13 21:15:56 +08:00
parent 6ab5d4c156
commit a54c0d6480
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
19 changed files with 206 additions and 146 deletions

View file

@ -2,5 +2,7 @@
**/build/
**/.envrc
**/.cache
out/
.vscode
compile_commands.json
cmakeUserh

View file

@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 11)
include(CMakeDependentOption)
include(CMakePackageConfigHelpers) # Used to find libcheck
include(CTest)
include(GNUInstallDirs)
# -- General options
set(ISA CACHE STRING "Target ISA")
@ -17,12 +18,15 @@ string(TOUPPER ${ISA} ISA_UPPER)
cmake_dependent_option(
__PLATFORM_NEMU__ "Run on NEMU"
ON "ISA MATCHES \"(riscv | x86)\"" OFF)
cmake_dependent_option(
__PLATFORM_NPC__ "Run on NPC"
ON "ISA MATCHES riscv" OFF)
cmake_dependent_option(
__PLATFORM_NATIVE__ "Run on native"
ON "ISA MATCHES native" OFF)
# -- Set PLATFORM according to options
set(MATCH_PLATFORM_PATTERN "^__PLATFORM_([A-Z]*)__")
set(MATCH_PLATFORM_PATTERN "^__PLATFORM_([A-Z]*)__$")
get_cmake_property(CACHE_VARS CACHE_VARIABLES)
message(STATUS "ISA: ${ISA}")
@ -32,16 +36,27 @@ foreach(VAR IN LISTS CACHE_VARS)
get_property(VAR_VALUE CACHE ${VAR} PROPERTY VALUE)
set(PLATFORM_UPPER ${CMAKE_MATCH_1})
string(TOLOWER ${PLATFORM_UPPER} PLATFORM)
list(APPEND PLATFORMS ${PLATFORM})
message(STATUS "Variable: ${VAR}=${VAR_VALUE}, Platform: ${PLATFORM}")
endif()
endforeach()
if(${PLATFORM} MATCHES "native")
if((NOT PLATFORM) AND (NOT ISA MATCHES native))
message(FATAL_ERROR "Platform not given!")
endif()
set(SUPPORTED_ARCH "riscv-nemu" "riscv-npc" "native")
foreach(PLATFORM IN LISTS PLATFORMS)
if(${ISA} MATCHES "native")
set(ARCH "native")
else()
set(ARCH ${ISA}-${PLATFORM})
endif()
string(TOUPPER ${ARCH} ARCH_UPPER)
if(NOT ARCH IN_LIST SUPPORTED_ARCH)
message(FATAL_ERROR "Given ISA-PLATFORM (${ISA}-${PLATFORM}) does not match one of the following: ${SUPPORTED_ARCH}")
endif()
endforeach()
# -- Target specific options
cmake_dependent_option(
@ -49,39 +64,34 @@ cmake_dependent_option(
ON "NOT __ISA_NATIVE__" OFF)
# -- Add compile definitions based on options
add_compile_definitions(
$<MAKE_C_IDENTIFIER:__ARCH_${ARCH_UPPER}__>
__ISA_${ISA_UPPER}__
__PLATFORM_${PLATFORM_UPPER}__
)
add_compile_definitions(
$<$<BOOL:${NATIVE_USE_KLIB}>:__NATIVE_USE_KLIB__>
)
# -- Required compiler flags
add_compile_options(
# -Werror
-Wno-main
-fno-asynchronous-unwind-tables
-fno-builtin
-fno-stack-protector
-U_FORTIFY_SOURCE
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-ffreestanding>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
add_link_options(
-znoexecstack
)
# -- Include linker script here. Use this linker script at link time if INCLUDE_LINKER_SCRIPT is set to true
set(LINKER_SCRIPT linker.ld)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
add_compile_options(-march=rv32if -mabi=ilp32)
add_link_options(-march=rv32if -mabi=ilp32)
# NOTE: klib and am include header files in each other,
# so we need to create interface libraries for correct dependency
add_library(am_interface INTERFACE)
target_include_directories(am_interface INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/am/include>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/am/src>
$<INSTALL_INTERFACE:include/abstract-machine>)
target_compile_definitions(am_interface INTERFACE ARCH_H=<arch/${ISA}.h>)
add_library(klib_interface INTERFACE)
target_include_directories(klib_interface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/klib/include>
$<INSTALL_INTERFACE:include/abstract-machine>)
install(TARGETS am_interface klib_interface
EXPORT interfaceTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine)
install(EXPORT interfaceTargets
FILE interfaceTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
add_subdirectory(klib)
add_subdirectory(am)
# -- Test depends on klib and am should be added last.
add_subdirectory(klib/tests)

View file

@ -14,14 +14,14 @@
}
},
{
"name": "riscv-nemu",
"displayName": "Riscv32 NEMU",
"name": "riscv",
"displayName": "RV32 all platform",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"ISA": "riscv",
"__PLATFORM_NPC__": true,
"__PLATFORM_NEMU__": true
}
}

View file

@ -0,0 +1,15 @@
{
"version": 6,
"configurePresets": [
{
"name": "Native install",
"inherits": "native",
"installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install"
},
{
"name": "RISCV install",
"inherits": "riscv",
"installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install"
}
]
}

View file

@ -1,10 +1,26 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(am_interface INTERFACE)
target_include_directories(am_interface INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/abstract-machine>)
add_subdirectory(src)
install(DIRECTORY include/ DESTINATION include/abstract-machine)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine)
foreach(PLATFORM IN LISTS PLATFORMS)
if(ISA MATCHES "native")
set(ARCH "native")
else()
set(ARCH ${ISA}-${PLATFORM})
endif()
install(TARGETS am-${ARCH}
EXPORT amTargets-${ARCH}
LIBRARY DESTINATION lib)
install(EXPORT amTargets-${ARCH}
FILE amTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
configure_package_config_file(
${CMAKE_SOURCE_DIR}/cmake/am-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
endforeach()

View file

@ -1,53 +1 @@
if(ISA MATCHES "native")
set(SOURCEDIR "./${PLATFORM}")
else()
set(SOURCEDIR "./${ISA}/${PLATFORM}")
endif()
add_subdirectory(${SOURCEDIR})
target_include_directories(am-${ARCH}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:include/abstract-machine>)
target_link_libraries(am-${ARCH}
PUBLIC klib_interface
INTERFACE m)
# TODO: Check
target_link_options(am-${ARCH} INTERFACE
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/${LINKER_SCRIPT}>
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}/${LINKER_SCRIPT}>)
# Interface compile flags
target_link_options(am-${ARCH} INTERFACE
-znoexecstack)
target_compile_options(am-${ARCH} INTERFACE
-fno-asynchronous-unwind-tables
-fno-builtin
-fno-stack-protector
-U_FORTIFY_SOURCE
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-ffreestanding>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
install(TARGETS am-${ARCH} klib_interface am_interface
EXPORT amTargets
LIBRARY DESTINATION lib)
install(EXPORT amTargets
FILE amTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/am-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
# TODO: check
install(FILES ${CMAKE_SOURCE_DIR}/scripts/${LINKER_SCRIPT}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH})
add_subdirectory(${ISA})

View file

@ -1,6 +1,3 @@
include(CheckPIESupported)
check_pie_supported()
set(SOURCES
trap.S
cte.c
@ -23,4 +20,4 @@ set_target_properties(am-native PROPERTIES
INTERFACE_POSITION_INDEPENDENT_CODE TRUE)
find_package(SDL2 REQUIRED)
target_link_libraries(am-${ARCH} PUBLIC SDL2::SDL2)
target_link_libraries(am-native PUBLIC SDL2::SDL2 PRIVATE klib_interface am_interface)

View file

@ -0,0 +1,12 @@
foreach(PLATFORM IN LISTS PLATFORMS)
string(TOUPPER ${ARCH} ARCH_UPPER)
set(AM_COMMON_COMPILE_DEF
# -- Arch related
$<MAKE_C_IDENTIFIER:__ARCH_${ARCH_UPPER}__>
__ISA_${ISA_UPPER}__
__PLATFORM_${PLATFORM_UPPER}__
$<$<BOOL:${NATIVE_USE_KLIB}>:__NATIVE_USE_KLIB__>
)
add_subdirectory(${PLATFORM})
endforeach()

View file

@ -1,7 +1,7 @@
include(nemu-settings)
include(riscv-settings)
add_library(am-${ISA}-nemu
add_library(am-riscv-nemu
cte.c
start.S
trap.S
@ -9,26 +9,44 @@ add_library(am-${ISA}-nemu
${NEMU_SOURCES}
)
target_compile_options(am-${ISA}-nemu PRIVATE
target_compile_options(am-riscv-nemu PRIVATE
${NEMU_COMPILE_OPTIONS}
${RISCV_COMPILE_OPTIONS})
target_link_options(am-${ISA}-nemu PRIVATE
target_link_options(am-riscv-nemu PRIVATE
${NEMU_LINK_OPITIONS}
${RISCV_LINK_OPTIONS})
target_include_directories(am-${ISA}-nemu PRIVATE
target_include_directories(am-riscv-nemu PRIVATE
${NEMU_INCLUDE_DIRECTORIES})
target_link_options(am-${ISA}-nemu INTERFACE
target_link_options(am-riscv-nemu INTERFACE
LINKER:--defsym=_pmem_start=0x80000000
LINKER:--defsym=_entry_offset=0x0
LINKER:--gc-sections
LINKER:-e _start
-nostartfiles)
target_compile_definitions(am-${ISA}-nemu PUBLIC
ARCH_H="arch/riscv.h")
target_compile_definitions(am-${ISA}-nemu PRIVATE
ISA_H="riscv/riscv.h")
target_link_options(am-riscv-nemu INTERFACE
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
set_target_properties(am-${ISA}-nemu PROPERTIES
target_include_directories(am-riscv-nemu
PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/am/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine>)
target_link_libraries(am-riscv-nemu
PUBLIC am_interface klib_interface
INTERFACE m)
target_compile_definitions(am-riscv-nemu PRIVATE
ISA_H=<riscv/riscv.h>)
set_target_properties(am-riscv-nemu PROPERTIES
POSITION_INDEPENDENT_CODE OFF
INTERFACE_POSITION_INDEPENDENT_CODE OFF)
install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld
DESTINATION ${CMAKE_INSTALL_DATADIR})

View file

@ -0,0 +1,39 @@
include(riscv-settings)
add_subdirectory(libgcc)
add_library(am-riscv-npc
cte.c
input.c
ioe.c
mpe.c
start.S
timer.c
trap.S
trm.c
vme.c
)
target_link_libraries(am-riscv-npc PRIVATE npcgcc PUBLIC am_interface klib_interface)
target_link_options(am-riscv-npc INTERFACE
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
target_link_options(am-riscv-npc INTERFACE
LINKER:--defsym=_pmem_start=0x80000000
LINKER:--defsym=_entry_offset=0x0
LINKER:--gc-sections
LINKER:-e _start
-nostartfiles)
target_link_options(am-riscv-npc INTERFACE
$<BUILD_INTERFACE:-T${CMAKE_SOURCE_DIR}/scripts/linker.ld>
$<INSTALL_INTERFACE:-T${CMAKE_INSTALL_FULL_DATADIR}/linker.ld>)
target_compile_definitions(am-riscv-npc
PUBLIC ${AM_COMMON_COMPILE_DEF} ARCH_H=<arch/riscv.h>
)
install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld
DESTINATION ${CMAKE_INSTALL_DATADIR})

View file

@ -0,0 +1,14 @@
add_library(npcgcc
ashldi3.c
div.S
muldi3.S
multi3.c
unused.c
)
target_link_libraries(npcgcc PRIVATE klib_interface am_interface)
target_link_options(npcgcc INTERFACE -nolibc -nostdlib)
install(TARGETS npcgcc
EXPORT amTargets-riscv-npc
LIBRARY DESTINATION lib)

View file

@ -14,11 +14,12 @@ Area heap = RANGE(&_heap_start, PMEM_END);
#endif
static const char mainargs[] = MAINARGS;
void putch(char ch) {
}
void putch(char ch) {}
void halt(int code) {
while (1);
asm volatile("mv a0, %0; ebreak" : : "r"(code));
while (1)
;
}
void _trm_init() {

View file

@ -6,4 +6,5 @@ find_dependency(SDL2 REQUIRED)
endif()
# Include the targets file
include("${CMAKE_CURRENT_LIST_DIR}/../interfaceTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/amTargets.cmake")

View file

@ -3,7 +3,7 @@
cmake,
SDL2,
isa ? "native",
platform ? "NEMU"
platform ? [ ]
}:
stdenv.mkDerivation {
pname = "abstract-machine";
@ -13,8 +13,7 @@ stdenv.mkDerivation {
cmakeFlags = [
(lib.cmakeFeature "ISA" isa)
(lib.cmakeBool "__PLATFORM_${lib.strings.toUpper platform}__" true)
];
] ++ map (p: (lib.cmakeBool "__PLATFORM_${lib.strings.toUpper p}__" true)) platform;
nativeBuildInputs = [
cmake
@ -22,5 +21,7 @@ stdenv.mkDerivation {
buildInputs = [
] ++ (if platform=="native" then [ SDL2 ] else [ ]);
] ++ (if isa=="native" then [ SDL2 ] else [ ]);
doCheck = true;
}

View file

@ -1,12 +1 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(klib_interface INTERFACE)
target_include_directories(klib_interface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/abstract-machine>)
add_subdirectory(src)
# add_subdirectory(tests)
install(DIRECTORY include/ DESTINATION include/abstract-machine)

View file

@ -13,8 +13,7 @@ set(SOURCES
)
add_library(klib ${SOURCES})
target_include_directories(klib PUBLIC $<TARGET_PROPERTY:am_interface,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(klib PUBLIC $<TARGET_PROPERTY:am-${ARCH},INTERFACE_COMPILE_DEFINITIONS>)
target_link_libraries(klib PRIVATE am_interface klib_interface)
install(TARGETS klib
EXPORT klibTargets

View file

@ -4,14 +4,11 @@ set(TEST_SOURCES
)
foreach(TEST IN LISTS TEST_SOURCES)
add_executable(${TEST} ${TEST}.c)
target_link_libraries(${TEST} am-${ARCH} klib m)
target_include_directories(${TEST}
PRIVATE $<TARGET_PROPERTY:am_interface,INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE $<TARGET_PROPERTY:klib_interface,INTERFACE_INCLUDE_DIRECTORIES>
)
# TODO: Run tests in other configurations
if(__PLATFORM_NATIVE__)
add_executable(${TEST} ${TEST}.c)
target_link_libraries(${TEST} PRIVATE am_interface klib_interface klib m)
target_link_libraries(${TEST} PRIVATE am-native)
add_test(NAME ${TEST} COMMAND ${TEST})
endif()
endforeach()

View file

@ -42,6 +42,8 @@
src = ./.;
hooks = {
trim-trailing-whitespace.enable = true;
end-of-file-fixer.enable = true;
cmake-format.enable = true;
clang-format = {
enable = true;
types_or = pkgs.lib.mkForce [ "c" "c++" ];
@ -52,7 +54,8 @@
packages.nemu = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels; };
packages.nemu-lib = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels; defconfig = "riscv32-lib_defconfig"; };
packages.abstract-machine = crossPkgs.callPackage ./abstract-machine { isa = "riscv"; platform = "nemu"; };
packages.abstract-machine = crossPkgs.callPackage ./abstract-machine { isa = "riscv"; platform = [ "nemu" "npc" ]; };
packages.abstract-machine-native = pkgs.callPackage ./abstract-machine { isa = "native"; };
packages.am-kernels = crossPkgs.stdenv.mkDerivation rec {
pname = "am-kernels-cmake";
@ -67,7 +70,6 @@
cmakeFlags = [
(pkgs.lib.cmakeFeature "ISA" "riscv")
(pkgs.lib.cmakeFeature "PLATFORM" "nemu")
(pkgs.lib.cmakeFeature "CMAKE_INSTALL_DATADIR" "share")
];
buildInputs = [
@ -87,13 +89,12 @@
IMAGES_PATH = "${self.packages.${system}.am-kernels}/share/binary";
};
devShells.npc = with pkgs; mkShell {
devShells.npc = with pkgs; mkShell.override { stdenv = ccacheStdenv; } {
inherit (self.checks.${system}.pre-commit-check) shellHook;
CHISEL_FIRTOOL_PATH = "${nixpkgs-circt162.legacyPackages.${system}.circt}/bin";
packages = [
clang-tools
cmake
ninja
coursier
espresso
bloop