diff --git a/.gitignore b/.gitignore index 8ad4a1e..41344e1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ *.code-workspace /example_callback/build /example_event/build +build +.cache diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6d2cbf0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,102 @@ +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() + +# 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 ${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 +) + +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 + source/usbhsfs_manager.c + source/usbhsfs_mount.c + source/usbhsfs_request.c + source/usbhsfs_scsi.c + 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 +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) + add_subdirectory(example_event) +endif() 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 -------------- diff --git a/example_callback/CMakeLists.txt b/example_callback/CMakeLists.txt new file mode 100644 index 0000000..a18f045 --- /dev/null +++ b/example_callback/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.12) + +project(libusbhsfs-example-callback + LANGUAGES C +) + +add_executable(libusbhsfs-example-callback source/main.c) + +target_link_libraries(libusbhsfs-example-callback PRIVATE libusbhsfs) +target_compile_definitions(libusbhsfs-example-callback PRIVATE + APP_TITLE="libusbhsfs-example-callback" + BUILD_TIMESTAMP="${BUILD_TIMESTAMP}" +) + +nx_generate_nacp(libusbhsfs-example-callback.nacp + NAME "libusbhsfs-example-callback" + AUTHOR "DarkMatterCore, XorTroll, Rhys Koedijk" + VERSION 0.0.2 +) + +nx_create_nro(libusbhsfs-example-callback + NACP libusbhsfs-example-callback.nacp +) diff --git a/example_event/CMakeLists.txt b/example_event/CMakeLists.txt new file mode 100644 index 0000000..73d8975 --- /dev/null +++ b/example_event/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.12) + +project(libusbhsfs-example-event + LANGUAGES C +) + +add_executable(libusbhsfs-example-event source/main.c) + +target_link_libraries(libusbhsfs-example-event PRIVATE libusbhsfs) +target_compile_definitions(libusbhsfs-example-event PRIVATE + APP_TITLE="libusbhsfs-example-event" + BUILD_TIMESTAMP="${BUILD_TIMESTAMP}" +) + +nx_generate_nacp(libusbhsfs-example-event.nacp + NAME "libusbhsfs-example-event" + AUTHOR "DarkMatterCore, XorTroll, Rhys Koedijk" + VERSION 0.0.2 +) + +nx_create_nro(libusbhsfs-example-event + NACP libusbhsfs-example-event.nacp +)