get_module_cache_dir Function

public function get_module_cache_dir() result(dir)

Get the default module cache directory

Arguments

None

Return Value character(len=:), allocatable


Source Code

    function get_module_cache_dir() result(dir)
        character(:), allocatable :: dir
        character(len=256) :: home_dir
        integer :: stat

        ! Try XDG_CACHE_HOME first
        call get_environment_variable('XDG_CACHE_HOME', home_dir, status=stat)

        if (stat == 0 .and. len_trim(home_dir) > 0) then
            dir = trim(home_dir)//'/fortran/modules'
        else
            ! Fallback to HOME
            call get_environment_variable('HOME', home_dir, status=stat)

            if (stat == 0) then
                select case (get_os_type())
                case (OS_LINUX, OS_MACOS)
                    dir = trim(home_dir)//'/.cache/fortran/modules'
                case (OS_WINDOWS)
                    call get_environment_variable('LOCALAPPDATA', home_dir, status=stat)
                    if (stat == 0) then
                        dir = trim(home_dir)//'\fortran\cache\modules'
                    else
                        dir = '.fortran-module-cache'
                    end if
                case default
                    dir = '.fortran-module-cache'
                end select
            else
                dir = '.fortran-module-cache'
            end if
        end if

    end function get_module_cache_dir