feat: add various detector.
- add endian and compiler detector, and modify os detector. - now we use CMake to add detector-used macro, instead of using some C++ features to detect them. - change Windows environment detection according to the change of os detector.
This commit is contained in:
@ -73,7 +73,7 @@ Assume \c blabla() function is Windows specific.
|
||||
We have following example code:
|
||||
|
||||
\code
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
blabla();
|
||||
#endif
|
||||
\endcode
|
||||
|
@ -11,7 +11,7 @@ Due to legacy reason, Windows defines various things which are not compatible wi
|
||||
YYCC has a way to solve the issue introduced above.
|
||||
|
||||
\code
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
#include <WinImportPrefix.hpp>
|
||||
#include <Windows.h>
|
||||
#include "other_header_depend_on_windows.h"
|
||||
|
@ -45,6 +45,8 @@ FILES
|
||||
yycc/macro/version_cmp.hpp
|
||||
yycc/macro/feature_probe.hpp
|
||||
yycc/macro/os_detector.hpp
|
||||
yycc/macro/endian_detector.hpp
|
||||
yycc/macro/compiler_detector.hpp
|
||||
yycc/macro/class_copy_move.hpp
|
||||
yycc/string.hpp
|
||||
yycc/string/reinterpret.hpp
|
||||
@ -124,6 +126,16 @@ PUBLIC
|
||||
$<$<BOOL:${YYCC_DEBUG_UE_FILTER}>:YYCC_DEBUG_UE_FILTER>
|
||||
# Iconv environment macro
|
||||
$<$<BOOL:${YYCC_ENFORCE_ICONV}>:YYCC_FEAT_ICONV>
|
||||
# OS macro
|
||||
$<$<BOOL:${WIN32}>:YYCC_OS_WINDOWS>
|
||||
$<$<PLATFORM_ID:Linux>:YYCC_OS_LINUX>
|
||||
# Compiler macro
|
||||
$<$<CXX_COMPILER_ID:GNU>:YYCC_CC_GCC>
|
||||
$<$<CXX_COMPILER_ID:Clang>:YYCC_CC_CLANG>
|
||||
$<$<CXX_COMPILER_ID:MSVC>:YYCC_CC_MSVC>
|
||||
# Endian macro
|
||||
$<$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},LITTLE_ENDIAN>:YYCC_ENDIAN_LITTLE>
|
||||
$<$<STREQUAL:${CMAKE_CXX_BYTE_ORDER},BIG_ENDIAN>:YYCC_ENDIAN_BIG>
|
||||
PRIVATE
|
||||
# Unicode charset for private using
|
||||
$<$<CXX_COMPILER_ID:MSVC>:UNICODE>
|
||||
@ -135,6 +147,8 @@ PRIVATE
|
||||
$<$<CXX_COMPILER_ID:MSVC>:/utf-8>
|
||||
)
|
||||
|
||||
# TODO: Fix GCC stacktrace link issue
|
||||
|
||||
# Install binary and headers
|
||||
install(TARGETS YYCCommonplace
|
||||
EXPORT YYCCommonplaceTargets
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "EncodingHelper.hpp"
|
||||
#include "ConsoleHelper.hpp"
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
#include "WinImportPrefix.hpp"
|
||||
#include <Windows.h>
|
||||
#include <shellapi.h>
|
||||
@ -24,7 +24,7 @@ namespace YYCC::ArgParser {
|
||||
return ArgumentList(std::move(args));
|
||||
}
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
ArgumentList ArgumentList::CreateFromWin32() {
|
||||
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw
|
||||
|
||||
|
@ -37,7 +37,7 @@ namespace YYCC::ArgParser {
|
||||
* and should not be seen as a part of arguments.
|
||||
*/
|
||||
static ArgumentList CreateFromStd(int argc, char* argv[]);
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
/**
|
||||
* @brief Create argument list from Win32 function.
|
||||
* @details
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "COMHelper.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
namespace YYCC::COMHelper {
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "YYCCInternal.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <iostream>
|
||||
|
||||
// Include Windows used headers in Windows.
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
#include "WinImportPrefix.hpp"
|
||||
#include <Windows.h>
|
||||
#include <io.h>
|
||||
@ -16,7 +16,7 @@
|
||||
namespace YYCC::ConsoleHelper {
|
||||
|
||||
#pragma region Windows Specific Functions
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
static bool RawEnableColorfulConsole(FILE* fs) {
|
||||
if (!_isatty(_fileno(fs))) return false;
|
||||
@ -161,7 +161,7 @@ namespace YYCC::ConsoleHelper {
|
||||
#pragma endregion
|
||||
|
||||
bool EnableColorfulConsole() {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
bool ret = true;
|
||||
ret &= RawEnableColorfulConsole(stdout);
|
||||
@ -177,7 +177,7 @@ namespace YYCC::ConsoleHelper {
|
||||
}
|
||||
|
||||
yycc_u8string ReadLine() {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// get stdin mode
|
||||
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
|
||||
@ -221,7 +221,7 @@ namespace YYCC::ConsoleHelper {
|
||||
strl += YYCC_U8("\n");
|
||||
}
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
// call Windows specific writer
|
||||
WinConsoleWrite(strl, bIsErr);
|
||||
#else
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "DialogHelper.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "EncodingHelper.hpp"
|
||||
#include "StringHelper.hpp"
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "YYCCInternal.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "COMHelper.hpp"
|
||||
#include <string>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "ExceptionHelper.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "WinFctHelper.hpp"
|
||||
#include "ConsoleHelper.hpp"
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "YYCCInternal.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
/**
|
||||
* @brief Windows specific unhandled exception processor.
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
#include "WinImportPrefix.hpp"
|
||||
#include <Windows.h>
|
||||
#include "WinImportSuffix.hpp"
|
||||
@ -16,7 +16,7 @@
|
||||
namespace YYCC::IOHelper {
|
||||
|
||||
std::FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode) {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// convert mode and file path to wchar
|
||||
std::wstring wmode, wpath;
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "WinFctHelper.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "EncodingHelper.hpp"
|
||||
#include "COMHelper.hpp"
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "YYCCInternal.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -50,7 +50,7 @@
|
||||
// If we are in Windows,
|
||||
// we need add 2 macros to disable Windows shitty warnings and errors of
|
||||
// depracted functions and not secure functions.
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
@ -1,8 +1,9 @@
|
||||
#include "iconv.hpp"
|
||||
|
||||
#if YYCC_FEAT_ICONV || (YYCC_OS != YYCC_OS_WINDOWS)
|
||||
#if YYCC_FEAT_ICONV || !defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "../string/reinterpret.hpp"
|
||||
#include "../macro/endian_detector.hpp"
|
||||
#include <cerrno>
|
||||
#include <stdexcept>
|
||||
#include <cstdint>
|
||||
@ -190,20 +191,30 @@ namespace yycc::encoding::iconv {
|
||||
// That's not what we expected.
|
||||
// So we need manually check runtime endian and explicitly specify endian in code name.
|
||||
|
||||
// TODO: fix this encoding endian issue.
|
||||
|
||||
static const NS_YYCC_STRING::u8char* UTF8_CODENAME_LITERAL = YYCC_U8("UTF-8");
|
||||
static const NS_YYCC_STRING::u8char* WCHAR_CODENAME_LITERAL = YYCC_U8("WCHAR_T");
|
||||
static const NS_YYCC_STRING::u8char* fetch_utf16_codename() {
|
||||
return YYCC_U8("UTF16");
|
||||
#if defined(YYCC_ENDIAN_LITTLE)
|
||||
return YYCC_U8("UTF16LE");
|
||||
#else
|
||||
return YYCC_U8("UTF16BE");
|
||||
#endif
|
||||
}
|
||||
static const NS_YYCC_STRING::u8char* UTF16_CODENAME_LITERAL = fetch_utf16_codename();
|
||||
static const NS_YYCC_STRING::u8char* fetch_utf32_codename() {
|
||||
return YYCC_U8("UTF32");
|
||||
#if defined(YYCC_ENDIAN_LITTLE)
|
||||
return YYCC_U8("UTF32LE");
|
||||
#else
|
||||
return YYCC_U8("UTF32BE");
|
||||
#endif
|
||||
}
|
||||
static const NS_YYCC_STRING::u8char* UTF32_CODENAME_LITERAL = fetch_utf32_codename();
|
||||
|
||||
// TODO: There is a memory copy in this function. Consider removing it in future.
|
||||
// TODO:
|
||||
// There is a memory copy in this function. Consider optimizing it in future.
|
||||
// A possible solution is that create a std::vector-like wrapper for std::basic_string and std::basic_string_view.
|
||||
// We call them VecString and VecStringView, and use them in "iconv_kernel" instead of real std::vector.
|
||||
// They exposed interface are std::vector-like but its inner is std::basic_string and std::basic_string_view.
|
||||
#define CONVFN_TYPE0(src_char_type, dst_char_type) \
|
||||
namespace expected = NS_YYCC_PATCH_EXPECTED; \
|
||||
auto rv = iconv_kernel(this->token, reinterpret_cast<const uint8_t*>(src.data()), src.size()); \
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include "../macro/os_detector.hpp"
|
||||
|
||||
#if YYCC_FEAT_ICONV || (YYCC_OS != YYCC_OS_WINDOWS)
|
||||
#if YYCC_FEAT_ICONV || !defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "../macro/class_copy_move.hpp"
|
||||
#include "../patch/expected.hpp"
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "windows.hpp"
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "../string/reinterpret.hpp"
|
||||
#include <limits>
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include "../macro/os_detector.hpp"
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#include "../patch/expected.hpp"
|
||||
#include "../string.hpp"
|
||||
|
5
src/yycc/macro/compiler_detector.hpp
Normal file
5
src/yycc/macro/compiler_detector.hpp
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#if (defined(YYCC_CC_MSVC) + defined(YYCC_CC_GCC) + defined(YYCC_CC_CLANG)) != 1
|
||||
#error "Current compiler is not supported!"
|
||||
#endif
|
7
src/yycc/macro/endian_detector.hpp
Normal file
7
src/yycc/macro/endian_detector.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
// Check endian
|
||||
#if (defined(YYCC_ENDIAN_LITTLE) + defined(YYCC_ENDIAN_BIG)) != 1
|
||||
#error "Current system endian (byte order) is not supported!"
|
||||
#endif
|
||||
|
@ -1,11 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
// Define operating system macros
|
||||
#define YYCC_OS_WINDOWS 2
|
||||
#define YYCC_OS_LINUX 3
|
||||
// Check current operating system.
|
||||
#if defined(_WIN32)
|
||||
#define YYCC_OS YYCC_OS_WINDOWS
|
||||
#else
|
||||
#define YYCC_OS YYCC_OS_LINUX
|
||||
// Check OS macro
|
||||
#if (defined(YYCC_OS_WINDOWS) + defined(YYCC_OS_LINUX)) != 1
|
||||
#error "Current operating system is not supported!"
|
||||
#endif
|
||||
|
@ -12,7 +12,7 @@ namespace yycc::patch::path {
|
||||
// So we need add feature test macro at the same time.
|
||||
|
||||
std::filesystem::path to_std_path(const NS_YYCC_STRING::u8string_view& u8_path) {
|
||||
// #if YYCC_OS == YYCC_OS_WINDOWS
|
||||
// #if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// // convert path to wchar
|
||||
// std::wstring wpath;
|
||||
@ -29,7 +29,7 @@ namespace yycc::patch::path {
|
||||
}
|
||||
|
||||
NS_YYCC_STRING::u8string to_u8string(const std::filesystem::path& path) {
|
||||
// #if YYCC_OS == YYCC_OS_WINDOWS
|
||||
// #if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// // get and convert to utf8
|
||||
// NS_YYCC_STRING::u8string u8_path;
|
||||
|
@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
// Suppress unsafe warning on Windows
|
||||
#include "../windows/unsafe_suppressor.hpp"
|
||||
|
||||
// Prelude section
|
||||
#include "../string.hpp"
|
||||
namespace yycc::prelude {
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include "../macro/os_detector.hpp"
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// Define 2 macros to disallow Windows generate MIN and MAX macros
|
||||
// which cause std::min and std::max can not function as normal.
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include "../macro/os_detector.hpp"
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
// Windows also will generate following macros
|
||||
// which may cause the function sign is different in Windows and other platforms.
|
||||
|
@ -4,7 +4,7 @@
|
||||
// If we are in Windows,
|
||||
// we need add 2 macros to disable Windows shitty warnings and errors of
|
||||
// depracted functions and not secure functions.
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
@ -167,7 +167,7 @@ namespace YYCCTestbench {
|
||||
}
|
||||
|
||||
// check wstring convertion on windows
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
for (size_t i = 0u; i < count; ++i) {
|
||||
// get item
|
||||
const auto& u8str = c_UTF8TestStrTable[i];
|
||||
@ -306,7 +306,7 @@ namespace YYCCTestbench {
|
||||
}
|
||||
|
||||
static void DialogTestbench() {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
YYCC::yycc_u8string ret;
|
||||
std::vector<YYCC::yycc_u8string> rets;
|
||||
@ -340,7 +340,7 @@ namespace YYCCTestbench {
|
||||
}
|
||||
|
||||
static void ExceptionTestbench() {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
YYCC::ExceptionHelper::Register([](const YYCC::yycc_u8string& log_path, const YYCC::yycc_u8string& coredump_path) -> void {
|
||||
MessageBoxW(
|
||||
@ -374,7 +374,7 @@ namespace YYCCTestbench {
|
||||
}
|
||||
|
||||
static void WinFctTestbench() {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
HMODULE test_current_module;
|
||||
Assert((test_current_module = YYCC::WinFctHelper::GetCurrentModule()) != nullptr, YYCC_U8("YYCC::WinFctHelper::GetCurrentModule"));
|
||||
@ -412,7 +412,7 @@ namespace YYCCTestbench {
|
||||
}
|
||||
YYCC::yycc_u8string test_slashed_path(YYCC::StdPatch::ToUTF8Path(test_path));
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
std::wstring wdelimiter(1u, std::filesystem::path::preferred_separator);
|
||||
YYCC::yycc_u8string delimiter(YYCC::EncodingHelper::WcharToUTF8(wdelimiter));
|
||||
#else
|
||||
@ -622,7 +622,7 @@ namespace YYCCTestbench {
|
||||
YYCC::ConsoleHelper::FormatLine(YYCC_U8("\t%s"), result.Argument().c_str());
|
||||
}
|
||||
}
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
{
|
||||
YYCC::ConsoleHelper::WriteLine(YYCC_U8("YYCC::ArgParser::ArgumentList::CreateFromWin32"));
|
||||
auto result = YYCC::ArgParser::ArgumentList::CreateFromWin32();
|
||||
|
Reference in New Issue
Block a user