Skip to content

Commit 0e88cf2

Browse files
committed
Add exported/installed CMake target
This commit adds the `WIL` CMake interface library, which allows the project to be exported and installed for consumption by other CMake projects in the standard fashion. The logic to create the NuGet package has been replaced with the built-in CPack NuGet support introduced with CMake 3.13, which automatically packages any files and directories installed by the target. This replaces the `make_wil_nupkg` custom target with the standard CPack `package` target, which builds the package with all the configured package generators (for now that's just NuGet). Note that the original NuGet specification file used the new license style, whereas CPack still generates a NuGet specification with the deprecated style. This will hopefully be resolved by future versions of CPack. Resolves #118.
1 parent c50b3d3 commit 0e88cf2

File tree

14 files changed

+97
-61
lines changed

14 files changed

+97
-61
lines changed

CMakeLists.txt

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
cmake_minimum_required(VERSION 3.11)
2-
project(WIL)
2+
3+
project(WIL
4+
VERSION 0.0.0.0
5+
DESCRIPTION "The Windows Implementation Libraries (wil) were created to improve productivity and solve problems commonly seen by Windows developers."
6+
HOMEPAGE_URL "https://github.com/Microsoft/wil"
7+
LANGUAGES CXX)
38

49
# Set by build server to speed up build/reduce file/object size
510
option(FAST_BUILD "Sets options to speed up build/reduce obj/executable size" OFF)
611

7-
if (NOT DEFINED WIL_BUILD_VERSION)
8-
set(WIL_BUILD_VERSION "0.0.0")
9-
endif()
10-
1112
# Detect the Windows SDK version. If we're using the Visual Studio generator, this will be provided for us. Otherwise
1213
# we'll need to assume that this value comes from the command line (e.g. through the VS command prompt)
1314
if (DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
@@ -17,5 +18,63 @@ else()
1718
string(REGEX REPLACE "\\\\$" "" WIL_WINDOWS_SDK_VERSION "$ENV{WindowsSDKVersion}")
1819
endif()
1920

20-
add_subdirectory(packaging)
21+
add_library(WIL INTERFACE)
22+
23+
target_include_directories(WIL
24+
INTERFACE
25+
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>"
26+
"$<INSTALL_INTERFACE:include>")
27+
28+
install(
29+
DIRECTORY "include/"
30+
DESTINATION "include")
31+
32+
install(
33+
FILES "ThirdPartyNotices.txt"
34+
DESTINATION ".")
35+
36+
# Export the targets for consumption by other CMake projects
37+
38+
include(CMakePackageConfigHelpers)
39+
40+
configure_package_config_file(
41+
"cmake/WILConfig.cmake.in"
42+
"${CMAKE_BINARY_DIR}/cmake/WILConfig.cmake"
43+
INSTALL_DESTINATION "cmake")
44+
45+
write_basic_package_version_file(
46+
"${CMAKE_BINARY_DIR}/cmake/WILConfigVersion.cmake"
47+
COMPATIBILITY ExactVersion ARCH_INDEPENDENT)
48+
49+
install(
50+
FILES
51+
"${CMAKE_BINARY_DIR}/cmake/WILConfig.cmake"
52+
"${CMAKE_BINARY_DIR}/cmake/WILConfigVersion.cmake"
53+
DESTINATION "cmake")
54+
55+
export(TARGETS WIL FILE "cmake/WILTargets.cmake")
56+
57+
install(TARGETS WIL EXPORT WILTargets)
58+
install(EXPORT WILTargets DESTINATION cmake)
59+
60+
# Configure and build the NuGet package
61+
62+
set(CPACK_GENERATOR "NuGet")
63+
64+
set(CPACK_PACKAGE_NAME "Microsoft.Windows.ImplementationLibrary")
65+
set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}")
66+
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "")
67+
set(CPACK_PACKAGE_VENDOR "Microsoft")
68+
69+
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
70+
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
71+
72+
set(CPACK_NUGET_PACKAGE_LICENSEURL "https://github.com/microsoft/wil/blob/master/LICENSE")
73+
74+
set(CPACK_NUGET_PACKAGE_TITLE "Windows Implementation Library")
75+
set(CPACK_NUGET_PACKAGE_COPYRIGHT "© Microsoft Corporation. All rights reserved.")
76+
set(CPACK_NUGET_PACKAGE_TAGS "windows utility wil native")
77+
78+
include(CPack)
79+
2180
add_subdirectory(tests)

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ C:\vcpkg> vcpkg install wil:x64-windows
4747
```
4848
Note that even though WIL is a header-only library, you still need to install the package for all architectures/platforms you wish to use it with. Otherwise, WIL won't be added to the include path for the missing architectures/platforms. Execute `vcpkg help triplet` for a list of available options.
4949

50+
## Consuming WIL via CMake
51+
52+
WIL exports and installs a CMake target for use in other CMake projects. You can link your target to WIL with the following:
53+
54+
```cmake
55+
find_package(WIL)
56+
57+
add_library(MyCMakeTarget PUBLIC WIL)
58+
```
59+
60+
If CMake is unable to locate WIL automatically, you can configure `WIL_DIR` to ensure it is found:
61+
62+
```cmd
63+
C:\my\project> cmake -DWIL_DIR=%YOUR_WIL_INSTALL_DIRECTORY%/cmake
64+
```
65+
66+
These instructions can be combined with the package manager instructions above to consume a stable version of WIL from a CMake project.
67+
5068
# Building/Testing
5169
To get started testing WIL, first make sure that you have a recent version of [Visual Studio](https://visualstudio.microsoft.com/downloads/) and the most recent [Windows SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk) installed. If you are doing
5270
any non-trivial work, also be sure to have a recent version of [Clang](http://releases.llvm.org/download.html) installed. Once everything is installed, open a VS

cmake/WILConfig.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/WILTargets.cmake")

packaging/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

packaging/nuget/CMakeLists.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec

Lines changed: 0 additions & 21 deletions
This file was deleted.

packaging/nuget/Microsoft.Windows.ImplementationLibrary.targets

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/init.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ goto :init
171171
set CMAKE_ARGS=%CMAKE_ARGS% -DCMAKE_LINKER=lld-link
172172
)
173173

174-
if "%VERSION%" NEQ "" set CMAKE_ARGS=%CMAKE_ARGS% -DWIL_BUILD_VERSION=%VERSION%
174+
if "%VERSION%" NEQ "" set CMAKE_ARGS=%CMAKE_ARGS% -DCPACK_PACKAGE_VERSION=%VERSION%
175175

176176
if %FAST_BUILD%==1 set CMAKE_ARGS=%CMAKE_ARGS% -DFAST_BUILD=ON
177177

tests/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
include(${CMAKE_SOURCE_DIR}/cmake/common_build_flags.cmake)
33

4-
# All projects need to reference the WIL headers
5-
include_directories(${CMAKE_SOURCE_DIR}/include)
6-
74
# TODO: Might be worth trying to conditionally do this on SDK version, assuming there's a semi-easy way to detect that
85
include_directories(BEFORE SYSTEM ./workarounds/wrl)
96

tests/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ target_sources(witest.app PUBLIC
1919
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
2020
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp
2121
)
22+
23+
target_link_libraries(witest.app PUBLIC WIL)

0 commit comments

Comments
 (0)