Поиск стандартных каталогов
This commit is contained in:
@@ -35,6 +35,6 @@ install(FILES ${CMAKE_BINARY_DIR}/include/cmlib_config.hpp
|
||||
${CMAKE_BINARY_DIR}/include/config_flags.hpp
|
||||
${current_target_headers}
|
||||
COMPONENT headers
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${current_target})
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${current_target})
|
||||
install(FILES ${CMAKE_BINARY_DIR}/${current_target}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
||||
|
||||
|
@@ -11,7 +11,8 @@ set(current_target_headers
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/paths.hpp
|
||||
)
|
||||
|
||||
add_common_library(TARGET ${current_target} SOURCES ${current_target_sources})
|
||||
add_common_library(TARGET ${current_target} SOURCES ${current_target_sources}
|
||||
${CMAKE_SOURCE_DIR}/thirdparty/whereami/whereami.c)
|
||||
common_target_properties(${current_target})
|
||||
add_clang_tidy_check(${current_target} ${current_target_sources})
|
||||
add_clang_analyze_check(${current_target} ${current_target_sources})
|
||||
@@ -20,6 +21,10 @@ add_pvs_check(${current_target})
|
||||
add_uncrustify_format(${current_target} ${current_target_sources} ${current_target_headers})
|
||||
|
||||
target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||
target_include_directories(${current_target} SYSTEM PUBLIC ${FMT_INCLUDE_DIRS})
|
||||
target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS})
|
||||
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src/base)
|
||||
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/thirdparty/whereami)
|
||||
|
||||
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
|
||||
add_custom_target(${current_target}-install-headers
|
||||
@@ -34,6 +39,6 @@ if(BUILD_SHARED_LIBS)
|
||||
endif()
|
||||
install(FILES ${current_target_headers}
|
||||
COMPONENT headers
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${current_target})
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${current_target})
|
||||
install(FILES ${CMAKE_BINARY_DIR}/${current_target}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
||||
|
||||
|
@@ -1,9 +1,223 @@
|
||||
#include "paths.hpp"
|
||||
#include "config.hpp"
|
||||
#include "whereami.h"
|
||||
|
||||
namespace MF = myx::filesystem;
|
||||
#include <paths.h>
|
||||
|
||||
namespace myx {
|
||||
|
||||
int32_t MF::init( int32_t i = 0 )
|
||||
namespace filesystem {
|
||||
|
||||
Paths::Paths() :
|
||||
m_prefixDirectory( "/opt/" ORGANIZATION_NAME_LOWER "/" PROJECT_NAME_LOWER ),
|
||||
m_binaryDirectory( m_prefixDirectory.absolutePath() + "/bin" ),
|
||||
m_configDirectory( m_prefixDirectory.absolutePath() + "/etc" ),
|
||||
m_cacheDirectory( m_prefixDirectory.absolutePath() + "/var" ),
|
||||
m_tempDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "TMPDIR" ) ) ) ),
|
||||
m_dataDirectory( m_prefixDirectory.absolutePath() + "/share" ),
|
||||
m_homeDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "HOME" ) ) ) ),
|
||||
m_executableFile( m_binaryDirectory.absolutePath() + "/" + PROJECT_NAME_LOWER ),
|
||||
m_configFile( m_configDirectory.absolutePath() + "/" + PROJECT_NAME_LOWER + ".conf" )
|
||||
{
|
||||
return( i );
|
||||
if ( m_tempDirectory.isEmpty() || ( m_tempDirectory.path() == "." ) )
|
||||
{
|
||||
m_tempDirectory = _PATH_TMP;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::prefixDirectory() const
|
||||
{
|
||||
return( m_prefixDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setPrefixDirectory( const QString& prefixDirectory )
|
||||
{
|
||||
m_prefixDirectory = prefixDirectory;
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::binaryDirectory() const
|
||||
{
|
||||
return( m_binaryDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setBinaryDirectory( const QString& binaryDirectory )
|
||||
{
|
||||
m_binaryDirectory = binaryDirectory;
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::configDirectory() const
|
||||
{
|
||||
return( m_configDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setConfigDirectory( const QString& configDirectory )
|
||||
{
|
||||
m_configDirectory = configDirectory;
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::cacheDirectory() const
|
||||
{
|
||||
return( m_cacheDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setCacheDirectory( const QString& cacheDirectory )
|
||||
{
|
||||
m_cacheDirectory = cacheDirectory;
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::tempDirectory() const
|
||||
{
|
||||
return( m_tempDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setTempDirectory( const QString& tempDirectory )
|
||||
{
|
||||
m_tempDirectory = tempDirectory;
|
||||
}
|
||||
|
||||
|
||||
QDir Paths::dataDirectory() const
|
||||
{
|
||||
return( m_dataDirectory );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setDataDirectory( const QString& dataDirectory )
|
||||
{
|
||||
m_dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
|
||||
QFileInfo Paths::executableFile() const
|
||||
{
|
||||
return( m_executableFile );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setExecutableFile( const QString& executableFile )
|
||||
{
|
||||
m_executableFile = executableFile;
|
||||
}
|
||||
|
||||
|
||||
QFileInfo Paths::configFile() const
|
||||
{
|
||||
return( m_configFile );
|
||||
}
|
||||
|
||||
|
||||
void Paths::setConfigFile( const QString& configFile )
|
||||
{
|
||||
m_configFile = configFile;
|
||||
}
|
||||
|
||||
|
||||
bool Paths::updatePaths()
|
||||
{
|
||||
char* path = nullptr;
|
||||
int length, dirname_length;
|
||||
|
||||
length = wai_getExecutablePath( nullptr, 0, &dirname_length );
|
||||
if ( length > 0 )
|
||||
{
|
||||
path = static_cast< char* >( malloc( static_cast< size_t >( length ) + 1 ) );
|
||||
if ( !path )
|
||||
{
|
||||
return( false );
|
||||
}
|
||||
wai_getExecutablePath( path, length, &dirname_length );
|
||||
path[length] = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
return( false );
|
||||
}
|
||||
|
||||
m_executableFile = QFile( path );
|
||||
path[dirname_length] = '\0';
|
||||
m_binaryDirectory = path;
|
||||
free( path );
|
||||
|
||||
if ( m_binaryDirectory.absolutePath().endsWith( "/bin" ) )
|
||||
{
|
||||
m_prefixDirectory = m_binaryDirectory.absolutePath().remove( QRegExp( "/bin$" ) );
|
||||
m_configDirectory = m_prefixDirectory.absolutePath() + "/etc";
|
||||
m_cacheDirectory = m_prefixDirectory.absolutePath() + "/var";
|
||||
m_dataDirectory = m_prefixDirectory.absolutePath() + "/share";
|
||||
m_configFile = m_configDirectory.absolutePath() + "/" + PROJECT_NAME_LOWER + ".conf";
|
||||
}
|
||||
|
||||
if ( m_prefixDirectory.absolutePath().startsWith( "/opt" ) ||
|
||||
m_prefixDirectory.absolutePath().startsWith( "/usr" ) )
|
||||
{
|
||||
QString dataDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_DATA_HOME" ) ) );
|
||||
if ( dataDirectory.isEmpty() )
|
||||
{
|
||||
dataDirectory = m_homeDirectory.absolutePath() + ".local/share/";
|
||||
}
|
||||
m_dataDirectory = dataDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
||||
|
||||
QString configDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CONFIG_HOME" ) ) );
|
||||
if ( configDirectory.isEmpty() )
|
||||
{
|
||||
configDirectory = m_homeDirectory.absolutePath() + ".config/";
|
||||
}
|
||||
m_configDirectory = configDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
||||
|
||||
QString cacheDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CACHE_HOME" ) ) );
|
||||
if ( cacheDirectory.isEmpty() )
|
||||
{
|
||||
cacheDirectory = m_homeDirectory.absolutePath() + ".cache/";
|
||||
}
|
||||
m_cacheDirectory = cacheDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
||||
}
|
||||
|
||||
return( true );
|
||||
} // Paths::updatePaths
|
||||
|
||||
|
||||
bool Paths::makePaths()
|
||||
{
|
||||
bool status = true;
|
||||
|
||||
if ( !m_dataDirectory.mkpath( m_dataDirectory.absolutePath() ) ) { status = false; }
|
||||
if ( !m_configDirectory.mkpath( m_configDirectory.absolutePath() ) ) { status = false; }
|
||||
if ( !m_cacheDirectory.mkpath( m_cacheDirectory.absolutePath() ) ) { status = false; }
|
||||
return( status );
|
||||
}
|
||||
|
||||
|
||||
QString Paths::findConfigFile( const QString& defaultConfigFile )
|
||||
{
|
||||
if ( QFileInfo( defaultConfigFile ).isReadable() )
|
||||
{
|
||||
return( defaultConfigFile );
|
||||
}
|
||||
|
||||
auto fileName = QString::fromLocal8Bit( qgetenv( qPrintable( PROJECT_NAME_UPPER "_CONFIG" ) ) );
|
||||
if ( QFileInfo( fileName ).isReadable() )
|
||||
{
|
||||
return( fileName );
|
||||
}
|
||||
|
||||
if ( QFileInfo( m_configFile ).isReadable() )
|
||||
{
|
||||
return( m_configFile.absoluteFilePath() );
|
||||
}
|
||||
|
||||
return( QString() );
|
||||
}
|
||||
|
||||
} // namespace filesystem
|
||||
|
||||
} // namespace myx
|
||||
|
@@ -1,13 +1,49 @@
|
||||
#ifndef MYX_FILESYSTEM_PATHS_HPP_
|
||||
#define MYX_FILESYSTEM_PATHS_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <QString>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace filesystem {
|
||||
|
||||
int32_t init( int32_t i );
|
||||
class Paths
|
||||
{
|
||||
QDir m_prefixDirectory;
|
||||
QDir m_binaryDirectory;
|
||||
QDir m_configDirectory;
|
||||
QDir m_cacheDirectory;
|
||||
QDir m_tempDirectory;
|
||||
QDir m_dataDirectory;
|
||||
QDir m_homeDirectory;
|
||||
QFileInfo m_executableFile;
|
||||
QFileInfo m_configFile;
|
||||
|
||||
public:
|
||||
Paths();
|
||||
QDir prefixDirectory() const;
|
||||
void setPrefixDirectory( const QString& prefixDirectory );
|
||||
QDir binaryDirectory() const;
|
||||
void setBinaryDirectory( const QString& binaryDirectory );
|
||||
QDir configDirectory() const;
|
||||
void setConfigDirectory( const QString& configDirectory );
|
||||
QDir cacheDirectory() const;
|
||||
void setCacheDirectory( const QString& cacheDirectory );
|
||||
QDir tempDirectory() const;
|
||||
void setTempDirectory( const QString& tempDirectory );
|
||||
QDir dataDirectory() const;
|
||||
void setDataDirectory( const QString& dataDirectory );
|
||||
QFileInfo executableFile() const;
|
||||
void setExecutableFile( const QString& executableFile );
|
||||
QFileInfo configFile() const;
|
||||
void setConfigFile( const QString& configFile );
|
||||
|
||||
bool updatePaths();
|
||||
bool makePaths();
|
||||
QString findConfigFile( const QString& defaultConfigFile = "" );
|
||||
}; // class Paths
|
||||
|
||||
} // namespace filesystem
|
||||
|
||||
|
@@ -39,6 +39,6 @@ if(BUILD_SHARED_LIBS)
|
||||
endif()
|
||||
install(FILES ${current_target_headers}
|
||||
COMPONENT headers
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${current_target})
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${current_target})
|
||||
install(FILES ${CMAKE_BINARY_DIR}/${current_target}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
||||
|
||||
|
Reference in New Issue
Block a user