Skip to content
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

[WIP/RFC] Cmake #664

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.10)
project(ps2sdk)
include(ExternalProject)

ExternalProject_Add(tools
SOURCE_DIR ${PROJECT_SOURCE_DIR}/tools
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/tools
BUILD_ALWAYS true)

ExternalProject_Add(ee
SOURCE_DIR ${PROJECT_SOURCE_DIR}/ee
CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${PROJECT_SOURCE_DIR}/cmake/ee_toolchain.cmake -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/ee
BUILD_ALWAYS true)

#ExternalProject_Add(iop
# SOURCE_DIR ${PROJECT_SOURCE_DIR}/iop
# CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${PROJECT_SOURCE_DIR}/cmake/iop_toolchain.cmake -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/iop
# BUILD_ALWAYS true)
41 changes: 41 additions & 0 deletions cmake/ee_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# CMake platform file for PS2 EE processor
#
# Copyright (C) 2009-2010 Mathias Lafeldt <[email protected]>
# Copyright (C) 2023 Francisco Javier Trujillo Mata <[email protected]>
# Copyright (C) 2024 André Guilherme <[email protected]>
# Copyright (C) 2024-Present PS2DEV Team
#

cmake_minimum_required(VERSION 3.10)

INCLUDE(CMakeForceCompiler)


SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR mips)

SET(CMAKE_ASM_COMPILER mips64r5900el-ps2-elf-gcc)
SET(CMAKE_C_COMPILER mips64r5900el-ps2-elf-gcc)
SET(CMAKE_CXX_COMPILER mips64r5900el-ps2-elf-g++)

find_program(CMAKE_OBJCOPY mips64r5900el-ps2-elf-objcopy)

SET(EE_CFLAGS "-D_EE -G0 -O2 -Wall -Werror -gdwarf-2 -gz" CACHE STRING "EE C compiler flags" FORCE)
SET(EE_LDFLAGS "-Wl,-zmax-page-size=128" CACHE STRING "EE linker flags" FORCE)

SET(CMAKE_TARGET_INSTALL_PREFIX $ENV{PS2DEV}/ports)

SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)

SET(CMAKE_C_FLAGS_INIT ${EE_CFLAGS})
SET(CMAKE_CXX_FLAGS_INIT ${EE_CFLAGS})
SET(CMAKE_EXE_LINKER_FLAGS_INIT ${EE_LDFLAGS})


SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-nostartfiles -Wl,-r -Wl,-d")

SET(PS2 TRUE)
SET(PLATFORM_PS2 TRUE)
SET(EE TRUE)
53 changes: 53 additions & 0 deletions cmake/iop_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# CMake platform file for PS2 EE processor
#
# Copyright (C) 2009-2010 Mathias Lafeldt <[email protected]>
# Copyright (C) 2023 Francisco Javier Trujillo Mata <[email protected]>
# Copyright (C) 2024 André Guilherme <[email protected]>
# Copyright (C) 2024-Present PS2DEV Team
#

cmake_minimum_required(VERSION 3.0)

INCLUDE(CMakeForceCompiler)
if(DEFINED ENV{PS2SDK})
SET(PS2SDK $ENV{PS2SDK})
else()
message(FATAL_ERROR "The environment variable PS2SDK needs to be defined.")
endif()

if(DEFINED ENV{PS2DEV})
SET(PS2DEV $ENV{PS2DEV})
else()
message(FATAL_ERROR "The environment variable PS2DEV needs to be defined.")
endif()

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR mips)

SET(CMAKE_C_COMPILER mips64r5900el-ps2-elf-gcc)
SET(CMAKE_CXX_COMPILER mips64r5900el-ps2-elf-g++)

SET(EE_CFLAGS "-I$ENV{PS2SDK}/ee/include -I$ENV{PS2SDK}/common/include -I$ENV{PS2SDK}/ports/include -D_EE -DPS2 -D__PS2__ -O2 -G0" CACHE STRING "EE C compiler flags" FORCE)
SET(EE_LDFLAGS "-L$ENV{PS2SDK}/ee/lib -L$ENV{PS2DEV}/gsKit/lib -L$ENV{PS2SDK}/ports/lib -Wl,-zmax-page-size=128 -T$ENV{PS2SDK}/ee/startup/linkfile" CACHE STRING "EE linker flags" FORCE)

SET(CMAKE_TARGET_INSTALL_PREFIX $ENV{PS2DEV}/ports)

SET(CMAKE_FIND_ROOT_PATH $ENV{PS2DEV} $ENV{PS2DEV}/ee $ENV{PS2DEV}/ee/ee $ENV{PS2SDK} $ENV{PS2SDK}/ports)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)

SET(CMAKE_C_FLAGS_INIT ${EE_CFLAGS})
SET(CMAKE_CXX_FLAGS_INIT ${EE_CFLAGS})
SET(CMAKE_EXE_LINKER_FLAGS_INIT ${EE_LDFLAGS})


SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-nostartfiles -Wl,-r -Wl,-d")

SET(PS2 TRUE)
SET(PLATFORM_PS2 TRUE)
SET(EE TRUE)
43 changes: 43 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# TODO investigate using interface libraries, will help with erl generation
# Compiles the same source file multiple times with different defines
function(compile_multiple target srcfile)
cmake_parse_arguments(PARSE_ARGV 2 "arg" "" "" "OBJECTS")

foreach(obj ${arg_OBJECTS})
add_library(${obj} OBJECT ${srcfile})
get_filename_component(def ${obj} NAME_WLE)
target_compile_definitions(${obj} PRIVATE "F_${def}")

get_target_property(target_id ${target} INCLUDE_DIRECTORIES)
target_include_directories(${obj} PRIVATE ${target_id})

target_link_libraries(${target} PRIVATE ${obj})
endforeach()
endfunction()

# Add an erl output for a given target
function(target_add_erl target)
add_custom_command(OUTPUT "lib${target}.erl"
COMMAND ${CMAKE_C_COMPILER} -nostdlib -Wl,-r -Wl,-d -o "lib${target}.erl" $<TARGET_OBJECTS:${target}>
COMMAND ${CMAKE_STRIP} --strip-unneeded -R .mdebug.eabi64 -R .reginfo -R .comment lib${target}.erl
DEPENDS ${target}
COMMAND_EXPAND_LISTS
)
add_custom_target(${target}_erl ALL
DEPENDS lib${target}.erl
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${target}.erl
DESTINATION lib
)
endfunction()

# Generates a C array of the binary output of a target
# objcopy -Obinary <elf> <bin> && bin2c <bin> <output_name.c> <target_name>
macro(bin_include from_target output_name)
add_custom_command(OUTPUT "${output_name}.c"
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${from_target}> "${output_name}.bin"
COMMAND bin2c "${output_name}.bin" "${output_name}.c" "${from_target}"
BYPRODUCTS "${output_name}.bin"
DEPENDS ${from_target}
)
endmacro()
34 changes: 34 additions & 0 deletions ee/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.10)
project(ps2sdk-ee C ASM)
file(REAL_PATH ../ PS2SDKSRC_ROOT)

set(EE_INC
${PS2SDKSRC_ROOT}/common/include
${PROJECT_SOURCE_DIR}/kernel/include
${PROJECT_SOURCE_DIR}/erl/include
)

include(${PS2SDKSRC_ROOT}/cmake/utils.cmake)

add_subdirectory(debug)
add_subdirectory(dma)
add_subdirectory(draw)
add_subdirectory(eedebug)
add_subdirectory(elf-loader)
add_subdirectory(erl)
add_subdirectory(font)
add_subdirectory(graph)
add_subdirectory(input)
add_subdirectory(kernel)
add_subdirectory(libcglue)
add_subdirectory(libgs)
add_subdirectory(libprofglue)
add_subdirectory(libvux)
add_subdirectory(math3d)
add_subdirectory(mpeg)
add_subdirectory(network)
add_subdirectory(packet)
add_subdirectory(packet2)
add_subdirectory(rpc)
add_subdirectory(sbv)
add_subdirectory(startup)
17 changes: 17 additions & 0 deletions ee/debug/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_library(debug)
target_include_directories(debug PUBLIC include)
target_include_directories(debug PRIVATE ${PS2SDKSRC_ROOT}/common/include)
target_link_libraries(debug PRIVATE kernel)
target_sources(debug PRIVATE
src/callstack.c
src/callstackget.S
#src/erl-support.c TODO
src/font.c
src/hwbp.S
src/scr_printf.c
src/screenshot.c
)
file(GLOB DEBUG_INCLUDE_FILES "include/*.h*")
set_target_properties(debug PROPERTIES PUBLIC_HEADER "${DEBUG_INCLUDE_FILES}")

install(TARGETS debug)
8 changes: 8 additions & 0 deletions ee/dma/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(dma)
target_include_directories(dma PUBLIC include)
target_include_directories(dma PRIVATE ${EE_INC} ${PROJECT_SOURCE_DIR}/packet2/include)
target_sources(dma PRIVATE src/dma.c src/erl-support.c)
file(GLOB DMA_INCLUDE_FILES "include/*.h*")
set_target_properties(dma PROPERTIES PUBLIC_HEADER "${DMA_INCLUDE_FILES}")
install(TARGETS dma)
target_add_erl(dma)
19 changes: 19 additions & 0 deletions ee/draw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
add_library(draw)
target_include_directories(draw PUBLIC include)
target_include_directories(draw PRIVATE
${EE_INC}
)
target_link_libraries(draw PRIVATE math3d dma)
target_sources(draw PRIVATE
src/draw_environment.c
src/draw.c
src/draw2d.c
src/draw3d.c
src/erl-support.c
)

file(GLOB DRAW_INCLUDE_FILES "include/*.h*")
set_target_properties(draw PROPERTIES PUBLIC_HEADER "${DRAW_INCLUDE_FILES}")
install(TARGETS draw)

target_add_erl(draw)
18 changes: 18 additions & 0 deletions ee/eedebug/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_library(eedebug)
target_include_directories(eedebug PUBLIC include)
target_include_directories(eedebug PRIVATE
${EE_INC}
)

target_sources(eedebug PRIVATE
src/ee_dbg_low.S
src/ee_debug.c
src/ee_exceptions.S
src/erl-support.c
)

file(GLOB eedebug_INCLUDE_FILES "include/*.h*")
set_target_properties(eedebug PROPERTIES PUBLIC_HEADER "${eedebug_INCLUDE_FILES}")
install(TARGETS eedebug)

target_add_erl(eedebug)
19 changes: 19 additions & 0 deletions ee/elf-loader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
add_subdirectory(src/loader)

add_library(elf-loader)
target_include_directories(elf-loader PUBLIC include)
target_include_directories(elf-loader PRIVATE
${EE_INC}
)

bin_include(loader loader_bin)
target_sources(elf-loader PRIVATE
src/elf.c
loader_bin.c
)

file(GLOB elf-loader_INCLUDE_FILES "include/*.h*")
set_target_properties(elf-loader PROPERTIES PUBLIC_HEADER "${elf-loader_INCLUDE_FILES}")
install(TARGETS elf-loader)

target_add_erl(elf-loader)
5 changes: 5 additions & 0 deletions ee/elf-loader/src/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(loader)
target_sources(loader PRIVATE src/loader.c)
target_include_directories(loader PRIVATE ${EE_INC})
target_link_libraries(loader PRIVATE kernel cglue)
target_link_options(loader PRIVATE -T${CMAKE_CURRENT_SOURCE_DIR}/linkfile)
4 changes: 4 additions & 0 deletions ee/erl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_library(erl)
target_include_directories(erl PUBLIC include)
target_include_directories(erl PRIVATE ${EE_INC})
target_sources(erl PRIVATE src/erl.c src/hashtab.c src/lookupa.c src/recycle.c)
17 changes: 17 additions & 0 deletions ee/font/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_library(font)
target_include_directories(font PUBLIC include)
target_include_directories(font PRIVATE
${EE_INC}
)
target_link_libraries(font PRIVATE draw math3d)
target_sources(font PRIVATE
src/fontx.c
src/fsfont.c
src/erl-support.c
)

file(GLOB FONT_INCLUDE_FILES "include/*.h*")
set_target_properties(font PROPERTIES PUBLIC_HEADER "${FONT_INCLUDE_FILES}")
install(TARGETS font)

target_add_erl(font)
19 changes: 19 additions & 0 deletions ee/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
add_library(graph)
target_include_directories(graph PUBLIC include)
target_include_directories(graph PRIVATE
${EE_INC}
)
target_link_libraries(graph PRIVATE cdvd)
target_sources(graph PRIVATE
src/graph_config.c
src/graph_mode.c
src/graph_vram.c
src/graph.c
src/erl-support.c
)

file(GLOB GRAPH_INCLUDE_FILES "include/*.h*")
set_target_properties(graph PROPERTIES PUBLIC_HEADER "${GRAPH_INCLUDE_FILES}")
install(TARGETS graph)

target_add_erl(graph)
36 changes: 36 additions & 0 deletions ee/input/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
add_library(input)
target_include_directories(input PUBLIC include)
target_include_directories(input PRIVATE
${EE_INC}
)

target_link_libraries(input PRIVATE pad)

target_sources(input PRIVATE
src/input.c
src/erl-support.c
)

file(GLOB INPUT_INCLUDE_FILES "include/*.h*")
set_target_properties(input PROPERTIES PUBLIC_HEADER "${INPUT_INCLUDE_FILES}")
install(TARGETS input)

target_add_erl(input)

# inputx
add_library(inputx)
target_include_directories(inputx PUBLIC include)
target_include_directories(inputx PRIVATE
${EE_INC}
)
target_compile_definitions(inputx PRIVATE _XINPUT)

target_link_libraries(inputx PRIVATE pad multitap)

target_sources(inputx PRIVATE
src/input.c
src/erl-support.c
)

install(TARGETS inputx)
target_add_erl(inputx)
Empty file added ee/iopreboot/CMakeLists.txt
Empty file.
Loading