From cf8b5805335f15a3b6b6cbb221b90ec490489a4e Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Wed, 18 May 2022 23:38:17 +0100 Subject: [PATCH 1/9] add cmake build support --- .gitignore | 3 +++ CMakeLists.txt | 17 ++++++++++++++ example/CMakeLists.txt | 25 +++++++++++++++++++++ source/CMakeLists.txt | 51 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 example/CMakeLists.txt create mode 100644 source/CMakeLists.txt diff --git a/.gitignore b/.gitignore index 457ec0d..76f93d1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ /libntfs-3g/pkg /liblwext4/src /liblwext4/pkg + +build +.cache diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..af366b5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.12) + +project(libusbhsfs + VERSION 0.2.6 + DESCRIPTION "USB Mass Storage Class Host + Filesystem Mounter static library for Nintendo Switch homebrew applications." + HOMEPAGE_URL "https://github.com/DarkMatterCore/libusbhsfs" + LANGUAGES C +) + +option(USBHSFS_GPL "ext4 and ntfs support" OFF) +option(USBHSFS_EXAMPLE "build example" OFF) + +add_subdirectory(source) + +if (USBHSFS_EXAMPLE) + add_subdirectory(example) +endif() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..6abc228 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.12) + +project(libusbhsfs-example + LANGUAGES C +) + +string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S" UTC) + +add_executable(libusbhsfs-example source/main.c) + +target_link_libraries(libusbhsfs-example PRIVATE libusbhsfs) +target_compile_definitions(libusbhsfs-example PRIVATE + APP_TITLE="libusbhsfs-example" + BUILD_TIMESTAMP="${BUILD_TIMESTAMP}" +) + +nx_generate_nacp(libusbhsfs-example.nacp + NAME "libusbhsfs-example" + AUTHOR "DarkMatterCore, XorTroll, Rhys Koedijk" + VERSION 0.0.1 +) + +nx_create_nro(libusbhsfs-example + NACP libusbhsfs-example.nacp +) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt new file mode 100644 index 0000000..45f62a1 --- /dev/null +++ b/source/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.12) + +add_library(libusbhsfs + usbhsfs_drive.c + usbhsfs_log.c + usbhsfs_manager.c + usbhsfs_mount.c + usbhsfs_request.c + usbhsfs_scsi.c + usbhsfs_utils.c +) + +target_include_directories(libusbhsfs PUBLIC ${CMAKE_SOURCE_DIR}/include) + +# fatfs stuff +target_sources(libusbhsfs PRIVATE + fatfs/diskio.c + fatfs/ff_dev.c + fatfs/ff.c + fatfs/ffsystem.c + fatfs/ffunicode.c +) + +# sxos stuff +target_sources(libusbhsfs PRIVATE + sxos/usbfs_dev.c + sxos/usbfs.c +) + +if (USBHSFS_GPL) + target_sources(libusbhsfs PRIVATE + lwext4/ext_dev.c + lwext4/ext_disk_io.c + lwext4/ext.c + + ntfs-3g/ntfs_dev.c + ntfs-3g/ntfs_disk_io.c + ntfs-3g/ntfs.c + ) + + find_library(ntfs-3g_lib ntfs-3g REQUIRED) + find_path(ntfs-3g_inc ntfs-3g REUIRED) + + find_library(lwext4_lib lwext4 REUIRED) + find_path(lwext4_inc lwext4 REUIRED) + + target_link_libraries(libusbhsfs PRIVATE ${ntfs-3g_lib} ${lwext4_lib}) + target_include_directories(libusbhsfs PRIVATE ${ntfs-3g_inc} ${lwext4_inc}) + + target_compile_definitions(libusbhsfs PRIVATE GPL_BUILD) +endif() From f444c63cebce6c85ba65ec473aa9a66ce56bade8 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Wed, 18 May 2022 23:56:33 +0100 Subject: [PATCH 2/9] auto set CMAKE_TOOLCHAIN_FILE if the user doesn't set the var themselves --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index af366b5..6673ed6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,11 @@ cmake_minimum_required(VERSION 3.12) +# this may stop working if dkp decides to change the location +# of the cmake files, in which case, pass -DCMAKE_TOOLCHAIN_FILE="the/new/path" +if (NOT DEFINED CMAKE_TOOLCHAIN_FILE) + set(CMAKE_TOOLCHAIN_FILE "$ENV{DEVKITPRO}/cmake/Switch.cmake") +endif() + project(libusbhsfs VERSION 0.2.6 DESCRIPTION "USB Mass Storage Class Host + Filesystem Mounter static library for Nintendo Switch homebrew applications." From c13742ac4557c78956dae25737304142046e6c12 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 19 May 2022 00:48:15 +0100 Subject: [PATCH 3/9] removed "required" from find_xxx() as it was added in cmake 3.18 3.18 is too new of a version imo. --- source/CMakeLists.txt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 45f62a1..3727bfc 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -38,11 +38,19 @@ if (USBHSFS_GPL) ntfs-3g/ntfs.c ) - find_library(ntfs-3g_lib ntfs-3g REQUIRED) - find_path(ntfs-3g_inc ntfs-3g REUIRED) + find_library(ntfs-3g_lib ntfs-3g) + find_path(ntfs-3g_inc ntfs-3g) - find_library(lwext4_lib lwext4 REUIRED) - find_path(lwext4_inc lwext4 REUIRED) + find_library(lwext4_lib lwext4) + find_path(lwext4_inc lwext4) + + if (NOT ntfs-3g_lib OR NOT ntfs-3g_inc) + message(FATAL_ERROR "ntfs-3g is not installed!") + endif() + + if (NOT lwext4_lib OR NOT lwext4_inc) + message(FATAL_ERROR "lwext4 is not installed!") + endif() target_link_libraries(libusbhsfs PRIVATE ${ntfs-3g_lib} ${lwext4_lib}) target_include_directories(libusbhsfs PRIVATE ${ntfs-3g_inc} ${lwext4_inc}) From 3231c9d2fbbcf51c2e65fefa8a83a2cd4caf7c01 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 11:41:22 +0100 Subject: [PATCH 4/9] document cmake support in the readme --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 0da3517..54bf7f8 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,34 @@ This section assumes you've already built the library by following the steps fro Please check both the header file located at `/include/usbhsfs.h` and the provided test applications in `/example_event` (event-driven system) and `/example_callback` (callback-based system) for additional information. +How to use (Cmake) +-------------- + +You can include libusbhsfs into your project using cmake. +```cmake +set(USBHSFS_GPL OFF) +add_subdirectory(libusbhsfs) +target_link_libraries(you_app PRIVATE libusbhsfs) +``` + +You can also use cmake to fetch directly from github. + +```cmake +include(FetchContent) + +FetchContent_Declare(libusbhsfs + GIT_REPOSITORY https://github.com/DarkMatterCore/libusbhsfs.git + # fetch latest version. + GIT_TAG dev + # fetch tagged release. + # GIT_TAG v0.2.9 +) + +set(USBHSFS_GPL OFF) +FetchContent_MakeAvailable(libusbhsfs) +target_link_libraries(you_app PRIVATE libusbhsfs) +``` + Relative path support -------------- From 0493081eb38aa869ae662d13330d3613743ff622 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 11:44:44 +0100 Subject: [PATCH 5/9] fix gpl build --- source/CMakeLists.txt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 3727bfc..8437c73 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -38,11 +38,11 @@ if (USBHSFS_GPL) ntfs-3g/ntfs.c ) - find_library(ntfs-3g_lib ntfs-3g) - find_path(ntfs-3g_inc ntfs-3g) + find_library(ntfs_3g_lib ntfs-3g) + find_path(ntfs_3g_inc ntfs-3g) find_library(lwext4_lib lwext4) - find_path(lwext4_inc lwext4) + find_path(lwext4_inc ext4.h) if (NOT ntfs-3g_lib OR NOT ntfs-3g_inc) message(FATAL_ERROR "ntfs-3g is not installed!") @@ -52,8 +52,7 @@ if (USBHSFS_GPL) message(FATAL_ERROR "lwext4 is not installed!") endif() - target_link_libraries(libusbhsfs PRIVATE ${ntfs-3g_lib} ${lwext4_lib}) - target_include_directories(libusbhsfs PRIVATE ${ntfs-3g_inc} ${lwext4_inc}) - + target_link_libraries(libusbhsfs PRIVATE ${ntfs_3g_lib} ${lwext4_lib}) + target_include_directories(libusbhsfs PRIVATE ${ntfs_3g_inc} ${lwext4_inc}) target_compile_definitions(libusbhsfs PRIVATE GPL_BUILD) endif() From 7fc0191646c100df5ae5d141b7ae6ec9777f11ce Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 12:56:51 +0100 Subject: [PATCH 6/9] fetch version from header. --- CMakeLists.txt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4f17d8..c073bcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,23 @@ if (NOT DEFINED CMAKE_TOOLCHAIN_FILE) set(CMAKE_TOOLCHAIN_FILE "$ENV{DEVKITPRO}/cmake/Switch.cmake") endif() +# fetch version number from header: https://stackoverflow.com/a/47084079 +file(READ "${CMAKE_CURRENT_SOURCE_DIR}/include/usbhsfs.h" ver) + +string(REGEX MATCH "#define[ \t\r\n]+LIBUSBHSFS_VERSION_MAJOR[ \t\r\n]+([0-9]*)" _ ${ver}) +string(STRIP ${CMAKE_MATCH_1} LIBUSBHSFS_VERSION_MAJOR) + +string(REGEX MATCH "#define[ \t\r\n]+LIBUSBHSFS_VERSION_MINOR[ \t\r\n]+([0-9]*)" _ ${ver}) +string(STRIP ${CMAKE_MATCH_1} LIBUSBHSFS_VERSION_MINOR) + +string(REGEX MATCH "#define[ \t\r\n]+LIBUSBHSFS_VERSION_MICRO[ \t\r\n]+([0-9]*)" _ ${ver}) +string(STRIP ${CMAKE_MATCH_1} LIBUSBHSFS_VERSION_MICRO) + +set(LIBUSBHSFS_VERSION ${LIBUSBHSFS_VERSION_MAJOR}.${LIBUSBHSFS_VERSION_MINOR}.${LIBUSBHSFS_VERSION_MICRO}) +message(STATUS "LIBUSBHSFS_VERSION ${LIBUSBHSFS_VERSION}") + project(libusbhsfs - VERSION 0.2.9 + VERSION ${LIBUSBHSFS_VERSION} DESCRIPTION "USB Mass Storage Class Host + Filesystem Mounter static library for Nintendo Switch homebrew applications." HOMEPAGE_URL "https://github.com/DarkMatterCore/libusbhsfs" LANGUAGES C From 7e5e199200aed1cb64b9510ac39aedda17fd6b7f Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 14:04:50 +0100 Subject: [PATCH 7/9] fix cmake build when using fetch content --- CMakeLists.txt | 57 +++++++++++++++++++++++++++++++++++++++++- source/CMakeLists.txt | 58 ------------------------------------------- 2 files changed, 56 insertions(+), 59 deletions(-) delete mode 100644 source/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index c073bcb..ca36218 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,62 @@ project(libusbhsfs option(USBHSFS_GPL "ext4 and ntfs support" OFF) option(USBHSFS_EXAMPLES "build examples" OFF) -add_subdirectory(source) +add_library(libusbhsfs + source/usbhsfs_drive.c + source/usbhsfs_log.c + source/usbhsfs_manager.c + source/usbhsfs_mount.c + source/usbhsfs_request.c + source/usbhsfs_scsi.c + source/usbhsfs_utils.c +) + +target_include_directories(libusbhsfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +# fatfs stuff +target_sources(libusbhsfs PRIVATE + source/fatfs/diskio.c + source/fatfs/ff_dev.c + source/fatfs/ff.c + source/fatfs/ffsystem.c + source/fatfs/ffunicode.c +) + +# sxos stuff +target_sources(libusbhsfs PRIVATE + source/sxos/usbfs_dev.c + source/sxos/usbfs.c +) + +if (USBHSFS_GPL) + target_sources(libusbhsfs PRIVATE + source/lwext4/ext_dev.c + source/lwext4/ext_disk_io.c + source/lwext4/ext.c + + source/ntfs-3g/ntfs_dev.c + source/ntfs-3g/ntfs_disk_io.c + source/ntfs-3g/ntfs.c + ) + + find_library(ntfs_3g_lib ntfs-3g) + find_path(ntfs_3g_inc ntfs-3g) + + find_library(lwext4_lib lwext4) + find_path(lwext4_inc ext4.h) + + if (NOT ntfs-3g_lib OR NOT ntfs-3g_inc) + message(FATAL_ERROR "ntfs-3g is not installed!") + endif() + + if (NOT lwext4_lib OR NOT lwext4_inc) + message(FATAL_ERROR "lwext4 is not installed!") + endif() + + target_link_libraries(libusbhsfs PRIVATE ${ntfs_3g_lib} ${lwext4_lib}) + target_include_directories(libusbhsfs PRIVATE ${ntfs_3g_inc} ${lwext4_inc}) + target_compile_definitions(libusbhsfs PRIVATE GPL_BUILD) +endif() if (USBHSFS_EXAMPLES) add_subdirectory(example_callback) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt deleted file mode 100644 index 8437c73..0000000 --- a/source/CMakeLists.txt +++ /dev/null @@ -1,58 +0,0 @@ -cmake_minimum_required(VERSION 3.12) - -add_library(libusbhsfs - usbhsfs_drive.c - usbhsfs_log.c - usbhsfs_manager.c - usbhsfs_mount.c - usbhsfs_request.c - usbhsfs_scsi.c - usbhsfs_utils.c -) - -target_include_directories(libusbhsfs PUBLIC ${CMAKE_SOURCE_DIR}/include) - -# fatfs stuff -target_sources(libusbhsfs PRIVATE - fatfs/diskio.c - fatfs/ff_dev.c - fatfs/ff.c - fatfs/ffsystem.c - fatfs/ffunicode.c -) - -# sxos stuff -target_sources(libusbhsfs PRIVATE - sxos/usbfs_dev.c - sxos/usbfs.c -) - -if (USBHSFS_GPL) - target_sources(libusbhsfs PRIVATE - lwext4/ext_dev.c - lwext4/ext_disk_io.c - lwext4/ext.c - - ntfs-3g/ntfs_dev.c - ntfs-3g/ntfs_disk_io.c - ntfs-3g/ntfs.c - ) - - find_library(ntfs_3g_lib ntfs-3g) - find_path(ntfs_3g_inc ntfs-3g) - - find_library(lwext4_lib lwext4) - find_path(lwext4_inc ext4.h) - - if (NOT ntfs-3g_lib OR NOT ntfs-3g_inc) - message(FATAL_ERROR "ntfs-3g is not installed!") - endif() - - if (NOT lwext4_lib OR NOT lwext4_inc) - message(FATAL_ERROR "lwext4 is not installed!") - endif() - - target_link_libraries(libusbhsfs PRIVATE ${ntfs_3g_lib} ${lwext4_lib}) - target_include_directories(libusbhsfs PRIVATE ${ntfs_3g_inc} ${lwext4_inc}) - target_compile_definitions(libusbhsfs PRIVATE GPL_BUILD) -endif() From bc605e86f5c74fff8d2ad978c27a7756c236103b Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 14:54:44 +0100 Subject: [PATCH 8/9] fix building examples with cmake debug enabled. --- CMakeLists.txt | 8 ++++++++ example_callback/CMakeLists.txt | 2 -- example_event/CMakeLists.txt | 2 -- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca36218..3bcfb01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,9 @@ project(libusbhsfs option(USBHSFS_GPL "ext4 and ntfs support" OFF) option(USBHSFS_EXAMPLES "build examples" OFF) +set(LIB_TITLE "usbhsfs") +string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S" UTC) + add_library(libusbhsfs source/usbhsfs_drive.c source/usbhsfs_log.c @@ -41,6 +44,11 @@ add_library(libusbhsfs source/usbhsfs_utils.c ) +target_compile_definitions(libusbhsfs PRIVATE + LIB_TITLE="${LIB_TITLE}" + BUILD_TIMESTAMP="${BUILD_TIMESTAMP}" +) + target_include_directories(libusbhsfs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) # fatfs stuff diff --git a/example_callback/CMakeLists.txt b/example_callback/CMakeLists.txt index aec1db4..a18f045 100644 --- a/example_callback/CMakeLists.txt +++ b/example_callback/CMakeLists.txt @@ -4,8 +4,6 @@ project(libusbhsfs-example-callback LANGUAGES C ) -string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S" UTC) - add_executable(libusbhsfs-example-callback source/main.c) target_link_libraries(libusbhsfs-example-callback PRIVATE libusbhsfs) diff --git a/example_event/CMakeLists.txt b/example_event/CMakeLists.txt index 65c5e57..73d8975 100644 --- a/example_event/CMakeLists.txt +++ b/example_event/CMakeLists.txt @@ -4,8 +4,6 @@ project(libusbhsfs-example-event LANGUAGES C ) -string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S" UTC) - add_executable(libusbhsfs-example-event source/main.c) target_link_libraries(libusbhsfs-example-event PRIVATE libusbhsfs) From c6e764d8ad46ad5bc1b8e97e1de64c83b78c6f8e Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Thu, 22 May 2025 15:04:15 +0100 Subject: [PATCH 9/9] fix gpl build not finding ntfs --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bcfb01..6d2cbf0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,7 @@ if (USBHSFS_GPL) find_library(lwext4_lib lwext4) find_path(lwext4_inc ext4.h) - if (NOT ntfs-3g_lib OR NOT ntfs-3g_inc) + if (NOT ntfs_3g_lib OR NOT ntfs_3g_inc) message(FATAL_ERROR "ntfs-3g is not installed!") endif()