From 730189315283005605580922c2ffaf1de8f6bb30 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Wed, 9 Jun 2021 23:52:51 +0200 Subject: [PATCH 1/4] Add Nintendo Switch support through CMake --- .github/workflows/build-switch.yml | 26 ++++++++++++++++++++++++++ CMakeLists.txt | 2 ++ src/CMakeLists.txt | 24 ++++++++++++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/build-switch.yml diff --git a/.github/workflows/build-switch.yml b/.github/workflows/build-switch.yml new file mode 100644 index 0000000..8a1f4c0 --- /dev/null +++ b/.github/workflows/build-switch.yml @@ -0,0 +1,26 @@ +name: Build for Nintendo Switch using CMake provided by devkitPro +on: + pull_request: + push: + release: + types: published +jobs: + build-nintendo-switch: + runs-on: ubuntu-latest + container: devkitpro/devkita64:latest + steps: + - uses: actions/checkout@v2 + - name: "Build files" + run: | + /opt/devkitpro/portlibs/switch/bin/aarch64-none-elf-cmake -S. -Bbuild .. -DLIBRW_PLATFORM=GL3 -DLIBRW_GL3_GFXLIB=GLFW -DLIBRW_INSTALL=True + cmake --build build -j + - name: "Create binary package (cpack)" + working-directory: ./build + run: | + cpack + - name: "Archive binary package (github artifacts)" + uses: actions/upload-artifact@v2 + with: + name: "switch-gl3" + path: build/*.tar.xz + if-no-files-found: error diff --git a/CMakeLists.txt b/CMakeLists.txt index 81d3a43..190cfba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,8 @@ if(LIBRW_INSTALL) set(os "-apple") elseif(UNIX) set(os "-linux") + elseif(NINTENDO_SWITCH) + set(os "-switch") else() set(compiler "-UNK") message(WARNING "Unknown os. Created cpack package will be wrong. (override using cpack -P)") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aeb6afc..68c906a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,13 +148,16 @@ set_target_properties(librw ) if(LIBRW_PLATFORM_GL3) - set(OpenGL_GL_PREFERENCE GLVND) - find_package(OpenGL REQUIRED) + if(NOT NINTENDO_SWITCH) + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL REQUIRED) + + target_link_libraries(librw + PUBLIC + OpenGL::GL + ) + endif() - target_link_libraries(librw - PUBLIC - OpenGL::GL - ) if (LIBRW_GL3_GFXLIB STREQUAL "GLFW") find_package(glfw3 REQUIRED) target_link_libraries(librw @@ -175,6 +178,15 @@ if(LIBRW_PLATFORM_GL3) ) endif() endif() + + if(NINTENDO_SWITCH) + target_link_libraries(librw + PUBLIC + EGL + glapi + drm_nouveau + ) + endif() elseif(LIBRW_PLATFORM_D3D9) target_link_libraries(librw PUBLIC From 06a59173bc463f5c7bfe9463a40408061dfe5d48 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Thu, 17 Jun 2021 15:44:20 +0200 Subject: [PATCH 2/4] Add Switch NRO executables to Actions' artifacts --- tools/dumprwtree/CMakeLists.txt | 18 ++++++++++++++++++ tools/imguitest/CMakeLists.txt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tools/dumprwtree/CMakeLists.txt b/tools/dumprwtree/CMakeLists.txt index b2c35bc..f3fb388 100644 --- a/tools/dumprwtree/CMakeLists.txt +++ b/tools/dumprwtree/CMakeLists.txt @@ -12,3 +12,21 @@ if(LIBRW_INSTALL) RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) endif() + +if(NINTENDO_SWITCH) + nx_generate_nacp( dumprwtree.nacp + NAME "dumprwtree" + AUTHOR "aap" + VERSION "1.0.0" + ) + + nx_create_nro(dumprwtree + NACP dumprwtree.nacp + ) + + if(LIBRW_INSTALL) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/dumprwtree.nro" + DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) + endif() +endif() diff --git a/tools/imguitest/CMakeLists.txt b/tools/imguitest/CMakeLists.txt index 7cd5ed0..632388d 100644 --- a/tools/imguitest/CMakeLists.txt +++ b/tools/imguitest/CMakeLists.txt @@ -13,3 +13,21 @@ if(LIBRW_INSTALL) RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) endif() + +if(NINTENDO_SWITCH) + nx_generate_nacp( imguitest.nacp + NAME "librw imguitest" + AUTHOR "aap" + VERSION "1.0.0" + ) + + nx_create_nro(imguitest + NACP imguitest.nacp + ) + + if(LIBRW_INSTALL) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/imguitest.nro" + DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) + endif() +endif() From 03df7307bbccbd714f5516116a11632af6af54b2 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Thu, 17 Jun 2021 15:57:52 +0200 Subject: [PATCH 3/4] Add find_library checks for Switch libraries Makes sure that required Switch libraries are present before proceeding --- src/CMakeLists.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 68c906a..7c63e1d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -180,11 +180,15 @@ if(LIBRW_PLATFORM_GL3) endif() if(NINTENDO_SWITCH) + find_library(SWITCH_EGL_LIBRARY EGL) + find_library(SWITCH_GLAPI_LIBRARY glapi) + find_library(SWITCH_DRM_NOUVEAU_LIBRARY drm_nouveau) + target_link_libraries(librw PUBLIC - EGL - glapi - drm_nouveau + ${SWITCH_EGL_LIBRARY} + ${SWITCH_GLAPI_LIBRARY} + ${SWITCH_DRM_NOUVEAU_LIBRARY} ) endif() elseif(LIBRW_PLATFORM_D3D9) From 44a619a6e6c84b2a87a526f6262509ef659ec35d Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Jun 2021 23:44:07 +0200 Subject: [PATCH 4/4] cmake+nx: make binary tarball relocatable --- .github/workflows/build-cmake-conan.yml | 2 +- .github/workflows/build-switch.yml | 2 +- CMakeLists.txt | 26 +++++++++++++++- cmake/librw-config.cmake.in | 10 +++++-- cmake/nxtoolchain/CheckNXFunctions.cmake | 7 +++++ cmake/nxtoolchain/FindNXGL.cmake | 38 ++++++++++++++++++++++++ src/CMakeLists.txt | 11 ++----- tools/dumprwtree/CMakeLists.txt | 6 ++-- tools/imguitest/CMakeLists.txt | 6 ++-- 9 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 cmake/nxtoolchain/CheckNXFunctions.cmake create mode 100644 cmake/nxtoolchain/FindNXGL.cmake diff --git a/.github/workflows/build-cmake-conan.yml b/.github/workflows/build-cmake-conan.yml index 092e1a6..78bed1d 100644 --- a/.github/workflows/build-cmake-conan.yml +++ b/.github/workflows/build-cmake-conan.yml @@ -62,7 +62,7 @@ jobs: conan export cmake/ps2toolchain ps2dev-cmaketoolchain/master@ - name: "Download/build dependencies (conan install)" run: | - conan install ${{ github.workspace }} librw/master@ -if build -o platform=${{ matrix.platform }} -o gl3_gfxlib=${{ matrix.gl3_gfxlib}} --build missing -pr:h ./host_profile -pr:b default + conan install ${{ github.workspace }} librw/master@ -if build -o librw:platform=${{ matrix.platform }} -o librw:gl3_gfxlib=${{ matrix.gl3_gfxlib}} --build missing -pr:h ./host_profile -pr:b default env: CONAN_SYSREQUIRES_MODE: enabled - name: "Build librw (conan build)" diff --git a/.github/workflows/build-switch.yml b/.github/workflows/build-switch.yml index 8a1f4c0..faa5865 100644 --- a/.github/workflows/build-switch.yml +++ b/.github/workflows/build-switch.yml @@ -13,7 +13,7 @@ jobs: - name: "Build files" run: | /opt/devkitpro/portlibs/switch/bin/aarch64-none-elf-cmake -S. -Bbuild .. -DLIBRW_PLATFORM=GL3 -DLIBRW_GL3_GFXLIB=GLFW -DLIBRW_INSTALL=True - cmake --build build -j + cmake --build build --parallel - name: "Create binary package (cpack)" working-directory: ./build run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 190cfba..417c77a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,23 @@ cmake_minimum_required(VERSION 3.8) -project(librw C CXX) +project(librw + VERSION 0.0.1 + LANGUAGES C CXX +) +set(librw_AUTHOR aap) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") if(WIN32) set(LIBRW_PLATFORMS "NULL" "GL3" "D3D9") + set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL ON) +elseif(NINTENDO_SWITCH) + set(LIBRW_PLATFORMS "NULL" "GL3") + set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL OFF) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/nxtoolchain") + include(CheckNXFunctions) else() set(LIBRW_PLATFORMS "NULL" "GL3" "PS2") + set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL ON) endif() set(LIBRW_PLATFORM "NULL" CACHE STRING "Platform") set_property(CACHE LIBRW_PLATFORM PROPERTY STRINGS ${LIBRW_PLATFORMS}) @@ -60,6 +71,19 @@ if(LIBRW_INSTALL) DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) + if(LIBRW_GL3_GFXLIB STREQUAL "SDL2") + install( + FILES "${CMAKE_CURRENT_LIST_DIR}/cmake/FindSDL2.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake" + ) + endif() + if(NINTENDO_SWITCH) + install( + FILES "${CMAKE_CURRENT_LIST_DIR}/cmake/nxtoolchain/FindNXGL.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake" + ) + endif() + string(REPLACE "." ";" cmake_c_compiler_version_list "${CMAKE_C_COMPILER_VERSION}") list(GET cmake_c_compiler_version_list 0 cmake_c_compiler_version_major) diff --git a/cmake/librw-config.cmake.in b/cmake/librw-config.cmake.in index 67dd050..6ea7619 100644 --- a/cmake/librw-config.cmake.in +++ b/cmake/librw-config.cmake.in @@ -3,13 +3,19 @@ include("${CMAKE_CURRENT_LIST_DIR}/librw-targets.cmake") set(LIBRW_PLATFORM "@LIBRW_PLATFORM@") set(LIBRW_PLATFORMS "@LIBRW_PLATFORMS@") set(LIBRW_PLATFORM_@LIBRW_PLATFORM@ ON) +set(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL @LIBRW_PLATFORM_GL3_REQUIRES_OPENGL@) if(LIBRW_PLATFORM_GL3) set(LIBRW_GL3_GFXLIB "@LIBRW_GL3_GFXLIB@") set(LIBRW_GL3_GFXLIBS "@LIBRW_GL3_GFXLIBS@") - set(OpenGL_GL_PREFERENCE GLVND) - find_package(OpenGL REQUIRED) + if(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL) + set(OpenGL_GL_PREFERENCE GLVND) + find_package(OpenGL REQUIRED) + endif() + if(NINTENDO_SWITCH) + find_package(NXGL REQUIRED) + endif() if(LIBRW_GL3_GFXLIB STREQUAL "GLFW") find_package(glfw3 REQUIRED) elseif(LIBRW_GL3_GFXLIB STREQUAL "SDL2") diff --git a/cmake/nxtoolchain/CheckNXFunctions.cmake b/cmake/nxtoolchain/CheckNXFunctions.cmake new file mode 100644 index 0000000..8fa23fa --- /dev/null +++ b/cmake/nxtoolchain/CheckNXFunctions.cmake @@ -0,0 +1,7 @@ +if(NOT COMMAND nx_generate_nacp) + message(FATAL_ERROR "The `nx_generate_nacp` cmake command is not available. Please use an appropriate Nintendo Switch toolchain.") +endif() + +if(NOT COMMAND nx_create_nro) + message(FATAL_ERROR "The `nx_create_nro` cmake command is not available. Please use an appropriate Nintendo Switch toolchain.") +endif() diff --git a/cmake/nxtoolchain/FindNXGL.cmake b/cmake/nxtoolchain/FindNXGL.cmake new file mode 100644 index 0000000..25e86d2 --- /dev/null +++ b/cmake/nxtoolchain/FindNXGL.cmake @@ -0,0 +1,38 @@ +find_library(NXGL_EGL_LIBRARY EGL) +find_library(NXGL_GLAPI_LIBRARY glapi) +find_library(NXGL_DRM_NOUVEAU_LIBRARY drm_nouveau) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(NXGL + REQUIRED_VARS NXGL_EGL_LIBRARY NXGL_GLAPI_LIBRARY NXGL_DRM_NOUVEAU_LIBRARY +) + +if(NXGL_FOUND) + if(NOT TARGET NXGL::EGL) + add_library(NXGL::EGL UNKNOWN IMPORTED) + set_target_properties(NXGL::EGL PROPERTIES + IMPORTED_LOCATION "${NXGL_EGL_LIBRARY}" + ) + endif() + + if(NOT TARGET NXGL::glapi) + add_library(NXGL::glapi UNKNOWN IMPORTED) + set_target_properties(NXGL::glapi PROPERTIES + IMPORTED_LOCATION "${NXGL_GLAPI_LIBRARY}" + ) + endif() + + if(NOT TARGET NXGL::drm_nouveau) + add_library(NXGL::drm_nouveau UNKNOWN IMPORTED) + set_target_properties(NXGL::drm_nouveau PROPERTIES + IMPORTED_LOCATION "${NXGL_DRM_NOUVEAU_LIBRARY}" + ) + endif() + + if(NOT TARGET NXGL::OpenGL) + add_library(NXGL::OpenGL INTERFACE IMPORTED) + set_target_properties(NXGL::OpenGL PROPERTIES + INTERFACE_LINK_LIBRARIES "NXGL::EGL;NXGL::glapi;NXGL::drm_nouveau" + ) + endif() +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7c63e1d..423883a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,7 +148,7 @@ set_target_properties(librw ) if(LIBRW_PLATFORM_GL3) - if(NOT NINTENDO_SWITCH) + if(LIBRW_PLATFORM_GL3_REQUIRES_OPENGL) set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) @@ -180,15 +180,10 @@ if(LIBRW_PLATFORM_GL3) endif() if(NINTENDO_SWITCH) - find_library(SWITCH_EGL_LIBRARY EGL) - find_library(SWITCH_GLAPI_LIBRARY glapi) - find_library(SWITCH_DRM_NOUVEAU_LIBRARY drm_nouveau) - + find_package(NXGL REQUIRED) target_link_libraries(librw PUBLIC - ${SWITCH_EGL_LIBRARY} - ${SWITCH_GLAPI_LIBRARY} - ${SWITCH_DRM_NOUVEAU_LIBRARY} + NXGL::OpenGL ) endif() elseif(LIBRW_PLATFORM_D3D9) diff --git a/tools/dumprwtree/CMakeLists.txt b/tools/dumprwtree/CMakeLists.txt index f3fb388..96b05e7 100644 --- a/tools/dumprwtree/CMakeLists.txt +++ b/tools/dumprwtree/CMakeLists.txt @@ -14,10 +14,10 @@ if(LIBRW_INSTALL) endif() if(NINTENDO_SWITCH) - nx_generate_nacp( dumprwtree.nacp + nx_generate_nacp(dumprwtree.nacp NAME "dumprwtree" - AUTHOR "aap" - VERSION "1.0.0" + AUTHOR "${librw_AUTHOR}" + VERSION "${librw_VERSION}" ) nx_create_nro(dumprwtree diff --git a/tools/imguitest/CMakeLists.txt b/tools/imguitest/CMakeLists.txt index 632388d..293a4f1 100644 --- a/tools/imguitest/CMakeLists.txt +++ b/tools/imguitest/CMakeLists.txt @@ -15,10 +15,10 @@ if(LIBRW_INSTALL) endif() if(NINTENDO_SWITCH) - nx_generate_nacp( imguitest.nacp + nx_generate_nacp(imguitest.nacp NAME "librw imguitest" - AUTHOR "aap" - VERSION "1.0.0" + AUTHOR "${librw_AUTHOR}" + VERSION "${librw_VERSION}" ) nx_create_nro(imguitest