tutorial:cmake

Project tree

This is a typical directory structure, but it can be different if you need it to be.

  • src/ - A place for your .cpp/.c files
  • include/ - For .hpp/.h files
  • build/ or build/platform - CMake junk and your executables and EBOOT

Some other popular directories are tests/ and some-platform/. These can be useful if you are making a game for multiple platforms.

Writing CMakeLists.txt

Friendly reminder: While Ctrl+C Ctrl+V dont forget to look at what you are copying to make sure you don't have template values in your files.

Also if you are lost you can look it up on the CMake reference or by clicking on the CMake function.

There is nothing much different here. In the root file add CMake boilerplate.

cmake_minimum_required(VERSION 3.10)
 
project(psp-cmake
  VERSION 0.0
  DESCRIPTION "CMake PSP template project"
  HOMEPAGE_URL "https://psp-dev.org/doku.php?id=cmake"
  LANGUAGES CXX)

Then add your include directory. Could be multiple directories.

include_directories(include)

Add paths to all your other CMakeLists.txt.

add_subdirectory(src)

If you need to share some code between multiple executables (e.g. tests, PSP and desktop version) then make that shared code into a library. If its just a shared piece of code that you don't want to compile 2 times on the same platform then use OBJECT library. Also use object libraries if you need that code to be inside EBOOT.PBP on PSP version.

add_library(library-name OBJECT
  foo.cpp
  bar.cpp)

Add your executables. And those are not the same foo and bar as in add_library(), they are just placeholder names.

add_executable(game-name
  foo.cpp
  bar.cpp)

Next link your libraries to executables.

target_link_libraries(game_name
  PUBLIC library-name
  PUBLIC maybe-another-library)

Make an EBOOT. Here we use a simple trick to write once, but use the same CMakeLists on multiple platforms. By the way your EBOOT will be in build/[path to file that has code below]

add_executable(your-game
  main.cpp)
 
# No PSP libraries? Then no PSP specific building!
if (DEFINED PSP_LIBRARIES)
  target_link_libraries(psp 
    PUBLIC stdc++
    PUBLIC ${PSP_LIBRARIES})
 
  create_pbp_file(TARGET your-game # CMake executable target
    TITLE "${CMAKE_PROJECT_NAME}" # displayed in game selection
    # ICON_PATH
    # BACKGROUND_PATH
    # PREVIEW_PATH
    )
endif()

We are done with CMake configs now.

Building

Usually you initialize your CMake project with cmake …, but its different for Sony PSP.

psp-cmake -S . -B build/ # if you need more than one platform then use build/psp

No matter if you are building for Sony PSP or for desktop its always the same command, just different paths inside build/

cmake --build build/psp   # this is the path you specified in psp-cmake -S . -B build/psp
cmake --build build/linux # and this for a regular desktop build, if you set up one

At the same location as your CMakeLists.txt with create_pbp_file(), just under build/. So if that CMakeLists.txt is in src/ then EBOOT.PBP is in build/src.

  • tutorial/cmake.txt
  • Last modified: 2021/08/30 07:51
  • by max_ishere