Skip to content

Commit

Permalink
Merge pull request #4 from ntoskrnl7/features/iostream
Browse files Browse the repository at this point in the history
Features/iostream
  • Loading branch information
ntoskrnl7 authored Apr 30, 2022
2 parents 9c047cf + b8053b6 commit dfa196c
Show file tree
Hide file tree
Showing 61 changed files with 1,490 additions and 678 deletions.
195 changes: 114 additions & 81 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,42 @@ CPMAddPackage("gh:ntoskrnl7/FindWDK#master")
list(APPEND CMAKE_MODULE_PATH "${FindWDK_SOURCE_DIR}/cmake")
find_package(WDK REQUIRED)

#
# Add source files.
#

file(GLOB SOURCE_FILES
src/*.cpp

# common
src/custom/common/*.cpp
src/custom/common/*.c
src/custom/common/*/*.cpp
src/custom/common/*/*.c

# ucrt
src/custom/ucrt/common/*/*.cpp
src/custom/ucrt/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/*.cpp
)

if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
file(GLOB MATH_SOURCE_FILES
src/custom/common/math/*.c
)
list(APPEND SOURCE_FILES ${MATH_SOURCE_FILES})
endif()

if (UCXXRT_ENABLED)
CPMAddPackage("gh:ntoskrnl7/ucxxrt#master")

file(GLOB_RECURSE SOURCE_FILES
file(GLOB_RECURSE UCXXRT_SOURCE_FILES
${ucxxrt_SOURCE_DIR}/src/*.cpp
${ucxxrt_SOURCE_DIR}/vcruntime/*.cpp
${ucxxrt_SOURCE_DIR}/misc/*.cpp
)

list(REMOVE_ITEM SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/sys_main.cpp)
list(REMOVE_ITEM SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/unittest.cpp)

list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/crt.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/locales.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/sys_main.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/unittest.cpp)
if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
file(GLOB PLATFORM_SOURCE_FILES
${ucxxrt_SOURCE_DIR}/src/x64/*.cpp
Expand All @@ -63,38 +87,20 @@ if (UCXXRT_ENABLED)
${ucxxrt_SOURCE_DIR}/src/i386/*.asm
)
endif()

list(APPEND SOURCE_FILES ${PLATFORM_SOURCE_FILES})
add_definitions(/FI"${ucxxrt_SOURCE_DIR}/ucxxrt.inl")

list(APPEND SOURCE_FILES
src/main.cpp
src/custom/common/misc.cpp
src/custom/common/math/fpconst.c
src/custom/common/math/pow.c
src/custom/common/math/pow.c
src/custom/msvc/common/crt/src/stl/winapisupp.cpp
)
else()
file(GLOB SOURCE_FILES
src/*.cpp

# common
src/custom/common/*.cpp
src/custom/common/*.c
src/custom/common/*/*.cpp

# ucrt
src/custom/ucrt/common/*/*.cpp
src/custom/ucrt/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/*.cpp
)

if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
file(GLOB MATH_SOURCE_FILES
src/custom/common/math/*.c
)
list(APPEND SOURCE_FILES ${MATH_SOURCE_FILES})
endif()
list(APPEND UCXXRT_SOURCE_FILES ${PLATFORM_SOURCE_FILES})

list(APPEND SOURCE_FILES ${UCXXRT_SOURCE_FILES})
list(APPEND SOURCE_FILES src/custom/msvc/common/crt/src/stl/winapisupp.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/common/isa_available_init.c)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/common/errno.c)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/common/math/ftol3.c)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/startup/thread.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/startup/initterm.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/startup/onexit.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/startup/argv_parsing.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/heap/malloc.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/misc/terminate.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/common/misc/invalid_parameter.cpp)
endif()

wdk_add_library(
Expand All @@ -104,55 +110,93 @@ wdk_add_library(
${SOURCE_FILES}
)

#
# Add include directories
#
get_target_property(INC_DIR_TMP crtsys INCLUDE_DIRECTORIES)
if (UCXXRT_ENABLED)
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${Ldk_SOURCE_DIR}/include/Ldk;${ucxxrt_SOURCE_DIR}/src;${ucxxrt_SOURCE_DIR}/src/vcruntime;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
target_include_directories(crtsys PRIVATE "${ucxxrt_SOURCE_DIR}")
else()
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${Ldk_SOURCE_DIR}/include/Ldk;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
endif()

target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/include")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/vcruntime")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/stl")

#
# link libraries
#
if(CRTSYS_USE_LIBCNTPR)
target_link_libraries(crtsys PRIVATE $<BUILD_INTERFACE:Ldk> WDK::LIBCNTPR)
else()
target_link_libraries(crtsys PRIVATE $<BUILD_INTERFACE:Ldk>)
endif()

#
# Add compile definitions
#
target_compile_definitions(crtsys PUBLIC "_ITERATOR_DEBUG_LEVEL=0")
if (CRTSYS_USE_LIBCNTPR)
target_compile_definitions(crtsys PUBLIC "CRTSYS_USE_LIBCNTPR")
if ("${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" VERSION_GREATER_EQUAL "10.0.22000.0")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_IOB_FUNC")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_ABORT")
endif()
else()
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_IOB_FUNC")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_ABORT")
endif()

if (CRTSYS_NTL_MAIN)
target_compile_definitions(crtsys PUBLIC CRTSYS_USE_NTL_MAIN)
endif()
target_compile_definitions(crtsys PUBLIC "_HAS_EXCEPTIONS")



if (UCXXRT_ENABLED)
target_compile_definitions(crtsys PRIVATE "UCXXRT")
target_compile_definitions(crtsys PUBLIC "UCXXRT_X86_NO_THROW_TEST")
target_compile_definitions(crtsys PRIVATE "__KERNEL_MODE")
target_include_directories(crtsys PRIVATE "${ucxxrt_SOURCE_DIR}")
endif()



get_target_property(INC_DIR_TMP crtsys INCLUDE_DIRECTORIES)
#
# Forced Include File
#
target_compile_options(crtsys PRIVATE /FI"${CMAKE_CURRENT_SOURCE_DIR}/src/crtsys.h")
if (UCXXRT_ENABLED)
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${Ldk_SOURCE_DIR}/include/Ldk;${ucxxrt_SOURCE_DIR}/src;${ucxxrt_SOURCE_DIR}/src/vcruntime;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
else()
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${Ldk_SOURCE_DIR}/include/Ldk;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
target_compile_options(crtsys PRIVATE /FI"${ucxxrt_SOURCE_DIR}/ucxxrt.inl")
endif()

target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/include")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/vcruntime")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/stl")


add_definitions(/FI"${CMAKE_CURRENT_SOURCE_DIR}/src/crtsys.h")
#
# TLS isn't supported yet.
#
target_compile_options(crtsys PUBLIC /Zc:threadSafeInit-)

if (UCXXRT_ENABLED AND "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
# VS2017 Build Tools
# ntoskrnl.lib(ntoskrnl.exe) : error LNK2005: RtlRaiseException already defined in Ldk.lib(except.obj).
target_link_options(crtsys PUBLIC "/FORCE:MULTIPLE")
endif()
target_compile_options(crtsys PUBLIC "/MT") # "/MT$<$<CONFIG:Debug>:d>"
# [not working] set_property(TARGET crtsys PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

#
# Generate Debug Info
#
target_compile_options(crtsys PUBLIC "$<$<CONFIG:Release>:/Zi>")
target_link_options(crtsys PUBLIC "$<$<CONFIG:Release>:/DEBUG>")

if (CRTSYS_USE_LIBCNTPR AND "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
#
# Force File Output
#
if (CRTSYS_USE_LIBCNTPR)
# 10.0.17763.0
# 10.0.22000.0
# km/x64/libcntpr.lib(fpexcept.obj) : error LNK2005: RaiseException already defined in Ldk.lib(errhandlingapi.obj)

# 10.0.17763.0
# km/x64/ntoskrnl.lib(ntoskrnl.exe) : error LNK2005: _wcslen already defined in km/x64/libcntpr.lib(wcslen.obj)

# 10.0.17763.0
# libcntpr.lib fflush
# src\custom\ucrt\common\stdio\fflush.cpp fflush
target_link_options(crtsys PUBLIC "/FORCE:MULTIPLE")
elseif(UCXXRT_ENABLED AND "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
# ntoskrnl.lib(ntoskrnl.exe) : error LNK2005: RtlRaiseException already defined in Ldk.lib(except.obj).
target_link_options(crtsys PUBLIC "/FORCE:MULTIPLE")
endif()

Expand All @@ -168,25 +212,14 @@ if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()

target_compile_options(crtsys PUBLIC "/MT") # "/MT$<$<CONFIG:Debug>:d>"
# [not working] set_property(TARGET crtsys PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
target_compile_definitions(crtsys PUBLIC "_ITERATOR_DEBUG_LEVEL=0")

if (UCXXRT_ENABLED)
set_target_properties(crtsys PROPERTIES VS_USER_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/stl.props")
else()
set_target_properties(crtsys PROPERTIES VS_USER_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/stl.props;${CMAKE_CURRENT_SOURCE_DIR}/src/vcruntime.props;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/crt.props")
endif()



if(CRTSYS_USE_LIBCNTPR)
target_link_libraries(crtsys PRIVATE $<BUILD_INTERFACE:Ldk> WDK::LIBCNTPR)
else()
target_link_libraries(crtsys PRIVATE $<BUILD_INTERFACE:Ldk>)
#
# set visual studio properties file
#
set(VS_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/stl.props")
if (NOT UCXXRT_ENABLED)
list(APPEND VS_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/vcruntime.props;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/crt.props")
endif()


set_target_properties(crtsys PROPERTIES VS_USER_PROPS "${VS_PROPS}")

#
# set library output path (release only)
Expand Down
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ crtsys가 장점은 아래와 같습니다.

- Windows 8 or later
- Visual Studio 2017 or later
- CMake 3.16 or later
- Git

## Test Environments

- Windows 10
- CMake 3.21.4
- Git 2.23.0
- Visual Studio 2017
- Visual Studio 2017의 CRT 소스 코드는 일부 헤더가 누락되어 빌드가 되지 않아서, UCXXRT의 코드를 일부 사용하여 지원합니다.
- 추후 누락된 헤더를 직접 작성하여 지원할 예정입니다.
Expand Down Expand Up @@ -125,8 +129,14 @@ crtsys가 장점은 아래와 같습니다.
- [x] [std::future](https://en.cppreference.com/w/cpp/thread/future) [(tested)](./test/src/cpp/stl/thread.cpp#L164)
- [x] [std::promise](https://en.cppreference.com/w/cpp/thread/promise) [(tested)](./test/src/cpp/stl/thread.cpp#L212)
- [x] [std::packaged_task](https://en.cppreference.com/w/cpp/thread/packaged_task) [(tested)](./test/src/cpp/stl/thread.cpp#L280)
- [ ] [std::cout](https://en.cppreference.com/w/cpp/io/cout)
- [ ] [std::cerr](https://en.cppreference.com/w/cpp/io/cerr)
- [x] [std::cin](https://en.cppreference.com/w/cpp/io/cin)
- [x] [std::clog](https://en.cppreference.com/w/cpp/io/clog)
- [x] [std::cerr](https://en.cppreference.com/w/cpp/io/cerr)
- [x] [std::cout](https://en.cppreference.com/w/cpp/io/cout) (tested)
- [x] [std::wcin](https://en.cppreference.com/w/cpp/io/cin)
- [x] [std::wclog](https://en.cppreference.com/w/cpp/io/clog)
- [x] [std::wcerr](https://en.cppreference.com/w/cpp/io/cerr)
- [x] [std::wcout](https://en.cppreference.com/w/cpp/io/cout) (tested)

### C Standard

Expand All @@ -147,8 +157,8 @@ crtsys가 장점은 아래와 같습니다.
- 기능
- [x] DriverUnload [(tested)](./test/src/main.cpp#L25)
- [ ] DriverDispatch
- ntl::driver_main
- C++ 용 드라이버 진입점 [(tested)](./test/src/main.cpp#L20)
- ntl::driver_main [(tested)](./test/src/main.cpp#L21)
- C++ 용 드라이버 진입점
- ntl::expand_stack 함수로 스택을 최대 크기로 확장하여 호출됩니다.

## Build
Expand Down Expand Up @@ -257,23 +267,20 @@ crtsys_add_driver(crtsys_test main.cpp)
아래는 ntl::main를 진입점으로 설정한 프로젝트의 예제 코드입니다.
```C
#include <wdm.h>
#include <iostream>
#include <ntl/driver>
namespace ntl {
status main(ntl::driver &driver, const std::wstring &registry_path) {
DbgPrint("load (registry_path : %ws)\n", registry_path.c_str());
ntl::status ntl::main(ntl::driver &driver, const std::wstring &registry_path) {
std::wcout << "load (registry_path :" << registry_path << ")\n";
// TODO
driver.on_unload([registry_path]() {
DbgPrint("unload (registry_path : %ws)\n", registry_path.c_str());
std::wcout << "unload (registry_path :" << registry_path << ")\n";
});
return status::ok();
}
} // namespace ntl
```

## TODO
Expand Down
3 changes: 2 additions & 1 deletion cmake/CrtSys.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# INCLUDE_DIRECTORIES $(VC_IncludePath);$(WindowsSDK_IncludePath)
cmake_policy(SET CMP0021 OLD)


set(CMAKE_CXX_STANDARD_LIBRARIES " ")
set(CMAKE_C_STANDARD_LIBRARIES ${CMAKE_CXX_STANDARD_LIBRARIES})

#---------------------------------------------------------------------------------------------------
# include("${CMAKE_CURRENT_LIST_DIR}/CPM.cmake")
Expand Down
7 changes: 4 additions & 3 deletions include/14.29.30133/stl/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,11 @@ public:
// #ifdef _M_CEE
#define _WINDOWS_API __stdcall
#define _RENAME_WINDOWS_API(_Api) _Api##_clr
// #else // ^^^ _M_CEE // !_M_CEE vvv
#else // ^^^ _M_CEE // !_M_CEE vvv
// #define _WINDOWS_API __declspec(dllimport) __stdcall
// #define _RENAME_WINDOWS_API(_Api) _Api
// #endif // _M_CEE
#define _WINDOWS_API __stdcall
#define _RENAME_WINDOWS_API(_Api) _Api
#endif // _M_CEE

// WINBASEAPI
// BOOL
Expand Down
11 changes: 6 additions & 5 deletions include/143/stl/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,14 @@ public:
};
#endif // _HAS_CXX17

//#ifdef _M_CEE
#ifdef _M_CEE
#define _WINDOWS_API __stdcall
#define _RENAME_WINDOWS_API(_Api) _Api##_clr
//#else // ^^^ _M_CEE // !_M_CEE vvv
//#define _WINDOWS_API __declspec(dllimport) __stdcall
//#define _RENAME_WINDOWS_API(_Api) _Api
//#endif // _M_CEE
#else // ^^^ _M_CEE // !_M_CEE vvv
// #define _WINDOWS_API __declspec(dllimport) __stdcall
#define _WINDOWS_API __stdcall
#define _RENAME_WINDOWS_API(_Api) _Api
#endif // _M_CEE

// WINBASEAPI
// BOOL
Expand Down
6 changes: 4 additions & 2 deletions include/ntl/driver
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/
#pragma once

#ifndef _NTDDK_
#include <wdm.h>
#endif
#include "status"

#include <functional>
#include <string>

Expand Down Expand Up @@ -41,7 +43,7 @@ private:

#if _X86_
// warning C4007: 'main' : must be '__cdecl'
#pragma warning(disable:4007)
#pragma warning(disable : 4007)
#endif
status main(driver &driver, const std::wstring &registry_path);
} // namespace ntl
1 change: 1 addition & 0 deletions include/ntl/except
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "status"

#include <ntstatus.h>
#include <string>

namespace ntl {
Expand Down
Loading

0 comments on commit dfa196c

Please sign in to comment.