8000 Feat: support msys2 by yumetodo · Pull Request #8 · kazatsuyu/fafnir · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feat: support msys2 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ RelWithDebInfo/
*.sublime-project

# CodeBlocks
*.cbp
*.cbp
### https://raw.github.com/github/gitignore/1a9f616ce525987555d43c72d59eb830d1856bd7/Global/VisualStudioCode.gitignore

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# other
install*/
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.10.0)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17) # C++17...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この方法に変えてしまうと非mingw環境で動かないので戻してください

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どう動かないのでしょう?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMakeの-Gオプションで"Visual Studio 15 2017" を指定した場合に set(CMAKE_CXX_STANDARD 17) だと-std=c++17もしくは/std:c++17オプションが付かない。原因を詳しく調査はしていないが、CMAKE_CXX_FLAGSを使っているのはそのため。回避策が分かるなら変更してもいい


project(fafnir VERSION 1.0.1.0)

if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang|GNU|Intel")
set(CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_FLAGS}")
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
set(CMAKE_CXX_FLAGS "/std:c++17 ${CMAKE_CXX_FLAGS}")
endif()

enable_testing()

add_subdirectory(src)
Expand Down
34 changes: 34 additions & 0 deletions cmake/FindIutest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# - Try to find iutest include dirs
#
# Usage of this module as follows:
#
# find_package(IUTEST)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
# IUTEST_ROOT_DIR Set this variable to the root installation of
# iutest if the module has problems finding the
# proper installation path.
#
# Variables defined by this module:
#
# IUTEST_FOUND System has iutest, include dirs found
# IUTEST_INCLUDE_DIR The iutest include directories.

find_path(IUTEST_ROOT_DIR
NAMES include/iutest.hpp
HINTS
ENV IUTEST_ROOT
)

find_path(IUTEST_INCLUDE_DIR
NAMES iutest.hpp
HINTS
${IUTEST_ROOT_DIR}/include
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(IUTEST DEFAULT_MSG
IUTEST_INCLUDE_DIR
)
34 changes: 21 additions & 13 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $VSInstallDir = Get-Registry Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Vis
if (!$VSInstallDir) {
$VSInstallDir = Get-Registry Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 "15.0"
}
$LLVMDir = Get-Registry Registry::HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM -ErrorAction
$LLVMDir = Get-Registry Registry::HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM -ErrorAction
if (!$LLVMDir) {
$LLVMDir = Get-Registry Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\LLVM\LLVM
}
Expand Down Expand Up @@ -78,11 +78,11 @@ do {
break
}
}

if ($reset -or $ToolsetName -eq "") {
$prompt = "What is the clang toolset name?"
if ($reset) {
$prompt += "(current: $ToolsetName)"
$prompt += "(current: $ToolsetName)"
} else {
$prompt += " (default: v100_clang_fafnir)"
}
Expand All @@ -93,11 +93,11 @@ do {
$ToolsetName = $tmp
}
}

if ($reset -or ($ClangClToolsetName -eq "" -and (Read-Host "Do you want to install a toolset for clang-cl? (y/N)") -match "y|yes")) {
$prompt = "What is the clang-cl toolset name?"
if ($reset) {
$prompt += "(current: $ClangClToolsetName)"
$prompt += "(current: $ClangClToolsetName)"
} else {
$prompt += " (default: fafnir_clang_cl)"
}
Expand All @@ -108,7 +108,7 @@ do {
$ClangClToolsetName = $tmp
}
}

""
"=== Install configuration ==="
"* LLVM install directory: $LLVMDirectory"
Expand All @@ -133,20 +133,28 @@ function Install ($arch) {
return
}
$targetPath = "$platformDir\$ToolsetName"

if (!(Test-Path $targetPath)) {
New-Item -ItemType Directory $targetPath
}
Copy-Item "$assets\clang\Toolset.targets" "$targetPath"
$content = (Get-Content -Encoding UTF8 "$assets\clang\Toolset.props") -replace "{{LLVMDir}}",$LLVMDirectory
Set-Content "$targetPath\Toolset.props" $content -Encoding UTF8

if (!(Test-Path "$targetPath\bin")) {
New-Item -ItemType Directory "$targetPath\bin"

$targetBinPath = "$targetPath\bin"
if (!(Test-Path $targetBinPath)) {
New-Item -ItemType Directory $targetBinPath
}
Copy-Item $bin $targetBinPath
Copy-Item $dll $targetBinPath
$mingwDLLs = @("libstdc++-6.dll", "libwinpthread-1.dll", "libgcc_s_seh-1.dll", "libgcc_s_dw2-1.dll")
foreach ($mingwDLL in $mingwDLLs) {
$mingwDLLFullPath = "${rootDir}\bin\${mingwDLL}"
if (Test-Path $mingwDLLFullPath) {
Copy-Item $mingwDLLFullPath $targetBinPath
}
}
Copy-Item $bin "$targetPath\bin"
Copy-Item $dll "$targetPath\bin"
Set-Content -Path "$targetPath\bin\.target" "$LLVMDirectory\bin\clang.exe" -Encoding UTF8 -NoNewline
Set-Content -Path "${targetBinPath}\.target" "$LLVMDirectory\bin\clang.exe" -Encoding UTF8 -NoNewline

if ($ClangClToolsetName -ne "") {
$targetPath = "$platformDir\$ClangClToolsetName"
Expand Down
25 changes: 25 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
cmake_minimum_required(VERSION 3.10.0)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17) # C++17...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
add_executable(clang main.cpp custom_api.cpp)
add_library(fafnir_injection SHARED dll_main.cpp custom_api.cpp)
# remove "lib" prefix
set_target_properties(fafnir_injection PROPERTIES PREFIX "")

target_link_libraries(fafnir_injection ImageHlp)
if(CMAKE_COMPILER_IS_GNUCXX)
target_link_libraries(clang stdc++fs)
target_link_libraries(fafnir_injection stdc++fs)
endif()
if(MINGW)
message(STATUS "Installing system-libraries: MinGW DLLs.")
get_filename_component(Mingw_Path ${CMAKE_CXX_COMPILER} PATH)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${Mingw_Path}/libstdc++-6.dll ${Mingw_Path}/libwinpthread-1.dll)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
# The exception model of msys2-mingw64-gcc is SEH
list(APPEND CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${Mingw_Path}/libgcc_s_seh-1.dll)
else()
# The exception model of msys2-mingw64-gcc is DWARF-2
list(APPEND CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${Mingw_Path}/libgcc_s_dw2-1.dll)
endif()
include(InstallRequiredSystemLibraries)
install(PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT System)
endif()

target_compile_definitions(fafnir_injection PRIVATE FAFNIR_DLL)

install(TARGETS clang RUNTIME DESTINATION bin)
Expand Down
3 changes: 2 additions & 1 deletion src/custom_api.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "set_winver.hpp"
#include <fstream>

#include "fafnir.hpp"
Expand Down Expand Up @@ -143,7 +144,7 @@ BOOL WINAPI set_file_information_by_handle(
) {
if (information_class == FileRenameInfo) {
auto& info = *static_cast<PFILE_RENAME_INFO>(file_information);
std::ofstream(info.FileName, std::ios::ate | std::ios::binary);
std::ofstream(fs::path(info.FileName).string(), std::ios::ate | std::ios::binary);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字コード変換をサボって雑にやっている。日本語パスが来た時にどうなるのか怖い

}
return orig_SetFileInformationByHandle(file, information_class, file_information, size);
}
Expand Down
3 changes: 2 additions & 1 deletion src/dll_main.cpp
10000
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "set_winver.hpp"
#include <cstddef>
#include <Windows.h>
#include <TlHelp32.h>
#include <Dbghelp.h>
#include <Dbghelp.h>

#include "custom_api.hpp"
#include "fafnir.hpp"
Expand Down
6 changes: 3 additions & 3 deletions src/fafnir.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#ifndef FAFNIR_FAFNIR_HPP
#define FAFNIR_FAFNIR_HPP

#include <filesystem>
#include "filesystem.hpp"
#include <memory>

#include <Windows.h>

namespace fafnir {

inline std::experimental::filesystem::path get_bin_path() {
inline fs::path get_bin_path() {
auto module = GetModuleHandleW(L"fafnir_injection.dll");
for(std::vector<wchar_t> buf(256);;buf.resize(buf.size() * 2)) {
std::size_t size = GetModuleFileNameW(module, buf.data(), buf.size());
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
std::experimental::filesystem::path path{buf.begin(), buf.begin() + size};
fs::path path{buf.begin(), buf.begin() + size};
return path.parent_path();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef FAFNIR_FILESYSTEM_HPP
#define FAFNIR_FILESYSTEM_HPP

#ifdef __GNUC__
# include <experimental/filesystem>
#else
# include <filesytsem>
#endif
namespace fs = std::experimental::filesystem;
#endif //FAFNIR_FILESYSTEM_HPP
13 changes: 7 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "set_winver.hpp"
#include <string_view>
#include <iostream>
#include <filesystem>
#include "filesystem.hpp"
#include <fstream>
#include <vector>
#include <sstream>
Expand Down Expand Up @@ -61,7 +62,7 @@ file_type get_file_type(std::istream& in) noexcept {
}

template<auto f>
std::experimental::filesystem::path read_path_impl(std::istream& in) noexcept {
fs::path read_path_impl(std::istream& in) noexcept {
std::ostringstream ss;

for (auto ch = f(in); ch != std::char_traits<char32_t>::eof(); ch = f(in)) {
Expand All @@ -73,7 +74,7 @@ std::experimental::filesystem::path read_path_impl(std::istream& in) noexcept {
reinterpret_cast<const wchar_t*>(str.data() + str.size())};
}

std::experimental::filesystem::path read_path(std::istream& in) noexcept {
fs::path read_path(std::istream& in) noexcept {
auto type = get_file_type(in);
if (type == file_type::utf8_with_bom) {
in.seekg(3);
Expand All @@ -90,14 +91,14 @@ int main() {
using namespace fafnir;

auto path = get_bin_path() / ".target";
if (!std::experimental::filesystem::exists(path)) {
if (!fs::exists(path)) {
std::wcerr << "error: " << path << " doesn't exist." << std::endl;
return 1;
}
std::ifstream target(path, std::ios::binary);
std::ifstream target(path.string(), std::ios::binary);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字コード変換をサボって雑にやっている。日本語パスが来た時にどうなるのか怖い

auto target_path = read_path(target);
target.close();
if (!std::experimental::filesystem::exists(target_path)) {
if (!fs::exists(target_path)) {
std::wcerr << "error: " << target_path << " doesn't exist." << std::endl;
return 1;
}
Expand Down
11 changes: 11 additions & 0 deletions src/set_winver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef FAFNIR_SET_WINVER_HPP
#define FAFNIR_SET_WINVER_HPP
#ifdef _WIN32_WINNT
# if _WIN32_WINNT < 0x0600
# error "target OS must be Windows Vista or higher"
# endif
#endif
#ifdef __GNUC__
# define _WIN32_WINNT 0x0600 // Windows Vista
#endif
#endif //FAFNIR_SET_WINVER_HPP
14 changes: 13 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
cmake_minimum_required(VERSION 3.10.0)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17) # C++17...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...

add_executable(unicode_test unicode_test.cpp)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
if(EXISTS ${CMAKE_SOURCE_DIR}/submodules/iutest/include)
set(IUTEST_ROOT_DIR ${CMAKE_SOURCE_DIR}/submodules/iutest)
endif()
include(FindIutest)
find_package(IUTEST)

add_executable(unicode_test unicode_test.cpp)
include_directories(${IUTEST_INCLUDE_DIR})
target_link_libraries(unicode_test Ws2_32)
add_test(
NAME unicode_test
COMMAND $<TARGET_FILE:unicode_test>
Expand Down
2 changes: 1 addition & 1 deletion tests/unicode_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../src/unicode.hpp"
#include "../submodules/iutest/include/iutest.hpp"
#include <iutest.hpp>
#include <sstream>
#include <string_view>

Expand Down
0