Skip to content

Commit

Permalink
chore: update build script
Browse files Browse the repository at this point in the history
  • Loading branch information
FeiChongCalmCar committed May 11, 2022
1 parent 901a378 commit bfe59a0
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 60 deletions.
9 changes: 9 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
BasedOnStyle: Google
UseTab: Never
---
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Left
SortIncludes: false
---
60 changes: 28 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,10 @@ SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_DEBUG_POSTFIX d)
set(CMAKE_CXX_STANDARD 14)

# Setup detailed warning
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/MP /wd4996 /wd4251)
add_compile_options(/Zc:__cplusplus) # Enable __cplusplus macro
add_compile_options(/utf-8)
string(REPLACE "/W3" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string(REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
if(CMAKE_BUILD_TYPE MATCHES "Debug")
# Not find pdb file while linking debug dll
list(APPEND CMAKE_EXE_LINKER_FLAGS /ignore:4099)
endif()
else() # gcc clang
add_compile_options(-Wall -Wextra -pedantic -Weffc++)
add_compile_options(-Wno-gnu-zero-variadic-macro-arguments)
endif()

# Setup static build option
option(BUILD_SHARED_LIBS "Build/link shared library" OFF)
option(BUILD_TESTING "Build test" OFF)
option(BUILD_EXAMPLES "Build examples" OFF)
if(NOT BUILD_SHARED_LIBS)
# Use /MT /MTd instead of /MD /MDd when building static library
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif() # BUILD_SHARED_LIBS



Expand All @@ -45,6 +25,9 @@ find_package(Qt5
Svg
REQUIRED
)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(Qt5 COMPONENTS X11Extras REQUIRED)
endif()



Expand All @@ -61,6 +44,11 @@ else()
add_library(${PROJECT_NAME} STATIC)
endif()

target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include
)

target_precompile_headers(${PROJECT_NAME}
INTERFACE
${CMAKE_CURRENT_LIST_DIR}/include/KtUtils/KtUtils.hpp
Expand All @@ -85,6 +73,11 @@ target_sources(${PROJECT_NAME}
${CMAKE_CURRENT_LIST_DIR}/src/Settings_p.hpp
${CMAKE_CURRENT_LIST_DIR}/src/Settings.cpp

${CMAKE_CURRENT_LIST_DIR}/include/KtUtils/FramelessWindow.hpp
${CMAKE_CURRENT_LIST_DIR}/src/FramelessWindow.cpp
${CMAKE_CURRENT_LIST_DIR}/src/FramelessHelper.hpp
${CMAKE_CURRENT_LIST_DIR}/src/FramelessHelper.cpp

${CMAKE_CURRENT_LIST_DIR}/KtUtils.qrc
)

Expand All @@ -93,21 +86,22 @@ target_link_libraries(${PROJECT_NAME}
Qt5::Core
Qt5::Gui
Qt5::Svg
$<$<PLATFORM_ID:Linux>:Qt5::X11Extras>
$<$<PLATFORM_ID:Linux>:X11>
)

if(NOT BUILD_SHARED_LIBS)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::QSvgPlugin
Qt5::QSvgIconPlugin
$<$<PLATFORM_ID:Windows>:Qt5::QWindowsDirect2DIntegrationPlugin>
)
endif()


target_include_directories(${PROJECT_NAME}
target_compile_options(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include
$<$<PLATFORM_ID:Windows>:/W3>
$<$<PLATFORM_ID:Windows>:/wd4996>
$<$<PLATFORM_ID:Windows>:/wd4251>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:C>:-Wall>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:CXX>:-Wall>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:C>:-Wextra>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:CXX>:-Wextra>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:C>:-pedantic>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:CXX>:-pedantic>>
$<$<PLATFORM_ID:Linux>:$<$<COMPILE_LANGUAGE:CXX>:-Weffc++>>
)


Expand All @@ -119,6 +113,8 @@ if(BUILD_TESTING)
add_test(NAME TestGlobal COMMAND TestGlobal)
endif()



# Build example
if(BUILD_EXAMPLES)
add_subdirectory(examples)
Expand Down
3 changes: 1 addition & 2 deletions include/KtUtils/Global.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#ifndef KTUTILS_GLOBAL_HPP
#ifndef KTUTILS_GLOBAL_HPP
#define KTUTILS_GLOBAL_HPP

#include <chrono>
Expand Down
3 changes: 1 addition & 2 deletions include/KtUtils/IconHelper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#ifndef KTUTILS_ICONHELPER_HPP
#ifndef KTUTILS_ICONHELPER_HPP
#define KTUTILS_ICONHELPER_HPP

#include "Global.hpp"
Expand Down
1 change: 0 additions & 1 deletion include/KtUtils/Json.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef KT_UTILS_JSON_HPP
#define KT_UTILS_JSON_HPP

Expand Down
3 changes: 1 addition & 2 deletions include/KtUtils/KtUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef KTUTILS_KTUTILS_HPP
#define KTUTILS_KTUTILS_HPP

Expand All @@ -7,4 +6,4 @@
#include "Json.hpp"
#include "Settings.hpp"

#endif // KTUTILS_KTUTILS_HPP
#endif // KTUTILS_KTUTILS_HPP
15 changes: 5 additions & 10 deletions include/KtUtils/Settings.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#pragma once
#ifndef KTUTILS_SETTINGS_HPP
#ifndef KTUTILS_SETTINGS_HPP
#define KTUTILS_SETTINGS_HPP

#include "Global.hpp"
#include "Json.hpp"

namespace KtUtils {
// Helper functions for QSettings::registerFormat
namespace SettingsExtra {
inline bool jsonReadFunc(QIODevice& device, QSettings::SettingsMap& map) {
return ::KtUtils::Json::settingsReadFunc(device, map);
}
inline bool jsonWriteFunc(QIODevice& device,
const QSettings::SettingsMap& map) {
return ::KtUtils::Json::settingsWriteFunc(device, map);
}
KTUTILS_EXPORT bool jsonReadFunc(QIODevice& device,
QSettings::SettingsMap& map);
KTUTILS_EXPORT bool jsonWriteFunc(QIODevice& device,
const QSettings::SettingsMap& map);

KTUTILS_EXPORT bool xmlReadFunc(QIODevice& device, QSettings::SettingsMap& map);
KTUTILS_EXPORT bool xmlWriteFunc(QIODevice& device,
Expand Down
12 changes: 7 additions & 5 deletions src/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ bool WaitFor(double timeout_milliseconds, QEventLoop::ProcessEventsFlags flags,
const std::function<bool(void)>& isValid) {
QElapsedTimer timer;
timer.start();
Wait([&] {
return (isValid && isValid()) ||
(timer.nsecsElapsed() >= (timeout_milliseconds * 1e6));
}, flags);
if (isValid){
Wait(
[&] {
return (isValid && isValid()) ||
(timer.nsecsElapsed() >= (timeout_milliseconds * 1e6));
},
flags);
if (isValid) {
return isValid();
} else {
return true;
Expand Down
13 changes: 11 additions & 2 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include <KtUtils/Settings>
#include "Settings_p.hpp"
#include "Settings_p.hpp"
#include <KtUtils/Json>
#include <KtUtils/Settings>

namespace KtUtils {
namespace SettingsExtra {
bool jsonReadFunc(QIODevice& device, QSettings::SettingsMap& map) {
return ::KtUtils::Json::settingsReadFunc(device, map);
}

bool jsonWriteFunc(QIODevice& device, const QSettings::SettingsMap& map) {
return ::KtUtils::Json::settingsWriteFunc(device, map);
}

bool xmlReadFunc(QIODevice& device, QSettings::SettingsMap& map) {
QXmlStreamReader xml(&device);
std::function<QVariant()> readNextValue = [&readNextValue,
Expand Down
8 changes: 4 additions & 4 deletions src/Settings_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace KtUtils {
namespace SettingsExtra {
// Check if a QSettings array
bool CheckIfArray(const QVariantMap& map) {
inline bool CheckIfArray(const QVariantMap& map) {
const auto it = map.find(QStringLiteral("size"));
if (it == map.cend()) return false;

Expand All @@ -21,7 +21,7 @@ bool CheckIfArray(const QVariantMap& map) {
}

// Convert QSettings array to QVariantList
QVariant FromSettingsArray(const QVariant& value) {
inline QVariant FromSettingsArray(const QVariant& value) {
if (int(value.type()) != QMetaType::QVariantMap) return value;

const QVariantMap map = value.toMap();
Expand All @@ -41,7 +41,7 @@ QVariant FromSettingsArray(const QVariant& value) {
}

// Convert QSettings::SettingsMap to QVariantMap tree
QVariantMap FromSettingsMap(const QSettings::SettingsMap& map) {
inline QVariantMap FromSettingsMap(const QSettings::SettingsMap& map) {
std::function<void(QVariantMap*, QStringList, const QVariant&)> InsertKeys =
[&InsertKeys](QVariantMap* map, QStringList keys, const QVariant& value) {
const QString key = keys.takeFirst();
Expand All @@ -63,7 +63,7 @@ QVariantMap FromSettingsMap(const QSettings::SettingsMap& map) {
}

// Convert QVariantMap tree to QSettings::SettingsMap
QSettings::SettingsMap ToSettingsMap(const QVariantMap& map) {
inline QSettings::SettingsMap ToSettingsMap(const QVariantMap& map) {
QSettings::SettingsMap ret;
std::function<void(QString, const QVariant&)> InsertValue =
[&InsertValue, &ret](QString keys, const QVariant& value) {
Expand Down

0 comments on commit bfe59a0

Please sign in to comment.