PS4 CMake toolchain

About a month ago veteran PS scener @psxdev also known as bigboss released liborbis, liborbis is a collection of libraries for drawning, gamepad and audio utility for the PS4 to ease the development of homebrews.

Since then, I''ve been wanting to do something productive as such I start looking into getting CMake working for the PS4 and for good reasons:

  1. CMake is a popular mutiplatform build system, thus it''s possible we''d be able to compile other CMake projects for the PS4 (hopefully with minimal changes)
  2. I didn''t like how Hito''s PS4SDK just included ALL your source code (*.c) file into the build
  3. I worked with cmake before and I like how it''s really easy to add other cmake projects libraries to you own project.

on Note 1, I''ve been successful in compiling freetype2 library directly from upstream and I''ll be creating a new blog post about that soonTM but to any devs out there you can have a look at the wrapper i wrote for it libPS4freetype2

as such I decided to go for it, the process was fairly simple, I just had to look at Hito''s PS4SDK flags that are used during compiling and use them to setup the toolchain and this was the result.

Note

I build clang from source and I install it to $PS4SDK/usr so while I''m at it, i also tend to softlink $PS4SDK/include & $PS4SDK/lib to $PS4SDK/usr/include & $PS4SDK/usr/lib respectively.
As I just find it much easier to have everything under one directory, so my sysroot can be set to $PS4SDK/usr.
(Side note, I''m also contemplating building and installing cmake to to sysroot so that the toolchain will be completely self contained and isolated from any system files.. aka headers, programs, libraries etcs)

for veteran developers bigboss provided a set of instructions.
for beginners, I will create a blog post in the next few days detailing exactly how to setup your toolchain like mine and this will be needed if you want to use the provided cmake toolchain as-is but even now it''s a small change so it''s nothing to really worry about.

How-To-Use

  • for veterans cmake users
    • requisite
      • hito''s PS4SDK
      • environment variable PS4SDK set to your PS4SDK folder
      • clang installed to $PS4SDK/usr
      • cmake
    • use -DCMAKE_TOOLCHAIN_FILE=/location/to/PS4.toolchain.cmake (personally I have it saved at $PS4SDK/cmake/PS4.toolchain.cmake) during cmake build setup
    • link against PS4::LIBS and PS4::HEADERS which will link against the default PS4 libraries and would also setup your include directories.

but if you''re new to the entire cmake system keep reading as I''ll provide a basic sample setup to get you started.

  • for beginners cmake users
    • requisites
      • Linux based OS e.g Ubuntu
        • you can also use OSX but then you must build clang from source (instructions from bigboss, Note: my setup is slightly different and if you dont know what your doing, I recommand you wait for my blog post)
      • clang compiler (on Ubuntu the default one would most likely work, however as i stated earlier I tend to build mine from source)
      • Hito''s PS4SDK (maybe a blog post on how to set it up?) with your enviromental variable PS4SDK pointing to it
    • download PS4-CMake-Toolchain
      • edit lines 10 & 11 and remove /usr
        • if you followed my blog post (Link to be added in few days), please don''t edit those lines
      • optional: save the toolchain to $PS4SDK/cmake/PS4.toolchain.cmake
    • create a new folder for your app e.g PS4Test
    • inside PS4Test folder create these new folders and call them source, include & build
    • inside source create main.c which will contain your code
    • go back to your root project directory PS4Test and create a file called CMakeLists.txt
    cmake_minimum_required(VERSION 3.0.0)
    project(PS4Test)
    
    set(src
        ./source/main.c
    )
    
    # optional: we dont currently any headers
    # and even if we did, we dont have to add them, but I prefer to do so, more on that at the bottom of this post
    set(headers
        ./include/main.h
    )
    
    add_executable(PS4App ${src} ${headers})
    target_link_libraries(PS4App PS4::LIBS PS4::HEADERS)
    
    
    • go back to PS4Test open build
    • inside build run the following command cmake -DCMAKE_TOOLCHAIN_FILE=/location/to/PS4.toolchain.cmake ..
    • the .. refers to the location of CMakeLists.txt
    • run cmake --build . to start the build
    • congradulation, you just compiled your 1st app with cmake

On ubuntu I use Qt Creator to work with CMake projects as it''s able to parse CMakeLists.txt, Qt Creator is actually now smart enough to be able to auto detect your headers and display them on the side navigation view, but it wasn''t before, as such you had to include the headers as part of your build, now you don''t but as a personal now, I still do it.

Notes

I''ve noticed that the archiver ar used tend to be from the system installation, you''d get away with this on Ubuntu but not OSX.
My quick workaround until I find a better solution was to edit the toolchain file and adding set(CMAKE_AR "$ENV{PS4SDK}/usr/bin/ar" CACHE STRING "Archiver") just after set(CMAKE_CXX_COMPILER "clang++")