Skip to content

Commit

Permalink
Add exported/installed CMake target
Browse files Browse the repository at this point in the history
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 microsoft#118.
  • Loading branch information
CJKay committed Apr 1, 2020
1 parent 212ecbf commit b53dc5e
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 61 deletions.
71 changes: 65 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
cmake_minimum_required(VERSION 3.11)
project(WIL)

project(WIL
VERSION 0.0.0.0
DESCRIPTION "The Windows Implementation Libraries (wil) were created to improve productivity and solve problems commonly seen by Windows developers."
HOMEPAGE_URL "https://github.com/Microsoft/wil"
LANGUAGES CXX)

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

if (NOT DEFINED WIL_BUILD_VERSION)
set(WIL_BUILD_VERSION "0.0.0")
endif()

# Detect the Windows SDK version. If we're using the Visual Studio generator, this will be provided for us. Otherwise
# we'll need to assume that this value comes from the command line (e.g. through the VS command prompt)
if (DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
Expand All @@ -17,5 +18,63 @@ else()
string(REGEX REPLACE "\\\\$" "" WIL_WINDOWS_SDK_VERSION "$ENV{WindowsSDKVersion}")
endif()

add_subdirectory(packaging)
add_library(WIL INTERFACE)

target_include_directories(WIL
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

install(
DIRECTORY "include/"
DESTINATION "include")

install(
FILES "ThirdPartyNotices.txt"
DESTINATION ".")

# Export the targets for consumption by other CMake projects

include(CMakePackageConfigHelpers)

configure_package_config_file(
"cmake/WILConfig.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/WILConfig.cmake"
INSTALL_DESTINATION "cmake")

write_basic_package_version_file(
"${CMAKE_BINARY_DIR}/cmake/WILConfigVersion.cmake"
COMPATIBILITY ExactVersion ARCH_INDEPENDENT)

install(
FILES
"${CMAKE_BINARY_DIR}/cmake/WILConfig.cmake"
"${CMAKE_BINARY_DIR}/cmake/WILConfigVersion.cmake"
DESTINATION "cmake")

export(TARGETS WIL FILE "cmake/WILTargets.cmake")

install(TARGETS WIL EXPORT WILTargets)
install(EXPORT WILTargets DESTINATION cmake)

# Configure and build the NuGet package

set(CPACK_GENERATOR "NuGet")

set(CPACK_PACKAGE_NAME "Microsoft.Windows.ImplementationLibrary")
set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "")
set(CPACK_PACKAGE_VENDOR "Microsoft")

set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")

set(CPACK_NUGET_PACKAGE_LICENSEURL "https://github.com/microsoft/wil/blob/master/LICENSE")

set(CPACK_NUGET_PACKAGE_TITLE "Windows Implementation Library")
set(CPACK_NUGET_PACKAGE_COPYRIGHT "© Microsoft Corporation. All rights reserved.")
set(CPACK_NUGET_PACKAGE_TAGS "windows utility wil native")

include(CPack)

add_subdirectory(tests)
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ C:\vcpkg> vcpkg install wil:x64-windows
```
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.

## Consuming WIL via CMake

WIL exports and installs a CMake target for use in other CMake projects. You can link your target to WIL with the following:

```cmake
find_package(WIL)
add_library(MyCMakeTarget PUBLIC WIL)
```

If CMake is unable to locate WIL automatically, you can configure `WIL_DIR` to ensure it is found:

```cmd
C:\my\project> cmake -DWIL_DIR=%YOUR_WIL_INSTALL_DIRECTORY%/cmake
```

These instructions can be combined with the package manager instructions above to consume a stable version of WIL from a CMake project.

# Building/Testing
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
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
Expand Down
3 changes: 3 additions & 0 deletions cmake/WILConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/WILTargets.cmake")
2 changes: 0 additions & 2 deletions packaging/CMakeLists.txt

This file was deleted.

20 changes: 0 additions & 20 deletions packaging/nuget/CMakeLists.txt

This file was deleted.

21 changes: 0 additions & 21 deletions packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/init.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ goto :init
set CMAKE_ARGS=%CMAKE_ARGS% -DCMAKE_LINKER=lld-link
)

if "%VERSION%" NEQ "" set CMAKE_ARGS=%CMAKE_ARGS% -DWIL_BUILD_VERSION=%VERSION%
if "%VERSION%" NEQ "" set CMAKE_ARGS=%CMAKE_ARGS% -DCPACK_PACKAGE_VERSION=%VERSION%

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

Expand Down
3 changes: 0 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

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

# All projects need to reference the WIL headers
include_directories(${CMAKE_SOURCE_DIR}/include)

# TODO: Might be worth trying to conditionally do this on SDK version, assuming there's a semi-easy way to detect that
include_directories(BEFORE SYSTEM ./workarounds/wrl)

Expand Down
2 changes: 2 additions & 0 deletions tests/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ target_sources(witest.app PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp
)

target_link_libraries(witest.app PUBLIC WIL)
2 changes: 2 additions & 0 deletions tests/cpplatest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ target_sources(witest.cpplatest PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp
)

target_link_libraries(witest.cpplatest PUBLIC WIL)
2 changes: 2 additions & 0 deletions tests/noexcept/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ target_sources(witest.noexcept PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../WinRTTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp)

target_link_libraries(witest.noexcept PUBLIC WIL)
2 changes: 2 additions & 0 deletions tests/normal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ target_sources(witest PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp
)

target_link_libraries(witest PUBLIC WIL)
2 changes: 2 additions & 0 deletions tests/win7/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ target_sources(witest.win7 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../WistdTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../wiTest.cpp
)

target_link_libraries(witest.win7 PUBLIC WIL)

0 comments on commit b53dc5e

Please sign in to comment.