From 44a619a6e6c84b2a87a526f6262509ef659ec35d Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Jun 2021 23:44:07 +0200 Subject: [PATCH] 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