Правка структуры проекта

This commit is contained in:
2019-10-08 23:58:42 +03:00
parent 07110430c9
commit 8bbf5e6c6f
21 changed files with 38 additions and 63 deletions

View File

@@ -0,0 +1,54 @@
# Название основной цели и имя библиотеки в текущем каталоге
set(current_target filesystem)
# Список файлов исходных текстов
set(current_target_sources
${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp
)
# Список заголовочных файлов (используется для установки)
set(current_target_headers
${CMAKE_CURRENT_SOURCE_DIR}/paths.hpp
)
add_common_library(TARGET ${current_target}
OUTPUT_NAME myx-${current_target}
SOURCES ${current_target_sources})
common_target_properties(${current_target})
add_dependencies(${current_target} whereami)
add_clang_tidy_check(${current_target} ${current_target_sources})
add_clang_analyze_check(${current_target} ${current_target_sources})
add_clazy_check(${current_target} ${current_target_sources})
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} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${current_target} PRIVATE ${CMAKE_BINARY_DIR}/include)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${current_target}-install-headers
COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=headers -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)
set_target_properties(${current_target}
PROPERTIES
OUTPUT_NAME myx-filesystem
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
)
# Правила для установки
install(TARGETS ${current_target}_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${current_target}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES ${current_target_headers}
COMPONENT headers
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${current_target})
install(FILES ${CMAKE_BINARY_DIR}/${current_target}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

View File

@@ -0,0 +1,234 @@
#include "paths.hpp"
#include <myx/base/config.hpp>
#include "whereami++.h"
#include <paths.h>
#include <QString>
namespace myx {
namespace filesystem {
QString Paths::executableFileName() const
{
return( m_executableFileName );
}
void Paths::setExecutableFileName( const QString& executableFileName )
{
m_executableFileName = executableFileName;
}
QString Paths::configFileName() const
{
return( m_configFileName );
}
void Paths::setConfigFileName( const QString& configFileName )
{
m_configFileName = configFileName;
}
QFileInfo Paths::executableFilePath() const
{
return( m_executableFilePath );
}
void Paths::setExecutableFilePath( const QFileInfo& executableFilePath )
{
m_executableFilePath = executableFilePath;
}
QFileInfo Paths::configFilePath() const
{
return( m_configFilePath );
}
void Paths::setConfigFilePath( const QFileInfo& configFilePath )
{
m_configFilePath = configFilePath;
}
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_executableFileName( PROJECT_NAME_LOWER ),
m_configFileName( PROJECT_NAME_LOWER ".conf" ),
m_executableFilePath( m_binaryDirectory.absolutePath() + "/" + m_executableFileName ),
m_configFilePath( m_binaryDirectory.absolutePath() + "/" + m_configFileName )
{
if ( m_tempDirectory.absolutePath().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;
}
bool Paths::updatePaths()
{
whereami::whereami_path_t executablePath = whereami::getExecutablePath();
m_executableFileName = QString::fromStdString( executablePath.basename() );
m_binaryDirectory = QString::fromStdString( executablePath.dirname() );
m_executableFilePath = QFile( m_binaryDirectory.absolutePath() + "/" + m_executableFileName );
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_configFilePath = QFile( 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() )
{
m_configFilePath = defaultConfigFile;
return( defaultConfigFile );
}
auto fileName = QString::fromLocal8Bit( qgetenv( qPrintable( PROJECT_NAME_UPPER "_CONFIG" ) ) );
if ( QFileInfo( fileName ).isReadable() )
{
m_configFilePath = fileName;
return( fileName );
}
if ( QFileInfo( m_configFilePath ).isReadable() )
{
return( m_configFilePath.absoluteFilePath() );
}
return( QString() );
} // Paths::findConfigFile
} // namespace filesystem
} // namespace myx

View File

@@ -0,0 +1,58 @@
#ifndef MYX_FILESYSTEM_PATHS_HPP_
#define MYX_FILESYSTEM_PATHS_HPP_
#include <QString>
#include <QDir>
#include <QFileInfo>
namespace myx {
namespace filesystem {
class Paths
{
QDir m_prefixDirectory;
QDir m_binaryDirectory;
QDir m_configDirectory;
QDir m_cacheDirectory;
QDir m_tempDirectory;
QDir m_dataDirectory;
QDir m_homeDirectory;
QString m_executableFileName;
QString m_configFileName;
QFileInfo m_executableFilePath;
QFileInfo m_configFilePath;
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 );
QString executableFileName() const;
void setExecutableFileName( const QString& executableFileName );
QString configFileName() const;
void setConfigFileName( const QString& configFileName );
QFileInfo executableFilePath() const;
void setExecutableFilePath( const QFileInfo& executableFilePath );
QFileInfo configFilePath() const;
void setConfigFilePath( const QFileInfo& configFilePath );
bool updatePaths();
bool makePaths();
QString findConfigFile( const QString& defaultConfigFile = "" );
}; // class Paths
} // namespace filesystem
} // namespace myx
#endif // MYX_FILESYSTEM_PATHS_HPP_