[env]: Change hard-code settings to be generated by py

This commit is contained in:
fanlumaster
2025-06-02 10:12:09 +08:00
parent 01514100a1
commit 5cd5964351
13 changed files with 288 additions and 27 deletions

11
.clangd
View File

@ -1,8 +1,11 @@
CompileFlags:
Add: [
"-std=c++17",
# -Wno-unused-value,
] # ignore some specific warnings
CompilationDatabase: build/ # Search build/ directory for compile_commands.json
-DUNICODE,
-D_UNICODE,
"-std=c++17",
"-IC:/Users/19833/EDisk/CppCodes/ImeCodes/googlepinyinime-rev/build/vcpkg_installed/x64-windows/include",
# -Wno-unused-value,
] # ignore some specific warnings
CompilationDatabase: build/ # Search build/ directory for compile_commands.json
# Diagnostics:
# UnusedIncludes: None # ignore unused includes

View File

@ -4,13 +4,24 @@ project(pinyinime VERSION 1.0 LANGUAGES CXX)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/include)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_C_STANDARD_REQUIRED ON)
add_definitions(
/D_UNICODE=1
/DUNICODE=1
)
# For utf8cpp, make it use C++11 and later
add_compile_options(/Zc:__cplusplus)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# For pure-header libs installed by vcpkg like utf8cpp
include_directories("./build/vcpkg_installed/x64-windows/include")
set(HEADERS
./src/include/atomdictbase.h
./src/include/dictbuilder.h

17
CMakePresets.json Normal file
View File

@ -0,0 +1,17 @@
{
"version": 2,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build",
"environment": {
"VCPKG_ROOT": "C:/Users/19833/scoop/apps/vcpkg/current/"
},
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "C:/Users/19833/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake",
"CMAKE_BUILD_TYPE": "Debug"
}
}
]
}

11
CMakeUserPresets.json Normal file
View File

@ -0,0 +1,11 @@
{
"version": 2,
"configurePresets": [
{
"name": "default",
"inherits": "vcpkg",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build"
}
]
}

View File

@ -0,0 +1,11 @@
CompileFlags:
Add: [
-DUNICODE,
-D_UNICODE,
"-std=c++17",
"-IC:/Users/19833/EDisk/CppCodes/ImeCodes/googlepinyinime-rev/build/vcpkg_installed/x64-windows/include",
# -Wno-unused-value,
] # ignore some specific warnings
CompilationDatabase: build/ # Search build/ directory for compile_commands.json
# Diagnostics:
# UnusedIncludes: None # ignore unused includes

View File

@ -0,0 +1,68 @@
cmake_minimum_required(VERSION 3.15)
project(pinyinime VERSION 1.0 LANGUAGES CXX)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/include)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
add_definitions(
/D_UNICODE=1
/DUNICODE=1
)
# For utf8cpp, make it use C++11 and later
add_compile_options(/Zc:__cplusplus)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# For pure-header libs installed by vcpkg like utf8cpp
include_directories("./build/vcpkg_installed/x64-windows/include")
set(HEADERS
./src/include/atomdictbase.h
./src/include/dictbuilder.h
./src/include/dictdef.h
./src/include/dictlist.h
./src/include/dicttrie.h
./src/include/lpicache.h
./src/include/matrixsearch.h
./src/include/mystdlib.h
./src/include/ngram.h
./src/include/pinyinime.h
./src/include/searchutility.h
./src/include/spellingtable.h
./src/include/spellingtrie.h
./src/include/splparser.h
./src/include/sync.h
./src/include/userdict.h
./src/include/utf16char.h
./src/include/utf16reader.h
)
set(SOURCES
./src/share/dictbuilder.cpp
./src/share/dictlist.cpp
./src/share/dicttrie.cpp
./src/share/lpicache.cpp
./src/share/matrixsearch.cpp
./src/share/mystdlib.cpp
./src/share/ngram.cpp
./src/share/pinyinime.cpp
./src/share/searchutility.cpp
./src/share/spellingtable.cpp
./src/share/spellingtrie.cpp
./src/share/splparser.cpp
./src/share/sync.cpp
./src/share/userdict.cpp
./src/share/utf16char.cpp
./src/share/utf16reader.cpp
./tests/main.cpp
)
set(MY_EXECUTABLE_NAME "pinyinime")
add_executable(pinyinime ${SOURCES} ${HEADERS})

View File

@ -0,0 +1,17 @@
{
"version": 2,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build",
"environment": {
"VCPKG_ROOT": "C:/Users/19833/scoop/apps/vcpkg/current/"
},
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "C:/Users/19833/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake",
"CMAKE_BUILD_TYPE": "Debug"
}
}
]
}

View File

@ -2,25 +2,23 @@
$currentDirectory = Get-Location
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt"
if (-not (Test-Path $cmakeListsPath))
{
Write-Host("No CMakeLists.txt in current directory, please check.")
return
if (-not (Test-Path $cmakeListsPath)) {
Write-Host("No CMakeLists.txt in current directory, please check.")
return
}
Write-Host "Start generating and compiling..."
$buildFolderPath = ".\build"
if (-not (Test-Path $buildFolderPath))
{
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
if (-not (Test-Path $buildFolderPath)) {
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null
Write-Host "build folder created."
}
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/
# cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/
cmake --preset=default
if ($LASTEXITCODE -eq 0)
{
cmake --build ./build/ --config DEBUG
if ($LASTEXITCODE -eq 0) {
cmake --build build
}

View File

@ -31,10 +31,11 @@ if (-not (Test-Path $buildFolderPath)) {
Write-Host "build folder created."
}
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build/
# cmake -G -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/Users/SonnyCalcr/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake -S . -B ./build/
cmake --preset=default
if ($LASTEXITCODE -eq 0) {
cmake --build ./build/ --config DEBUG
cmake --build build
if ($LASTEXITCODE -eq 0) {
$exePath = getExePathFromCMakeLists
Write-Host "start running as follows..."
@ -42,4 +43,3 @@ if ($LASTEXITCODE -eq 0) {
Invoke-Expression $exePath
}
}

96
scripts/prepare_env.py Normal file
View File

@ -0,0 +1,96 @@
import os
def normpath(path):
return path.replace("\\", "/")
local_app_data_dir = os.environ.get("LOCALAPPDATA")
home_dir = os.path.expanduser("~")
cur_file_path = os.path.dirname(os.path.abspath(__file__))
project_root_path = os.path.dirname(cur_file_path)
user_home = os.path.expanduser("~")
googlepinyinime_rev_root_path = normpath(project_root_path)
googlepinyinime_rev_src_path = normpath(
os.path.join(googlepinyinime_rev_root_path, "src")
)
vcpkg_include_path = normpath(
os.path.join(
googlepinyinime_rev_root_path,
"build",
"vcpkg_installed",
"x64-windows",
"include",
)
)
webview2_path = normpath(
os.path.join(
user_home,
".nuget",
"packages",
"microsoft.web.webview2",
"1.0.3240.44",
"build",
"native",
"include",
)
)
wim_path = normpath(
os.path.join(
user_home,
".nuget",
"packages",
"microsoft.windows.implementationlibrary",
"1.0.240803.1",
"include",
)
)
boost_path = normpath(os.path.join(user_home, "scoop", "apps", "boost", "current"))
#
# project_root/.clangd
#
dot_clangd_file = os.path.join(
googlepinyinime_rev_root_path, "scripts", "config_files", ".clangd"
)
dot_clangd_output_file = os.path.join(googlepinyinime_rev_root_path, ".clangd")
with open(dot_clangd_file, "r", encoding="utf-8") as f:
lines = f.readlines()
lines[5] = (
f' "-I{googlepinyinime_rev_root_path}/build/vcpkg_installed/x64-windows/include",\n'
)
with open(dot_clangd_output_file, "w", encoding="utf-8") as f:
f.writelines(lines)
#
# project_root/tests/CMakeLists.txt
#
CMakeLists_file = os.path.join(
googlepinyinime_rev_root_path, "scripts", "config_files", "CMakeLists.txt"
)
CMakeLists_output_file = os.path.join(googlepinyinime_rev_root_path, "CMakeLists.txt")
with open(CMakeLists_file, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(CMakeLists_output_file, "w", encoding="utf-8") as f:
f.writelines(lines)
#
# CMakePresets.json
#
CMakePresets_file = os.path.join(
googlepinyinime_rev_root_path, "scripts", "config_files", "CMakePresets.json"
)
CMakePresets_file_output_file = os.path.join(
googlepinyinime_rev_root_path, "CMakePresets.json"
)
with open(CMakePresets_file, "r", encoding="utf-8") as f:
lines = f.readlines()
lines[8] = f' "VCPKG_ROOT": "{normpath(user_home)}/scoop/apps/vcpkg/current/"\n'
lines[11] = (
f' "CMAKE_TOOLCHAIN_FILE": "{normpath(user_home)}/scoop/apps/vcpkg/current/scripts/buildsystems/vcpkg.cmake",\n'
)
with open(CMakePresets_file_output_file, "w", encoding="utf-8") as f:
f.writelines(lines)

View File

@ -1,16 +1,22 @@
#include "../src/include/pinyinime.h"
#include <codecvt>
#include <chrono>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
#include <cstring>
#include <utf8cpp/utf8.h>
#include <Windows.h>
std::string fromUtf16(const ime_pinyin::char16 *buf, size_t len) {
std::u16string utf16Str(reinterpret_cast<const char16_t *>(buf), len);
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(utf16Str);
// 先将输入的 UTF-16 buffer 转成 std::u16string等价于 char16_t
const char16_t *utf16Data = reinterpret_cast<const char16_t *>(buf);
std::u16string utf16Str(utf16Data, len);
std::string utf8Result;
// 使用 utfcpp 提供的 utf16to8 转换迭代器,将 UTF-16 转为 UTF-8
utf8::utf16to8(utf16Str.begin(), utf16Str.end(), std::back_inserter(utf8Result));
return utf8Result;
}
void test_pinyin_search_and_segment(const std::string &user_pinyin) {
@ -88,6 +94,9 @@ void test_pinyin_search_when_retriving_first_element(const std::string &user_pin
}
int main() {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
ime_pinyin::im_set_max_lens(64, 32);
if (!ime_pinyin::im_open_decoder("./data/dict_pinyin.dat", "./data/user_dict.dat")) {
std::cout << "fany bug.\n";
@ -107,5 +116,6 @@ int main() {
test_pinyin_search_and_segment("ni'shuo'ni'ma'ne");
test_pinyin_search_when_retriving_first_element("qunimadegouridequsibawonengzenmeban");
test_pinyin_search_when_retriving_first_element("zh'ta'ma'an'jing");
test_pinyin_search_when_retriving_first_element("ni'shuo'ne'ma'de");
return 0;
}

14
vcpkg-configuration.json Normal file
View File

@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "b02e341c927f16d991edbd915d8ea43eac52096c",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}

5
vcpkg.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": [
"utfcpp"
]
}