Skip to content

Commit 24cc30e

Browse files
jeremyongnibanks
andauthored
Add initial static library support (Windows Only) (#1446)
* Add initial static library support * Revert change to msquic.lttng target Also adds a missing newline at init.c's EOF * Add whole static lib archiving support * Add static build configuration to windows platforms in build.ps1 and exclude system libs * Invert build.ps1 switch and add documentation * Resolve static link issues with repeated definition of QuicTraceRundown * Add win32 static configurations to the azure pipeline build job lists * Correctly ignore MSVC runtime libraries and fix documentation * Passively create the static lib directly if it doesn't exist In the event that a different output directory is specified, cmake's generator will generally create the directory if it doesn't exist. For the static monolith however, we emit the file with link.exe directly, and this will fail if the parent directory doesn't exist. * Try fix CI * Add STATIC target annotation on perflib and testlib * Reject libs without a valid ARCHIVE_OUTPUT_DIRECTORY path and inject CI cached binaries * Try to fix tests * Force static build of gtest and support statically linked tools * Remove kernel static build mods and handle MsQuicUnload in atexit handlers * Enforce a different means to enforce static builds for gtest artifacts * Fix interop static * One more? * Change static linkage load/unload invocations to lazy lockfree approach * Re-add exclusion list which was needed for the release build * Introduce empty.c file and make msquic_static library a physical target * Add missing InterlockedAnd to advertise the unloading sequence is complete * Inline MsQuicOpen to prevent duplicate symbol issues on Windows * Temp workaround - Just use macro * One more fix Co-authored-by: Nick Banks <[email protected]>
1 parent 200dd6c commit 24cc30e

File tree

18 files changed

+311
-45
lines changed

18 files changed

+311
-45
lines changed

.azure/azure-pipelines.ci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ stages:
9999
tls: openssl
100100
config: Release
101101
extraBuildArgs: -EnableTelemetryAsserts
102+
- template: ./templates/build-config-user.yml
103+
parameters:
104+
image: windows-latest
105+
platform: windows
106+
arch: x64
107+
tls: schannel
108+
config: Release
109+
extraName: 'static'
110+
extraBuildArgs: -EnableTelemetryAsserts -Static -ExtraArtifactDir Static
111+
- template: ./templates/build-config-user.yml
112+
parameters:
113+
image: windows-latest
114+
platform: windows
115+
arch: x64
116+
tls: openssl
117+
config: Release
118+
extraName: 'static'
119+
extraBuildArgs: -EnableTelemetryAsserts -Static -ExtraArtifactDir Static
102120

103121
- stage: build_windows_debug
104122
displayName: Build Windows - Debug
@@ -129,6 +147,24 @@ stages:
129147
tls: openssl
130148
config: Debug
131149
extraBuildArgs: -EnableTelemetryAsserts
150+
- template: ./templates/build-config-user.yml
151+
parameters:
152+
image: windows-latest
153+
platform: windows
154+
arch: x64
155+
tls: schannel
156+
config: Debug
157+
extraName: 'static'
158+
extraBuildArgs: -EnableTelemetryAsserts -Static -ExtraArtifactDir Static
159+
- template: ./templates/build-config-user.yml
160+
parameters:
161+
image: windows-latest
162+
platform: windows
163+
arch: x64
164+
tls: openssl
165+
config: Debug
166+
extraName: 'static'
167+
extraBuildArgs: -EnableTelemetryAsserts -Static -ExtraArtifactDir Static
132168

133169
- stage: build_windows_nontest
134170
displayName: Build Windows - Non Tested

CMakeLists.txt

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ endif()
113113
option(QUIC_BUILD_TOOLS "Builds the tools code" ON)
114114
option(QUIC_BUILD_TEST "Builds the test code" ON)
115115
option(QUIC_BUILD_PERF "Builds the perf code" ON)
116+
option(QUIC_BUILD_SHARED "Builds msquic as a dynamic library" ON)
116117
option(QUIC_ENABLE_LOGGING "Enables logging" OFF)
117118
option(QUIC_ENABLE_SANITIZERS "Enables sanitizers" OFF)
118119
option(QUIC_STATIC_LINK_CRT "Statically links the C runtime" ON)
@@ -127,6 +128,9 @@ option(QUIC_TLS_SECRETS_SUPPORT "Enable export of TLS secrets" OFF)
127128
option(QUIC_TELEMETRY_ASSERTS "Enable telemetry asserts in release builds" OFF)
128129
option(QUIC_USE_SYSTEM_LIBCRYPTO "Use system libcrypto if openssl TLS" OFF)
129130
option(QUIC_DISABLE_POSIX_GSO "Disable GSO for systems that say they support it but don't" OFF)
131+
option(QUIC_FOLDER_PREFIX "Optional prefix for source group folders when using an IDE generator" "")
132+
133+
set(BUILD_SHARED_LIBS ${QUIC_BUILD_SHARED})
130134

131135
# FindLTTngUST does not exist before CMake 3.6, so disable logging for older cmake versions
132136
if (${CMAKE_VERSION} VERSION_LESS "3.6.0")
@@ -252,14 +256,14 @@ if(WIN32)
252256
add_custom_target(MsQuicEtw_HeaderBuild
253257
DEPENDS ${QUIC_BUILD_DIR}/inc/MsQuicEtw.h)
254258

255-
set_property(TARGET MsQuicEtw_HeaderBuild PROPERTY FOLDER "helpers")
259+
set_property(TARGET MsQuicEtw_HeaderBuild PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}helpers")
256260

257261
add_library(MsQuicEtw_Header INTERFACE)
258262
target_include_directories(MsQuicEtw_Header INTERFACE ${QUIC_BUILD_DIR}/inc)
259263
add_dependencies(MsQuicEtw_Header MsQuicEtw_HeaderBuild)
260264

261265
add_library(MsQuicEtw_Resource OBJECT ${QUIC_BUILD_DIR}/inc/MsQuicEtw.rc)
262-
set_property(TARGET MsQuicEtw_Resource PROPERTY FOLDER "helpers")
266+
set_property(TARGET MsQuicEtw_Resource PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}helpers")
263267

264268
message(STATUS "Disabling (client) shared port support")
265269
list(APPEND QUIC_COMMON_DEFINES QUIC_DISABLE_SHARED_PORT_TESTS)
@@ -403,7 +407,7 @@ if(QUIC_TLS STREQUAL "openssl")
403407
set(LIBCRYPTO_PATH ${OPENSSL_DIR}/release/lib/libcrypto${CMAKE_STATIC_LIBRARY_SUFFIX})
404408

405409
add_library(OpenSSL_Crypto STATIC IMPORTED)
406-
set_property(TARGET OpenSSL_Crypto PROPERTY FOLDER "libraries")
410+
set_property(TARGET OpenSSL_Crypto PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}libraries")
407411
set_target_properties(OpenSSL_Crypto PROPERTIES
408412
IMPORTED_LOCATION_DEBUG ${LIBCRYPTO_DEBUG_PATH}
409413
IMPORTED_LOCATION_RELEASE ${LIBCRYPTO_PATH}
@@ -412,7 +416,7 @@ if(QUIC_TLS STREQUAL "openssl")
412416
MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)
413417

414418
add_library(OpenSSL_SSL STATIC IMPORTED)
415-
set_property(TARGET OpenSSL_SSL PROPERTY FOLDER "libraries")
419+
set_property(TARGET OpenSSL_SSL PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}libraries")
416420
set_target_properties(OpenSSL_SSL PROPERTIES
417421
IMPORTED_LOCATION_DEBUG ${LIBSSL_DEBUG_PATH}
418422
IMPORTED_LOCATION_RELEASE ${LIBSSL_PATH}
@@ -548,7 +552,7 @@ if (QUIC_ENABLE_LOGGING)
548552
function(add_clog_library)
549553
CLOG_GENERATE_TARGET(${ARGV})
550554
target_link_libraries(${ARGV0} PRIVATE inc)
551-
set_property(TARGET ${ARGV0} PROPERTY FOLDER "helpers")
555+
set_property(TARGET ${ARGV0} PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}helpers")
552556
endfunction()
553557
else()
554558
function(add_clog_library)
@@ -621,21 +625,28 @@ endif()
621625

622626
# Test code
623627
if(QUIC_BUILD_TEST)
624-
# Build the googletest framework.
625-
set(BUILD_GMOCK OFF CACHE BOOL "Builds the googlemock subproject")
626-
set(INSTALL_GTEST OFF CACHE BOOL "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)")
627-
if(WIN32)
628-
option(gtest_force_shared_crt "Use shared (DLL) run-time lib even when Google Test is built as static lib." ON)
629-
endif()
628+
include(FetchContent)
630629

631630
enable_testing()
632-
add_subdirectory(submodules/googletest)
631+
632+
# Build the googletest framework.
633+
634+
# Enforce static builds for test artifacts
635+
set(PREV_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS} CACHE INTERNAL "")
636+
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "")
637+
FetchContent_Declare(
638+
googletest
639+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/submodules/googletest
640+
CMAKE_ARGS "-DBUILD_GMOCK=OFF -DINSTALL_GTEST=OFF -Dgtest_force_shared_crt=ON"
641+
)
642+
FetchContent_MakeAvailable(googletest)
643+
set(BUILD_SHARED_LIBS ${PREV_BUILD_SHARED_LIBS} CACHE INTERNAL "")
633644

634645
set_property(TARGET gtest PROPERTY CXX_STANDARD 17)
635646
set_property(TARGET gtest_main PROPERTY CXX_STANDARD 17)
636647

637-
set_property(TARGET gtest PROPERTY FOLDER "tests")
638-
set_property(TARGET gtest_main PROPERTY FOLDER "tests")
648+
set_property(TARGET gtest PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}tests")
649+
set_property(TARGET gtest_main PROPERTY FOLDER "${QUIC_FOLDER_PREFIX}tests")
639650

640651
add_subdirectory(src/core/unittest)
641652
add_subdirectory(src/platform/unittest)

docs/BUILD.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ The script has a lot of additional configuration options, but the default should
129129

130130
`-Arch <x86/x64/arm/arm64>` Allow for building for different architectures. **x64** is the defualt architecture.
131131

132+
`-Static` Compiles msquic as a monolithic statically linkable library.
133+
Supported only by Windows currently.
134+
132135
`-Tls <schannel/openssl>` Allows for building with different TLS providers. The default is platform dependent (Windows = schannel, Linux = openssl).
133136

134137
`-Clean` Forces a clean build of everything.

scripts/build.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ This script provides helpers for building msquic.
1010
The CPU architecture to build for.
1111
1212
.PARAMETER Platform
13-
Specify which platform to build for
13+
Specify which platform to build for.
14+
15+
.PARAMETER Static
16+
Specify a static library is preferred (shared is the default).
1417
1518
.PARAMETER Tls
1619
The TLS library to use.
1720
1821
.PARAMETER ToolchainFile
19-
Toolchain file to use (if cross)
22+
Toolchain file to use (if cross).
2023
2124
.PARAMETER DisableLogs
2225
Disables log collection.
@@ -105,6 +108,9 @@ param (
105108
[ValidateSet("uwp", "windows", "linux", "macos")] # For future expansion
106109
[string]$Platform = "",
107110

111+
[Parameter(Mandatory = $false)]
112+
[switch]$Static = $false,
113+
108114
[Parameter(Mandatory = $false)]
109115
[ValidateSet("schannel", "openssl")]
110116
[string]$Tls = "",
@@ -199,6 +205,11 @@ if (!$IsWindows -And $Platform -eq "uwp") {
199205
exit
200206
}
201207

208+
if (!$IsWindows -And $Static) {
209+
Write-Error "[$(Get-Date)] Static linkage on non windows platforms not yet supported"
210+
exit
211+
}
212+
202213
# Root directory of the project.
203214
$RootDir = Split-Path $PSScriptRoot -Parent
204215

@@ -270,6 +281,9 @@ function CMake-Generate {
270281
} else {
271282
$Arguments += " $Generator"
272283
}
284+
if($Static) {
285+
$Arguments += " -DQUIC_BUILD_SHARED=off"
286+
}
273287
$Arguments += " -DQUIC_TLS=" + $Tls
274288
$Arguments += " -DQUIC_OUTPUT_DIR=" + $ArtifactsDir
275289
if (!$DisableLogs) {

0 commit comments

Comments
 (0)