Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix clrcompression windows build
Browse files Browse the repository at this point in the history
The Open Windows native build of clrcompression was inferior to the one in TFS for a few reasons:
- The Open clrcompression imported vcruntime140.dll
- It did not produce a pdb for either build config
- The linker/compiler flags being used were incorrect or inadequate

Those problems have been resolved. The clrcompression.dll produced in the open is now the same as the one produced internally with the exception that it is linked against the UCRT instead of a fixed msvcrt from TFS. I also took this opportunity to fix the appx package and add the /guard:cf flag which is missing from the TFS clrcompression.
  • Loading branch information
Ian Hays committed Apr 19, 2016
1 parent 7142c6d commit 920fd2f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 60 deletions.
128 changes: 110 additions & 18 deletions src/Native/Windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,37 +1,129 @@
cmake_minimum_required(VERSION 2.8.12)

# C Compiler flags
SET (CMAKE_C_FLAGS_INIT "/W0 /FC")
SET (CMAKE_C_FLAGS_DEBUG_INIT "/Od /Zi")
SET (CMAKE_C_FLAGS_RELEASE_INIT "/Ox")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/O2 /Zi")

# CXX Compiler flags
SET (CMAKE_CXX_FLAGS "-std=c++11")
SET (CMAKE_CXX_FLAGS_INIT "/W0 /FC")
SET (CMAKE_CXX_FLAGS_DEBUG_INIT "/Od /Zi")
SET (CMAKE_CXX_FLAGS_RELEASE_INIT "/Ox")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "/O2 /Zi")

# Configuration of our libray specs and our directories
SET (CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
SET (CMAKE_INCLUDE_CURRENT_DIR ON)
SET (CMAKE_SHARED_LIBRARY_PREFIX "")

# Force an out of source build
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "Binary directory isn't being correctly set before calling Cmake. Tree must be built in separate directory from source.")
endif()

project(CoreFX)

set(CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_SHARED_LIBRARY_PREFIX "")
string(TOUPPER $ENV{CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)

if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5)
add_compile_options(-Wno-unreachable-code)
# The following options are set by the razzle build
add_compile_options(/d2Zi+) # make optimized builds debugging easier
add_compile_options(/nologo) # Suppress Startup Banner
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(/Zi) # enable debugging information
add_compile_options(/Zl) # enable debugging information
add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)

if ($ENV{__BuildArch} STREQUAL "x86")
add_compile_options(/Gz)
endif ()
if(NOT $ENV{__BuildArch} STREQUAL "arm64")
# enable control-flow-guard support for native components for non-Arm64 builds
add_compile_options(/guard:cf)
set(__LinkArgs "${__LinkArgs} /guard:cf")
endif ()

if (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
# 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.
if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
add_compile_options(/MT)
add_compile_options(/GL)
add_compile_options(/O1)
set(__LinkLibraries "${__LinkLibraries} libcmt.lib")
set(__LinkLibraries "${__LinkLibraries} libvcruntime.lib")
set(STATIC_UCRT_LIB "libucrt.lib")
set(DYNAMIC_UCRT_LIB "ucrt.lib")
else()
add_compile_options(/MTd)
set(__LinkLibraries "${__LinkLibraries} libcmtd.lib")
set(__LinkLibraries "${__LinkLibraries} libvcruntimed.lib")
set(STATIC_UCRT_LIB "libucrtd.lib")
set(DYNAMIC_UCRT_LIB "ucrtd.lib")
endif()

# Linker flags
set(__LinkArgs "${__LinkArgs} /INCREMENTAL:NO")
set(__LinkArgs "${__LinkArgs} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
set(__LinkArgs "${__LinkArgs} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
set(__LinkArgs "${__LinkArgs} /RELEASE") #sets the checksum in the header
set(__LinkArgs "${__LinkArgs} /NXCOMPAT") #Compatible with Data Execution Prevention
set(__LinkArgs "${__LinkArgs} /DYNAMICBASE") #Use address space layout randomization
set(__LinkArgs "${__LinkArgs} /DEBUGTYPE:cv,fixup") #debugging format
set(__LinkArgs "${__LinkArgs} /PDBCOMPRESS") #shrink pdb size
set(__LinkArgs "${__LinkArgs} /DEBUG")
set(__LinkArgs "${__LinkArgs} /IGNORE:4197,4013,4254,4070,4221")

if (NOT $ENV{__BuildArch} STREQUAL "arm")
set(__LinkArgs "${__LinkArgs} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem
endif()

if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
# Release build specific flags
set(__LinkArgs "${__LinkArgs} /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")

# Force uCRT to be dynamically linked for Release build
set(__LinkArgs "${__LinkArgs} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")

if ($ENV{__BuildArch} STREQUAL x86)
set(__LinkArgs "${__LinkArgs} /SAFESEH")
endif()
else()
# Debug build specific flags
set(__LinkArgs "/NOVCFEATURE ${__LinkArgs}")
endif()

# Temporary until cmake has VS generators for arm64
if($ENV{__BuildArch} STREQUAL "arm64")
set(__LinkArgs "${__LinkArgs} /machine:arm64")
endif()

if ($ENV{__BuildArch} STREQUAL "x86_64" OR $ENV{__BuildArch} STREQUAL "amd64")
add_definitions(-DBIT64=1)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
add_definitions(-DBIT32=1)
# Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
# we have to set the triple by adding a compiler argument
add_compile_options(-target armv7-linux-gnueabihf)
add_compile_options(-mthumb)
add_compile_options(-mfpu=vfpv3)
endif ()

string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
# Do not define DEBUG. zlib has asserts under DEBUG for non-catastrophic cases,
# such as on bad user-provided inputs. We leave NDEBUG defined, however,
# as other asserts should still be included.
elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
add_definitions(-DNDEBUG)
else ()
message(FATAL_ERROR "Unknown build type. Set CMAKE_BUILD_TYPE to DEBUG or RELEASE.")
Expand Down
34 changes: 16 additions & 18 deletions src/Native/Windows/build-native.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ set __IntermediatesDir=""
set __BuildArch=x64
set __VCBuildArch=x86_amd64
set CMAKE_BUILD_TYPE=Debug
set "__LinkArgs= "
set "__LinkLibraries= "

:Arg_Loop
:: Since the native build requires some configuration information before msbuild is called, we have to do some manual args parsing
:: For consistency with building the managed components, args are taken in the msbuild style i.e. /p:
:: For consistency with building the managed components, some args are taken in the msbuild style i.e. /p:
if [%1] == [] goto :ToolsVersion
if /i [%1] == [/p:ConfigurationGroup] (
if /i [%2] == [Release] (set CMAKE_BUILD_TYPE=Release&&shift&&shift&goto Arg_Loop)
if /i [%2] == [Debug] (set CMAKE_BUILD_TYPE=Debug&&shift&&shift&goto Arg_Loop)
if /i [%2] == [Release] ( set CMAKE_BUILD_TYPE=Release&&shift&&shift&goto Arg_Loop)
if /i [%2] == [Debug] ( set CMAKE_BUILD_TYPE=Debug&&shift&&shift&goto Arg_Loop)
echo Error: Invalid configuration args "%1 and %2"
exit /b 1
)
if /i [%1] == [/p:Platform] (
if /i [%2] == [AnyCPU] (set __BuildArch=x64&&set __VCBuildArch=x86_amd64&&shift&&shift&goto Arg_Loop)
if /i [%2] == [x86] (set __BuildArch=x86&&set __VCBuildArch=x86&&shift&&shift&goto Arg_Loop)
if /i [%2] == [arm] (set __BuildArch=arm&&set __VCBuildArch=x86_arm&&shift&&shift&goto Arg_Loop)
if /i [%2] == [x64] (set __BuildArch=x64&&set __VCBuildArch=x86_amd64&&shift&&shift&goto Arg_Loop)
if /i [%1] == [/p:Platform] (
if /i [%2] == [AnyCPU] ( set __BuildArch=x64&&set __VCBuildArch=x86_amd64&&shift&&shift&goto Arg_Loop)
if /i [%2] == [x86] ( set __BuildArch=x86&&set __VCBuildArch=x86&&shift&&shift&goto Arg_Loop)
if /i [%2] == [arm] ( set __BuildArch=arm&&set __VCBuildArch=x86_arm&&shift&&shift&goto Arg_Loop)
if /i [%2] == [x64] ( set __BuildArch=x64&&set __VCBuildArch=x86_amd64&&shift&&shift&goto Arg_Loop)
if /i [%2] == [amd64] ( set __BuildArch=x64&&set __VCBuildArch=x86_amd64&&shift&&shift&goto Arg_Loop)
echo Error: Invalid platform args "%1 and %2"
exit /b 1
)
if /i [%1] == [-intermediateDir] (
set __IntermediatesDir=%2
)
if /i [%1] == [-binDir] (
set __CMakeBinDir=%2
)
if /i [%1] == [-LinkArgument] ( set "__LinkArgs=%__LinkArgs% %2"&&shift&&shift&goto Arg_Loop)
if /i [%1] == [-LinkLibraries] ( set "__LinkLibraries=%__LinkLibraries% %2"&&shift&&shift&goto Arg_Loop)

shift
goto :Arg_Loop

Expand Down Expand Up @@ -90,10 +90,8 @@ if %__CMakeBinDir% == "" (
if %__IntermediatesDir% == "" (
set "__IntermediatesDir=%__binDir%\obj\Windows_NT.%__BuildArch%.%CMAKE_BUILD_TYPE%\Native"
)
set "__CMakeBinDir=%__CMakeBinDir:\=/%"
set "__IntermediatesDir=%__IntermediatesDir:\=/%"

echo %__CMakeBinDir%
set "__CMakeBinDir=%__CMakeBinDir:\=/%"
set "__IntermediatesDir=%__IntermediatesDir:\=/%"

:: Check that the intermediate directory exists so we can place our cmake build tree there
if exist "%__IntermediatesDir%" rd /s /q "%__IntermediatesDir%"
Expand Down
10 changes: 10 additions & 0 deletions src/Native/Windows/clrcompression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ add_library(clrcompression
${CMAKE_SOURCE_DIR}/../../../Tools/NativeVersion.rc
)

# Allow specification of arguments that should be passed to the linker
SET_TARGET_PROPERTIES(clrcompression PROPERTIES LINK_FLAGS ${__LinkArgs})

# Allow specification of libraries that should be linked against
# CMake doesn't like space delimiters as input to target_link_libraries
separate_arguments(linker_libs_sanitized WINDOWS_COMMAND ${__LinkLibraries})
target_link_libraries(clrcompression ${linker_libs_sanitized})

GENERATE_EXPORT_HEADER( clrcompression
BASE_NAME clrcompression
EXPORT_MACRO_NAME clrcompression_EXPORT
Expand All @@ -74,3 +82,5 @@ GENERATE_EXPORT_HEADER( clrcompression
)

install (TARGETS clrcompression DESTINATION .)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/clrcompression.pdb DESTINATION .)

9 changes: 2 additions & 7 deletions src/Native/Windows/gen-buildsys-win.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@if "%_echo%" neq "on" echo off
rem
rem This file invokes cmake and generates the build system for windows.

set argC=0
for %%x in (%*) do Set /A argC+=1

Expand Down Expand Up @@ -31,7 +32,7 @@ for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy RemoteSigned "&
popd

:DoGen
"%CMakePath%" "-DCMAKE_USER_MAKE_RULES_OVERRIDE=%basePath%/windows-compiler-override.txt" "-DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE%" -G "Visual Studio %__VSString%" -B. -H%1
"%CMakePath%" "-DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE%" "-DCMAKE_INSTALL_PREFIX=%__CMakeBinDir%" -G "Visual Studio %__VSString%" -B. -H%1
endlocal
GOTO :DONE

Expand All @@ -45,9 +46,3 @@ GOTO :DONE

:DONE
EXIT /B 0






15 changes: 0 additions & 15 deletions src/Native/Windows/windows-compiler-override.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
</PropertyGroup>

<ItemGroup>
<!-- use Win10 temporarily as the distinguishing factor, in reality the distinction is app-container vs not. -->
<File Include="$(WinNativePath)..\..\NetCoreForCoreCLR\native\clrcompression.dll">
<TargetPath>runtimes/win10-$(PackagePlatform)/native</TargetPath>
<TargetPath>runtimes/win10-$(PackagePlatform)/lib/netcore50</TargetPath>
</File>
</ItemGroup>

Expand Down

0 comments on commit 920fd2f

Please sign in to comment.