C

Qt Quick Ultralite fileloading Example

# Copyright (C) 2026 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial
cmake_minimum_required(VERSION 3.21.1)

project(fileloading VERSION 0.0.1 LANGUAGES C CXX ASM)

find_package(QulApp)

qul_add_executable(fileloading os/${QUL_OS_LOWERCASE}/main.cpp)

# 2007.png is embedded as a regular resource (to contrast with the other
# three, which demonstrate loading from an actual filesystem).
set_source_files_properties(2008.png 2015.png 2016.png PROPERTIES
    QUL_RESOURCE_AS_FILE ON
)
set_source_files_properties(2015.png PROPERTIES
    QUL_RESOURCE_COMPRESSION ON
)
set_source_files_properties(2016.png PROPERTIES
    QUL_RESOURCE_IMAGE_PIXEL_FORMAT "ARGB8888RLE"
    QUL_RESOURCE_CACHE_POLICY "OnDemand"
)

qul_add_qml_module(fileloading
    URI FileloadingApp
    QML_FILES fileloading.qml
    RESOURCES
        2007.png
        2008.png
        2015.png
)

# 2016.png is declared with a custom resource prefix purely to show what that changes:
# only the qrc path and the sub-folder the generated asset is written to.
# The file:// URL used to load it at runtime is unaffected by the resource prefix
qul_add_resources(fileloading
    FILES 2016.png
    PREFIX "custom_prefix"
)

qul_set_maximum_resource_cache_size(fileloading CACHE_SIZE 100000)

if (QUL_PLATFORM STREQUAL "qt" OR (CMAKE_CROSSCOMPILING AND QUL_OS STREQUAL "linux"))
    target_sources(fileloading PRIVATE posix/board_config.cpp)
    target_sources(fileloading PRIVATE posix/posixfilesystem.cpp)

    target_include_directories(fileloading PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

    # On Linux / desktop the files by default need to be available next
    # to the built binary on the filesystem
    add_custom_command(
            TARGET fileloading POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_directory
                    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/fileloading.dir/resources/FileloadingApp
                    $<TARGET_FILE_DIR:${PROJECT_NAME}>/logos
            COMMAND ${CMAKE_COMMAND} -E copy_directory
                    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/fileloading.dir/resources/custom_prefix
                    $<TARGET_FILE_DIR:${PROJECT_NAME}>/logos)
elseif(CMAKE_CROSSCOMPILING)
    if(QUL_PLATFORM MATCHES "^stm32f769i")
        target_sources(fileloading PRIVATE
            3rdparty/FatFs/src/diskio.c
            3rdparty/FatFs/src/ff.c
            3rdparty/FatFs/src/ff_gen_drv.c
            3rdparty/FatFs/src/option/unicode.c
            3rdparty/FatFs/src/sd_diskio.c
            stm/board_config.cpp)

        target_sources(fileloading PRIVATE
            ${QUL_BOARD_SDK_DIR}/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_ll_sdmmc.c
            ${QUL_BOARD_SDK_DIR}/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c
            ${QUL_BOARD_SDK_DIR}/Drivers/BSP/STM32F769I-Discovery/stm32f769i_discovery_sd.c)

        if(NOT IAR)
            # Required for SDK code to build
            target_compile_options(fileloading PRIVATE
                -Wno-unused-parameter
            )
        endif()

        target_include_directories(fileloading PRIVATE 3rdparty/FatFs/src)
        target_compile_definitions(fileloading PRIVATE FF_FS_READONLY)

        target_link_libraries(fileloading PRIVATE Qul::PlatformBSPConfig)
    elseif(QUL_PLATFORM MATCHES "^stm32u5")
        target_sources(fileloading PRIVATE
                       3rdparty/FatFs/src/diskio.c
                       3rdparty/FatFs/src/ff.c
                       3rdparty/FatFs/src/ff_gen_drv.c
                       3rdparty/FatFs/src/option/unicode.c
                       stm/stm32u5_board_config.cpp
        )
        target_include_directories(fileloading PRIVATE
                                   3rdparty/FatFs/src
                                   ${QUL_PLATFORM_BOARDS_DIR}/st/stm32u5x9-discovery-common
        )
        target_compile_definitions(fileloading PRIVATE FF_FS_READONLY)
        target_link_libraries(fileloading PRIVATE Qul::PlatformBSPConfig)

        if(NOT IAR)
            # Required for SDK code to build
            target_compile_options(fileloading PRIVATE
                                   -Wno-unused-parameter
            )
        endif()
    elseif(QUL_PLATFORM MATCHES "^mimxrt")
        set(NXP_BOARD_CORE_DIR "")
        if (QUL_PLATFORM MATCHES "^mimxrt1050")
            set(NXP_BOARD_DRIVER_DIR MIMXRT1052)
        elseif (QUL_PLATFORM MATCHES "^mimxrt1060")
            set(NXP_BOARD_DRIVER_DIR MIMXRT1062)
        elseif (QUL_PLATFORM MATCHES "^mimxrt1064")
            set(NXP_BOARD_DRIVER_DIR MIMXRT1064)
        elseif (QUL_PLATFORM MATCHES "^mimxrt1170")
            set(NXP_BOARD_DRIVER_DIR MIMXRT1176)
            set(NXP_BOARD_CORE_DIR cm7)
        else()
            message(FATAL_ERROR "Unsupported NXP board")
        endif()

        target_sources(fileloading PRIVATE
            3rdparty/FatFs/src/diskio.c
            3rdparty/FatFs/src/ff.c
            3rdparty/FatFs/src/ff_gen_drv.c
            3rdparty/FatFs/src/option/unicode.c
            3rdparty/NXP/${NXP_BOARD_DRIVER_DIR}/sdmmc_config.c
            # We have to use a modified version of fsl_adapter_gpio.c to comment out the
            # GPIO1_Combined_0_15_IRQHandler() handler, which otherwise conflicts with the one in platform_irq.c
            3rdparty/NXP/fsl_adapter_igpio.c
            ${QUL_BOARD_SDK_DIR}/devices/${NXP_BOARD_DRIVER_DIR}/drivers/fsl_usdhc.c
            ${QUL_BOARD_SDK_DIR}/devices/${NXP_BOARD_DRIVER_DIR}/drivers/${NXP_BOARD_CORE_DIR}/fsl_cache.c
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/common/fsl_sdmmc_common.c
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/host/usdhc/non_blocking/fsl_sdmmc_host.c
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/osa/fsl_sdmmc_osa.c
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/sd/fsl_sd.c
            nxp/board_config.cpp)

        if (QUL_OS STREQUAL "FreeRTOS")
            target_sources(fileloading PRIVATE ${QUL_BOARD_SDK_DIR}/components/osa/fsl_os_abstraction_free_rtos.c)
        else()
            target_sources(fileloading PRIVATE ${QUL_BOARD_SDK_DIR}/components/osa/fsl_os_abstraction_bm.c)
        endif()

        if(NOT IAR)
            # Required for SDK code to build
            target_compile_options(fileloading PRIVATE
                -Wno-unused-parameter
                -Wno-unused-variable
                )
        endif()

        target_include_directories(fileloading PRIVATE
            3rdparty/FatFs/src
            3rdparty/NXP/${NXP_BOARD_DRIVER_DIR}
            ${QUL_BOARD_SDK_DIR}/components/gpio
            ${QUL_BOARD_SDK_DIR}/components/lists
            ${QUL_BOARD_SDK_DIR}/components/osa
            ${QUL_BOARD_SDK_DIR}/devices/${NXP_BOARD_DRIVER_DIR}/drivers/${NXP_BOARD_CORE_DIR}
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/common
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/host/usdhc/
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/osa
            ${QUL_BOARD_SDK_DIR}/middleware/sdmmc/sd)

        target_compile_definitions(fileloading PRIVATE FF_FS_READONLY SD_ENABLED)

        target_link_libraries(fileloading PRIVATE Qul::PlatformBSPConfig)
    else()
        message(FATAL_ERROR "Unsupported platform")
    endif()
endif()

app_target_setup_os(fileloading)