From 8948aa7efb9e3e54a38907dbb9dd1d6346484b90 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Fri, 1 Jul 2016 14:10:32 -0700 Subject: [PATCH 1/9] Initial changes to split the CMake project into a hierarchical set of CMakeLists.txt files --- build.psm1 | 4 +- src/powershell-native/CMakeLists.txt | 57 +- .../nativemsh/pwrshcommon/CMakeLists.txt | 37 ++ .../nativemsh/pwrshcommon/ClrHostWrapper.h | 382 +++++++------ .../nativemsh/pwrshcommon/NativeMsh.h | 35 +- .../nativemsh/pwrshcommon/SystemCallFacade.h | 21 +- .../pwrshcommon/WinSystemCallFacade.cpp | 32 +- .../pwrshcommon/WinSystemCallFacade.h | 21 +- .../nativemsh/pwrshcommon/pwrshcommon.cpp | 524 +++++++----------- .../nativemsh/pwrshexe/CMakeLists.txt | 48 ++ .../nativemsh/pwrshplugin/CMakeLists.txt | 37 ++ .../nativemsh/pwrshplugin/pwrshclrhost.cpp | 39 +- .../nativemsh/pwrshplugin/pwrshclrhost.h | 9 +- .../nativemsh/pwrshplugin/pwrshplugin.h | 4 +- 14 files changed, 596 insertions(+), 654 deletions(-) create mode 100644 src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt create mode 100644 src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt create mode 100644 src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt diff --git a/build.psm1 b/build.psm1 index 95ee565a8..8d9998cf4 100644 --- a/build.psm1 +++ b/build.psm1 @@ -221,7 +221,7 @@ function Start-PSBuild { @("nativemsh/pwrshplugin") | % { $nativeResourcesFolder = $_ Get-ChildItem $nativeResourcesFolder -Filter "*.mc" | % { - # & $mcexe -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder + & $mcexe -o -d -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder } } @@ -231,7 +231,7 @@ function Start-PSBuild { Start-NativeExecution { cmake . } } - Start-NativeExecution { msbuild powershell.vcxproj /p:Configuration=$msbuildConfiguration } + Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } } finally { Pop-Location diff --git a/src/powershell-native/CMakeLists.txt b/src/powershell-native/CMakeLists.txt index 0388e2644..171ed8fe7 100644 --- a/src/powershell-native/CMakeLists.txt +++ b/src/powershell-native/CMakeLists.txt @@ -10,54 +10,29 @@ foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES ) +# +# TODO: Will this interfere with OneCore compilations? +# # set these flags, so build does static linking for msvcr120.dll # otherwise this dll need to be present on the system set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") -include_directories( - nativemsh/pwrshcommon - nativemsh/pwrshplugin) +# Compiler flags that are universal to all powershell native binaries +SET(common_pwrsh_definitions + UNICODE + _UNICODE) -link_directories( - nativemsh/pwrshcommon) +# Default of BUILD_ONECORE should be ON once it is supported +option(BUILD_ONECORE "Compile the OneCore version of the binaries" OFF) +option(BUILD_DOWNLEVEL "Compile the downlevel version of the binaries" OFF) -add_executable(powershell WIN32 - nativemsh/pwrshcommon/pwrshcommon.cpp - nativemsh/pwrshcommon/WinSystemCallFacade.cpp - nativemsh/pwrshexe/MainEntry.cpp) +# Build the common library that powershell.exe and pwrshplugin.dll depend on +add_subdirectory(nativemsh/pwrshcommon) -add_library(pwrshplugin - nativemsh/pwrshplugin/entrypoints.cpp - nativemsh/pwrshplugin/pwrshclrhost.cpp - #nativemsh/pwrshplugin/pwrshpluginerrorcodes.mc - #nativemsh/pwrshplugin/pwrshpluginerrorcodes.rc - nativemsh/pwrshplugin/pwrshplugin.def) +# Build pwrshplugin.dll +add_subdirectory(nativemsh/pwrshplugin) - -SET(powershell_definitions - _CONSOLE - UNICODE - _UNICODE) - -set_target_properties(powershell PROPERTIES COMPILE_DEFINITIONS "${powershell_definitions}") -set_target_properties(powershell PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") - -SET(pwrshplugin_definitions - UNICODE - _UNICODE) - -set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") - -target_link_libraries(powershell - mscoree.lib - MUILoad.lib - kernel32.lib - msxml6.lib - legacy_stdio_definitions.lib) - -target_link_libraries(pwrshplugin) +# Build powershell.exe +add_subdirectory(nativemsh/pwrshexe) diff --git a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt new file mode 100644 index 000000000..947ab92c5 --- /dev/null +++ b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt @@ -0,0 +1,37 @@ +# +# CMake Instructions for producing pwrshcommon.lib for consumption by powershell.exe and +# pwrshplugin.dll. It is a library that is statically compiled into each binary. +# +add_library(pwrshcommon + pwrshcommon.cpp + WinSystemCallFacade.cpp) + +target_include_directories(pwrshcommon PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +if (BUILD_ONECORE) + # Libraries to use when creating this binary for Windows on OneCore-based SKUs + set(PWRSHCOMMON_WINDOWS_LIBS + onecore.lib) + set(pwrshcommon_definitions ${common_pwrsh_definitions} CORECLR) +else () # !BUILD_ONECORE + # Libraries to use when creating this binary for Windows on full SKUs + # Note: The appropriate libs get added automatically by VS + set(PWRSHCOMMON_WINDOWS_LIBS + # ole32.lib + # oleaut32.lib + # uuid.lib + # user32.lib + # MUILoad.lib + # msxml6.lib + # mscoree.lib) + ) + set(pwrshcommon_definitions ${common_pwrsh_definitions}) + # target_include_directories(pwrshcommon PUBLIC + # "%ProgramFiles(x86)%/Windows Kits/NETFXSDK/4.6/Include/um") +endif (BUILD_ONECORE) + +set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") + +target_link_libraries(pwrshcommon + ${PWRSHCOMMON_WINDOWS_LIBS}) + diff --git a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h index b1c3b7cfb..861581cc5 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h +++ b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h @@ -12,7 +12,6 @@ #pragma once #include -#include #include "NativeMshConstants.h" namespace NativeMsh @@ -32,322 +31,315 @@ namespace NativeMsh virtual bool IsInitialized() { return false; } - // For setting a host ptr since it is not known at container creation time. - // TODO: Can I make it immutable? - virtual void SetClrHost( - void* hostPtr) = 0; - // Graceful clean up of the object to prevent leaks virtual unsigned int CleanUpHostWrapper() = 0; - void SetAppDomainId(DWORD id) - { - m_appDomainId = id; - } - - DWORD GetAppDomainId() - { - return m_appDomainId; - } - // // The following methods are direct thin wrappers for the host calls. // - virtual HRESULT STDMETHODCALLTYPE CreateAppDomainWithManager( - /* [in] */ LPCWSTR wszFriendlyName, - /* [in] */ DWORD dwFlags, - /* [in] */ LPCWSTR wszAppDomainManagerAssemblyName, - /* [in] */ LPCWSTR wszAppDomainManagerTypeName, - /* [in] */ int nProperties, - /* [in] */ LPCWSTR *pPropertyNames, - /* [in] */ LPCWSTR *pPropertyValues, - /* [out] */ DWORD *pAppDomainID) = 0; + virtual HMODULE SetupWrapper(LPCSTR coreClrPathPtr) = 0; - virtual HRESULT STDMETHODCALLTYPE UnloadAppDomain( - /* [in] */ DWORD dwAppDomainId, - /* [in] */ BOOL fWaitUntilDone) = 0; + virtual int InitializeClr( + const char* exePath, + const char* appDomainFriendlyName, + int propertyCount, + const char** propertyKeys, + const char** propertyValues) = 0; - virtual HRESULT STDMETHODCALLTYPE CreateDelegate( - /* [in] */ DWORD appDomainID, - /* [in] */ LPCWSTR wszAssemblyName, - /* [in] */ LPCWSTR wszClassName, - /* [in] */ LPCWSTR wszMethodName, - /* [out] */ INT_PTR *fnPtr) = 0; + virtual int CreateDelegate( + const char* entryPointAssemblyName, + const char* entryPointTypeName, + const char* entryPointMethodName, + void** delegate) = 0; - virtual HRESULT STDMETHODCALLTYPE Authenticate( - /* [in] */ ULONGLONG authKey) = 0; - - virtual HRESULT STDMETHODCALLTYPE SetStartupFlags( - /* [in] */ STARTUP_FLAGS dwFlags) = 0; - - virtual HRESULT STDMETHODCALLTYPE Start() = 0; - - virtual HRESULT STDMETHODCALLTYPE Stop() = 0; - - virtual HRESULT STDMETHODCALLTYPE Release() = 0; + // TODO: This probably isn't needed + virtual int ShutdownClr() = 0; }; -#if CORECLR // - // Concrete implementation of the wrapper for the ICLRRuntimeHost2 interface. + // Concrete implementation of the wrapper for CoreClr.dll's + // Platform-Agnostic hosting interface. // - class ICLRRuntimeHost2Wrapper : public ClrHostWrapper + class CoreClrHostingApiWrapper : public ClrHostWrapper { private: - ICLRRuntimeHost2* pHost; + // Handle of CoreClr.dll + HMODULE coreClrHandle; + HMODULE pinnedModuleHandle; + + // CoreCLR.dll API values that are hidden from the user and kept internal. + void* hostHandle; + unsigned int domainId; + + // The name of the CoreCLR native runtime DLL. + PCSTR coreClrDllName = "CoreCLR.dll"; + + // + // Function Pointer Definitions for the function pointers to load from CoreCLR.dll + // + typedef int (*coreclr_initialize_ptr)( + const char* exePath, + const char* appDomainFriendlyName, + int propertyCount, + const char** propertyKeys, + const char** propertyValues, + void** hostHandle, + unsigned int* domainId); + + typedef int (*coreclr_shutdown_ptr)( + void* hostHandle, + unsigned int domainId); + + typedef int (*coreclr_create_delegate_ptr)( + void* hostHandle, + unsigned int domainId, + const char* entryPointAssemblyName, + const char* entryPointTypeName, + const char* entryPointMethodName, + void** delegate); + + // Pointers to exported functions of CoreClr.dll + coreclr_initialize_ptr initPtr; + coreclr_shutdown_ptr shutdownPtr; + coreclr_create_delegate_ptr createDelegatePtr; public: - ICLRRuntimeHost2Wrapper() : pHost(NULL) {} - virtual ~ICLRRuntimeHost2Wrapper() + CoreClrHostingApiWrapper() + : coreClrHandle(NULL), + pinnedModuleHandle(NULL), + hostHandle(NULL), + domainId(0), + initPtr(NULL), + shutdownPtr(NULL), + createDelegatePtr(NULL) + {} + + virtual ~CoreClrHostingApiWrapper() { - CleanUpHostWrapper(); + this->CleanUpHostWrapper(); } - virtual bool IsInitialized() { return (pHost != NULL); } - - // For setting a host ptr since it is not known at container creation time. - virtual void SetClrHost(void* hostPtr) + virtual bool IsInitialized() + { + return (NULL != coreClrHandle); + } + + // + // TODO: This shouldn't return HMODULE because that breaks encapsulation. I need a better way to + // have the caller extract the value. Either Pass as a reference or provide an accessor. + // + // Attempts to load CoreCLR.dll from the specified directory. + // On success pins the dll, sets coreCLRDirectoryPath and returns the HMODULE. + // On failure returns NULL. + virtual HMODULE SetupWrapper(LPCSTR coreClrPathPtr) { - pHost = (ICLRRuntimeHost2*)hostPtr; + std::string coreClrPath(coreClrPathPtr); + coreClrPath += coreClrDllName; + + HMODULE result = LoadLibraryExA(coreClrPath.c_str(), NULL, 0); + if (!result) + { + return NULL; + } + + // Pin the module - CoreCLR.dll does not support being unloaded. + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN, coreClrPath.c_str(), &pinnedModuleHandle)) + { + return NULL; + } + + // Initialization succeeded. Save the handle and return success; + coreClrHandle = result; + return result; } virtual unsigned int CleanUpHostWrapper() { if (this->IsInitialized()) { - if (FAILED(this->UnloadAppDomain(this->GetAppDomainId(), true))) - { - return g_UNLOAD_APPDOMAIN_FAILED; - } - - if (FAILED(this->Stop())) + HRESULT status = this->ShutdownClr(); + if (FAILED(status)) { return g_STOP_CLR_HOST_FAILED; } - - // Release the reference to the host - if (FAILED(this->Release())) + + if (this->coreClrHandle) { - return g_RELEASE_CLR_HOST_FAILED; + // TODO: Is this comment still relevant with the new hosting API? + // + // Free the module. This is done for completeness, but in fact CoreCLR.dll + // was pinned earlier so this call won't actually free it. The pinning is + // done because CoreCLR does not support unloading. + ::FreeLibrary(this->coreClrHandle); + this->coreClrHandle = NULL; } - this->SetClrHost(NULL); } return EXIT_CODE_SUCCESS; } - virtual HRESULT STDMETHODCALLTYPE CreateAppDomainWithManager( - /* [in] */ LPCWSTR wszFriendlyName, - /* [in] */ DWORD dwFlags, - /* [in] */ LPCWSTR wszAppDomainManagerAssemblyName, - /* [in] */ LPCWSTR wszAppDomainManagerTypeName, - /* [in] */ int nProperties, - /* [in] */ LPCWSTR *pPropertyNames, - /* [in] */ LPCWSTR *pPropertyValues, - /* [out] */ DWORD *pAppDomainID) + virtual int InitializeClr( + const char* exePath, + const char* appDomainFriendlyName, + int propertyCount, + const char** propertyKeys, + const char** propertyValues) { - if (pHost) + if (initPtr) { - return pHost->CreateAppDomainWithManager( - wszFriendlyName, - dwFlags, - wszAppDomainManagerAssemblyName, - wszAppDomainManagerTypeName, - nProperties, - pPropertyNames, - pPropertyValues, - pAppDomainID); + return initPtr( + exePath, + appDomainFriendlyName, + propertyCount, + propertyKeys, + propertyValues, + &(this->hostHandle), + &(this->domainId)); } return E_FAIL; } - virtual HRESULT STDMETHODCALLTYPE UnloadAppDomain( - /* [in] */ DWORD dwAppDomainId, - /* [in] */ BOOL fWaitUntilDone) + virtual int CreateDelegate( + const char* entryPointAssemblyName, + const char* entryPointTypeName, + const char* entryPointMethodName, + void** delegate) { - if (pHost) + if (createDelegatePtr) { - return pHost->UnloadAppDomain( - dwAppDomainId, - fWaitUntilDone); + return createDelegatePtr( + this->hostHandle, + this->domainId, + entryPointAssemblyName, + entryPointTypeName, + entryPointMethodName, + delegate); } return E_FAIL; } - virtual HRESULT STDMETHODCALLTYPE CreateDelegate( - /* [in] */ DWORD appDomainID, - /* [in] */ LPCWSTR wszAssemblyName, - /* [in] */ LPCWSTR wszClassName, - /* [in] */ LPCWSTR wszMethodName, - /* [out] */ INT_PTR *fnPtr) + virtual int ShutdownClr() { - if (pHost) + if (shutdownPtr) { - return pHost->CreateDelegate( - appDomainID, - wszAssemblyName, - wszClassName, - wszMethodName, - fnPtr); - } - return E_FAIL; - } - - virtual HRESULT STDMETHODCALLTYPE Authenticate( - /* [in] */ ULONGLONG authKey) - { - if (pHost) - { - return pHost->Authenticate( - authKey); - } - return E_FAIL; - } - - virtual HRESULT STDMETHODCALLTYPE SetStartupFlags( - /* [in] */ STARTUP_FLAGS dwFlags) - { - if (pHost) - { - return pHost->SetStartupFlags( - dwFlags); - } - return E_FAIL; - } - - virtual HRESULT STDMETHODCALLTYPE Start() - { - if (pHost) - { - return pHost->Start(); - } - return E_FAIL; - } - - virtual HRESULT STDMETHODCALLTYPE Stop() - { - if (pHost) - { - return pHost->Stop(); - } - return E_FAIL; - } - - virtual HRESULT STDMETHODCALLTYPE Release() - { - if (pHost) - { - return pHost->Release(); + return shutdownPtr( + this->hostHandle, + this->domainId); } return E_FAIL; } }; -#endif // Encapsulates the environment that CoreCLR will run in, including the TPALIST class HostEnvironment { private: // The path to this module - wchar_t m_hostPath[MAX_PATH]; + char m_hostPath[MAX_PATH]; // The path to the directory containing this module - wchar_t m_hostDirectoryPath[MAX_PATH]; + char m_hostDirectoryPath[MAX_PATH]; + wchar_t m_hostDirectoryPathW[MAX_PATH]; // The name of this module, without the path - std::wstring m_hostBinaryName; + std::string m_hostBinaryName; // The path to the directory that CoreCLR is in - wchar_t m_coreCLRDirectoryPath[MAX_PATH]; - - HMODULE m_coreCLRModule; + char m_coreCLRDirectoryPath[MAX_PATH]; public: HostEnvironment() - : m_coreCLRModule(0) { memset(m_hostPath, 0, sizeof(m_hostPath)); memset(m_hostDirectoryPath, 0, sizeof(m_hostDirectoryPath)); memset(m_coreCLRDirectoryPath, 0, sizeof(m_coreCLRDirectoryPath)); } - ~HostEnvironment() - { - if (m_coreCLRModule) - { - // Free the module. This is done for completeness, but in fact CoreCLR.dll - // was pinned earlier so this call won't actually free it. The pinning is - // done because CoreCLR does not support unloading. - ::FreeLibrary(m_coreCLRModule); - } - } + ~HostEnvironment() {} // Safely copies in a host path - void SetHostPath(PCWSTR hostPath) + void SetHostPath(PCSTR hostPath) { if (hostPath) { - ::ExpandEnvironmentStringsW(hostPath, m_hostPath, MAX_PATH); - //::wcsncpy_s(m_hostPath, hostPath, _TRUNCATE); + ::ExpandEnvironmentStringsA(hostPath, m_hostPath, MAX_PATH); } } // Returns the path to the host module - PCWSTR GetHostPath() + PCSTR GetHostPath() { return m_hostPath; } // Safely copies in a host binary name - void SetHostBinaryName(PCWSTR hostBinaryName) + void SetHostBinaryName(PCSTR hostBinaryName) { if (hostBinaryName) { - m_hostBinaryName = std::wstring(hostBinaryName); + m_hostBinaryName = std::string(hostBinaryName); } } // Returns the name of the host module - PCWSTR GetHostBinaryName() + PCSTR GetHostBinaryName() { return m_hostBinaryName.c_str(); } // Safely copies in a host directory path - void SetHostDirectoryPath(PCWSTR hostDirPath) + void SetHostDirectoryPath(PCSTR hostDirPath) { if (hostDirPath) { - ::ExpandEnvironmentStringsW(hostDirPath, m_hostDirectoryPath, MAX_PATH); - //::wcsncpy_s(m_hostDirectoryPath, hostDirPath, _TRUNCATE); + ::ExpandEnvironmentStringsA(hostDirPath, m_hostDirectoryPath, MAX_PATH); + + // Generate the wide version of the string and save its value; + // + // This is a two call function. The first call is to get the necessary length. + // The second call is to perform the actual operation. + int length = MultiByteToWideChar(CP_UTF8, 0, m_hostDirectoryPath, -1, NULL, 0); + if (0 < length) + { + LPWSTR result = new wchar_t[length]; + if (NULL != result) + { + length = ::MultiByteToWideChar(CP_UTF8, 0, m_hostDirectoryPath, -1, result, length); + if (0 < length) + { + wcscpy_s(m_hostDirectoryPathW, MAX_PATH, result); + } + delete[] result; // Free the allocated string to avoid a memory leak + } + } } } // Returns the directory path of the host module - PCWSTR GetHostDirectoryPath() + PCSTR GetHostDirectoryPath() { return m_hostDirectoryPath; } + // Returns the directory path of the host module as a wide char string + PCWSTR GetHostDirectoryPathW() + { + return m_hostDirectoryPathW; + } + // Safely copies in a core clr directory path - void SetCoreCLRDirectoryPath(PCWSTR hostClrPath) + void SetCoreCLRDirectoryPath(PCSTR hostClrPath) { if (hostClrPath) { - ::ExpandEnvironmentStringsW(hostClrPath, m_coreCLRDirectoryPath, MAX_PATH); - //::wcsncpy_s(m_coreCLRDirectoryPath, hostClrPath, _TRUNCATE); + ::ExpandEnvironmentStringsA(hostClrPath, m_coreCLRDirectoryPath, MAX_PATH); } } // Returns the directory path of the host module - PCWSTR GetCoreCLRDirectoryPath() + PCSTR GetCoreCLRDirectoryPath() { return m_coreCLRDirectoryPath; } - - void SetCoreCLRModule(HMODULE module) - { - m_coreCLRModule = module; - } }; -} // namespace NativeMsh \ No newline at end of file +} // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h b/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h index df477f4cc..eee01a498 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h +++ b/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h @@ -19,6 +19,10 @@ #include "SystemCallFacade.h" #include "IPwrshCommonOutput.h" +#if !CORECLR +#include +#endif + namespace NativeMsh { class PwrshCommon @@ -91,17 +95,12 @@ namespace NativeMsh __deref_out_opt PWSTR * pwszRuntimeVersion, __deref_out_opt PWSTR * pwszConsoleHostAssemblyName); -#if CORECLR unsigned int LaunchCoreCLR( ClrHostWrapper* hostWrapper, - HostEnvironment& hostEnvironment); + HostEnvironment& hostEnvironment, + PCSTR friendlyName); - unsigned int CreateAppDomain( - ClrHostWrapper* hostWrapper, - PCWSTR friendlyName, - HostEnvironment& hostEnvironment); - -#else // !CORECLR +#if !CORECLR // NOTE: // This must be ifdef'd out of the CoreCLR build because it uses .NET 1.0 // types that have been deprecated and removed from mscoree.h. @@ -113,7 +112,6 @@ namespace NativeMsh LPCWSTR wszMonadVersion, LPCWSTR wszRuntimeVersion, __in_ecount(1) ICorRuntimeHost** pCLR); - #endif // !CORECLR protected: @@ -170,28 +168,21 @@ namespace NativeMsh __out_ecount(1) int * lpMinorVersion); virtual bool DoesAssemblyExist( - std::wstring& fileToTest); + std::string& fileToTest); virtual void ProbeAssembly( - _In_z_ PCWSTR directoryPath, - _In_z_ PCWSTR assemblyName, - std::wstring& result); + _In_z_ PCSTR directoryPath, + _In_z_ PCSTR assemblyName, + std::string& result); virtual void GetTrustedAssemblyList( - PCWSTR coreCLRDirectoryPath, - std::wstringstream& assemblyList, + PCSTR coreCLRDirectoryPath, + std::stringstream& assemblyList, bool& listEmpty); virtual unsigned int IdentifyHostDirectory( HostEnvironment& hostEnvironment); - virtual HMODULE TryLoadCoreCLR( - _In_ PCWSTR directoryPath); - - virtual unsigned int InitializeClr( - _In_ ClrHostWrapper* hostWrapper, - _In_ HMODULE coreClrModule); - private: // Explicitly disallow copy construction and assignment PwrshCommon(const PwrshCommon&); diff --git a/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h b/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h index 877f3d2b0..97dcfa194 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h +++ b/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h @@ -32,18 +32,13 @@ namespace NativeMsh _Reserved_ HANDLE hFile, _In_ DWORD dwFlags) = 0; - virtual DWORD WINAPI GetModuleFileNameW( + virtual DWORD WINAPI GetModuleFileNameA( _In_opt_ HMODULE hModule, - _Out_ PWSTR lpFilename, + _Out_ LPSTR lpFilename, _In_ DWORD nSize) = 0; - virtual HMODULE WINAPI GetModuleHandleW( - _In_opt_ PCWSTR lpModuleName) = 0; - - virtual BOOL WINAPI GetModuleHandleExW( - _In_ DWORD dwFlags, - _In_opt_ PCWSTR lpModuleName, - _Out_ HMODULE *phModule) = 0; + virtual HMODULE WINAPI GetModuleHandleA( + _In_opt_ PCSTR lpModuleName) = 0; virtual FARPROC WINAPI GetProcAddress( _In_ HMODULE hModule, @@ -53,12 +48,12 @@ namespace NativeMsh _In_ HMODULE hModule) = 0; // File Manipulation Wrappers - virtual FILE* _wfopen( - const wchar_t *filename, - const wchar_t *mode) = 0; + virtual FILE* fopen( + const char *filename, + const char *mode) = 0; virtual int fclose( FILE *stream) = 0; }; -} // namespace NativeMsh \ No newline at end of file +} // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp index 8ccfb2b25..9f49a43d5 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp @@ -14,7 +14,7 @@ namespace NativeMsh { HMODULE WINAPI WinSystemCallFacade::LoadLibraryExW( - _In_ LPCTSTR lpFileName, + _In_ LPCWSTR lpFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags) { @@ -24,28 +24,20 @@ namespace NativeMsh #pragma prefast(push) #pragma prefast (disable: 26006) // Possibly incorrect single element annotation on string buffer - This is a thin wrapper around a system call, so it's behavior is not controllable. - DWORD WINAPI WinSystemCallFacade::GetModuleFileNameW( + DWORD WINAPI WinSystemCallFacade::GetModuleFileNameA( _In_opt_ HMODULE hModule, - _Out_ LPTSTR lpFilename, // _Out_writes_to_(nSize, return +1) OR __out_ecount_part(nSize, return + 1)? __out_ecount(nSize) + _Out_ LPSTR lpFilename, // _Out_writes_to_(nSize, return +1) OR __out_ecount_part(nSize, return + 1)? __out_ecount(nSize) _In_ DWORD nSize) { - return ::GetModuleFileNameW(hModule, lpFilename, nSize); + return ::GetModuleFileNameA(hModule, lpFilename, nSize); } #pragma prefast(pop) - HMODULE WINAPI WinSystemCallFacade::GetModuleHandleW( - _In_opt_ LPCTSTR lpModuleName) + HMODULE WINAPI WinSystemCallFacade::GetModuleHandleA( + _In_opt_ LPCSTR lpModuleName) { - return ::GetModuleHandleW(lpModuleName); - } - - BOOL WINAPI WinSystemCallFacade::GetModuleHandleExW( - _In_ DWORD dwFlags, - _In_opt_ LPCTSTR lpModuleName, - _Out_ HMODULE *phModule) - { - return ::GetModuleHandleExW(dwFlags, lpModuleName, phModule); + return ::GetModuleHandleA(lpModuleName); } FARPROC WINAPI WinSystemCallFacade::GetProcAddress( @@ -61,11 +53,11 @@ namespace NativeMsh return ::FreeLibrary(hModule); } - FILE* WinSystemCallFacade::_wfopen( - const wchar_t *filename, - const wchar_t *mode) + FILE* WinSystemCallFacade::fopen( + const char *filename, + const char *mode) { - return ::_wfopen(filename, mode); + return ::fopen(filename, mode); } int WinSystemCallFacade::fclose( @@ -74,4 +66,4 @@ namespace NativeMsh return ::fclose(stream); } -} // namespace NativeMsh \ No newline at end of file +} // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h index bdaa3a116..1ec4379e8 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h +++ b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h @@ -30,18 +30,13 @@ namespace NativeMsh _Reserved_ HANDLE hFile, _In_ DWORD dwFlags); - virtual DWORD WINAPI GetModuleFileNameW( + virtual DWORD WINAPI GetModuleFileNameA( _In_opt_ HMODULE hModule, - _Out_ PWSTR lpFilename, + _Out_ LPSTR lpFilename, _In_ DWORD nSize); - virtual HMODULE WINAPI GetModuleHandleW( - _In_opt_ PCWSTR lpModuleName); - - virtual BOOL WINAPI GetModuleHandleExW( - _In_ DWORD dwFlags, - _In_opt_ PCWSTR lpModuleName, - _Out_ HMODULE *phModule); + virtual HMODULE WINAPI GetModuleHandleA( + _In_opt_ LPCSTR lpModuleName); virtual FARPROC WINAPI GetProcAddress( _In_ HMODULE hModule, @@ -51,12 +46,12 @@ namespace NativeMsh _In_ HMODULE hModule); // File Manipulation Wrappers - virtual FILE* _wfopen( - const wchar_t *filename, - const wchar_t *mode); + virtual FILE* fopen( + const char *filename, + const char *mode); virtual int fclose( FILE *stream); }; -} // namespace NativeMsh \ No newline at end of file +} // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp index 64d0a014d..5d1229e32 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp @@ -667,176 +667,176 @@ namespace NativeMsh // System.Management.Automation must not be listed here. I should exist on the APP_PATH. // // NOTE: The names must not include the .dll extension because it will be added programmatically. - static PCWSTR trustedAssemblies[] = + static PCSTR trustedAssemblies[] = { - L"Microsoft.CSharp", - L"Microsoft.VisualBasic", - L"Microsoft.Win32.Primitives", - L"Microsoft.Win32.Registry.AccessControl", - L"Microsoft.Win32.Registry", - L"mscorlib", - L"System.AppContext", - L"System.Buffers", - L"System.Collections.Concurrent", - L"System.Collections", - L"System.Collections.Immutable", - L"System.Collections.NonGeneric", - L"System.Collections.Specialized", - L"System.ComponentModel.Annotations", - L"System.ComponentModel.DataAnnotations", - L"System.ComponentModel", - L"System.ComponentModel.EventBasedAsync", - L"System.ComponentModel.Primitives", - L"System.ComponentModel.TypeConverter", - L"System.Console", - L"System.Core", - L"System.Data.Common", - L"System.Diagnostics.Contracts", - L"System.Diagnostics.Debug", - L"System.Diagnostics.DiagnosticSource", - L"System.Diagnostics.FileVersionInfo", - L"System.Diagnostics.Process", - L"System.Diagnostics.StackTrace", - L"System.Diagnostics.TextWriterTraceListener", - L"System.Diagnostics.Tools", - L"System.Diagnostics.TraceSource", - L"System.Diagnostics.Tracing", - L"System", - L"System.Dynamic.Runtime", - L"System.Globalization.Calendars", - L"System.Globalization", - L"System.Globalization.Extensions", - L"System.IO.Compression", - L"System.IO.Compression.ZipFile", - L"System.IO", - L"System.IO.FileSystem.AccessControl", - L"System.IO.FileSystem", - L"System.IO.FileSystem.DriveInfo", - L"System.IO.FileSystem.Primitives", - L"System.IO.FileSystem.Watcher", - L"System.IO.MemoryMappedFiles", - L"System.IO.Packaging", - L"System.IO.Pipes", - L"System.IO.UnmanagedMemoryStream", - L"System.Linq", - L"System.Linq.Expressions", - L"System.Linq.Parallel", - L"System.Linq.Queryable", - L"System.Net", - L"System.Net.Http", - L"System.Net.Http.WinHttpHandler", - L"System.Net.NameResolution", - L"System.Net.NetworkInformation", - L"System.Net.Ping", - L"System.Net.Primitives", - L"System.Net.Requests", - L"System.Net.Security", - L"System.Net.Sockets", - L"System.Net.WebHeaderCollection", - L"System.Net.WebSockets.Client", - L"System.Net.WebSockets", - L"System.Numerics", - L"System.Numerics.Vectors", - L"System.ObjectModel", - L"System.Private.DataContractSerialization", - L"System.Private.ServiceModel", - L"System.Private.Uri", - L"System.Reflection.DispatchProxy", - L"System.Reflection", - L"System.Reflection.Emit", - L"System.Reflection.Emit.ILGeneration", - L"System.Reflection.Emit.Lightweight", - L"System.Reflection.Extensions", - L"System.Reflection.Metadata", - L"System.Reflection.Primitives", - L"System.Reflection.TypeExtensions", - L"System.Resources.ReaderWriter", - L"System.Resources.ResourceManager", - L"System.Runtime.CompilerServices.VisualC", - L"System.Runtime", - L"System.Runtime.Extensions", - L"System.Runtime.Handles", - L"System.Runtime.InteropServices", - L"System.Runtime.InteropServices.PInvoke", - L"System.Runtime.InteropServices.RuntimeInformation", - L"System.Runtime.Loader", - L"System.Runtime.Numerics", - L"System.Runtime.Serialization", - L"System.Runtime.Serialization.Json", - L"System.Runtime.Serialization.Primitives", - L"System.Runtime.Serialization.Xml", - L"System.Security.AccessControl", - L"System.Security.Claims", - L"System.Security.Cryptography.Algorithms", - L"System.Security.Cryptography.Cng", - L"System.Security.Cryptography.Csp", - L"System.Security.Cryptography.Encoding", - L"System.Security.Cryptography.OpenSsl", - L"System.Security.Cryptography.Primitives", - L"System.Security.Cryptography.X509Certificates", - L"System.Security.Principal", - L"System.Security.Principal.Windows", - L"System.Security.SecureString", - L"System.ServiceModel", - L"System.ServiceModel.Duplex", - L"System.ServiceModel.Http", - L"System.ServiceModel.NetTcp", - L"System.ServiceModel.Primitives", - L"System.ServiceModel.Security", - L"System.ServiceModel.Web", - L"System.ServiceProcess.ServiceController", - L"System.Text.Encoding.CodePages", - L"System.Text.Encoding", - L"System.Text.Encoding.Extensions", - L"System.Text.Encodings.Web", - L"System.Text.RegularExpressions", - L"System.Threading.AccessControl", - L"System.Threading", - L"System.Threading.Overlapped", - L"System.Threading.Tasks.Dataflow", - L"System.Threading.Tasks", - L"System.Threading.Tasks.Extensions", - L"System.Threading.Tasks.Parallel", - L"System.Threading.Thread", - L"System.Threading.ThreadPool", - L"System.Threading.Timer", - L"System.Windows", - L"System.Xml", - L"System.Xml.Linq", - L"System.Xml.ReaderWriter", - L"System.Xml.Serialization", - L"System.Xml.XDocument", - L"System.Xml.XmlDocument", - L"System.Xml.XmlSerializer", - L"System.Xml.XPath", - L"System.Xml.XPath.XDocument", - L"System.Xml.XPath.XmlDocument", - L"Microsoft.PowerShell.CoreCLR.AssemblyLoadContext" + "Microsoft.CSharp", + "Microsoft.VisualBasic", + "Microsoft.Win32.Primitives", + "Microsoft.Win32.Registry.AccessControl", + "Microsoft.Win32.Registry", + "mscorlib", + "System.AppContext", + "System.Buffers", + "System.Collections.Concurrent", + "System.Collections", + "System.Collections.Immutable", + "System.Collections.NonGeneric", + "System.Collections.Specialized", + "System.ComponentModel.Annotations", + "System.ComponentModel.DataAnnotations", + "System.ComponentModel", + "System.ComponentModel.EventBasedAsync", + "System.ComponentModel.Primitives", + "System.ComponentModel.TypeConverter", + "System.Console", + "System.Core", + "System.Data.Common", + "System.Diagnostics.Contracts", + "System.Diagnostics.Debug", + "System.Diagnostics.DiagnosticSource", + "System.Diagnostics.FileVersionInfo", + "System.Diagnostics.Process", + "System.Diagnostics.StackTrace", + "System.Diagnostics.TextWriterTraceListener", + "System.Diagnostics.Tools", + "System.Diagnostics.TraceSource", + "System.Diagnostics.Tracing", + "System", + "System.Dynamic.Runtime", + "System.Globalization.Calendars", + "System.Globalization", + "System.Globalization.Extensions", + "System.IO.Compression", + "System.IO.Compression.ZipFile", + "System.IO", + "System.IO.FileSystem.AccessControl", + "System.IO.FileSystem", + "System.IO.FileSystem.DriveInfo", + "System.IO.FileSystem.Primitives", + "System.IO.FileSystem.Watcher", + "System.IO.MemoryMappedFiles", + "System.IO.Packaging", + "System.IO.Pipes", + "System.IO.UnmanagedMemoryStream", + "System.Linq", + "System.Linq.Expressions", + "System.Linq.Parallel", + "System.Linq.Queryable", + "System.Net", + "System.Net.Http", + "System.Net.Http.WinHttpHandler", + "System.Net.NameResolution", + "System.Net.NetworkInformation", + "System.Net.Ping", + "System.Net.Primitives", + "System.Net.Requests", + "System.Net.Security", + "System.Net.Sockets", + "System.Net.WebHeaderCollection", + "System.Net.WebSockets.Client", + "System.Net.WebSockets", + "System.Numerics", + "System.Numerics.Vectors", + "System.ObjectModel", + "System.Private.DataContractSerialization", + "System.Private.ServiceModel", + "System.Private.Uri", + "System.Reflection.DispatchProxy", + "System.Reflection", + "System.Reflection.Emit", + "System.Reflection.Emit.ILGeneration", + "System.Reflection.Emit.Lightweight", + "System.Reflection.Extensions", + "System.Reflection.Metadata", + "System.Reflection.Primitives", + "System.Reflection.TypeExtensions", + "System.Resources.ReaderWriter", + "System.Resources.ResourceManager", + "System.Runtime.CompilerServices.VisualC", + "System.Runtime", + "System.Runtime.Extensions", + "System.Runtime.Handles", + "System.Runtime.InteropServices", + "System.Runtime.InteropServices.PInvoke", + "System.Runtime.InteropServices.RuntimeInformation", + "System.Runtime.Loader", + "System.Runtime.Numerics", + "System.Runtime.Serialization", + "System.Runtime.Serialization.Json", + "System.Runtime.Serialization.Primitives", + "System.Runtime.Serialization.Xml", + "System.Security.AccessControl", + "System.Security.Claims", + "System.Security.Cryptography.Algorithms", + "System.Security.Cryptography.Cng", + "System.Security.Cryptography.Csp", + "System.Security.Cryptography.Encoding", + "System.Security.Cryptography.OpenSsl", + "System.Security.Cryptography.Primitives", + "System.Security.Cryptography.X509Certificates", + "System.Security.Principal", + "System.Security.Principal.Windows", + "System.Security.SecureString", + "System.ServiceModel", + "System.ServiceModel.Duplex", + "System.ServiceModel.Http", + "System.ServiceModel.NetTcp", + "System.ServiceModel.Primitives", + "System.ServiceModel.Security", + "System.ServiceModel.Web", + "System.ServiceProcess.ServiceController", + "System.Text.Encoding.CodePages", + "System.Text.Encoding", + "System.Text.Encoding.Extensions", + "System.Text.Encodings.Web", + "System.Text.RegularExpressions", + "System.Threading.AccessControl", + "System.Threading", + "System.Threading.Overlapped", + "System.Threading.Tasks.Dataflow", + "System.Threading.Tasks", + "System.Threading.Tasks.Extensions", + "System.Threading.Tasks.Parallel", + "System.Threading.Thread", + "System.Threading.ThreadPool", + "System.Threading.Timer", + "System.Windows", + "System.Xml", + "System.Xml.Linq", + "System.Xml.ReaderWriter", + "System.Xml.Serialization", + "System.Xml.XDocument", + "System.Xml.XmlDocument", + "System.Xml.XmlSerializer", + "System.Xml.XPath", + "System.Xml.XPath.XDocument", + "System.Xml.XPath.XmlDocument", + "Microsoft.PowerShell.CoreCLR.AssemblyLoadContext" }; // Define the function pointer for the CLR entry point typedef HRESULT(STDAPICALLTYPE *GetCLRRuntimeHostFp)(REFIID riid, IUnknown** pUnk); // The name of the CoreCLR native runtime DLL. - static PCWSTR coreClrDll = L"CoreCLR.dll"; + static PCSTR coreClrDll = "CoreCLR.dll"; // The location where CoreCLR is expected to be installed. If CoreCLR.dll isn't // found in the same directory as the host, it will be looked for here. - static PCWSTR coreCLRInstallDirectory = L"%windir%\\system32\\DotNetCore\\v1.0\\"; + static PCSTR coreCLRInstallDirectory = "%windir%\\system32\\DotNetCore\\v1.0\\"; // The location where CoreCLR PowerShell Ext binaries are expected to be installed. - static PCWSTR coreCLRPowerShellExtInstallDirectory = L"%windir%\\system32\\CoreClrPowerShellExt\\v1.0\\"; + static PCSTR coreCLRPowerShellExtInstallDirectory = "%windir%\\system32\\CoreClrPowerShellExt\\v1.0\\"; // The default PowerShell install directory. This location may be overridden through a config file in %windir%\System32. - static PCWSTR powerShellInstallPath = L"%windir%\\System32\\WindowsPowerShell\\v1.0\\"; + static PCSTR powerShellInstallPath = "%windir%\\System32\\WindowsPowerShell\\v1.0\\"; unsigned int PwrshCommon::IdentifyHostDirectory( HostEnvironment& hostEnvironment) { // Discover the path to the exe's module (powershell.exe or wsmprovhost.exe). // For remoting, this is expected to be %windir%\system32 since that is the location of wsmprovhost.exe. - wchar_t hostPath[MAX_PATH]; - DWORD thisModuleLength = sysCalls->GetModuleFileNameW(sysCalls->GetModuleHandleW(NULL), hostPath, MAX_PATH); + char hostPath[MAX_PATH]; + DWORD thisModuleLength = sysCalls->GetModuleFileNameA(sysCalls->GetModuleHandleA(NULL), hostPath, MAX_PATH); if (0 == thisModuleLength) { @@ -865,76 +865,10 @@ namespace NativeMsh return EXIT_CODE_SUCCESS; } - // Attempts to load CoreCLR.dll from the specified directory. - // On success pins the dll, sets coreCLRDirectoryPath and returns the HMODULE. - // On failure returns NULL. - HMODULE PwrshCommon::TryLoadCoreCLR( - _In_ PCWSTR directoryPath) - { - std::wstring coreCLRPath(directoryPath); - coreCLRPath += coreClrDll; - - HMODULE result = sysCalls->LoadLibraryExW(coreCLRPath.c_str(), NULL, 0); - if (!result) - { - return NULL; - } - - // Pin the module - CoreCLR.dll does not support being unloaded. - HMODULE dummy_coreCLRModule; - if (!sysCalls->GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, coreCLRPath.c_str(), &dummy_coreCLRModule)) - { - return NULL; - } - - return result; - } - - unsigned int PwrshCommon::InitializeClr( - _In_ ClrHostWrapper* hostWrapper, - _In_ HMODULE coreClrModule) - { -#if CORECLR - // Get the function pointer for "GetCLRRuntimeHost" - GetCLRRuntimeHostFp getCLRRuntimeHostfp = (GetCLRRuntimeHostFp)sysCalls->GetProcAddress(coreClrModule, "GetCLRRuntimeHost"); - if (getCLRRuntimeHostfp == NULL) - { - return EXIT_CODE_INIT_FAILURE; - } - - // Get the CLR runtime host - ICLRRuntimeHost2* pHost = NULL; - HRESULT hResult = getCLRRuntimeHostfp(IID_ICLRRuntimeHost2, (IUnknown**)&pHost); - if (FAILED(hResult)) - { - return EXIT_CODE_INIT_FAILURE; - } - - hostWrapper->SetClrHost(pHost); - // Authenticate with either - // CORECLR_HOST_AUTHENTICATION_KEY or - // CORECLR_HOST_AUTHENTICATION_KEY_NONGEN - hResult = hostWrapper->Authenticate(CORECLR_HOST_AUTHENTICATION_KEY); - if (FAILED(hResult)) - { - return EXIT_CODE_INIT_FAILURE; - } - - hostWrapper->SetStartupFlags((STARTUP_FLAGS)(STARTUP_SINGLE_APPDOMAIN | STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN)); - - hResult = hostWrapper->Start(); - if (FAILED(hResult)) - { - return EXIT_CODE_INIT_FAILURE; - } -#endif - return EXIT_CODE_SUCCESS; - } - bool PwrshCommon::DoesAssemblyExist( - std::wstring& fileToTest) + std::string& fileToTest) { - FILE *file = sysCalls->_wfopen(fileToTest.c_str(), L"r"); + FILE *file = sysCalls->fopen(fileToTest.c_str(), "r"); // TODO: Use fopen_s? if (file != NULL) { sysCalls->fclose(file); @@ -945,15 +879,15 @@ namespace NativeMsh // This assumes that directoryPath already includes a trailing "\\" void PwrshCommon::ProbeAssembly( - _In_z_ PCWSTR directoryPath, - _In_z_ PCWSTR assemblyName, - std::wstring& result) + _In_z_ PCSTR directoryPath, + _In_z_ PCSTR assemblyName, + std::string& result) { - PCWSTR niExtension = L".ni.dll"; - PCWSTR ilExtension = L".dll"; + PCSTR niExtension = ".ni.dll"; + PCSTR ilExtension = ".dll"; // Test NI extension first because it is preferable to IL - std::wstring fileToTest(directoryPath); + std::string fileToTest(directoryPath); fileToTest += assemblyName; fileToTest += niExtension; if (DoesAssemblyExist(fileToTest)) { @@ -973,13 +907,13 @@ namespace NativeMsh // Returns the semicolon-separated list of paths to runtime dlls that are considered trusted. // Do not put powershell assemblies in the TPA list as it will cause 'Security Transparent V.S. Security Critical' error. void PwrshCommon::GetTrustedAssemblyList( - PCWSTR coreCLRDirectoryPath, - std::wstringstream& assemblyList, + PCSTR coreCLRDirectoryPath, + std::stringstream& assemblyList, bool& listEmpty) { - for (const wchar_t * &assembly : trustedAssemblies) + for (const char* &assembly : trustedAssemblies) { - std::wstring assemblyPath; + std::string assemblyPath; ProbeAssembly(coreCLRDirectoryPath, assembly, assemblyPath); if (assemblyPath.length() > 0) @@ -987,7 +921,7 @@ namespace NativeMsh if (listEmpty) listEmpty = false; else - assemblyList << L";"; + assemblyList << ";"; assemblyList << assemblyPath; } } @@ -1351,10 +1285,10 @@ namespace NativeMsh pwszRuntimeVersion, L"ConsoleHostAssemblyName", pwszConsoleHostAssemblyName); } - #if CORECLR unsigned int PwrshCommon::LaunchCoreCLR( ClrHostWrapper* hostWrapper, - HostEnvironment& hostEnvironment) + HostEnvironment& hostEnvironment, + PCSTR friendlyName) { unsigned int exitCode = this->IdentifyHostDirectory(hostEnvironment); if (EXIT_CODE_SUCCESS != exitCode) @@ -1364,130 +1298,80 @@ namespace NativeMsh } // Try to load from the well-known location. - wchar_t coreCLRInstallPath[MAX_PATH]; - exitCode = ::ExpandEnvironmentStringsW(coreCLRInstallDirectory, coreCLRInstallPath, MAX_PATH); + char coreCLRInstallPath[MAX_PATH]; + exitCode = ::ExpandEnvironmentStringsA(coreCLRInstallDirectory, coreCLRInstallPath, MAX_PATH); if (0 == exitCode || _countof(coreCLRInstallPath) <= exitCode) { this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); return EXIT_CODE_INIT_FAILURE; } - HMODULE coreClrModule = this->TryLoadCoreCLR(coreCLRInstallPath); - - if (coreClrModule) + HMODULE coreClrModule = hostWrapper->SetupWrapper(coreCLRInstallPath); + if (!coreClrModule) { - // Save the directory that CoreCLR was found in - WCHAR coreCLRDirectoryPath[MAX_PATH]; - DWORD modulePathLength = sysCalls->GetModuleFileNameW(coreClrModule, coreCLRDirectoryPath, MAX_PATH); + this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); + return EXIT_CODE_INIT_FAILURE; + } + + // Save the directory that CoreCLR was found in + char coreCLRDirectoryPath[MAX_PATH]; + DWORD modulePathLength = sysCalls->GetModuleFileNameA(coreClrModule, coreCLRDirectoryPath, MAX_PATH); - // Search for the last backslash and terminate it there to keep just the directory path with trailing slash - for (int lastBackslashIndex = modulePathLength - 1; lastBackslashIndex >= 0; lastBackslashIndex--) + // Search for the last backslash and terminate it there to keep just the directory path with trailing slash + for (int lastBackslashIndex = modulePathLength - 1; lastBackslashIndex >= 0; lastBackslashIndex--) + { + if (coreCLRDirectoryPath[lastBackslashIndex] == L'\\') { - if (coreCLRDirectoryPath[lastBackslashIndex] == L'\\') - { - coreCLRDirectoryPath[lastBackslashIndex + 1] = L'\0'; - break; - } + coreCLRDirectoryPath[lastBackslashIndex + 1] = L'\0'; + break; } - hostEnvironment.SetCoreCLRDirectoryPath(coreCLRDirectoryPath); - } - else - { - this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); - return EXIT_CODE_INIT_FAILURE; } + hostEnvironment.SetCoreCLRDirectoryPath(coreCLRDirectoryPath); - exitCode = this->InitializeClr(hostWrapper, coreClrModule); - if (EXIT_CODE_SUCCESS != exitCode) - { - this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); - return exitCode; - } - - hostEnvironment.SetCoreCLRModule(coreClrModule); - - return exitCode; - } - - unsigned int PwrshCommon::CreateAppDomain( - ClrHostWrapper* hostWrapper, - PCWSTR friendlyName, - HostEnvironment& hostEnvironment) - { - const int nMaxProps = 8; - LPCWSTR props[nMaxProps]; - LPCWSTR vals[nMaxProps]; - - if (!hostWrapper->IsInitialized()) - { - return EXIT_CODE_INIT_FAILURE; - } - - //PAL_LeaveHolder holder; - DWORD dwDomainFlags = 0; - dwDomainFlags = APPDOMAIN_SECURITY_DEFAULT; - dwDomainFlags |= APPDOMAIN_ENABLE_ASSEMBLY_LOADFILE; - dwDomainFlags |= APPDOMAIN_DISABLE_TRANSPARENCY_ENFORCEMENT; - - // By default CoreCLR only allows platform neutral assembly to be run. To allow - // assemblies marked as platform specific, include this flag - dwDomainFlags |= APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS; - // Enable PInvoke - dwDomainFlags |= APPDOMAIN_ENABLE_PINVOKE_AND_CLASSIC_COMINTEROP; - // This will not tear down an application if a managed exception goes unhandled - dwDomainFlags |= APPDOMAIN_IGNORE_UNHANDLED_EXCEPTIONS; - + const int nMaxProps = 8; + LPCSTR props[nMaxProps]; + LPCSTR vals[nMaxProps]; int nProps = 0; - props[nProps] = L"APPBASE"; - vals[nProps] = hostEnvironment.GetHostDirectoryPath(); - nProps++; // If I do not include my managed enload point dll in this list, I get a security error. - std::wstringstream assemblyList; + std::stringstream assemblyList; bool listEmpty = true; this->GetTrustedAssemblyList(hostEnvironment.GetCoreCLRDirectoryPath(), assemblyList, listEmpty); - wchar_t coreCLRPowerShellExtInstallPath[MAX_PATH]; - ::ExpandEnvironmentStringsW(coreCLRPowerShellExtInstallDirectory, coreCLRPowerShellExtInstallPath, MAX_PATH); + char coreCLRPowerShellExtInstallPath[MAX_PATH]; + ::ExpandEnvironmentStringsA(coreCLRPowerShellExtInstallDirectory, coreCLRPowerShellExtInstallPath, MAX_PATH); this->GetTrustedAssemblyList(coreCLRPowerShellExtInstallPath, assemblyList, listEmpty); - props[nProps] = L"TRUSTED_PLATFORM_ASSEMBLIES"; - std::wstring tempStr = assemblyList.str(); + props[nProps] = "TRUSTED_PLATFORM_ASSEMBLIES"; + std::string tempStr = assemblyList.str(); vals[nProps] = tempStr.c_str(); nProps++; props[nProps] = L"APP_PATHS"; - vals[nProps] = L""; + vals[nProps] = L""; // Used to be hostEnvironment.GetHostDirectoryPath() nProps++; props[nProps] = L"APP_NI_PATHS"; - vals[nProps] = L""; + vals[nProps] = L""; // Used to be hostEnvironment.GetHostDirectoryPath() nProps++; - // Create the customized AppDomainManager out of the SandboxHelper class - DWORD appDomainId = INVALID_APPDOMAIN_ID; - HRESULT hr = hostWrapper->CreateAppDomainWithManager( - friendlyName, - dwDomainFlags, - NULL, // AppDomainManager is no longer required now that we can use AssemblyLoadContext to access arbitrary assemblies from within SMA.dll - NULL, - nProps, - props, - vals, - &appDomainId); + int hr = hostWrapper->InitializeClr( + hostEnvironment.GetHostDirectoryPath(), + friendlyName, + nProps, + props, + vals); + if (FAILED(hr)) { - //LONG systemErrorCode = GetLastError(); - this->output->DisplayMessage(false, g_GETTING_DEFAULT_DOMAIN_FAILED, hr); - + this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); return EXIT_CODE_INIT_FAILURE; } - hostWrapper->SetAppDomainId(appDomainId); + return EXIT_CODE_SUCCESS; } - #else // !CORECLR - +#if !CORECLR // NOTE: // This must be ifdef'd out of the CoreCLR build because it uses .NET 1.0 // types that have been deprecated and removed from mscoree.h. @@ -1550,7 +1434,7 @@ namespace NativeMsh return exitCode; } - #endif // !CORECLR +#endif // !CORECLR #pragma prefast(pop) diff --git a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt new file mode 100644 index 000000000..dc339562e --- /dev/null +++ b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt @@ -0,0 +1,48 @@ +# +# Builds PowerShell.exe, the native host for PowerShell. +# +if (BUILD_ONECORE) + # Settings to use when creating PowerShell.exe for Windows on OneCore-based SKUs + set(PWRSHEXE_WINDOWS_SOURCES + # TODO: Fix this once the file is migrated from SD + # CssMainEntry.cpp) + MainEntry.cpp) + set(PWRSHEXE_WINDOWS_LIBS + onecore.lib) + set(powershell_definitions + _CONSOLE + CORECLR + ${common_pwrsh_definitions}) +else (BUILD_ONECORE) + # Settings to use when creating PowerShell.exe for Windows on full SKUs or downlevel platforms + set(PWRSHEXE_WINDOWS_SOURCES + MainEntry.cpp) + # Most libs are automatically added by VS + set(PWRSHEXE_WINDOWS_LIBS + # ole32.lib + # oleaut32.lib + # uuid.lib + # user32.lib + MUILoad.lib + msxml6.lib + mscoree.lib + ) + set(powershell_definitions + _CONSOLE + ${common_pwrsh_definitions}) +endif (BUILD_ONECORE) + +add_executable(powershell + ${PWRSHEXE_WINDOWS_SOURCES}) + +set_target_properties(powershell PROPERTIES COMPILE_DEFINITIONS "${powershell_definitions}") +set_target_properties(powershell PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") +set_target_properties(powershell PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") +set_target_properties(powershell PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") +set_target_properties(powershell PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") + +target_link_libraries(powershell + ${PWRSHEXE_WINDOWS_LIBS}) + +#target_link_libraries(powershell LINK_PUBLIC pwrshcommon) +target_link_libraries(powershell pwrshcommon) diff --git a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt new file mode 100644 index 000000000..817dceac2 --- /dev/null +++ b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt @@ -0,0 +1,37 @@ +# +# Builds pwrshplugin.dll, the WinRM plugin for PowerShell remote hosting. +# +add_library(pwrshplugin + entrypoints.cpp + pwrshclrhost.cpp + pwrshpluginerrorcodes.mc + pwrshpluginerrorcodes.rc + pwrshplugin.def) + +if (BUILD_ONECORE) + # Libraries to use when creating this binary for Windows on OneCore-based SKUs + set(PWRSHPLUGIN_WINDOWS_LIBS + onecore.lib) + set(pwrshplugin_definitions ${common_pwrsh_definitions} CORECLR) +else (BUILD_ONECORE) + # Libraries to use when creating this binary for Windows on full SKUs + set(PWRSHPLUGIN_WINDOWS_LIBS + ntdll.lib + kernel32.lib + advapi32.lib + ole32.lib + MUILoad.lib + mscoree.lib + oleaut32.lib + uuid.lib + wsmsvc.lib) + set(pwrshplugin_definitions ${common_pwrsh_definitions}) +endif (BUILD_ONECORE) + +set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") + +target_link_libraries(pwrshplugin + ${PWRSHPLUGIN_WINDOWS_LIBS}) + +#target_link_libraries(pwrshplugin LINK_PUBLIC pwrshcommon) +target_link_libraries(pwrshplugin pwrshcommon) diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp index db5a72c3d..533b0efe4 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp @@ -40,9 +40,10 @@ typedef void (STDMETHODCALLTYPE *LoaderRunHelperFp)(LPCWSTR appPath); unsigned int PowerShellCoreClrWorker::LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion) + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCSTR friendlyName) { - return commonLib->LaunchCoreCLR(hostWrapper, hostEnvironment); + return commonLib->LaunchCoreCLR(hostWrapper, hostEnvironment, friendlyName); } unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( @@ -50,23 +51,15 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( _In_z_ wchar_t* wszMgdPlugInFileName, _Outptr_result_maybenull_ PlugInException** pPluginException) { - unsigned int exitCode = EXIT_CODE_SUCCESS; *pPluginException = NULL; - exitCode = commonLib->CreateAppDomain(hostWrapper, L"PwrshPlugin", hostEnvironment); - if (EXIT_CODE_SUCCESS != exitCode) - { - return exitCode; - } - // Set the powershell custom assembly loader to be the default LoaderRunHelperFp initDelegate = NULL; HRESULT hr = hostWrapper->CreateDelegate( - hostWrapper->GetAppDomainId(), - L"Microsoft.PowerShell.CoreCLR.AssemblyLoadContext, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", - L"System.Management.Automation.PowerShellAssemblyLoadContextInitializer", - L"SetPowerShellAssemblyLoadContext", + "Microsoft.PowerShell.CoreCLR.AssemblyLoadContext, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", + "System.Management.Automation.PowerShellAssemblyLoadContextInitializer", + "SetPowerShellAssemblyLoadContext", (INT_PTR*)&initDelegate); if (FAILED(hr)) @@ -75,7 +68,7 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( } else { - initDelegate(hostEnvironment.GetHostDirectoryPath()); + initDelegate(hostEnvironment.GetHostDirectoryPathW()); } // Call into powershell entry point @@ -84,14 +77,12 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( // Create the function pointer for the managed entry point // It must be targeted at a static method in the managed code. hr = hostWrapper->CreateDelegate( - hostWrapper->GetAppDomainId(), - L"System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", - L"System.Management.Automation.Remoting.WSManPluginManagedEntryWrapper", - L"InitPlugin", + "System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", + "System.Management.Automation.Remoting.WSManPluginManagedEntryWrapper", + "InitPlugin", (INT_PTR*)&entryPointDelegate); if (FAILED(hr)) { - //LONG systemErrorCode = GetLastError(); output->DisplayMessage(false, g_CREATING_MSH_ENTRANCE_FAILED, hr); return EXIT_CODE_INIT_FAILURE; @@ -103,7 +94,7 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( PowerShellCoreClrWorker::PowerShellCoreClrWorker() : systemCalls(new WinSystemCallFacade()), - hostWrapper(new ICLRRuntimeHost2Wrapper()), + hostWrapper(new CoreClrHostingApiWrapper()), output(new PwrshPluginOutputDefault()), commonLib(new PwrshCommon()) { @@ -133,7 +124,7 @@ PowerShellCoreClrWorker::PowerShellCoreClrWorker( { // Instantiate it even if one is not provided to guarantee that it will // always be non-NULL during execution. - hostWrapper = new ICLRRuntimeHost2Wrapper(); + hostWrapper = new CoreClrHostingApiWrapper(); } if (NULL == commonLib) @@ -240,7 +231,8 @@ PowerShellClrWorker::~PowerShellClrWorker() unsigned int PowerShellClrWorker::LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion) + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCWSTR friendlyName) { return commonLib.LaunchCLR(wszMonadVersion, wszRuntimeVersion, &pHost); } @@ -310,7 +302,8 @@ unsigned int PowerShellClrWorker::LoadWorkerCallbackPtrs( unsigned int PowerShellClrManagedWorker::LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion) + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCWSTR friendlyName) { return commonLib.LaunchCLR(wszMonadVersion, wszRuntimeVersion, &pHost); } diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h index 50ea8258e..82852c4e4 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h @@ -46,7 +46,8 @@ public: virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion) = 0; + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCWSTR friendlyName) = 0; virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, @@ -88,7 +89,8 @@ public: // virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion); + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCWSTR friendlyName); virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, @@ -114,7 +116,8 @@ public: // virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion); + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCWSTR friendlyName); virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h index ab7c6d267..6a45f248b 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h @@ -714,7 +714,7 @@ private: break; } - exitCode = powerShellClrHost->LaunchClr(wszMonadVersion, wszTempCLRVersion); + exitCode = powerShellClrHost->LaunchClr(wszMonadVersion, wszTempCLRVersion, L"PwrshPlugin"); if (EXIT_CODE_SUCCESS != exitCode) { PWSTR msg = NULL; @@ -1098,4 +1098,4 @@ void WINAPI PerformWSManPluginReportCompletion() // - pluginContext MUST be the same context that plugin provided to the WSManPluginStartup method // - flags are reserved, so 0 WSManPluginReportCompletion(g_pPluginContext, 0); -} \ No newline at end of file +} From 77a26b6b77794717f6ece6a04a5d47dc023f5dd9 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Thu, 7 Jul 2016 11:34:10 -0700 Subject: [PATCH 2/9] Additional code changes and link fixes --- build.psm1 | 101 +++-- src/powershell-native/CMakeLists.txt | 382 +++++++++++++++++- .../nativemsh/pwrshcommon/CMakeLists.txt | 9 +- .../nativemsh/pwrshcommon/ClrHostWrapper.h | 26 ++ .../nativemsh/pwrshcommon/SystemCallFacade.h | 3 +- .../pwrshcommon/WinSystemCallFacade.cpp | 5 +- .../pwrshcommon/WinSystemCallFacade.h | 3 +- .../nativemsh/pwrshcommon/pwrshcommon.cpp | 6 +- .../nativemsh/pwrshexe/CMakeLists.txt | 35 +- .../nativemsh/pwrshplugin/CMakeLists.txt | 36 +- .../nativemsh/pwrshplugin/pwrshclrhost.cpp | 9 +- .../nativemsh/pwrshplugin/pwrshclrhost.h | 13 +- .../nativemsh/pwrshplugin/pwrshplugin.h | 2 +- .../windows-compiler-override.txt | 16 + 14 files changed, 563 insertions(+), 83 deletions(-) create mode 100644 src/powershell-native/windows-compiler-override.txt diff --git a/build.psm1 b/build.psm1 index 8d9998cf4..c877ddc8d 100644 --- a/build.psm1 +++ b/build.psm1 @@ -62,7 +62,7 @@ function Start-PSBuild { [ValidateSet('x86', 'x64')] [string]$NativeHostArch = "x64", - [ValidateSet('Linux', 'Debug', 'Release', '')] + [ValidateSet('Linux', 'Debug', 'Release', '')] # We might need "Checked" as well [string]$Configuration ) @@ -156,36 +156,36 @@ function Start-PSBuild { $Arguments += "--runtime", $Options.Runtime # handle Restore - if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) { - log "Run dotnet restore" +# if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) { +# log "Run dotnet restore" - $RestoreArguments = @("--verbosity") - if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { - $RestoreArguments += "Info" - } else { - $RestoreArguments += "Warning" - } - - $RestoreArguments += "$PSScriptRoot" - - Start-NativeExecution { dotnet restore $RestoreArguments } - } +# $RestoreArguments = @("--verbosity") +# if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { +# $RestoreArguments += "Info" +# } else { +# $RestoreArguments += "Warning" +# } +# +# $RestoreArguments += "$PSScriptRoot" +# +# Start-NativeExecution { dotnet restore $RestoreArguments } +# } # handle ResGen # Heuristic to run ResGen on the fresh machine - if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) - { - log "Run ResGen (generating C# bindings for resx files)" - Start-ResGen - } +# if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) +# { +# log "Run ResGen (generating C# bindings for resx files)" +# Start-ResGen +# } # handle xaml files # Heuristic to resolve xaml on the fresh machine - if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) - { - log "Run XamlGen (generating .g.cs and .resources for .xaml files)" - Start-XamlGen -MSBuildConfiguration $msbuildConfiguration - } +# if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) +# { +# log "Run XamlGen (generating .g.cs and .resources for .xaml files)" +# Start-XamlGen -MSBuildConfiguration $msbuildConfiguration +# } # Build native components if ($IsLinux -or $IsOSX) { @@ -217,6 +217,12 @@ function Start-PSBuild { try { Push-Location "$PSScriptRoot\src\powershell-native" + $vcPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName + if ((Test-Path -Path $vcPath\vcvarsall.bat) -eq $false) + { + throw "Could not find Visual Studio vcvarsall.bat at" + $vcPath + } + # Compile native resources @("nativemsh/pwrshplugin") | % { $nativeResourcesFolder = $_ @@ -224,18 +230,46 @@ function Start-PSBuild { & $mcexe -o -d -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder } } - - if ($cmakeGenerator) { - Start-NativeExecution { cmake -G $cmakeGenerator . } - } else { - Start-NativeExecution { cmake . } - } - - Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } + + $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt" + $vcArch = "Win64" + $cmakeGenerator = "Visual Studio 14 2015 $vcArch" +# $CMakeArguments = "-G $cmakeGenerator ." + $vcPlatform = "x64" + $location = Get-Location +# $MSBuildArguments = "ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" +# if ($cmakeGenerator) { +# Start-NativeExecution { cmake $overrideFlags -G $cmakeGenerator . } +# } else { +# Start-NativeExecution { cmake $overrideFlags . } +# } +# # +# # BUILD_ONECORE +# # + $command = @" +cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" cmake "$overrideFlags" -DBUILD_ONECORE=ON -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" +"@ +#cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" "$cmakeCmd" "&" "msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" +# "&" exit + log " Executing command: $command" + #Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } + Start-NativeExecution { Invoke-Expression -Command:$command } + # + # NOT BUILD_ONECORE + # + $command = @" +cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" cmake "$overrideFlags" -DBUILD_ONECORE=OFF -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" +"@ +#cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" "$cmakeCmd" "&" "msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" +# "&" exit + log " Executing command: $command" + #Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } + Start-NativeExecution { Invoke-Expression -Command:$command } } finally { Pop-Location } + throw "All done" } # handle TypeGen @@ -254,7 +288,6 @@ function Start-PSBuild { } finally { Pop-Location } - } @@ -1312,7 +1345,7 @@ function script:Start-NativeExecution([scriptblock]$sb) try { & $sb - # note, if $sb doens't have a native invokation, $LASTEXITCODE will + # note, if $sb doens't have a native invocation, $LASTEXITCODE will # point to the obsolete value if ($LASTEXITCODE -ne 0) { diff --git a/src/powershell-native/CMakeLists.txt b/src/powershell-native/CMakeLists.txt index 171ed8fe7..07fc79ef8 100644 --- a/src/powershell-native/CMakeLists.txt +++ b/src/powershell-native/CMakeLists.txt @@ -1,27 +1,385 @@ cmake_minimum_required(VERSION 2.8.4) -project(PowerShell) +project(PowerShellNative) -add_compile_options() +# +# Verify prerequisites +# +if (NOT $ENV{${WindowsSDKVersion}}) + message (FATAL_ERROR "WindowsSDKVersion environment variable not found") +endif () -# set the output path for `powershell.exe` -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../Microsoft.PowerShell.ConsoleHost") +if (NOT $ENV{${Platform}}) + message(FATAL_ERROR "Platform environment variable not found") +else () + SET(WindowsSDKPlatform "$ENV{Platform}") +endif () + +# +# Get the path to the Windows SDK build tools. +# +if (WindowsSDKPlatform STREQUAL "x64" OR WindowsSDKPlatform STREQUAL "X64" OR WindowsSDKPlatform STREQUAL "amd64" OR WindowsSDKPlatform STREQUAL "AMD64") + # Normalize the platform name + SET(WindowsSDKPlatform "x64") +elseif (WindowsSDKPlatform STREQUAL "x86" OR WindowsSDKPlatform STREQUAL "X86") + # Normalize the platform name + SET(WindowsSDKPlatform "x86") +else() + message(FATAL_ERROR "Unsupported WindowsSDKPlatform ${WindowsSDKPlatform}") +endif () + +# +# set the output path for all binaries +# +if (BUILD_ONECORE) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Bin/CoreClr") +else () + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Bin/FullClr") +endif (BUILD_ONECORE) foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + message(" Setting output directory for ${OUTPUTCONFIG} to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES ) +# +# Definitions for ease of reading +# +SET (WIN_VERSION_WIN10_RS1 0x0A000002) +SET (WIN_VERSION_WIN10_TH2 0x0A000001) +SET (WIN_VERSION_WIN10 0x0A00) +SET (WIN_VERSION_WINTHRESHOLD 0x0A00) +SET (WIN_VERSION_WINBLUE 0x0603) +SET (WIN_VERSION_WIN8 0x0602) +SET (WIN_VERSION_WIN7 0x0601) +SET (NTDDI_VERSION_WIN7 0x06010000) +SET (WIN_VERSION_VISTA 0x0600) +SET (WIN_VERSION_LONGHORN 0x0600) +SET (WIN_VERSION_WS03 0x0502) +SET (WIN_VERSION_WINXP 0x0501) + +# *** +# *** Original flags: BEGIN +# *** # # TODO: Will this interfere with OneCore compilations? # # set these flags, so build does static linking for msvcr120.dll # otherwise this dll need to be present on the system -set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") +#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") +#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") # Compiler flags that are universal to all powershell native binaries -SET(common_pwrsh_definitions - UNICODE - _UNICODE) +#SET(common_pwrsh_definitions +# UNICODE +# _UNICODE) + +#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /VERBOSE") + +# *** +# *** Original flags: END +# *** + +# *** +# *** CoreCLR flags: BEGIN +# *** + +set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. + +#if(NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf") +#endif (NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) + +# Incremental linking with CFG is broken until next VS release. +# This needs to be appended to the last for each build type to override the default flag. +set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO") + +# Linker flags +# +# Disable the following line for UNIX altjit on Windows +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization +#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG") + +# +# TODO: Should these be ignored???? +# +#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221") +#set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221") + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS") +#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864") + +# Debug build specific flags +#set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") + +# Checked build specific flags +#set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") +#set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}") +#set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# Release build specific flags +#set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") +#set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") +#set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# ReleaseWithDebugInfo build specific flags +#set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") +#set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") +#set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# Temporary until cmake has VS generators for arm64 +#if(CLR_CMAKE_PLATFORM_ARCH_ARM64) +# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64") +# set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64") +# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64") +#endif(CLR_CMAKE_PLATFORM_ARCH_ARM64) + +# Force uCRT to be dynamically linked for Release build +#set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +#set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +#set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +#set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") + +#------------------------------------ +# Definitions (for platform) +#----------------------------------- +#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) + add_definitions(-D_AMD64_) + add_definitions(-D_WIN64) + add_definitions(-DAMD64) + add_definitions(-DBIT64=1) +#elseif (CLR_CMAKE_PLATFORM_ARCH_I386) +# add_definitions(-D_X86_) +#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM) +# add_definitions(-D_ARM_) +# add_definitions(-DARM) +#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64) +# add_definitions(-D_ARM64_) +# add_definitions(-DARM64) +# add_definitions(-D_WIN64) +# add_definitions(-DBIT64=1) +#else () +# clr_unknown_arch() +#endif () + +# Define the CRT lib references that link into Desktop imports +set(STATIC_MT_CRT_LIB "libcmt$<$,$>:d>.lib") +set(STATIC_MT_VCRT_LIB "libvcruntime$<$,$>:d>.lib") +set(STATIC_MT_CPP_LIB "libcpmt$<$,$>:d>.lib") + +# Define the uCRT lib reference +set(STATIC_UCRT_LIB "libucrt$<$,$>:d>.lib") +set(DYNAMIC_UCRT_LIB "ucrt$<$,$>:d>.lib") + +#------------------------------------ +# Definitions for compile +#----------------------------------- + +add_compile_options(/Zl) # omit default library name in .OBJ + +# The following options are set by the razzle build +add_compile_options(/TP) # compile all files as C++ +add_compile_options(/d2Zi+) # make optimized builds debugging easier +add_compile_options(/nologo) # Suppress Startup Banner +add_compile_options(/W3) # set warning level to 3 +#add_compile_options(/WX) # treat warnings as errors +add_compile_options(/Oi) # enable intrinsics +add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls +#add_compile_options(/U_MT) # undefine the predefined _MT macro +add_compile_options(/GF) # enable read-only string pooling +add_compile_options(/Gm-) # disable minimal rebuild +add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions) +add_compile_options(/Zp8) # pack structs on 8-byte boundary +add_compile_options(/Gy) # separate functions for linker +add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef +add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules +add_compile_options(/GR-) # disable C++ RTTI +add_compile_options(/FC) # use full pathnames in diagnostics +#add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors) +add_compile_options(/GS) # Buffer Security Check +add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB +add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640) +add_compile_options(/Zi) # enable debugging information +add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files. + +#if (CLR_CMAKE_PLATFORM_ARCH_I386) +# add_compile_options(/Gz) +#endif (CLR_CMAKE_PLATFORM_ARCH_I386) + +add_compile_options($<$,$>:/GL>) +add_compile_options($<$,$>,$>:/O1>) + +#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) +# The generator expression in the following command means that the /homeparams option is added only for debug builds + add_compile_options($<$:/homeparams>) # Force parameters passed in registers to be written to the stack +#endif (CLR_CMAKE_PLATFORM_ARCH_AMD64) + +# enable control-flow-guard support for native components for non-Arm64 builds +add_compile_options(/guard:cf) + +# Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid +# linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST. +# +# For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but +# wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not +# production-time scenarios. +add_compile_options($<$,$>:/MT>) +add_compile_options($<$,$>:/MTd>) + +set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256") + +#if (CLR_CMAKE_TARGET_ARCH_AMD64) + add_definitions(-D_TARGET_AMD64_=1) + add_definitions(-DDBG_TARGET_64BIT=1) + add_definitions(-DDBG_TARGET_AMD64=1) + add_definitions(-DDBG_TARGET_WIN64=1) +#elseif (CLR_CMAKE_TARGET_ARCH_ARM) +# add_definitions(-D_TARGET_ARM_=1) +# add_definitions(-DDBG_TARGET_32BIT=1) +# add_definitions(-DDBG_TARGET_ARM=1) +#elseif (CLR_CMAKE_TARGET_ARCH_I386) +# add_definitions(-D_TARGET_X86_=1) +# add_definitions(-DDBG_TARGET_32BIT=1) +# add_definitions(-DDBG_TARGET_X86=1) +#else () +# clr_unknown_arch() +#endif (CLR_CMAKE_TARGET_ARCH_AMD64) + +# +# TODO: Are these needed??????????? +# +#add_definitions(-D_BLD_CLR) +#add_definitions(-DDEBUGGING_SUPPORTED) +#add_definitions(-DPROFILING_SUPPORTED) + +add_definitions(-DWIN32) +add_definitions(-D_WIN32) +add_definitions(-DWINVER=${WIN_VERSION_WIN7})# 0x0601 for Win7, was 0x0602 +add_definitions(-D_WIN32_WINNT=${WIN_VERSION_WIN7}) # 0x0601 for Win7 +add_definitions(-DWIN32_LEAN_AND_MEAN=1) +add_definitions(-D_CRT_SECURE_NO_WARNINGS) +#if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) + # Only enable edit and continue on windows x86 and x64 + # exclude Linux, arm & arm64 + add_definitions(-DEnC_SUPPORTED) +#endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) + +add_definitions(-DUNICODE) +add_definitions(-D_UNICODE) + +# *** +# *** CoreCLR flags: END +# *** + +# *** +# *** DSC flags: BEGIN +# *** + + +SET (WindowsSdkDir $ENV{WindowsSdkDir}) +SET (WindowsSDKVersion $ENV{WindowsSDKVersion}) +SET (NETFXSdkDir $ENV{NETFXSDKDir}) +#SET (FrameWorkLibPath $ENV{FrameworkDir}/$ENV{FrameworkVersion}) + +# +# Configure include directories +# +SET (WindowsSDKIncludeBase "${WindowsSdkDir}/Include/${WindowsSDKVersion}") + +SET (IncludePath) +#list (APPEND IncludePath "${INTERNAL_HEADER_DIR}") +#list (APPEND IncludePath "${PUBLIC_HEADER_DIR}") +list (APPEND IncludePath "${WindowsSDKIncludeBase}winrt") +# Don't include due to incompatible instance.h +# list (APPEND IncludePath "${WindowsSDKIncludeBase}um") +list (APPEND IncludePath "${WindowsSDKIncludeBase}shared") +list (APPEND IncludePath "${NETFXSdkDir}/Include/um") +list (APPEND IncludePath "${WindowsSDKIncludeBase}ucrt") +include_directories(BEFORE ${IncludePath}) + +# +# Configure lib directories +# +SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") + +SET (LibraryPath) +list (APPEND LibraryPath "${WindowsSDKLibBase}ucrt/${WindowsSDKPlatform}") +list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") +list (APPEND LibraryPath "${WindowsSDKLibBase}um/${WindowsSDKPlatform}" ) +list (APPEND LibraryPath "${INTERNAL_LIBRARY_DIR}") +list (APPEND LibraryPath "${PUBLIC_LIBRARY_DIR}") +#list (APPEND LibraryPath ${FrameWorkLibPath}) +SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") +link_directories(${LibraryPath}) + +#if (${WindowsSDKPlatform} STREQUAL "x64") +# add_definitions ( +# -D_WIN64 +# -D_AMD64_ +# -DAMD64 +# -DBUILD_WOW64_ENABLED=1 +# -DBUILD_UMS_ENABLED=1 +# ) +#else() +# add_definitions ( +# -DBUILD_WOW64_ENABLED=1 +# -DBUILD_UMS_ENABLED=0 +# ) +#endif() + +# +# Common defines. +# +#add_definitions ( +# -D_CRT_SECURE_NO_WARNINGS +# -DCONDITION_HANDLING=1 +# -DNT_UP=1 +# -DNT_INST=0 +# -D_NT1X_=100 +# -DWINNT=1 +# -DWIN32_LEAN_AND_MEAN=1 +# -DDEVL=1 +# -D_MT=1 +# -D_STL70_ +# -DMI_INTERNAL +# -DWINBUILD +# -DHOOK_BUILD # TODO: should be target specific +# -DCONFIG_ENABLE_WCHAR +# -D_INTLSTR_NOTAPPEND_NULL +# -DMSC_NOOPT +# -DBUILD_WINDOWS +# -D_USE_DECLSPECS_FOR_SAL=1 +# -DUNICODE +# -D_UNICODE +# -D_USE_DEV11_CRT +#) + +# +# platform specific defines +# +#add_definitions( +# -DNTDDI_VERSION=${NTDDI_VERSION_WIN7} +# -DWINBLUE_KBSPRING14 +# -D_APISET_WINDOWS_VERSION=${WIN_VERSION_WIN7} +# -D_APISET_MINWIN_VERSION=0x0100 +# -D_APISET_MINCORE_VERSION=0x0100 +# -D_WIN32_IE=0x0800 +# -D_WIN32_WINNT=${WIN_VERSION_WIN7} +# -DWINVER=${WIN_VERSION_WIN7} +# ) + +# *** +# *** DSC flags: END +# *** # Default of BUILD_ONECORE should be ON once it is supported option(BUILD_ONECORE "Compile the OneCore version of the binaries" OFF) @@ -34,5 +392,7 @@ add_subdirectory(nativemsh/pwrshcommon) add_subdirectory(nativemsh/pwrshplugin) # Build powershell.exe -add_subdirectory(nativemsh/pwrshexe) - +if (NOT BUILD_ONECORE) + # Conditionally build powershell.exe until CssMainEntry.cpp appears. + add_subdirectory(nativemsh/pwrshexe) +endif () diff --git a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt index 947ab92c5..0330f7f48 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt @@ -9,14 +9,21 @@ add_library(pwrshcommon target_include_directories(pwrshcommon PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) if (BUILD_ONECORE) + message("Building OneCore version of pwrshcommon.lib") # Libraries to use when creating this binary for Windows on OneCore-based SKUs set(PWRSHCOMMON_WINDOWS_LIBS onecore.lib) set(pwrshcommon_definitions ${common_pwrsh_definitions} CORECLR) -else () # !BUILD_ONECORE +else () # NOT BUILD_ONECORE + message("Building Full version of pwrshcommon.lib") # Libraries to use when creating this binary for Windows on full SKUs # Note: The appropriate libs get added automatically by VS set(PWRSHCOMMON_WINDOWS_LIBS + # CoreCLR libs + ${STATIC_MT_CRT_LIB} + ${STATIC_MT_VCRT_LIB} + ${STATIC_UCRT_LIB} + # # ole32.lib # oleaut32.lib # uuid.lib diff --git a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h index 861581cc5..54082ca9c 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h +++ b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h @@ -245,6 +245,7 @@ namespace NativeMsh // The path to the directory that CoreCLR is in char m_coreCLRDirectoryPath[MAX_PATH]; + wchar_t m_coreCLRDirectoryPathW[MAX_PATH]; public: @@ -333,6 +334,25 @@ namespace NativeMsh if (hostClrPath) { ::ExpandEnvironmentStringsA(hostClrPath, m_coreCLRDirectoryPath, MAX_PATH); + + // Generate the wide version of the string and save its value; + // + // This is a two call function. The first call is to get the necessary length. + // The second call is to perform the actual operation. + int length = MultiByteToWideChar(CP_UTF8, 0, m_coreCLRDirectoryPath, -1, NULL, 0); + if (0 < length) + { + LPWSTR result = new wchar_t[length]; + if (NULL != result) + { + length = ::MultiByteToWideChar(CP_UTF8, 0, m_coreCLRDirectoryPath, -1, result, length); + if (0 < length) + { + wcscpy_s(m_coreCLRDirectoryPathW, MAX_PATH, result); + } + delete[] result; // Free the allocated string to avoid a memory leak + } + } } } @@ -341,5 +361,11 @@ namespace NativeMsh { return m_coreCLRDirectoryPath; } + + // Returns the directory path of the host module + PCWSTR GetCoreCLRDirectoryPathW() + { + return m_coreCLRDirectoryPathW; + } }; } // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h b/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h index 97dcfa194..c44d6a314 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h +++ b/src/powershell-native/nativemsh/pwrshcommon/SystemCallFacade.h @@ -48,7 +48,8 @@ namespace NativeMsh _In_ HMODULE hModule) = 0; // File Manipulation Wrappers - virtual FILE* fopen( + virtual errno_t fopen_s( + FILE** file, const char *filename, const char *mode) = 0; diff --git a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp index 9f49a43d5..0c61d5bf6 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.cpp @@ -53,11 +53,12 @@ namespace NativeMsh return ::FreeLibrary(hModule); } - FILE* WinSystemCallFacade::fopen( + errno_t WinSystemCallFacade::fopen_s( + FILE** file, const char *filename, const char *mode) { - return ::fopen(filename, mode); + return ::fopen_s(file, filename, mode); } int WinSystemCallFacade::fclose( diff --git a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h index 1ec4379e8..76ecf01f3 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h +++ b/src/powershell-native/nativemsh/pwrshcommon/WinSystemCallFacade.h @@ -46,7 +46,8 @@ namespace NativeMsh _In_ HMODULE hModule); // File Manipulation Wrappers - virtual FILE* fopen( + virtual errno_t fopen_s( + FILE** file, const char *filename, const char *mode); diff --git a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp index 5d1229e32..daf3661e8 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp @@ -868,11 +868,13 @@ namespace NativeMsh bool PwrshCommon::DoesAssemblyExist( std::string& fileToTest) { - FILE *file = sysCalls->fopen(fileToTest.c_str(), "r"); // TODO: Use fopen_s? + //FILE *file = sysCalls->fopen(fileToTest.c_str(), "r"); // TODO: Use fopen_s? + FILE *file = NULL; + errno_t status = sysCalls->fopen_s(&file, fileToTest.c_str(), "r"); if (file != NULL) { sysCalls->fclose(file); - return true; + return (status == 0); } return false; } diff --git a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt index dc339562e..43aed8b47 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt @@ -1,24 +1,38 @@ # # Builds PowerShell.exe, the native host for PowerShell. # + +# Only build powershell.exe for "full" windows. Open PowerShell uses the managed executable. if (BUILD_ONECORE) + message("*") + message("*********** Building PowerShell.exe for 'full' CLR instead of OneCore. Add CssMainEntry.cpp") + message("*") + #set(BUILD_ONECORE OFF) +endif () + +#if (BUILD_ONECORE) # Settings to use when creating PowerShell.exe for Windows on OneCore-based SKUs - set(PWRSHEXE_WINDOWS_SOURCES +# set(PWRSHEXE_WINDOWS_SOURCES # TODO: Fix this once the file is migrated from SD # CssMainEntry.cpp) - MainEntry.cpp) - set(PWRSHEXE_WINDOWS_LIBS - onecore.lib) - set(powershell_definitions - _CONSOLE - CORECLR - ${common_pwrsh_definitions}) -else (BUILD_ONECORE) +# MainEntry.cpp) +# set(PWRSHEXE_WINDOWS_LIBS +# onecore.lib) +# set(powershell_definitions +# _CONSOLE +# CORECLR +# ${common_pwrsh_definitions}) +#else (BUILD_ONECORE) # Settings to use when creating PowerShell.exe for Windows on full SKUs or downlevel platforms set(PWRSHEXE_WINDOWS_SOURCES MainEntry.cpp) # Most libs are automatically added by VS set(PWRSHEXE_WINDOWS_LIBS + # CoreCLR libs + ${STATIC_MT_CRT_LIB} + ${STATIC_MT_VCRT_LIB} + ${STATIC_UCRT_LIB} + # # ole32.lib # oleaut32.lib # uuid.lib @@ -26,11 +40,12 @@ else (BUILD_ONECORE) MUILoad.lib msxml6.lib mscoree.lib + legacy_stdio_definitions.lib # Resolves: LNK2019: unresolved external symbol _vsnwprintf ) set(powershell_definitions _CONSOLE ${common_pwrsh_definitions}) -endif (BUILD_ONECORE) +#endif (BUILD_ONECORE) add_executable(powershell ${PWRSHEXE_WINDOWS_SOURCES}) diff --git a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt index 817dceac2..03449bc2b 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt @@ -1,7 +1,7 @@ # # Builds pwrshplugin.dll, the WinRM plugin for PowerShell remote hosting. # -add_library(pwrshplugin +add_library(pwrshplugin SHARED entrypoints.cpp pwrshclrhost.cpp pwrshpluginerrorcodes.mc @@ -9,26 +9,42 @@ add_library(pwrshplugin pwrshplugin.def) if (BUILD_ONECORE) + message("*") + message("* Setting up BUILD_ONECORE version of pwrshplugin.dll") + message("*") # Libraries to use when creating this binary for Windows on OneCore-based SKUs set(PWRSHPLUGIN_WINDOWS_LIBS - onecore.lib) + ${STATIC_MT_CRT_LIB} + ${STATIC_MT_VCRT_LIB} + ${STATIC_UCRT_LIB} + wsmsvc.lib) set(pwrshplugin_definitions ${common_pwrsh_definitions} CORECLR) -else (BUILD_ONECORE) +else () + message("*") + message("* Setting up NOT BUILD_ONECORE version of pwrshplugin.dll") + message("*") # Libraries to use when creating this binary for Windows on full SKUs set(PWRSHPLUGIN_WINDOWS_LIBS - ntdll.lib - kernel32.lib - advapi32.lib - ole32.lib +# ntdll.lib +# kernel32.lib +# advapi32.lib +# ole32.lib MUILoad.lib mscoree.lib - oleaut32.lib - uuid.lib - wsmsvc.lib) +# oleaut32.lib +# uuid.lib + wsmsvc.lib + # CoreCLR libs + ${STATIC_MT_CRT_LIB} + ${STATIC_MT_VCRT_LIB} + ${STATIC_UCRT_LIB} + legacy_stdio_definitions.lib # Resolves: LNK2019: unresolved external symbol _vsnwprintf + ) set(pwrshplugin_definitions ${common_pwrsh_definitions}) endif (BUILD_ONECORE) set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") +#set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "_DLL") target_link_libraries(pwrshplugin ${PWRSHPLUGIN_WINDOWS_LIBS}) diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp index 533b0efe4..8c0d5890e 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp @@ -60,7 +60,8 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( "Microsoft.PowerShell.CoreCLR.AssemblyLoadContext, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.Management.Automation.PowerShellAssemblyLoadContextInitializer", "SetPowerShellAssemblyLoadContext", - (INT_PTR*)&initDelegate); + //(INT_PTR*)&initDelegate); + (void**)&initDelegate); if (FAILED(hr)) { @@ -80,7 +81,7 @@ unsigned int PowerShellCoreClrWorker::LoadWorkerCallbackPtrs( "System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.Management.Automation.Remoting.WSManPluginManagedEntryWrapper", "InitPlugin", - (INT_PTR*)&entryPointDelegate); + (void**)&entryPointDelegate); if (FAILED(hr)) { output->DisplayMessage(false, g_CREATING_MSH_ENTRANCE_FAILED, hr); @@ -232,7 +233,7 @@ PowerShellClrWorker::~PowerShellClrWorker() unsigned int PowerShellClrWorker::LaunchClr( _In_ LPCWSTR wszMonadVersion, _In_ LPCWSTR wszRuntimeVersion, - _In_ LPCWSTR friendlyName) + _In_ LPCSTR friendlyName) { return commonLib.LaunchCLR(wszMonadVersion, wszRuntimeVersion, &pHost); } @@ -303,7 +304,7 @@ unsigned int PowerShellClrWorker::LoadWorkerCallbackPtrs( unsigned int PowerShellClrManagedWorker::LaunchClr( _In_ LPCWSTR wszMonadVersion, _In_ LPCWSTR wszRuntimeVersion, - _In_ LPCWSTR friendlyName) + _In_ LPCSTR friendlyName) { return commonLib.LaunchCLR(wszMonadVersion, wszRuntimeVersion, &pHost); } diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h index 82852c4e4..cef2d1c1c 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h @@ -47,7 +47,7 @@ public: virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, _In_ LPCWSTR wszRuntimeVersion, - _In_ LPCWSTR friendlyName) = 0; + _In_ LPCSTR friendlyName) = 0; virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, @@ -90,7 +90,7 @@ public: virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, _In_ LPCWSTR wszRuntimeVersion, - _In_ LPCWSTR friendlyName); + _In_ LPCSTR friendlyName); virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, @@ -117,7 +117,7 @@ public: virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, _In_ LPCWSTR wszRuntimeVersion, - _In_ LPCWSTR friendlyName); + _In_ LPCSTR friendlyName); virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, @@ -146,15 +146,16 @@ public: virtual ~PowerShellCoreClrWorker(); - std::wstring GetHostDirectory() { return std::wstring(hostEnvironment.GetHostDirectoryPath()); } - std::wstring GetClrDirectory() { return std::wstring(hostEnvironment.GetCoreCLRDirectoryPath()); } + std::wstring GetHostDirectory() { return std::wstring(hostEnvironment.GetHostDirectoryPathW()); } + std::wstring GetClrDirectory() { return std::wstring(hostEnvironment.GetCoreCLRDirectoryPathW()); } // // IPowerShellClrHost Methods // virtual unsigned int LaunchClr( _In_ LPCWSTR wszMonadVersion, - _In_ LPCWSTR wszRuntimeVersion); + _In_ LPCWSTR wszRuntimeVersion, + _In_ LPCSTR friendlyName); virtual unsigned int LoadWorkerCallbackPtrs( _In_ PwrshPluginWkr_Ptrs* workerCallbackPtrs, diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h index 6a45f248b..16efa0f3d 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h @@ -714,7 +714,7 @@ private: break; } - exitCode = powerShellClrHost->LaunchClr(wszMonadVersion, wszTempCLRVersion, L"PwrshPlugin"); + exitCode = powerShellClrHost->LaunchClr(wszMonadVersion, wszTempCLRVersion, "PwrshPlugin"); if (EXIT_CODE_SUCCESS != exitCode) { PWSTR msg = NULL; diff --git a/src/powershell-native/windows-compiler-override.txt b/src/powershell-native/windows-compiler-override.txt new file mode 100644 index 000000000..f6cb16856 --- /dev/null +++ b/src/powershell-native/windows-compiler-override.txt @@ -0,0 +1,16 @@ +SET (CMAKE_C_FLAGS_INIT "/Wall /FC") +SET (CMAKE_C_FLAGS_DEBUG_INIT "/Od /Zi") +SET (CLR_C_FLAGS_CHECKED_INIT "/O1 /Zi") +SET (CMAKE_C_FLAGS_RELEASE_INIT "/Ox /Zi") +SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/O2 /Zi") + +SET (CMAKE_CXX_FLAGS_INIT "/Wall /FC") +SET (CMAKE_CXX_FLAGS_DEBUG_INIT "/Od /Zi") +SET (CLR_CXX_FLAGS_CHECKED_INIT "/O1 /Zi") +SET (CMAKE_CXX_FLAGS_RELEASE_INIT "/Ox /Zi") +SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "/O2 /Zi") + +SET (CLR_DEFINES_DEBUG_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1) +SET (CLR_DEFINES_CHECKED_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1) +SET (CLR_DEFINES_RELEASE_INIT NDEBUG URTBLDENV_FRIENDLY=Retail) +SET (CLR_DEFINES_RELWITHDEBINFO_INIT NDEBUG URTBLDENV_FRIENDLY=Retail) \ No newline at end of file From c3bf2547e6fc978b92a0b8c20c0885d0cfa9f65e Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Wed, 13 Jul 2016 15:17:16 -0700 Subject: [PATCH 3/9] Backing up changes where the CoreCLR version runs on Nano Server. It still has the pwrshplugin!WSManPluginStartup call issue. --- build.psm1 | 8 +- src/powershell-native/CMakeLists.txt | 317 +----------------- src/powershell-native/coreclr_defs.cmake | 195 +++++++++++ src/powershell-native/dsc_defs.cmake | 111 ++++++ src/powershell-native/nano_defs.cmake | 82 +++++ .../nativemsh/pwrshcommon/CMakeLists.txt | 17 +- .../nativemsh/pwrshcommon/ClrHostWrapper.h | 11 + .../nativemsh/pwrshcommon/pwrshcommon.cpp | 8 +- .../nativemsh/pwrshexe/CMakeLists.txt | 63 ++-- .../nativemsh/pwrshexe/CssMainEntry.cpp | 36 +- .../nativemsh/pwrshplugin/CMakeLists.txt | 18 +- .../nativemsh/pwrshplugin/pwrshclrhost.h | 2 + 12 files changed, 496 insertions(+), 372 deletions(-) create mode 100644 src/powershell-native/coreclr_defs.cmake create mode 100644 src/powershell-native/dsc_defs.cmake create mode 100644 src/powershell-native/nano_defs.cmake diff --git a/build.psm1 b/build.psm1 index c877ddc8d..514b41912 100644 --- a/build.psm1 +++ b/build.psm1 @@ -223,6 +223,7 @@ function Start-PSBuild { throw "Could not find Visual Studio vcvarsall.bat at" + $vcPath } + <# # Compile native resources @("nativemsh/pwrshplugin") | % { $nativeResourcesFolder = $_ @@ -230,8 +231,11 @@ function Start-PSBuild { & $mcexe -o -d -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder } } - - $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt" + #> + +# Disabling until I figure out if it is necessary +# $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt" + $overrideFlags = "" $vcArch = "Win64" $cmakeGenerator = "Visual Studio 14 2015 $vcArch" # $CMakeArguments = "-G $cmakeGenerator ." diff --git a/src/powershell-native/CMakeLists.txt b/src/powershell-native/CMakeLists.txt index 07fc79ef8..4dfbd997c 100644 --- a/src/powershell-native/CMakeLists.txt +++ b/src/powershell-native/CMakeLists.txt @@ -1,4 +1,5 @@ cmake_minimum_required(VERSION 2.8.4) + project(PowerShellNative) # @@ -51,7 +52,8 @@ SET (WIN_VERSION_WINTHRESHOLD 0x0A00) SET (WIN_VERSION_WINBLUE 0x0603) SET (WIN_VERSION_WIN8 0x0602) SET (WIN_VERSION_WIN7 0x0601) -SET (NTDDI_VERSION_WIN7 0x06010000) +SET (NTDDI_VERSION_WIN7 0x06010000) +SET (NTDDI_VERSION_WIN10 0x0A000002) SET (WIN_VERSION_VISTA 0x0600) SET (WIN_VERSION_LONGHORN 0x0600) SET (WIN_VERSION_WS03 0x0502) @@ -73,313 +75,16 @@ SET (WIN_VERSION_WINXP 0x0501) # UNICODE # _UNICODE) -#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /VERBOSE") - # *** # *** Original flags: END # *** -# *** -# *** CoreCLR flags: BEGIN -# *** - -set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. - -#if(NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf") -#endif (NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) - -# Incremental linking with CFG is broken until next VS release. -# This needs to be appended to the last for each build type to override the default flag. -set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO") - -# Linker flags -# -# Disable the following line for UNIX altjit on Windows -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization -#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG") - -# -# TODO: Should these be ignored???? -# -#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221") -#set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221") - -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS") -#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864") - -# Debug build specific flags -#set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") -set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") -set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") - -# Checked build specific flags -#set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") -#set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}") -#set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}") - -# Release build specific flags -#set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") -#set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") -#set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") - -# ReleaseWithDebugInfo build specific flags -#set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") -#set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") -#set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") - -# Temporary until cmake has VS generators for arm64 -#if(CLR_CMAKE_PLATFORM_ARCH_ARM64) -# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64") -# set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64") -# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64") -#endif(CLR_CMAKE_PLATFORM_ARCH_ARM64) - -# Force uCRT to be dynamically linked for Release build -#set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") -#set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") -#set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") -#set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") - -#------------------------------------ -# Definitions (for platform) -#----------------------------------- -#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) - add_definitions(-D_AMD64_) - add_definitions(-D_WIN64) - add_definitions(-DAMD64) - add_definitions(-DBIT64=1) -#elseif (CLR_CMAKE_PLATFORM_ARCH_I386) -# add_definitions(-D_X86_) -#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM) -# add_definitions(-D_ARM_) -# add_definitions(-DARM) -#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64) -# add_definitions(-D_ARM64_) -# add_definitions(-DARM64) -# add_definitions(-D_WIN64) -# add_definitions(-DBIT64=1) -#else () -# clr_unknown_arch() -#endif () - -# Define the CRT lib references that link into Desktop imports -set(STATIC_MT_CRT_LIB "libcmt$<$,$>:d>.lib") -set(STATIC_MT_VCRT_LIB "libvcruntime$<$,$>:d>.lib") -set(STATIC_MT_CPP_LIB "libcpmt$<$,$>:d>.lib") - -# Define the uCRT lib reference -set(STATIC_UCRT_LIB "libucrt$<$,$>:d>.lib") -set(DYNAMIC_UCRT_LIB "ucrt$<$,$>:d>.lib") - -#------------------------------------ -# Definitions for compile -#----------------------------------- - -add_compile_options(/Zl) # omit default library name in .OBJ - -# The following options are set by the razzle build -add_compile_options(/TP) # compile all files as C++ -add_compile_options(/d2Zi+) # make optimized builds debugging easier -add_compile_options(/nologo) # Suppress Startup Banner -add_compile_options(/W3) # set warning level to 3 -#add_compile_options(/WX) # treat warnings as errors -add_compile_options(/Oi) # enable intrinsics -add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls -#add_compile_options(/U_MT) # undefine the predefined _MT macro -add_compile_options(/GF) # enable read-only string pooling -add_compile_options(/Gm-) # disable minimal rebuild -add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions) -add_compile_options(/Zp8) # pack structs on 8-byte boundary -add_compile_options(/Gy) # separate functions for linker -add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef -add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules -add_compile_options(/GR-) # disable C++ RTTI -add_compile_options(/FC) # use full pathnames in diagnostics -#add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors) -add_compile_options(/GS) # Buffer Security Check -add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB -add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640) -add_compile_options(/Zi) # enable debugging information -add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files. - -#if (CLR_CMAKE_PLATFORM_ARCH_I386) -# add_compile_options(/Gz) -#endif (CLR_CMAKE_PLATFORM_ARCH_I386) - -add_compile_options($<$,$>:/GL>) -add_compile_options($<$,$>,$>:/O1>) - -#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) -# The generator expression in the following command means that the /homeparams option is added only for debug builds - add_compile_options($<$:/homeparams>) # Force parameters passed in registers to be written to the stack -#endif (CLR_CMAKE_PLATFORM_ARCH_AMD64) - -# enable control-flow-guard support for native components for non-Arm64 builds -add_compile_options(/guard:cf) - -# Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid -# linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST. -# -# For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but -# wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not -# production-time scenarios. -add_compile_options($<$,$>:/MT>) -add_compile_options($<$,$>:/MTd>) - -set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256") - -#if (CLR_CMAKE_TARGET_ARCH_AMD64) - add_definitions(-D_TARGET_AMD64_=1) - add_definitions(-DDBG_TARGET_64BIT=1) - add_definitions(-DDBG_TARGET_AMD64=1) - add_definitions(-DDBG_TARGET_WIN64=1) -#elseif (CLR_CMAKE_TARGET_ARCH_ARM) -# add_definitions(-D_TARGET_ARM_=1) -# add_definitions(-DDBG_TARGET_32BIT=1) -# add_definitions(-DDBG_TARGET_ARM=1) -#elseif (CLR_CMAKE_TARGET_ARCH_I386) -# add_definitions(-D_TARGET_X86_=1) -# add_definitions(-DDBG_TARGET_32BIT=1) -# add_definitions(-DDBG_TARGET_X86=1) -#else () -# clr_unknown_arch() -#endif (CLR_CMAKE_TARGET_ARCH_AMD64) - -# -# TODO: Are these needed??????????? -# -#add_definitions(-D_BLD_CLR) -#add_definitions(-DDEBUGGING_SUPPORTED) -#add_definitions(-DPROFILING_SUPPORTED) - -add_definitions(-DWIN32) -add_definitions(-D_WIN32) -add_definitions(-DWINVER=${WIN_VERSION_WIN7})# 0x0601 for Win7, was 0x0602 -add_definitions(-D_WIN32_WINNT=${WIN_VERSION_WIN7}) # 0x0601 for Win7 -add_definitions(-DWIN32_LEAN_AND_MEAN=1) -add_definitions(-D_CRT_SECURE_NO_WARNINGS) -#if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) - # Only enable edit and continue on windows x86 and x64 - # exclude Linux, arm & arm64 - add_definitions(-DEnC_SUPPORTED) -#endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) - -add_definitions(-DUNICODE) -add_definitions(-D_UNICODE) - -# *** -# *** CoreCLR flags: END -# *** - -# *** -# *** DSC flags: BEGIN -# *** - - -SET (WindowsSdkDir $ENV{WindowsSdkDir}) -SET (WindowsSDKVersion $ENV{WindowsSDKVersion}) -SET (NETFXSdkDir $ENV{NETFXSDKDir}) -#SET (FrameWorkLibPath $ENV{FrameworkDir}/$ENV{FrameworkVersion}) - -# -# Configure include directories -# -SET (WindowsSDKIncludeBase "${WindowsSdkDir}/Include/${WindowsSDKVersion}") - -SET (IncludePath) -#list (APPEND IncludePath "${INTERNAL_HEADER_DIR}") -#list (APPEND IncludePath "${PUBLIC_HEADER_DIR}") -list (APPEND IncludePath "${WindowsSDKIncludeBase}winrt") -# Don't include due to incompatible instance.h -# list (APPEND IncludePath "${WindowsSDKIncludeBase}um") -list (APPEND IncludePath "${WindowsSDKIncludeBase}shared") -list (APPEND IncludePath "${NETFXSdkDir}/Include/um") -list (APPEND IncludePath "${WindowsSDKIncludeBase}ucrt") -include_directories(BEFORE ${IncludePath}) - -# -# Configure lib directories -# -SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") - -SET (LibraryPath) -list (APPEND LibraryPath "${WindowsSDKLibBase}ucrt/${WindowsSDKPlatform}") -list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") -list (APPEND LibraryPath "${WindowsSDKLibBase}um/${WindowsSDKPlatform}" ) -list (APPEND LibraryPath "${INTERNAL_LIBRARY_DIR}") -list (APPEND LibraryPath "${PUBLIC_LIBRARY_DIR}") -#list (APPEND LibraryPath ${FrameWorkLibPath}) -SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") -link_directories(${LibraryPath}) - -#if (${WindowsSDKPlatform} STREQUAL "x64") -# add_definitions ( -# -D_WIN64 -# -D_AMD64_ -# -DAMD64 -# -DBUILD_WOW64_ENABLED=1 -# -DBUILD_UMS_ENABLED=1 -# ) -#else() -# add_definitions ( -# -DBUILD_WOW64_ENABLED=1 -# -DBUILD_UMS_ENABLED=0 -# ) -#endif() - -# -# Common defines. -# -#add_definitions ( -# -D_CRT_SECURE_NO_WARNINGS -# -DCONDITION_HANDLING=1 -# -DNT_UP=1 -# -DNT_INST=0 -# -D_NT1X_=100 -# -DWINNT=1 -# -DWIN32_LEAN_AND_MEAN=1 -# -DDEVL=1 -# -D_MT=1 -# -D_STL70_ -# -DMI_INTERNAL -# -DWINBUILD -# -DHOOK_BUILD # TODO: should be target specific -# -DCONFIG_ENABLE_WCHAR -# -D_INTLSTR_NOTAPPEND_NULL -# -DMSC_NOOPT -# -DBUILD_WINDOWS -# -D_USE_DECLSPECS_FOR_SAL=1 -# -DUNICODE -# -D_UNICODE -# -D_USE_DEV11_CRT -#) - -# -# platform specific defines -# -#add_definitions( -# -DNTDDI_VERSION=${NTDDI_VERSION_WIN7} -# -DWINBLUE_KBSPRING14 -# -D_APISET_WINDOWS_VERSION=${WIN_VERSION_WIN7} -# -D_APISET_MINWIN_VERSION=0x0100 -# -D_APISET_MINCORE_VERSION=0x0100 -# -D_WIN32_IE=0x0800 -# -D_WIN32_WINNT=${WIN_VERSION_WIN7} -# -DWINVER=${WIN_VERSION_WIN7} -# ) - -# *** -# *** DSC flags: END -# *** +#include(dsc_defs.cmake) +if (BUILD_ONECORE) + include(nano_defs.cmake) +else () + include(coreclr_defs.cmake) +endif (BUILD_ONECORE) # Default of BUILD_ONECORE should be ON once it is supported option(BUILD_ONECORE "Compile the OneCore version of the binaries" OFF) @@ -392,7 +97,7 @@ add_subdirectory(nativemsh/pwrshcommon) add_subdirectory(nativemsh/pwrshplugin) # Build powershell.exe -if (NOT BUILD_ONECORE) +#if (NOT BUILD_ONECORE) # Conditionally build powershell.exe until CssMainEntry.cpp appears. add_subdirectory(nativemsh/pwrshexe) -endif () +#endif () diff --git a/src/powershell-native/coreclr_defs.cmake b/src/powershell-native/coreclr_defs.cmake new file mode 100644 index 000000000..1b4071895 --- /dev/null +++ b/src/powershell-native/coreclr_defs.cmake @@ -0,0 +1,195 @@ +cmake_minimum_required(VERSION 2.8.4) + +set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. + +#if(NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf") +#endif (NOT CLR_CMAKE_PLATFORM_ARCH_ARM64) + +# Incremental linking with CFG is broken until next VS release. +# This needs to be appended to the last for each build type to override the default flag. +set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO") + +# Linker flags +# +# Disable the following line for UNIX altjit on Windows +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG") + +# +# TODO: Should these be ignored???? +# +#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221") +#set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221") + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS") +#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864") + +# Debug build specific flags +set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}") + +# Checked build specific flags +set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}") +set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# Release build specific flags +set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# ReleaseWithDebugInfo build specific flags +set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") +set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") +set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}") + +# Temporary until cmake has VS generators for arm64 +#if(CLR_CMAKE_PLATFORM_ARCH_ARM64) +# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64") +# set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64") +# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64") +#endif(CLR_CMAKE_PLATFORM_ARCH_ARM64) + +# Force uCRT to be dynamically linked for Release build +set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") +set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib") + +#------------------------------------ +# Definitions (for platform) +#----------------------------------- +#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) + add_definitions(-D_AMD64_) + add_definitions(-D_WIN64) + add_definitions(-DAMD64) + add_definitions(-DBIT64=1) +#elseif (CLR_CMAKE_PLATFORM_ARCH_I386) +# add_definitions(-D_X86_) +#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM) +# add_definitions(-D_ARM_) +# add_definitions(-DARM) +#elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64) +# add_definitions(-D_ARM64_) +# add_definitions(-DARM64) +# add_definitions(-D_WIN64) +# add_definitions(-DBIT64=1) +#else () +# clr_unknown_arch() +#endif () + +# Define the CRT lib references that link into Desktop imports +set(STATIC_MT_CRT_LIB "libcmt$<$,$>:d>.lib") +set(STATIC_MT_VCRT_LIB "libvcruntime$<$,$>:d>.lib") +set(STATIC_MT_CPP_LIB "libcpmt$<$,$>:d>.lib") + +# Define the uCRT lib reference +set(STATIC_UCRT_LIB "libucrt$<$,$>:d>.lib") +set(DYNAMIC_UCRT_LIB "ucrt$<$,$>:d>.lib") + +#------------------------------------ +# Definitions for compile +#----------------------------------- + +add_compile_options(/Zl) # omit default library name in .OBJ + +# The following options are set by the razzle build +add_compile_options(/TP) # compile all files as C++ +add_compile_options(/d2Zi+) # make optimized builds debugging easier +add_compile_options(/nologo) # Suppress Startup Banner +add_compile_options(/W3) # set warning level to 3 +#add_compile_options(/WX) # treat warnings as errors +add_compile_options(/Oi) # enable intrinsics +add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls +#add_compile_options(/U_MT) # undefine the predefined _MT macro +add_compile_options(/GF) # enable read-only string pooling +add_compile_options(/Gm-) # disable minimal rebuild +add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions) +add_compile_options(/Zp8) # pack structs on 8-byte boundary +add_compile_options(/Gy) # separate functions for linker +add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef +add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules +add_compile_options(/GR-) # disable C++ RTTI +add_compile_options(/FC) # use full pathnames in diagnostics +#add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors) +add_compile_options(/GS) # Buffer Security Check +add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB +add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640) +add_compile_options(/Zi) # enable debugging information +add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files. + +#if (CLR_CMAKE_PLATFORM_ARCH_I386) +# add_compile_options(/Gz) +#endif (CLR_CMAKE_PLATFORM_ARCH_I386) + +add_compile_options($<$,$>:/GL>) +add_compile_options($<$,$>,$>:/O1>) + +#if (CLR_CMAKE_PLATFORM_ARCH_AMD64) +# The generator expression in the following command means that the /homeparams option is added only for debug builds + add_compile_options($<$:/homeparams>) # Force parameters passed in registers to be written to the stack +#endif (CLR_CMAKE_PLATFORM_ARCH_AMD64) + +# enable control-flow-guard support for native components for non-Arm64 builds +add_compile_options(/guard:cf) + +# Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid +# linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST. +# +# For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but +# wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not +# production-time scenarios. +add_compile_options($<$,$>:/MT>) +add_compile_options($<$,$>:/MTd>) + +#set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256") + +#if (CLR_CMAKE_TARGET_ARCH_AMD64) + add_definitions(-D_TARGET_AMD64_=1) + add_definitions(-DDBG_TARGET_64BIT=1) + add_definitions(-DDBG_TARGET_AMD64=1) + add_definitions(-DDBG_TARGET_WIN64=1) +#elseif (CLR_CMAKE_TARGET_ARCH_ARM) +# add_definitions(-D_TARGET_ARM_=1) +# add_definitions(-DDBG_TARGET_32BIT=1) +# add_definitions(-DDBG_TARGET_ARM=1) +#elseif (CLR_CMAKE_TARGET_ARCH_I386) +# add_definitions(-D_TARGET_X86_=1) +# add_definitions(-DDBG_TARGET_32BIT=1) +# add_definitions(-DDBG_TARGET_X86=1) +#else () +# clr_unknown_arch() +#endif (CLR_CMAKE_TARGET_ARCH_AMD64) + +# +# TODO: Are these needed??????????? +# +#add_definitions(-D_BLD_CLR) +#add_definitions(-DDEBUGGING_SUPPORTED) +#add_definitions(-DPROFILING_SUPPORTED) + +add_definitions(-DWIN32) +add_definitions(-D_WIN32) +add_definitions(-DWINVER=${WIN_VERSION_WIN8})# 0x0601 for Win7, was 0x0602 +add_definitions(-D_WIN32_WINNT=${WIN_VERSION_WIN8}) # 0x0601 for Win7 +add_definitions(-DWIN32_LEAN_AND_MEAN=1) +add_definitions(-D_CRT_SECURE_NO_WARNINGS) +#if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) + # Only enable edit and continue on windows x86 and x64 + # exclude Linux, arm & arm64 + add_definitions(-DEnC_SUPPORTED) +#endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_I386) + +add_definitions(-DUNICODE) +add_definitions(-D_UNICODE) + diff --git a/src/powershell-native/dsc_defs.cmake b/src/powershell-native/dsc_defs.cmake new file mode 100644 index 000000000..92bb2a4c5 --- /dev/null +++ b/src/powershell-native/dsc_defs.cmake @@ -0,0 +1,111 @@ +cmake_minimum_required(VERSION 2.8.4) + +SET (WindowsSdkDir $ENV{WindowsSdkDir}) +SET (WindowsSDKVersion $ENV{WindowsSDKVersion}) +SET (NETFXSdkDir $ENV{NETFXSDKDir}) +#SET (FrameWorkLibPath $ENV{FrameworkDir}/$ENV{FrameworkVersion}) + +# +# Configure include directories +# +SET (WindowsSDKIncludeBase "${WindowsSdkDir}/Include/${WindowsSDKVersion}") + +SET (IncludePath) +#list (APPEND IncludePath "${INTERNAL_HEADER_DIR}") +#list (APPEND IncludePath "${PUBLIC_HEADER_DIR}") +list (APPEND IncludePath "${WindowsSDKIncludeBase}winrt") +# Don't include due to incompatible instance.h +# list (APPEND IncludePath "${WindowsSDKIncludeBase}um") +list (APPEND IncludePath "${WindowsSDKIncludeBase}shared") +list (APPEND IncludePath "${NETFXSdkDir}/Include/um") +list (APPEND IncludePath "${WindowsSDKIncludeBase}ucrt") +include_directories(BEFORE ${IncludePath}) + +# +# Configure lib directories +# +SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") +SET (OneCoreLibBase "$ENV{VCInstallDir}lib/onecore/amd64") + +SET (LibraryPath) +if (BUILD_ONECORE) + list (APPEND LibraryPath "${OneCoreLibBase}") +endif (BUILD_ONECORE) +list (APPEND LibraryPath "${WindowsSDKLibBase}ucrt/${WindowsSDKPlatform}") +list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") +list (APPEND LibraryPath "${WindowsSDKLibBase}um/${WindowsSDKPlatform}" ) +list (APPEND LibraryPath "${INTERNAL_LIBRARY_DIR}") +list (APPEND LibraryPath "${PUBLIC_LIBRARY_DIR}") +#list (APPEND LibraryPath ${FrameWorkLibPath}) +SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") +link_directories(${LibraryPath}) + +if (${WindowsSDKPlatform} STREQUAL "x64") + add_definitions ( + -D_WIN64 + -D_AMD64_ + -DAMD64 + -DBUILD_WOW64_ENABLED=1 + -DBUILD_UMS_ENABLED=1 + ) +else() + add_definitions ( + -DBUILD_WOW64_ENABLED=1 + -DBUILD_UMS_ENABLED=0 + ) +endif() + +# +# Common defines. +# +add_definitions ( + -D_CRT_SECURE_NO_WARNINGS + -DCONDITION_HANDLING=1 + -DNT_UP=1 + -DNT_INST=0 + -D_NT1X_=100 + -DWINNT=1 + -DWIN32_LEAN_AND_MEAN=1 + -DDEVL=1 + #-D_MT=1 + -DMD + -D_STL70_ + -DMI_INTERNAL + -DWINBUILD + -DHOOK_BUILD # TODO: should be target specific + -DCONFIG_ENABLE_WCHAR + -D_INTLSTR_NOTAPPEND_NULL + -DMSC_NOOPT + -DBUILD_WINDOWS + -D_USE_DECLSPECS_FOR_SAL=1 + -DUNICODE + -D_UNICODE + -D_USE_DEV11_CRT +) + +# +# platform specific defines +# +add_definitions( + #-DNTDDI_VERSION=${NTDDI_VERSION_WIN7} + -DNTDDI_VERSION=${NTDDI_VERSION_WIN10} + -DWINBLUE_KBSPRING14 + #-D_APISET_WINDOWS_VERSION=${WIN_VERSION_WIN7} + -D_APISET_WINDOWS_VERSION=${WIN_VERSION_WIN10} + #-D_APISET_MINWIN_VERSION=0x0100 + -D_APISET_MINWIN_VERSION=0x0106 + #-D_APISET_MINCORE_VERSION=0x0100 + -D_APISET_MINCORE_VERSION=0x0105 + -D_WIN32_IE=0x0800 + #-D_WIN32_WINNT=${WIN_VERSION_WIN7} + -D_WIN32_WINNT=${WIN_VERSION_WIN10} + #-DWINVER=${WIN_VERSION_WIN7} + -DWINVER=${WIN_VERSION_WIN10} + ) + +# if not DEBUG for DSC +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi") +set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF") +#set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF /NODEFAULTLIB") +set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF /NODEFAULTLIB") + diff --git a/src/powershell-native/nano_defs.cmake b/src/powershell-native/nano_defs.cmake new file mode 100644 index 000000000..f64016f75 --- /dev/null +++ b/src/powershell-native/nano_defs.cmake @@ -0,0 +1,82 @@ +cmake_minimum_required(VERSION 2.8.4) + +SET (WindowsSdkDir $ENV{WindowsSdkDir}) +SET (WindowsSDKVersion $ENV{WindowsSDKVersion}) +SET (NETFXSdkDir $ENV{NETFXSDKDir}) + +# +# Configure include directories +# +SET (WindowsSDKIncludeBase "${WindowsSdkDir}/Include/${WindowsSDKVersion}") + +SET (IncludePath) +list (APPEND IncludePath "${WindowsSDKIncludeBase}winrt") +list (APPEND IncludePath "${WindowsSDKIncludeBase}shared") +#list (APPEND IncludePath "${NETFXSdkDir}/Include/um") +list (APPEND IncludePath "${WindowsSDKIncludeBase}ucrt") +include_directories(BEFORE ${IncludePath}) + +# +# Configure lib directories +# +SET (WindowsSDKLibBase "${WindowsSdkDir}/Lib/${WindowsSDKVersion}") +SET (OneCoreLibBase "$ENV{VCInstallDir}lib/onecore/amd64") + +SET (LibraryPath) +list (APPEND LibraryPath "${OneCoreLibBase}") +list (APPEND LibraryPath "${WindowsSDKLibBase}ucrt/${WindowsSDKPlatform}") +list (APPEND LibraryPath "${WindowsSDKLibBase}um/${WindowsSDKPlatform}" ) +#list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") +link_directories(${LibraryPath}) + +# +# Tell CMake to set the platform toolset. +# +set(CMAKE_VS_PLATFORM_TOOLSET "v140") # Use VS 2015 with Win 10 SDK +set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION "10.0.10586.0") # Targets Windows 10. Alt is ${WindowsSDKVersion} + +if (BUILD_ONECORE) + set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. +endif (BUILD_ONECORE) + +add_compile_options(/Zl) # omit default library name in .OBJ +add_compile_options(/Zi) # enable debugging information +add_compile_options(/nologo) # Suppress Startup Banner +add_compile_options(/W3) # set warning level to 3 +#add_compile_options(/WX-) # treat warnings as errors +add_compile_options(/wd4996) # Ignore deprecation warnings +add_compile_options(/Od) # enable intrinsics +add_compile_options(/sdl) + +add_compile_options(/Gm) # minimal rebuild +add_compile_options(/EHsc) # enable C++ EH (w/ SEH exceptions) +add_compile_options(/RTC1) +#add_compile_options(/MDd) +add_compile_options(/MD) +add_compile_options(/GS) # Buffer Security Check +add_compile_options(/fp:precise) +add_compile_options(/Zp8) # pack structs on 8-byte boundary +add_compile_options(/Zc:wchar_t) # C++ language conformance: wchar_t is NOT the native type, but a typedef +#add_compile_options(/U_WINDOWS) + +add_definitions( + -DWIN32 + -DNDEBUG + # -D_DEBUG + -D_UNICODE + -DUNICODE + -DWIN32_LEAN_AND_MEAN=1 + ) + + +#set(MY_COMMON_LINK_FLAGS "/INCREMENTAL /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /NODEFAULTLIB") +set(MY_COMMON_LINK_FLAGS "/INCREMENTAL:NO /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /guard:cf /OPT:REF /OPT:ICF") +set(MY_COMMON_LINK_FLAGS "${MY_COMMON_LINK_FLAGS} /NODEFAULTLIB:kernel32.lib /NODEFAULTLIB:advapi32.lib") # Explicitly exclude kernel32 and advapi32 since CMake is including them and they block execution on Nano Server + +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") +#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /OPT:REF") #windows subsystem +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") #windows subsystem + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE") #windows subsystem + diff --git a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt index 0330f7f48..09f80d8ec 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt @@ -12,17 +12,20 @@ if (BUILD_ONECORE) message("Building OneCore version of pwrshcommon.lib") # Libraries to use when creating this binary for Windows on OneCore-based SKUs set(PWRSHCOMMON_WINDOWS_LIBS - onecore.lib) - set(pwrshcommon_definitions ${common_pwrsh_definitions} CORECLR) + # onecore.lib + ) + #set(pwrshcommon_definitions ${common_pwrsh_definitions} CORECLR) + set(pwrshcommon_definitions CORECLR) + set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") else () # NOT BUILD_ONECORE message("Building Full version of pwrshcommon.lib") # Libraries to use when creating this binary for Windows on full SKUs # Note: The appropriate libs get added automatically by VS set(PWRSHCOMMON_WINDOWS_LIBS # CoreCLR libs - ${STATIC_MT_CRT_LIB} - ${STATIC_MT_VCRT_LIB} - ${STATIC_UCRT_LIB} +### ${STATIC_MT_CRT_LIB} +### ${STATIC_MT_VCRT_LIB} +### ${STATIC_UCRT_LIB} # # ole32.lib # oleaut32.lib @@ -32,12 +35,12 @@ else () # NOT BUILD_ONECORE # msxml6.lib # mscoree.lib) ) - set(pwrshcommon_definitions ${common_pwrsh_definitions}) + #set(pwrshcommon_definitions ${common_pwrsh_definitions}) # target_include_directories(pwrshcommon PUBLIC # "%ProgramFiles(x86)%/Windows Kits/NETFXSDK/4.6/Include/um") endif (BUILD_ONECORE) -set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") +#set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") target_link_libraries(pwrshcommon ${PWRSHCOMMON_WINDOWS_LIBS}) diff --git a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h index 54082ca9c..54df1e2b5 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h +++ b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h @@ -148,6 +148,17 @@ namespace NativeMsh return NULL; } + initPtr = (coreclr_initialize_ptr)GetProcAddress(result, "coreclr_initialize"); + shutdownPtr = (coreclr_shutdown_ptr)GetProcAddress(result, "coreclr_shutdown"); + createDelegatePtr = (coreclr_create_delegate_ptr)GetProcAddress(result, "coreclr_create_delegate"); + + if (NULL == initPtr || + NULL == shutdownPtr || + NULL == createDelegatePtr) + { + return NULL; + } + // Initialization succeeded. Save the handle and return success; coreClrHandle = result; return result; diff --git a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp index daf3661e8..b687fed71 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp @@ -1349,12 +1349,12 @@ namespace NativeMsh vals[nProps] = tempStr.c_str(); nProps++; - props[nProps] = L"APP_PATHS"; - vals[nProps] = L""; // Used to be hostEnvironment.GetHostDirectoryPath() + props[nProps] = "APP_PATHS"; + vals[nProps] = ""; // Used to be hostEnvironment.GetHostDirectoryPath() nProps++; - props[nProps] = L"APP_NI_PATHS"; - vals[nProps] = L""; // Used to be hostEnvironment.GetHostDirectoryPath() + props[nProps] = "APP_NI_PATHS"; + vals[nProps] = ""; // Used to be hostEnvironment.GetHostDirectoryPath() nProps++; int hr = hostWrapper->InitializeClr( diff --git a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt index 43aed8b47..9306c687b 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt @@ -4,25 +4,34 @@ # Only build powershell.exe for "full" windows. Open PowerShell uses the managed executable. if (BUILD_ONECORE) - message("*") - message("*********** Building PowerShell.exe for 'full' CLR instead of OneCore. Add CssMainEntry.cpp") - message("*") #set(BUILD_ONECORE OFF) endif () -#if (BUILD_ONECORE) +if (BUILD_ONECORE) + message("*") + message("*********** Building PowerShell.exe for CoreCLR. Add CssMainEntry.cpp") + message("*") # Settings to use when creating PowerShell.exe for Windows on OneCore-based SKUs -# set(PWRSHEXE_WINDOWS_SOURCES - # TODO: Fix this once the file is migrated from SD - # CssMainEntry.cpp) -# MainEntry.cpp) -# set(PWRSHEXE_WINDOWS_LIBS -# onecore.lib) -# set(powershell_definitions -# _CONSOLE -# CORECLR -# ${common_pwrsh_definitions}) -#else (BUILD_ONECORE) + set(PWRSHEXE_WINDOWS_SOURCES + CssMainEntry.cpp) + set(PWRSHEXE_WINDOWS_LIBS +### ${STATIC_MT_CRT_LIB} +### ${STATIC_MT_VCRT_LIB} +### ${STATIC_UCRT_LIB} + onecore.lib + vcruntime.lib + ucrtd.lib + msvcrt.lib + ) + set(powershell_definitions + _CONSOLE + CORECLR + ) + # ${common_pwrsh_definitions}) +else () + message("*") + message("*********** Building PowerShell.exe for 'Full' CLR.") + message("*") # Settings to use when creating PowerShell.exe for Windows on full SKUs or downlevel platforms set(PWRSHEXE_WINDOWS_SOURCES MainEntry.cpp) @@ -33,10 +42,10 @@ endif () ${STATIC_MT_VCRT_LIB} ${STATIC_UCRT_LIB} # - # ole32.lib - # oleaut32.lib - # uuid.lib - # user32.lib +# ole32.lib +# oleaut32.lib +# uuid.lib +# user32.lib MUILoad.lib msxml6.lib mscoree.lib @@ -44,17 +53,21 @@ endif () ) set(powershell_definitions _CONSOLE - ${common_pwrsh_definitions}) -#endif (BUILD_ONECORE) + # ${common_pwrsh_definitions}) + ) +endif (BUILD_ONECORE) add_executable(powershell ${PWRSHEXE_WINDOWS_SOURCES}) set_target_properties(powershell PROPERTIES COMPILE_DEFINITIONS "${powershell_definitions}") -set_target_properties(powershell PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") -set_target_properties(powershell PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") +# Set in nano_defs.cmake. Uncomment for other flavors +#set_target_properties(powershell PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") +#set_target_properties(powershell PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") +#set_target_properties(powershell PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") +#set_target_properties(powershell PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") + +message("==== Setting libs = ${PWRSHEXE_WINDOWS_LIBS}") target_link_libraries(powershell ${PWRSHEXE_WINDOWS_LIBS}) diff --git a/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp b/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp index 038ca0299..bf0853bff 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp +++ b/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp @@ -12,7 +12,9 @@ #include #include #include -#include "mscoree.h" +#if !CORECLR + #include "mscoree.h" +#endif #include "NativeMsh.h" #include "ClrHostWrapper.h" #include "OutputWriter.h" @@ -34,9 +36,9 @@ namespace NativeMsh // All these objects will be destroyed when commonFuncs goes out of scope. PwrshExeOutput* output = new PwrshExeOutput(); PwrshCommon commonFuncs(output, new WinSystemCallFacade()); - ICLRRuntimeHost2Wrapper hostWrapper; + CoreClrHostingApiWrapper hostWrapper; - exitCode = commonFuncs.LaunchCoreCLR(&hostWrapper, hostEnvironment); + exitCode = commonFuncs.LaunchCoreCLR(&hostWrapper, hostEnvironment, "powershell"); if (EXIT_CODE_SUCCESS != exitCode) { if (verbose) @@ -51,23 +53,14 @@ namespace NativeMsh return false; } - exitCode = commonFuncs.CreateAppDomain(&hostWrapper, hostEnvironment.GetHostBinaryName(), hostEnvironment); - if (EXIT_CODE_SUCCESS != exitCode) - { - if (verbose) - ::wprintf(L"Unable to create app domain\n"); - return false; - } - //------------------------------------------------------------- // Set the powershell custom assembly loader to be the default LoaderRunHelperFp initDelegate = NULL; HRESULT hr = hostWrapper.CreateDelegate( - hostWrapper.GetAppDomainId(), - L"Microsoft.PowerShell.CoreCLR.AssemblyLoadContext, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", - L"System.Management.Automation.PowerShellAssemblyLoadContextInitializer", - L"SetPowerShellAssemblyLoadContext", - (INT_PTR*)&initDelegate); + "Microsoft.PowerShell.CoreCLR.AssemblyLoadContext, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", + "System.Management.Automation.PowerShellAssemblyLoadContextInitializer", + "SetPowerShellAssemblyLoadContext", + (void**)&initDelegate); if (FAILED(hr)) { @@ -75,18 +68,17 @@ namespace NativeMsh } else { - initDelegate(hostEnvironment.GetHostDirectoryPath()); + initDelegate(hostEnvironment.GetHostDirectoryPathW()); } //------------------------------------------------------------- // Start the assembly MonadRunHelperFp pfnDelegate = NULL; hr = hostWrapper.CreateDelegate( - hostWrapper.GetAppDomainId(), - L"Microsoft.PowerShell.ConsoleHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", - L"Microsoft.PowerShell.UnmanagedPSEntry", - L"Start", - (INT_PTR*)&pfnDelegate); + "Microsoft.PowerShell.ConsoleHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", + "Microsoft.PowerShell.UnmanagedPSEntry", + "Start", + (void**)&pfnDelegate); if (FAILED(hr)) { diff --git a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt index 03449bc2b..bec4d1e68 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt @@ -14,11 +14,17 @@ if (BUILD_ONECORE) message("*") # Libraries to use when creating this binary for Windows on OneCore-based SKUs set(PWRSHPLUGIN_WINDOWS_LIBS - ${STATIC_MT_CRT_LIB} - ${STATIC_MT_VCRT_LIB} - ${STATIC_UCRT_LIB} +### ${STATIC_MT_CRT_LIB} +### ${STATIC_MT_VCRT_LIB} +### ${STATIC_UCRT_LIB} + onecore.lib + vcruntime.lib + ucrtd.lib + msvcrt.lib wsmsvc.lib) - set(pwrshplugin_definitions ${common_pwrsh_definitions} CORECLR) + #set(pwrshplugin_definitions ${common_pwrsh_definitions} CORECLR) + set(pwrshplugin_definitions CORECLR) + set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") else () message("*") message("* Setting up NOT BUILD_ONECORE version of pwrshplugin.dll") @@ -34,7 +40,7 @@ else () # oleaut32.lib # uuid.lib wsmsvc.lib - # CoreCLR libs + # CoreCLR lib ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} ${STATIC_UCRT_LIB} @@ -43,7 +49,7 @@ else () set(pwrshplugin_definitions ${common_pwrsh_definitions}) endif (BUILD_ONECORE) -set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") +#set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") #set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "_DLL") target_link_libraries(pwrshplugin diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h index cef2d1c1c..75c2e1f5c 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.h @@ -10,7 +10,9 @@ #pragma once #include "pwrshplugindefs.h" // PwrshPluginWkr_Ptrs and PlugInException +#if !CORECLR #include +#endif #include #include #include "SystemCallFacade.h" From 4e349ddc2c4a71e38da7b453511405861ed89770 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Fri, 15 Jul 2016 10:38:28 -0700 Subject: [PATCH 4/9] Resolved link issues to run pwrshplugin.dll on Nano Server and Windows 10 --- src/powershell-native/CMakeLists.txt | 42 +++++-------------- src/powershell-native/nano_defs.cmake | 24 +++++++---- .../nativemsh/pwrshcommon/CMakeLists.txt | 9 +--- .../nativemsh/pwrshcommon/ClrHostWrapper.h | 6 +-- .../nativemsh/pwrshexe/CMakeLists.txt | 36 ++++------------ .../nativemsh/pwrshplugin/CMakeLists.txt | 27 +++++------- .../nativemsh/pwrshplugin/entrypoints.cpp | 22 +++++++++- .../nativemsh/pwrshplugin/pwrshheaders.h | 12 +++--- .../nativemsh/pwrshplugin/pwrshplugin.def | 2 +- .../nativemsh/pwrshplugin/pwrshplugin.h | 2 + 10 files changed, 79 insertions(+), 103 deletions(-) diff --git a/src/powershell-native/CMakeLists.txt b/src/powershell-native/CMakeLists.txt index 4dfbd997c..7a7d72a53 100644 --- a/src/powershell-native/CMakeLists.txt +++ b/src/powershell-native/CMakeLists.txt @@ -16,13 +16,13 @@ else () endif () # -# Get the path to the Windows SDK build tools. +# Normalize the platform name +# +# TODO: Only x64 and x86 are supported right now. This needs to be expanded to arm as well # if (WindowsSDKPlatform STREQUAL "x64" OR WindowsSDKPlatform STREQUAL "X64" OR WindowsSDKPlatform STREQUAL "amd64" OR WindowsSDKPlatform STREQUAL "AMD64") - # Normalize the platform name SET(WindowsSDKPlatform "x64") elseif (WindowsSDKPlatform STREQUAL "x86" OR WindowsSDKPlatform STREQUAL "X86") - # Normalize the platform name SET(WindowsSDKPlatform "x86") else() message(FATAL_ERROR "Unsupported WindowsSDKPlatform ${WindowsSDKPlatform}") @@ -32,14 +32,15 @@ endif () # set the output path for all binaries # if (BUILD_ONECORE) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Bin/CoreClr") + set(PWRSH_NATIVE_OUTPUT_DIRECTORY "CoreClr") else () - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Bin/FullClr") + set(PWRSH_NATIVE_OUTPUT_DIRECTORY "FullClr") endif (BUILD_ONECORE) + foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) - message(" Setting output directory for ${OUTPUTCONFIG} to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} "${PROJECT_SOURCE_DIR}/Bin/${OUTPUTCONFIG}/${PWRSH_NATIVE_OUTPUT_DIRECTORY}") + message(" Setting output directory for ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG}}") endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES ) # @@ -59,26 +60,6 @@ SET (WIN_VERSION_LONGHORN 0x0600) SET (WIN_VERSION_WS03 0x0502) SET (WIN_VERSION_WINXP 0x0501) -# *** -# *** Original flags: BEGIN -# *** -# -# TODO: Will this interfere with OneCore compilations? -# -# set these flags, so build does static linking for msvcr120.dll -# otherwise this dll need to be present on the system -#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") -#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") - -# Compiler flags that are universal to all powershell native binaries -#SET(common_pwrsh_definitions -# UNICODE -# _UNICODE) - -# *** -# *** Original flags: END -# *** - #include(dsc_defs.cmake) if (BUILD_ONECORE) include(nano_defs.cmake) @@ -87,7 +68,7 @@ else () endif (BUILD_ONECORE) # Default of BUILD_ONECORE should be ON once it is supported -option(BUILD_ONECORE "Compile the OneCore version of the binaries" OFF) +option(BUILD_ONECORE "Compile the OneCore version of the binaries" ON) option(BUILD_DOWNLEVEL "Compile the downlevel version of the binaries" OFF) # Build the common library that powershell.exe and pwrshplugin.dll depend on @@ -97,7 +78,4 @@ add_subdirectory(nativemsh/pwrshcommon) add_subdirectory(nativemsh/pwrshplugin) # Build powershell.exe -#if (NOT BUILD_ONECORE) - # Conditionally build powershell.exe until CssMainEntry.cpp appears. - add_subdirectory(nativemsh/pwrshexe) -#endif () +add_subdirectory(nativemsh/pwrshexe) diff --git a/src/powershell-native/nano_defs.cmake b/src/powershell-native/nano_defs.cmake index f64016f75..983e0886e 100644 --- a/src/powershell-native/nano_defs.cmake +++ b/src/powershell-native/nano_defs.cmake @@ -26,11 +26,11 @@ SET (LibraryPath) list (APPEND LibraryPath "${OneCoreLibBase}") list (APPEND LibraryPath "${WindowsSDKLibBase}ucrt/${WindowsSDKPlatform}") list (APPEND LibraryPath "${WindowsSDKLibBase}um/${WindowsSDKPlatform}" ) -#list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") +###list (APPEND LibraryPath "${NETFXSdkDir}lib/um/${WindowsSDKPlatform}") link_directories(${LibraryPath}) # -# Tell CMake to set the platform toolset. +# Tell CMake to set the platform toolset. Nano Server requires the Win10 SDK and updated onecore.lib # set(CMAKE_VS_PLATFORM_TOOLSET "v140") # Use VS 2015 with Win 10 SDK set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION "10.0.10586.0") # Targets Windows 10. Alt is ${WindowsSDKVersion} @@ -60,22 +60,28 @@ add_compile_options(/Zc:wchar_t) # C++ language conformance: wchar_t is NOT the #add_compile_options(/U_WINDOWS) add_definitions( - -DWIN32 - -DNDEBUG - # -D_DEBUG + -D_WIN64 + -D_AMD64_ + -DAMD64 + -D_APISET_WINDOWS_VERSION=0x601 + -D_APISET_MINWIN_VERSION=0x0101 + -D_APISET_MINCORE_VERSION=0x0100 + -DNTDDI_VERSION=0x0A000002 + # -DWIN32=100 + -D_DEBUG -D_UNICODE -DUNICODE -DWIN32_LEAN_AND_MEAN=1 + #-DNDEBUG ) +set(CMAKE_ENABLE_EXPORTS ON) -#set(MY_COMMON_LINK_FLAGS "/INCREMENTAL /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /NODEFAULTLIB") -set(MY_COMMON_LINK_FLAGS "/INCREMENTAL:NO /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /guard:cf /OPT:REF /OPT:ICF") +set(MY_COMMON_LINK_FLAGS "/INCREMENTAL:NO /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /guard:cf /OPT:REF /OPT:ICF /NODEFAULTLIB") set(MY_COMMON_LINK_FLAGS "${MY_COMMON_LINK_FLAGS} /NODEFAULTLIB:kernel32.lib /NODEFAULTLIB:advapi32.lib") # Explicitly exclude kernel32 and advapi32 since CMake is including them and they block execution on Nano Server set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") -#set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /OPT:REF") #windows subsystem -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") #windows subsystem +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE") #windows subsystem diff --git a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt index 09f80d8ec..c3971e88d 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt @@ -14,9 +14,7 @@ if (BUILD_ONECORE) set(PWRSHCOMMON_WINDOWS_LIBS # onecore.lib ) - #set(pwrshcommon_definitions ${common_pwrsh_definitions} CORECLR) - set(pwrshcommon_definitions CORECLR) - set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") + set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "CORECLR") else () # NOT BUILD_ONECORE message("Building Full version of pwrshcommon.lib") # Libraries to use when creating this binary for Windows on full SKUs @@ -35,13 +33,8 @@ else () # NOT BUILD_ONECORE # msxml6.lib # mscoree.lib) ) - #set(pwrshcommon_definitions ${common_pwrsh_definitions}) - # target_include_directories(pwrshcommon PUBLIC - # "%ProgramFiles(x86)%/Windows Kits/NETFXSDK/4.6/Include/um") endif (BUILD_ONECORE) -#set_target_properties(pwrshcommon PROPERTIES COMPILE_DEFINITIONS "${pwrshcommon_definitions}") - target_link_libraries(pwrshcommon ${PWRSHCOMMON_WINDOWS_LIBS}) diff --git a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h index 54df1e2b5..2c86fb9e2 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h +++ b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h @@ -77,7 +77,7 @@ namespace NativeMsh // // Function Pointer Definitions for the function pointers to load from CoreCLR.dll // - typedef int (*coreclr_initialize_ptr)( + typedef int (STDMETHODCALLTYPE *coreclr_initialize_ptr)( const char* exePath, const char* appDomainFriendlyName, int propertyCount, @@ -86,11 +86,11 @@ namespace NativeMsh void** hostHandle, unsigned int* domainId); - typedef int (*coreclr_shutdown_ptr)( + typedef int (STDMETHODCALLTYPE *coreclr_shutdown_ptr)( void* hostHandle, unsigned int domainId); - typedef int (*coreclr_create_delegate_ptr)( + typedef int (STDMETHODCALLTYPE *coreclr_create_delegate_ptr)( void* hostHandle, unsigned int domainId, const char* entryPointAssemblyName, diff --git a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt index 9306c687b..cddff23d3 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt @@ -2,18 +2,11 @@ # Builds PowerShell.exe, the native host for PowerShell. # -# Only build powershell.exe for "full" windows. Open PowerShell uses the managed executable. if (BUILD_ONECORE) - #set(BUILD_ONECORE OFF) -endif () - -if (BUILD_ONECORE) - message("*") - message("*********** Building PowerShell.exe for CoreCLR. Add CssMainEntry.cpp") - message("*") # Settings to use when creating PowerShell.exe for Windows on OneCore-based SKUs set(PWRSHEXE_WINDOWS_SOURCES - CssMainEntry.cpp) + CssMainEntry.cpp + ) set(PWRSHEXE_WINDOWS_LIBS ### ${STATIC_MT_CRT_LIB} ### ${STATIC_MT_VCRT_LIB} @@ -22,26 +15,26 @@ if (BUILD_ONECORE) vcruntime.lib ucrtd.lib msvcrt.lib + msvcrt.lib + msvcprtd.lib + user32.lib + atls.lib ) set(powershell_definitions _CONSOLE CORECLR ) - # ${common_pwrsh_definitions}) else () - message("*") - message("*********** Building PowerShell.exe for 'Full' CLR.") - message("*") # Settings to use when creating PowerShell.exe for Windows on full SKUs or downlevel platforms set(PWRSHEXE_WINDOWS_SOURCES - MainEntry.cpp) + MainEntry.cpp + ) # Most libs are automatically added by VS set(PWRSHEXE_WINDOWS_LIBS # CoreCLR libs ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} ${STATIC_UCRT_LIB} - # # ole32.lib # oleaut32.lib # uuid.lib @@ -53,7 +46,6 @@ else () ) set(powershell_definitions _CONSOLE - # ${common_pwrsh_definitions}) ) endif (BUILD_ONECORE) @@ -61,16 +53,6 @@ add_executable(powershell ${PWRSHEXE_WINDOWS_SOURCES}) set_target_properties(powershell PROPERTIES COMPILE_DEFINITIONS "${powershell_definitions}") -# Set in nano_defs.cmake. Uncomment for other flavors -#set_target_properties(powershell PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") -#set_target_properties(powershell PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") -#set_target_properties(powershell PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") -#set_target_properties(powershell PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") -message("==== Setting libs = ${PWRSHEXE_WINDOWS_LIBS}") - -target_link_libraries(powershell - ${PWRSHEXE_WINDOWS_LIBS}) - -#target_link_libraries(powershell LINK_PUBLIC pwrshcommon) +target_link_libraries(powershell ${PWRSHEXE_WINDOWS_LIBS}) target_link_libraries(powershell pwrshcommon) diff --git a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt index bec4d1e68..fb5f05f82 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt @@ -6,12 +6,10 @@ add_library(pwrshplugin SHARED pwrshclrhost.cpp pwrshpluginerrorcodes.mc pwrshpluginerrorcodes.rc - pwrshplugin.def) + pwrshplugin.def + ) if (BUILD_ONECORE) - message("*") - message("* Setting up BUILD_ONECORE version of pwrshplugin.dll") - message("*") # Libraries to use when creating this binary for Windows on OneCore-based SKUs set(PWRSHPLUGIN_WINDOWS_LIBS ### ${STATIC_MT_CRT_LIB} @@ -21,24 +19,23 @@ if (BUILD_ONECORE) vcruntime.lib ucrtd.lib msvcrt.lib - wsmsvc.lib) - #set(pwrshplugin_definitions ${common_pwrsh_definitions} CORECLR) - set(pwrshplugin_definitions CORECLR) - set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") + msvcprtd.lib + user32.lib + atls.lib + wsmsvc.lib + ) + set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "CORECLR") else () - message("*") - message("* Setting up NOT BUILD_ONECORE version of pwrshplugin.dll") - message("*") # Libraries to use when creating this binary for Windows on full SKUs set(PWRSHPLUGIN_WINDOWS_LIBS # ntdll.lib # kernel32.lib # advapi32.lib # ole32.lib - MUILoad.lib - mscoree.lib # oleaut32.lib # uuid.lib + MUILoad.lib + mscoree.lib wsmsvc.lib # CoreCLR lib ${STATIC_MT_CRT_LIB} @@ -49,11 +46,7 @@ else () set(pwrshplugin_definitions ${common_pwrsh_definitions}) endif (BUILD_ONECORE) -#set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "${pwrshplugin_definitions}") -#set_target_properties(pwrshplugin PROPERTIES COMPILE_DEFINITIONS "_DLL") - target_link_libraries(pwrshplugin ${PWRSHPLUGIN_WINDOWS_LIBS}) -#target_link_libraries(pwrshplugin LINK_PUBLIC pwrshcommon) target_link_libraries(pwrshplugin pwrshcommon) diff --git a/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp b/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp index a521e0b89..e1ecbc9de 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp +++ b/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp @@ -186,6 +186,7 @@ static PwrshCommon sPwrshCommon; // pwszRuntimeVersion and pRuntimeVersionLength represents the size of pwszRuntimeVersion. // returns: 0 on success, non-zero on failure. _Success_(return == 0) //EXIT_CODE_SUCCESS +__declspec(dllexport) unsigned int GetCLRVersionForPSVersion(int iPSMajorVersion, int iPSMinorVersion, size_t runtimeVersionLength, @@ -323,6 +324,8 @@ DWORD ReportOperationComplete(WSMAN_PLUGIN_REQUEST *requestDetails, DWORD errorC // initialized more than once within the same process, but only once per // applicationIdentification. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" DWORD WINAPI WSManPluginStartup( __in DWORD flags, __in PCWSTR applicationIdentification, @@ -406,6 +409,8 @@ DWORD WINAPI WSManPluginStartup( // reason: If this is a system shutdown this will be WSMAN_PLUGIN_SHUTDOWN_SYSTEM. // For WSMan service shutdown this will be WSMAN_PLUGIN_SHUTDOWN_SERVICE. For an IIS host //shutdown this will be WSMAN_PLUGIN_SHUTDOWN_IISHOST. +__declspec(dllexport) +extern "C" DWORD WINAPI WSManPluginShutdown( __in PVOID pluginContext, __in DWORD flags, @@ -453,6 +458,8 @@ DWORD WINAPI WSManPluginShutdown( // requestDetails the plug-in needs to call WSManPluginOperationComplete. // The shell is active until this time. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginShell( __in PVOID pluginContext, __in WSMAN_PLUGIN_REQUEST *requestDetails, @@ -528,6 +535,8 @@ VOID WINAPI WSManPluginShell( // corresponding release function has been called. Failure to follow the contract // will result in errors being generated. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginReleaseShellContext(__in PVOID shellContext) { if ((NULL == shellContext)) @@ -560,6 +569,8 @@ VOID WINAPI WSManPluginReleaseShellContext(__in PVOID shellContext) // requestDetails the plug-in needs to call WSManPluginOperationComplete. // The command is active until this time. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginCommand( __in WSMAN_PLUGIN_REQUEST *requestDetails, __in DWORD flags, @@ -593,6 +604,8 @@ VOID WINAPI WSManPluginCommand( // corresponding release function has been called. Failure to follow the contract // will result in errors being generated. // --------------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginReleaseCommandContext( __in PVOID shellContext, __in PVOID commandContext @@ -624,6 +637,8 @@ VOID WINAPI WSManPluginReleaseCommandContext( // For each piece of data the plug-in calls WSManPluginResultComplete to // acknowledge receipt and to allow the next piece of data to be delivered. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginSend( __in WSMAN_PLUGIN_REQUEST *requestDetails, __in DWORD flags, @@ -663,6 +678,8 @@ VOID WINAPI WSManPluginSend( // WSManPluginResultComplete. The operation is marked as active until this // time. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginReceive( __in WSMAN_PLUGIN_REQUEST *requestDetails, __in DWORD flags, @@ -698,6 +715,8 @@ VOID WINAPI WSManPluginReceive( // of this callback may be many completion calls for the Signal, Receive, Command // and Shell operations. // ----------------------------------------------------------------------------- +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginSignal( __in WSMAN_PLUGIN_REQUEST *requestDetails, __in DWORD flags, @@ -723,7 +742,8 @@ VOID WINAPI WSManPluginSignal( } } - +__declspec(dllexport) +extern "C" VOID WINAPI WSManPluginConnect( __in WSMAN_PLUGIN_REQUEST *requestDetails, __in DWORD flags, diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h index d63dc56cb..908e9055f 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h @@ -15,8 +15,10 @@ // pwszRuntimeVersion and pRuntimeVersionLength represents the size of pwszRuntimeVersion. // returns: 0 on success, non-zero on failure. _Success_(return == 0) // EXIT_CODE_SUCCESS -extern unsigned int GetCLRVersionForPSVersion(int iPSMajorVersion, - int iPSMinorVersion, - size_t runtimeVersionLength, - __inout_ecount_part(runtimeVersionLength , *pRuntimeVersionLength) wchar_t* pwszRuntimeVersion, - __out_ecount(1) size_t* pRuntimeVersionLength); \ No newline at end of file +extern "C" +__declspec(dllexport) +unsigned int GetCLRVersionForPSVersion(int iPSMajorVersion, + int iPSMinorVersion, + size_t runtimeVersionLength, + __inout_ecount_part(runtimeVersionLength , *pRuntimeVersionLength) wchar_t* pwszRuntimeVersion, + __out_ecount(1) size_t* pRuntimeVersionLength); diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.def b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.def index f80ff8ac2..af45f5c78 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.def +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.def @@ -15,4 +15,4 @@ WSManPluginSend WSManPluginSignal WSManPluginReceive WSManPluginConnect -PerformWSManPluginReportCompletion \ No newline at end of file +PerformWSManPluginReportCompletion diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h index 16efa0f3d..61734b800 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h @@ -1091,6 +1091,8 @@ public: } }; +__declspec(dllexport) +extern "C" void WINAPI PerformWSManPluginReportCompletion() { // Now report the plugin completion, to indicate that plugin is ready to shutdown. From d7618d731de94ae5ae7895a1ea8d5619625c6903 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Fri, 15 Jul 2016 16:52:00 -0700 Subject: [PATCH 5/9] Additional fixes in preparation for review and pull request --- build.psm1 | 101 +++++++----------- src/powershell-native/nano_defs.cmake | 6 +- .../nativemsh/pwrshplugin/entrypoints.cpp | 12 +-- .../nativemsh/pwrshplugin/pwrshheaders.h | 1 - .../nativemsh/pwrshplugin/pwrshplugin.h | 1 - 5 files changed, 44 insertions(+), 77 deletions(-) diff --git a/build.psm1 b/build.psm1 index 514b41912..83a794801 100644 --- a/build.psm1 +++ b/build.psm1 @@ -59,7 +59,7 @@ function Start-PSBuild { [switch]$XamlGen, [Parameter(ParameterSetName='FullCLR')] - [ValidateSet('x86', 'x64')] + [ValidateSet('x86', 'x64')] # TODO: At some point, we need to add ARM support to match CoreCLR [string]$NativeHostArch = "x64", [ValidateSet('Linux', 'Debug', 'Release', '')] # We might need "Checked" as well @@ -106,6 +106,12 @@ function Start-PSBuild { } #> + $vcVarsPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName + if ((Test-Path -Path $vcVarsPath\vcvarsall.bat) -eq $false) + { + throw "Could not find Visual Studio vcvarsall.bat at" + $vcVarsPath + } + # setup msbuild configuration if ($Configuration -eq 'Debug' -or $Configuration -eq 'Release') { @@ -156,36 +162,36 @@ function Start-PSBuild { $Arguments += "--runtime", $Options.Runtime # handle Restore -# if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) { -# log "Run dotnet restore" + if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) { + log "Run dotnet restore" -# $RestoreArguments = @("--verbosity") -# if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { -# $RestoreArguments += "Info" -# } else { -# $RestoreArguments += "Warning" -# } -# -# $RestoreArguments += "$PSScriptRoot" -# -# Start-NativeExecution { dotnet restore $RestoreArguments } -# } + $RestoreArguments = @("--verbosity") + if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { + $RestoreArguments += "Info" + } else { + $RestoreArguments += "Warning" + } + + $RestoreArguments += "$PSScriptRoot" + + Start-NativeExecution { dotnet restore $RestoreArguments } + } # handle ResGen # Heuristic to run ResGen on the fresh machine -# if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) -# { -# log "Run ResGen (generating C# bindings for resx files)" -# Start-ResGen -# } + if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) + { + log "Run ResGen (generating C# bindings for resx files)" + Start-ResGen + } # handle xaml files # Heuristic to resolve xaml on the fresh machine -# if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) -# { -# log "Run XamlGen (generating .g.cs and .resources for .xaml files)" -# Start-XamlGen -MSBuildConfiguration $msbuildConfiguration -# } + if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) + { + log "Run XamlGen (generating .g.cs and .resources for .xaml files)" + Start-XamlGen -MSBuildConfiguration $msbuildConfiguration + } # Build native components if ($IsLinux -or $IsOSX) { @@ -217,12 +223,6 @@ function Start-PSBuild { try { Push-Location "$PSScriptRoot\src\powershell-native" - $vcPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName - if ((Test-Path -Path $vcPath\vcvarsall.bat) -eq $false) - { - throw "Could not find Visual Studio vcvarsall.bat at" + $vcPath - } - <# # Compile native resources @("nativemsh/pwrshplugin") | % { @@ -236,44 +236,23 @@ function Start-PSBuild { # Disabling until I figure out if it is necessary # $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt" $overrideFlags = "" - $vcArch = "Win64" - $cmakeGenerator = "Visual Studio 14 2015 $vcArch" -# $CMakeArguments = "-G $cmakeGenerator ." - $vcPlatform = "x64" $location = Get-Location -# $MSBuildArguments = "ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" -# if ($cmakeGenerator) { -# Start-NativeExecution { cmake $overrideFlags -G $cmakeGenerator . } -# } else { -# Start-NativeExecution { cmake $overrideFlags . } -# } -# # -# # BUILD_ONECORE -# # - $command = @" -cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" cmake "$overrideFlags" -DBUILD_ONECORE=ON -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" -"@ -#cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" "$cmakeCmd" "&" "msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" -# "&" exit - log " Executing command: $command" - #Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } - Start-NativeExecution { Invoke-Expression -Command:$command } - # - # NOT BUILD_ONECORE + # BUILD_ONECORE # - $command = @" -cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" cmake "$overrideFlags" -DBUILD_ONECORE=OFF -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" + + $BuildOneCoreValues = @("ON","OFF") + foreach ($oneCoreValue in $BuildOneCoreValues) + { + $command = @" +cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" cmake "$overrideFlags" -DBUILD_ONECORE=$oneCoreValue -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" "@ -#cmd.exe /C cd /d "$location" "&" "$($vcPath)\vcvarsall.bat" "$vcPlatform" "&" "$cmakeCmd" "&" "msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration" -# "&" exit - log " Executing command: $command" - #Start-NativeExecution { msbuild ALL_BUILD.vcxproj /p:Configuration=$msbuildConfiguration } - Start-NativeExecution { Invoke-Expression -Command:$command } + log " Executing command: $command" + Start-NativeExecution { Invoke-Expression -Command:$command } + } } finally { Pop-Location } - throw "All done" } # handle TypeGen diff --git a/src/powershell-native/nano_defs.cmake b/src/powershell-native/nano_defs.cmake index 983e0886e..fe52150aa 100644 --- a/src/powershell-native/nano_defs.cmake +++ b/src/powershell-native/nano_defs.cmake @@ -77,12 +77,12 @@ add_definitions( set(CMAKE_ENABLE_EXPORTS ON) -set(MY_COMMON_LINK_FLAGS "/INCREMENTAL:NO /NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /guard:cf /OPT:REF /OPT:ICF /NODEFAULTLIB") +set(MY_COMMON_LINK_FLAGS "/NOLOGO /MANIFEST:NO /NXCOMPAT /DYNAMICBASE /TLBID:1 /MACHINE:x64 /guard:cf /OPT:REF /OPT:ICF /NODEFAULTLIB") set(MY_COMMON_LINK_FLAGS "${MY_COMMON_LINK_FLAGS} /NODEFAULTLIB:kernel32.lib /NODEFAULTLIB:advapi32.lib") # Explicitly exclude kernel32 and advapi32 since CMake is including them and they block execution on Nano Server set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00 /INCREMENTAL:NO") #windows subsystem set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MY_COMMON_LINK_FLAGS}") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE") #windows subsystem +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE /INCREMENTAL:NO") #windows subsystem diff --git a/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp b/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp index e1ecbc9de..6490ec5ac 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp +++ b/src/powershell-native/nativemsh/pwrshplugin/entrypoints.cpp @@ -186,7 +186,7 @@ static PwrshCommon sPwrshCommon; // pwszRuntimeVersion and pRuntimeVersionLength represents the size of pwszRuntimeVersion. // returns: 0 on success, non-zero on failure. _Success_(return == 0) //EXIT_CODE_SUCCESS -__declspec(dllexport) +extern "C" unsigned int GetCLRVersionForPSVersion(int iPSMajorVersion, int iPSMinorVersion, size_t runtimeVersionLength, @@ -324,7 +324,6 @@ DWORD ReportOperationComplete(WSMAN_PLUGIN_REQUEST *requestDetails, DWORD errorC // initialized more than once within the same process, but only once per // applicationIdentification. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" DWORD WINAPI WSManPluginStartup( __in DWORD flags, @@ -409,7 +408,6 @@ DWORD WINAPI WSManPluginStartup( // reason: If this is a system shutdown this will be WSMAN_PLUGIN_SHUTDOWN_SYSTEM. // For WSMan service shutdown this will be WSMAN_PLUGIN_SHUTDOWN_SERVICE. For an IIS host //shutdown this will be WSMAN_PLUGIN_SHUTDOWN_IISHOST. -__declspec(dllexport) extern "C" DWORD WINAPI WSManPluginShutdown( __in PVOID pluginContext, @@ -458,7 +456,6 @@ DWORD WINAPI WSManPluginShutdown( // requestDetails the plug-in needs to call WSManPluginOperationComplete. // The shell is active until this time. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginShell( __in PVOID pluginContext, @@ -535,7 +532,6 @@ VOID WINAPI WSManPluginShell( // corresponding release function has been called. Failure to follow the contract // will result in errors being generated. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginReleaseShellContext(__in PVOID shellContext) { @@ -569,7 +565,6 @@ VOID WINAPI WSManPluginReleaseShellContext(__in PVOID shellContext) // requestDetails the plug-in needs to call WSManPluginOperationComplete. // The command is active until this time. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginCommand( __in WSMAN_PLUGIN_REQUEST *requestDetails, @@ -604,7 +599,6 @@ VOID WINAPI WSManPluginCommand( // corresponding release function has been called. Failure to follow the contract // will result in errors being generated. // --------------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginReleaseCommandContext( __in PVOID shellContext, @@ -637,7 +631,6 @@ VOID WINAPI WSManPluginReleaseCommandContext( // For each piece of data the plug-in calls WSManPluginResultComplete to // acknowledge receipt and to allow the next piece of data to be delivered. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginSend( __in WSMAN_PLUGIN_REQUEST *requestDetails, @@ -678,7 +671,6 @@ VOID WINAPI WSManPluginSend( // WSManPluginResultComplete. The operation is marked as active until this // time. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginReceive( __in WSMAN_PLUGIN_REQUEST *requestDetails, @@ -715,7 +707,6 @@ VOID WINAPI WSManPluginReceive( // of this callback may be many completion calls for the Signal, Receive, Command // and Shell operations. // ----------------------------------------------------------------------------- -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginSignal( __in WSMAN_PLUGIN_REQUEST *requestDetails, @@ -742,7 +733,6 @@ VOID WINAPI WSManPluginSignal( } } -__declspec(dllexport) extern "C" VOID WINAPI WSManPluginConnect( __in WSMAN_PLUGIN_REQUEST *requestDetails, diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h index 908e9055f..a10d741dc 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshheaders.h @@ -16,7 +16,6 @@ // returns: 0 on success, non-zero on failure. _Success_(return == 0) // EXIT_CODE_SUCCESS extern "C" -__declspec(dllexport) unsigned int GetCLRVersionForPSVersion(int iPSMajorVersion, int iPSMinorVersion, size_t runtimeVersionLength, diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h index 61734b800..c1135cfd0 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshplugin.h @@ -1091,7 +1091,6 @@ public: } }; -__declspec(dllexport) extern "C" void WINAPI PerformWSManPluginReportCompletion() { From 4e7f582d43662d1f51c6cd6c5c5fd34dd0aeed32 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Sat, 16 Jul 2016 22:01:06 -0700 Subject: [PATCH 6/9] Adding ConfigFileReader back into the plugin and fixing it to more efficiently parse the configuration file. --- build.psm1 | 5 +- .../nativemsh/pwrshcommon/CMakeLists.txt | 4 +- .../nativemsh/pwrshcommon/ClrHostWrapper.h | 155 ++++++++++++------ .../pwrshcommon/ConfigFileReader.cpp | 131 +++++++++++++++ .../nativemsh/pwrshcommon/ConfigFileReader.h | 36 ++++ .../nativemsh/pwrshcommon/NativeMsh.h | 3 + .../nativemsh/pwrshcommon/pwrshcommon.cpp | 101 +++++++----- .../nativemsh/pwrshexe/CMakeLists.txt | 2 +- .../nativemsh/pwrshexe/CssMainEntry.cpp | 3 +- .../nativemsh/pwrshexe/MainEntry.cpp | 3 +- .../nativemsh/pwrshplugin/CMakeLists.txt | 5 +- .../nativemsh/pwrshplugin/pwrshclrhost.cpp | 5 +- 12 files changed, 353 insertions(+), 100 deletions(-) create mode 100644 src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp create mode 100644 src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.h diff --git a/build.psm1 b/build.psm1 index 83a794801..5e508fa95 100644 --- a/build.psm1 +++ b/build.psm1 @@ -99,12 +99,11 @@ function Start-PSBuild { Use-MSBuild #mc.exe is Message Compiler for native resources - <# This currently doesn't work reliably and clean systems. Removing for now since mc.exe is not being used yet. + # This currently doesn't work reliably and clean systems. Removing for now since mc.exe is not being used yet. $mcexe = Get-ChildItem "${env:ProgramFiles(x86)}\Microsoft SDKs\Windows\" -Recurse -Filter 'mc.exe' | ? {$_.FullName -match 'x64'} | select -First 1 | % {$_.FullName} if (-not $mcexe) { throw 'mc.exe not found. Install Microsoft Windows SDK.' } - #> $vcVarsPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName if ((Test-Path -Path $vcVarsPath\vcvarsall.bat) -eq $false) @@ -223,7 +222,6 @@ function Start-PSBuild { try { Push-Location "$PSScriptRoot\src\powershell-native" - <# # Compile native resources @("nativemsh/pwrshplugin") | % { $nativeResourcesFolder = $_ @@ -231,7 +229,6 @@ function Start-PSBuild { & $mcexe -o -d -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder } } - #> # Disabling until I figure out if it is necessary # $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt" diff --git a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt index c3971e88d..24fd4afe4 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshcommon/CMakeLists.txt @@ -4,7 +4,9 @@ # add_library(pwrshcommon pwrshcommon.cpp - WinSystemCallFacade.cpp) + WinSystemCallFacade.cpp + ConfigFileReader.cpp + ) target_include_directories(pwrshcommon PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h index 2c86fb9e2..cb1b870cc 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h +++ b/src/powershell-native/nativemsh/pwrshcommon/ClrHostWrapper.h @@ -37,7 +37,7 @@ namespace NativeMsh // // The following methods are direct thin wrappers for the host calls. // - virtual HMODULE SetupWrapper(LPCSTR coreClrPathPtr) = 0; + virtual unsigned int SetupWrapper(LPCSTR coreClrPathPtr) = 0; virtual int InitializeClr( const char* exePath, @@ -123,15 +123,13 @@ namespace NativeMsh { return (NULL != coreClrHandle); } - - // - // TODO: This shouldn't return HMODULE because that breaks encapsulation. I need a better way to - // have the caller extract the value. Either Pass as a reference or provide an accessor. - // + + // // Attempts to load CoreCLR.dll from the specified directory. // On success pins the dll, sets coreCLRDirectoryPath and returns the HMODULE. // On failure returns NULL. - virtual HMODULE SetupWrapper(LPCSTR coreClrPathPtr) + // + virtual unsigned int SetupWrapper(LPCSTR coreClrPathPtr) { std::string coreClrPath(coreClrPathPtr); coreClrPath += coreClrDllName; @@ -139,13 +137,13 @@ namespace NativeMsh HMODULE result = LoadLibraryExA(coreClrPath.c_str(), NULL, 0); if (!result) { - return NULL; + return EXIT_CODE_INIT_FAILURE; } // Pin the module - CoreCLR.dll does not support being unloaded. if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_PIN, coreClrPath.c_str(), &pinnedModuleHandle)) { - return NULL; + return EXIT_CODE_INIT_FAILURE; } initPtr = (coreclr_initialize_ptr)GetProcAddress(result, "coreclr_initialize"); @@ -156,12 +154,12 @@ namespace NativeMsh NULL == shutdownPtr || NULL == createDelegatePtr) { - return NULL; + return EXIT_CODE_INIT_FAILURE; } // Initialization succeeded. Save the handle and return success; coreClrHandle = result; - return result; + return EXIT_CODE_SUCCESS; } virtual unsigned int CleanUpHostWrapper() @@ -246,6 +244,7 @@ namespace NativeMsh private: // The path to this module char m_hostPath[MAX_PATH]; + wchar_t m_hostPathW[MAX_PATH]; // The path to the directory containing this module char m_hostDirectoryPath[MAX_PATH]; @@ -253,11 +252,56 @@ namespace NativeMsh // The name of this module, without the path std::string m_hostBinaryName; + std::wstring m_hostBinaryNameW; // The path to the directory that CoreCLR is in char m_coreCLRDirectoryPath[MAX_PATH]; wchar_t m_coreCLRDirectoryPathW[MAX_PATH]; + void convertAnsiToWide(char* ansiArray, wchar_t* wideArray) + { + // Generate the wide version of the string and save its value; + // + // This is a two call function. The first call is to get the necessary length. + // The second call is to perform the actual operation. + int length = ::MultiByteToWideChar(CP_UTF8, 0, ansiArray, -1, NULL, 0); + if (0 < length) + { + LPWSTR result = new wchar_t[length]; + if (NULL != result) + { + length = ::MultiByteToWideChar(CP_UTF8, 0, ansiArray, -1, result, length); + if (0 < length) + { + wcscpy_s(wideArray, MAX_PATH, result); + } + delete[] result; // Free the allocated string to avoid a memory leak + } + } + } + + void convertWideToAnsi(wchar_t* wideArray, char* ansiArray) + { + // Generate the ansi version of the string and save its value; + // + // This is a two call function. The first call is to get the necessary length. + // The second call is to perform the actual operation. + int length = ::WideCharToMultiByte(CP_ACP, 0, wideArray, -1, NULL, 0, NULL, NULL); + if (0 < length) + { + LPSTR result = new char[length]; + if (NULL != result) + { + length = ::WideCharToMultiByte(CP_ACP, 0, wideArray, -1, result, length, NULL, NULL); + if (0 < length) + { + strcpy_s(ansiArray, MAX_PATH, result); + } + delete[] result; // Free the allocated string to avoid a memory leak + } + } + } + public: HostEnvironment() @@ -275,6 +319,17 @@ namespace NativeMsh if (hostPath) { ::ExpandEnvironmentStringsA(hostPath, m_hostPath, MAX_PATH); + + convertAnsiToWide(m_hostPath, m_hostPathW); + } + } + void SetHostPathW(PCWSTR hostPath) + { + if (hostPath) + { + ::ExpandEnvironmentStringsW(hostPath, m_hostPathW, MAX_PATH); + + convertWideToAnsi(m_hostPathW, m_hostPath); } } @@ -284,6 +339,11 @@ namespace NativeMsh return m_hostPath; } + PCWSTR GetHostPathW() + { + return m_hostPathW; + } + // Safely copies in a host binary name void SetHostBinaryName(PCSTR hostBinaryName) { @@ -293,12 +353,25 @@ namespace NativeMsh } } + void SetHostBinaryNameW(PCWSTR hostBinaryName) + { + if (hostBinaryName) + { + m_hostBinaryNameW = std::wstring(hostBinaryName); + } + } + // Returns the name of the host module PCSTR GetHostBinaryName() { return m_hostBinaryName.c_str(); } + PCWSTR GetHostBinaryNameW() + { + return m_hostBinaryNameW.c_str(); + } + // Safely copies in a host directory path void SetHostDirectoryPath(PCSTR hostDirPath) { @@ -306,24 +379,17 @@ namespace NativeMsh { ::ExpandEnvironmentStringsA(hostDirPath, m_hostDirectoryPath, MAX_PATH); - // Generate the wide version of the string and save its value; - // - // This is a two call function. The first call is to get the necessary length. - // The second call is to perform the actual operation. - int length = MultiByteToWideChar(CP_UTF8, 0, m_hostDirectoryPath, -1, NULL, 0); - if (0 < length) - { - LPWSTR result = new wchar_t[length]; - if (NULL != result) - { - length = ::MultiByteToWideChar(CP_UTF8, 0, m_hostDirectoryPath, -1, result, length); - if (0 < length) - { - wcscpy_s(m_hostDirectoryPathW, MAX_PATH, result); - } - delete[] result; // Free the allocated string to avoid a memory leak - } - } + convertAnsiToWide(m_hostDirectoryPath, m_hostDirectoryPathW); + } + } + + void SetHostDirectoryPathW(PCWSTR hostDirPath) + { + if (hostDirPath) + { + ::ExpandEnvironmentStringsW(hostDirPath, m_hostDirectoryPathW, MAX_PATH); + + convertWideToAnsi(m_hostDirectoryPathW, m_hostDirectoryPath); } } @@ -346,24 +412,17 @@ namespace NativeMsh { ::ExpandEnvironmentStringsA(hostClrPath, m_coreCLRDirectoryPath, MAX_PATH); - // Generate the wide version of the string and save its value; - // - // This is a two call function. The first call is to get the necessary length. - // The second call is to perform the actual operation. - int length = MultiByteToWideChar(CP_UTF8, 0, m_coreCLRDirectoryPath, -1, NULL, 0); - if (0 < length) - { - LPWSTR result = new wchar_t[length]; - if (NULL != result) - { - length = ::MultiByteToWideChar(CP_UTF8, 0, m_coreCLRDirectoryPath, -1, result, length); - if (0 < length) - { - wcscpy_s(m_coreCLRDirectoryPathW, MAX_PATH, result); - } - delete[] result; // Free the allocated string to avoid a memory leak - } - } + convertAnsiToWide(m_coreCLRDirectoryPath, m_coreCLRDirectoryPathW); + } + } + + void SetCoreCLRDirectoryPathW(PCWSTR hostClrPath) + { + if (hostClrPath) + { + ::ExpandEnvironmentStringsW(hostClrPath, m_coreCLRDirectoryPathW, MAX_PATH); + + convertWideToAnsi(m_coreCLRDirectoryPathW, m_coreCLRDirectoryPath); } } @@ -378,5 +437,7 @@ namespace NativeMsh { return m_coreCLRDirectoryPathW; } + + }; } // namespace NativeMsh diff --git a/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp new file mode 100644 index 000000000..4b1bd4b43 --- /dev/null +++ b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp @@ -0,0 +1,131 @@ +#include +#include +#include + +#include "NativeMsh.h" +#include "ConfigFileReader.h" + +namespace NativeMsh +{ + // The name of the PowerShell config file that identifies the PowerShell install location. + // We use a config file to avoid writing to the registry during install + // or hard coding paths into the binary. + static PCWSTR powerShellConfigFileName = L"RemotePowerShellConfig.txt"; + + ConfigFileReader::ConfigFileReader() + { + } + + unsigned int ConfigFileReader::Read( + std::wstring pathToConfig) + { + std::wstring absolutePathToConfigFile(pathToConfig); + absolutePathToConfigFile += powerShellConfigFileName; + std::wfstream psConfigFile(absolutePathToConfigFile.c_str()); + + if (!psConfigFile.is_open()) + { + return EXIT_CODE_INIT_FAILURE; + } + + std::wstring line; + std::wstring psHomeDirTag(L"PSHOMEDIR="); + std::wstring coreClrDirTag(L"CORECLRDIR="); + while (std::getline(psConfigFile, line)) + { + std::wstring::const_iterator iter = line.begin(); + + // Search for the first actionable character in the line to + // limit the number of passes that are made iterating through the line. + while(iter != line.end()) + { + if (*iter == L'#') + { + // Stop parsing the line because the rest of it is a comment + break; + } + else if (*iter == L'p' || *iter == L'P') + { + this->pathToPowerShellAssemblies = this->getValueFromLine(line, psHomeDirTag); + if (this->pathToPowerShellAssemblies.size() > 0) // Found a match + { + std::wstring::const_iterator slashIter = this->pathToPowerShellAssemblies.end(); + slashIter--; + if (*slashIter != L'\\') + { + // Guarantee that there is a '\' at the end of the path + this->pathToPowerShellAssemblies.append(L"\\"); + } + } + } + else if (*iter == L'c' || *iter == L'C') + { + this->coreClrDirectory = this->getValueFromLine(line, coreClrDirTag); + if (this->coreClrDirectory.size() > 0) // Found a match + { + std::wstring::const_iterator slashIter = this->coreClrDirectory.end(); + slashIter--; + if (*slashIter != L'\\') + { + // Guarantee that there is a '\' at the end of the path + this->coreClrDirectory.append(L"\\"); + } + } + } + + // Do nothing to ignore unmatched characters (whitespace, etc.) + iter++; + } + } + if (0 == this->pathToPowerShellAssemblies.size() || + 0 == this->coreClrDirectory.size()) + { + return EXIT_CODE_INIT_FAILURE; + } + else + { + return EXIT_CODE_SUCCESS; + } + } + + // This trim function removes beginning and ending whitespace. It does not + // remove internal whitespace because that is valid within paths. + std::wstring ConfigFileReader::trim( + const std::wstring& toTrim) + { + static PCWSTR WHITESPACE_CHARS = L" \n\r\t"; + std::wstring copyToTrim = toTrim; + std::size_t first = copyToTrim.find_first_not_of(WHITESPACE_CHARS); + + if (first == std::wstring::npos) + { + // No non-whitespace found + return std::wstring(L""); + } + + // Result not checked for std::wstring::npos because it is guaranteed to have a value if the first pass succeeded. + copyToTrim.erase(copyToTrim.find_last_not_of(WHITESPACE_CHARS)+1); + + return copyToTrim.substr(first); + } + + // Parses the specified line for the given tag. Tags are assumed to include + // the "=" separator character. + std::wstring ConfigFileReader::getValueFromLine( + const std::wstring& line, // Passed by value to preserve the original line. + std::wstring& tagToFind) + { + std::wstring trimmedLine = this->trim(line); + std::size_t index = trimmedLine.find(tagToFind); + if (std::string::npos != index) + { + std::wstring value = trimmedLine.substr(index + tagToFind.size()); // Everything else after the tag + return this->trim(value); + } + else + { + return std::wstring(L""); + } + } +} + diff --git a/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.h b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.h new file mode 100644 index 000000000..265992dbc --- /dev/null +++ b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.h @@ -0,0 +1,36 @@ +// --------------------------------------------------------------------------- +// +// Microsoft Windows NT +// Copyright (C) Microsoft Corporation, 2014. +// +// Contents: A class that extracts the path to $PSHOME and the path to its +// CoreCLR from a configuration file. +// --------------------------------------------------------------------------- + +#pragma once + +#include + +namespace NativeMsh +{ + class ConfigFileReader + { + private: + std::wstring pathToPowerShellAssemblies; + std::wstring coreClrDirectory; + + std::wstring trim(const std::wstring& toTrim); + std::wstring getValueFromLine(const std::wstring& line, std::wstring& tagToFind); + + public: + ConfigFileReader(); + + // Initiates a Read operation of the specified configuration file. + unsigned int Read(std::wstring pathToConfig); + + // Information Getters + std::wstring GetPathToPowerShell() { return pathToPowerShellAssemblies; } + std::wstring GetPathToCoreClr() { return coreClrDirectory; } + }; +} // namespace NativeMsh + diff --git a/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h b/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h index eee01a498..924fc5e34 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h +++ b/src/powershell-native/nativemsh/pwrshcommon/NativeMsh.h @@ -17,6 +17,7 @@ #include "NativeMshConstants.h" #include "ClrHostWrapper.h" #include "SystemCallFacade.h" +#include "ConfigFileReader.h" #include "IPwrshCommonOutput.h" #if !CORECLR @@ -29,6 +30,7 @@ namespace NativeMsh { private: IPwrshCommonOutput* output; + ConfigFileReader* reader; SystemCallFacade* sysCalls; public: @@ -41,6 +43,7 @@ namespace NativeMsh // allocate them with "new". PwrshCommon( IPwrshCommonOutput* outObj, // Enables clients to specify how error messages are displayed or suppressed + ConfigFileReader* rdr, // Enables specification of how the config file is read. SystemCallFacade* systemCalls); // Wraps all calls to Windows APIs to allow for dependency injection via unit test ~PwrshCommon(); diff --git a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp index b687fed71..fef838058 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/pwrshcommon.cpp @@ -820,29 +820,40 @@ namespace NativeMsh // The name of the CoreCLR native runtime DLL. static PCSTR coreClrDll = "CoreCLR.dll"; - // The location where CoreCLR is expected to be installed. If CoreCLR.dll isn't + // The location where CoreCLR is expected to be installed for inbox PowerShell. If CoreCLR.dll isn't // found in the same directory as the host, it will be looked for here. static PCSTR coreCLRInstallDirectory = "%windir%\\system32\\DotNetCore\\v1.0\\"; - // The location where CoreCLR PowerShell Ext binaries are expected to be installed. + // The location where CoreCLR PowerShell Ext binaries are expected to be installed for inbox PowerShell. static PCSTR coreCLRPowerShellExtInstallDirectory = "%windir%\\system32\\CoreClrPowerShellExt\\v1.0\\"; - // The default PowerShell install directory. This location may be overridden through a config file in %windir%\System32. + // The default PowerShell install directory for inbox PowerShell. + // This location may be overridden by placing a config file in the same directory as the PowerShell host. static PCSTR powerShellInstallPath = "%windir%\\System32\\WindowsPowerShell\\v1.0\\"; unsigned int PwrshCommon::IdentifyHostDirectory( HostEnvironment& hostEnvironment) { - // Discover the path to the exe's module (powershell.exe or wsmprovhost.exe). - // For remoting, this is expected to be %windir%\system32 since that is the location of wsmprovhost.exe. - char hostPath[MAX_PATH]; - DWORD thisModuleLength = sysCalls->GetModuleFileNameA(sysCalls->GetModuleHandleA(NULL), hostPath, MAX_PATH); + // Discover the path to the plugin or the executable (pwrshplugin.dll or powershell.exe). + // For PowerShell Core, the plugin no longer resides in %windir%\\system32 (it is in a sub-directory). + // If pwrshplugin.dll is not loaded, it means that this is running via powershell.exe. + wchar_t hostPath[MAX_PATH]; + DWORD thisModuleLength; - if (0 == thisModuleLength) + if (GetModuleHandleW(L"pwrshplugin.dll")) + { + thisModuleLength = GetModuleFileNameW(GetModuleHandleW(L"pwrshplugin.dll"), hostPath, MAX_PATH); + } + else + { + thisModuleLength = GetModuleFileNameW(GetModuleHandleW(NULL), hostPath, MAX_PATH); + } + if (0 == thisModuleLength) // Greater than zero means it is the length of the fully qualified path (without the NULL character) { // TODO: Use GetLastError() to find the specific error # return EXIT_CODE_INIT_FAILURE; } + // Search for the last backslash in the host path. int lastBackslashIndex; for (lastBackslashIndex = thisModuleLength - 1; lastBackslashIndex >= 0; lastBackslashIndex--) @@ -854,14 +865,34 @@ namespace NativeMsh } // The remaining part of the path after the last '\' is the binary name. - hostEnvironment.SetHostBinaryName(hostPath + lastBackslashIndex + 1); + hostEnvironment.SetHostBinaryNameW(hostPath + lastBackslashIndex + 1); // Copy the directory path portion of the path hostPath[lastBackslashIndex + 1] = '\0'; - hostEnvironment.SetHostPath(hostPath); - - hostEnvironment.SetHostDirectoryPath(powerShellInstallPath); + hostEnvironment.SetHostPathW(hostPath); + // Read the config file to determine the appropriate host path and CoreCLR path to use. + unsigned int result = reader->Read(hostPath); + if (EXIT_CODE_SUCCESS == result) + { + // The config file was successfully parsed. Use those directories. + hostEnvironment.SetHostDirectoryPathW(reader->GetPathToPowerShell().c_str()); + hostEnvironment.SetCoreCLRDirectoryPathW(reader->GetPathToCoreClr().c_str()); + } + else + { + // There was an issue accessing or parsing the config file OR + // we are working for the EXE. + // + // TODO: This should not be the fallback for inbox PowerShell.exe. + // It should use coreCLRInstallDirectory and coreCLRPowerShellExtInstallDirectory. + // + // Use the directory detected via GetModuleFileName + GetModuleHandle + hostEnvironment.SetHostDirectoryPathW(hostPath); + // At the moment, CoreCLR is in the same directory as PowerShell Core. + // This path must be modified if we decide to use a different directory. + hostEnvironment.SetCoreCLRDirectoryPathW(hostPath); + } return EXIT_CODE_SUCCESS; } @@ -959,20 +990,26 @@ namespace NativeMsh // PwrshCommon::PwrshCommon() - : output(new PwrshCommonOutputDefault()), sysCalls(new WinSystemCallFacade()) + : output(new PwrshCommonOutputDefault()), reader(new ConfigFileReader()), sysCalls(new WinSystemCallFacade()) { } PwrshCommon::PwrshCommon( IPwrshCommonOutput* outObj, + ConfigFileReader* rdr, SystemCallFacade* systemCalls) - : output(outObj), sysCalls(systemCalls) + : output(outObj), reader(rdr), sysCalls(systemCalls) { if (NULL == output) { output = new PwrshCommonOutputDefault(); } + if (NULL == reader) + { + reader = new ConfigFileReader(); + } + if (NULL == sysCalls) { sysCalls = new WinSystemCallFacade(); @@ -987,6 +1024,12 @@ namespace NativeMsh output = NULL; } + if (reader) + { + delete reader; + reader = NULL; + } + if (sysCalls) { delete sysCalls; @@ -1299,37 +1342,13 @@ namespace NativeMsh return exitCode; } - // Try to load from the well-known location. - char coreCLRInstallPath[MAX_PATH]; - exitCode = ::ExpandEnvironmentStringsA(coreCLRInstallDirectory, coreCLRInstallPath, MAX_PATH); - if (0 == exitCode || _countof(coreCLRInstallPath) <= exitCode) + exitCode = hostWrapper->SetupWrapper(hostEnvironment.GetCoreCLRDirectoryPath()); + if (EXIT_CODE_SUCCESS != exitCode) { this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); - return EXIT_CODE_INIT_FAILURE; - } - - HMODULE coreClrModule = hostWrapper->SetupWrapper(coreCLRInstallPath); - if (!coreClrModule) - { - this->output->DisplayMessage(false, g_STARTING_CLR_FAILED, GetLastError()); - return EXIT_CODE_INIT_FAILURE; + return exitCode; } - // Save the directory that CoreCLR was found in - char coreCLRDirectoryPath[MAX_PATH]; - DWORD modulePathLength = sysCalls->GetModuleFileNameA(coreClrModule, coreCLRDirectoryPath, MAX_PATH); - - // Search for the last backslash and terminate it there to keep just the directory path with trailing slash - for (int lastBackslashIndex = modulePathLength - 1; lastBackslashIndex >= 0; lastBackslashIndex--) - { - if (coreCLRDirectoryPath[lastBackslashIndex] == L'\\') - { - coreCLRDirectoryPath[lastBackslashIndex + 1] = L'\0'; - break; - } - } - hostEnvironment.SetCoreCLRDirectoryPath(coreCLRDirectoryPath); - const int nMaxProps = 8; LPCSTR props[nMaxProps]; LPCSTR vals[nMaxProps]; diff --git a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt index cddff23d3..e0f8ada21 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshexe/CMakeLists.txt @@ -34,7 +34,7 @@ else () # CoreCLR libs ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} - ${STATIC_UCRT_LIB} + # ${STATIC_UCRT_LIB} # ole32.lib # oleaut32.lib # uuid.lib diff --git a/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp b/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp index bf0853bff..c8a201760 100644 --- a/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp +++ b/src/powershell-native/nativemsh/pwrshexe/CssMainEntry.cpp @@ -18,6 +18,7 @@ #include "NativeMsh.h" #include "ClrHostWrapper.h" #include "OutputWriter.h" +#include "ConfigFileReader.h" #include "WinSystemCallFacade.h" namespace NativeMsh @@ -35,7 +36,7 @@ namespace NativeMsh // All these objects will be destroyed when commonFuncs goes out of scope. PwrshExeOutput* output = new PwrshExeOutput(); - PwrshCommon commonFuncs(output, new WinSystemCallFacade()); + PwrshCommon commonFuncs(output, new ConfigFileReader(), new WinSystemCallFacade()); CoreClrHostingApiWrapper hostWrapper; exitCode = commonFuncs.LaunchCoreCLR(&hostWrapper, hostEnvironment, "powershell"); diff --git a/src/powershell-native/nativemsh/pwrshexe/MainEntry.cpp b/src/powershell-native/nativemsh/pwrshexe/MainEntry.cpp index e85bed577..7253d31b6 100644 --- a/src/powershell-native/nativemsh/pwrshexe/MainEntry.cpp +++ b/src/powershell-native/nativemsh/pwrshexe/MainEntry.cpp @@ -26,6 +26,7 @@ #include #include #include "OutputWriter.h" +#include "ConfigFileReader.h" #include "WinSystemCallFacade.h" // include the tlb for mscorlib for access to the default AppDomain through COM Interop @@ -49,7 +50,7 @@ WCHAR g_IconApp[MAX_PATH+1]; // All these objects will be destroyed when pwrshCommon goes out of scope. PwrshExeOutput* pwrshExeOutput = new PwrshExeOutput(); -PwrshCommon pwrshCommon(pwrshExeOutput, new WinSystemCallFacade()); +PwrshCommon pwrshCommon(pwrshExeOutput, new ConfigFileReader(), new WinSystemCallFacade()); bool ConvertArgvToSafeArray( IN int argc, diff --git a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt index fb5f05f82..981d69d82 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt +++ b/src/powershell-native/nativemsh/pwrshplugin/CMakeLists.txt @@ -5,7 +5,8 @@ add_library(pwrshplugin SHARED entrypoints.cpp pwrshclrhost.cpp pwrshpluginerrorcodes.mc - pwrshpluginerrorcodes.rc + #pwrshpluginerrorcodes.rc + pwrshpluginResources.rc pwrshplugin.def ) @@ -40,7 +41,7 @@ else () # CoreCLR lib ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} - ${STATIC_UCRT_LIB} + # ${STATIC_UCRT_LIB} legacy_stdio_definitions.lib # Resolves: LNK2019: unresolved external symbol _vsnwprintf ) set(pwrshplugin_definitions ${common_pwrsh_definitions}) diff --git a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp index 8c0d5890e..054e1f1f5 100644 --- a/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp +++ b/src/powershell-native/nativemsh/pwrshplugin/pwrshclrhost.cpp @@ -130,7 +130,7 @@ PowerShellCoreClrWorker::PowerShellCoreClrWorker( if (NULL == commonLib) { - commonLib = new PwrshCommon(new PwrshPluginOutputDefault(), new WinSystemCallFacade()); + commonLib = new PwrshCommon(new PwrshPluginOutputDefault(), new ConfigFileReader(), new WinSystemCallFacade()); } } @@ -344,7 +344,8 @@ unsigned int PowerShellClrManagedWorker::LoadWorkerCallbackPtrs( // use CreateInstance because we use the assembly strong name (as opposed to CreateInstanceFrom) // - // wszMgdPlugInFileName is %systemdir%\Windows\System32\WindowsPowerShell\v1.0\system.management.automation.dll. + // wszMgdPlugInFileName is system.management.automation.dll within the powershell install path. + // For inbox PowerShell, this is %systemdir%\Windows\System32\WindowsPowerShell\v1.0\system.management.automation.dll (Aka $PSHOME\system.management.automation.dll) _bstr_t bstrConsoleHostAssemblyName = _bstr_t(L"System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); _bstr_t bstrUnmanagedMshEntryClass = _bstr_t(L"System.Management.Automation.Remoting.WSManPluginManagedEntryInstanceWrapper"); From 8ab10c52a95778298fbd93842f7733bd3c97a937 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Mon, 18 Jul 2016 15:04:45 -0700 Subject: [PATCH 7/9] Positioning powershell.exe in Microsoft.PowerShell.ConsoleHost for consumption by Start-DevPowerShell --- build.psm1 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/build.psm1 b/build.psm1 index 5e508fa95..f4e35281a 100644 --- a/build.psm1 +++ b/build.psm1 @@ -247,6 +247,14 @@ cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch log " Executing command: $command" Start-NativeExecution { Invoke-Expression -Command:$command } } + + # Copy the executable binary from the local build directory to the expected destination to enable Start-DevPowerShell to work + # + # TODO: This should be updated to handle per-architecture builds gracefully. + $srcPath = Join-Path (Join-Path (Join-Path (Get-Location) "bin") $msbuildConfiguration) "FullCLR/powershell.exe" + $dstPath = ($script:Options).Top + log " Copying $srcPath to $dstPath" + Copy-Item $srcPath $dstPath } finally { Pop-Location } From f68ec925d06b72c61fc27d0b75227d83e78dcfb9 Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Mon, 18 Jul 2016 15:32:19 -0700 Subject: [PATCH 8/9] Fixing comment support for the ConfigFileReader --- .../nativemsh/pwrshcommon/ConfigFileReader.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp index 4b1bd4b43..f1a13c00f 100644 --- a/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp +++ b/src/powershell-native/nativemsh/pwrshcommon/ConfigFileReader.cpp @@ -33,16 +33,16 @@ namespace NativeMsh std::wstring coreClrDirTag(L"CORECLRDIR="); while (std::getline(psConfigFile, line)) { - std::wstring::const_iterator iter = line.begin(); + ; // Search for the first actionable character in the line to // limit the number of passes that are made iterating through the line. - while(iter != line.end()) + for(std::wstring::const_iterator iter = line.begin(); iter != line.end(); iter++) { if (*iter == L'#') { // Stop parsing the line because the rest of it is a comment - break; + continue; } else if (*iter == L'p' || *iter == L'P') { @@ -72,9 +72,7 @@ namespace NativeMsh } } } - - // Do nothing to ignore unmatched characters (whitespace, etc.) - iter++; + // Else: Do nothing to ignore unmatched characters (whitespace, etc.) } } if (0 == this->pathToPowerShellAssemblies.size() || From ee9418f6b6d7e8bfde5738ba69b344ec7fa2532b Mon Sep 17 00:00:00 2001 From: Mike Richmond Date: Tue, 19 Jul 2016 16:38:40 -0700 Subject: [PATCH 9/9] Cleaning up build.psm1 per code review comments. --- build.psm1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build.psm1 b/build.psm1 index f4e35281a..e546506d4 100644 --- a/build.psm1 +++ b/build.psm1 @@ -99,7 +99,6 @@ function Start-PSBuild { Use-MSBuild #mc.exe is Message Compiler for native resources - # This currently doesn't work reliably and clean systems. Removing for now since mc.exe is not being used yet. $mcexe = Get-ChildItem "${env:ProgramFiles(x86)}\Microsoft SDKs\Windows\" -Recurse -Filter 'mc.exe' | ? {$_.FullName -match 'x64'} | select -First 1 | % {$_.FullName} if (-not $mcexe) { throw 'mc.exe not found. Install Microsoft Windows SDK.' @@ -226,7 +225,7 @@ function Start-PSBuild { @("nativemsh/pwrshplugin") | % { $nativeResourcesFolder = $_ Get-ChildItem $nativeResourcesFolder -Filter "*.mc" | % { - & $mcexe -o -d -c -U $_.FullName -h $nativeResourcesFolder -r $nativeResourcesFolder + Start-NativeExecution { & $mcexe -o -d -c -U $_.FullName -h "$nativeResourcesFolder" -r "$nativeResourcesFolder" } } } @@ -244,7 +243,7 @@ function Start-PSBuild { $command = @" cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" cmake "$overrideFlags" -DBUILD_ONECORE=$oneCoreValue -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration" "@ - log " Executing command: $command" + log " Executing Build Command: $command" Start-NativeExecution { Invoke-Expression -Command:$command } }