From 0e88cf220cada5de70a4f7f882a3371bc1bb43a2 Mon Sep 17 00:00:00 2001 From: Chris Kay Date: Wed, 1 Apr 2020 01:18:41 +0100 Subject: [PATCH] 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. --- CMakeLists.txt | 71 +++++++++++++++++-- README.md | 18 +++++ cmake/WILConfig.cmake.in | 3 + packaging/CMakeLists.txt | 2 - packaging/nuget/CMakeLists.txt | 20 ------ ...osoft.Windows.ImplementationLibrary.nuspec | 21 ------ ...soft.Windows.ImplementationLibrary.targets | 8 --- scripts/init.cmd | 2 +- tests/CMakeLists.txt | 3 - tests/app/CMakeLists.txt | 2 + tests/cpplatest/CMakeLists.txt | 2 + tests/noexcept/CMakeLists.txt | 2 + tests/normal/CMakeLists.txt | 2 + tests/win7/CMakeLists.txt | 2 + 14 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 cmake/WILConfig.cmake.in delete mode 100644 packaging/CMakeLists.txt delete mode 100644 packaging/nuget/CMakeLists.txt delete mode 100644 packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec delete mode 100644 packaging/nuget/Microsoft.Windows.ImplementationLibrary.targets diff --git a/CMakeLists.txt b/CMakeLists.txt index 27765fd1..182bd9d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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 + "$" + "$") + +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) diff --git a/README.md b/README.md index 8d4e538f..f4251f84 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmake/WILConfig.cmake.in b/cmake/WILConfig.cmake.in new file mode 100644 index 00000000..b871d90c --- /dev/null +++ b/cmake/WILConfig.cmake.in @@ -0,0 +1,3 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/WILTargets.cmake") diff --git a/packaging/CMakeLists.txt b/packaging/CMakeLists.txt deleted file mode 100644 index 1f568847..00000000 --- a/packaging/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ - -add_subdirectory(nuget) diff --git a/packaging/nuget/CMakeLists.txt b/packaging/nuget/CMakeLists.txt deleted file mode 100644 index f3fd938b..00000000 --- a/packaging/nuget/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ - -file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/build_tools/nuget.exe" nuget_exe) -file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}" nupkg_dir) -file(TO_NATIVE_PATH "${nupkg_dir}/Microsoft.Windows.ImplementationLibrary.${WIL_BUILD_VERSION}.nupkg" wil_nupkg) - -# The build servers don't have an up-to-date version of nuget, so pull it down ourselves... -file(DOWNLOAD https://dist.nuget.org/win-x86-commandline/latest/nuget.exe ${nuget_exe}) - -file(GLOB_RECURSE wil_headers ${CMAKE_SOURCE_DIR}/include/*.h) - -add_custom_command(OUTPUT ${wil_nupkg} - COMMAND ${nuget_exe} pack ${CMAKE_CURRENT_SOURCE_DIR}/Microsoft.Windows.ImplementationLibrary.nuspec -OutputDirectory ${nupkg_dir} -Version ${WIL_BUILD_VERSION} -NonInteractive - DEPENDS - ${CMAKE_CURRENT_SOURCE_DIR}/Microsoft.Windows.ImplementationLibrary.nuspec - ${CMAKE_CURRENT_SOURCE_DIR}/Microsoft.Windows.ImplementationLibrary.targets - ${wil_headers} - ${CMAKE_SOURCE_DIR}/LICENSE - ${CMAKE_SOURCE_DIR}/ThirdPartyNotices.txt) - -add_custom_target(make_wil_nupkg DEPENDS ${wil_nupkg}) diff --git a/packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec b/packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec deleted file mode 100644 index be709b9c..00000000 --- a/packaging/nuget/Microsoft.Windows.ImplementationLibrary.nuspec +++ /dev/null @@ -1,21 +0,0 @@ - - - - Microsoft.Windows.ImplementationLibrary - 0.0.0 - Windows Implementation Library - Microsoft - false - The Windows Implementation Libraries (wil) were created to improve productivity and solve problems commonly seen by Windows developers. - windows utility wil native - © Microsoft Corporation. All rights reserved. - LICENSE - https://github.com/Microsoft/wil - - - - - - - - \ No newline at end of file diff --git a/packaging/nuget/Microsoft.Windows.ImplementationLibrary.targets b/packaging/nuget/Microsoft.Windows.ImplementationLibrary.targets deleted file mode 100644 index 29d756a9..00000000 --- a/packaging/nuget/Microsoft.Windows.ImplementationLibrary.targets +++ /dev/null @@ -1,8 +0,0 @@ - - - - - $(MSBuildThisFileDirectory)..\..\include\;%(AdditionalIncludeDirectories) - - - diff --git a/scripts/init.cmd b/scripts/init.cmd index cc8efbfb..92111946 100644 --- a/scripts/init.cmd +++ b/scripts/init.cmd @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bd4e4f55..69702ee1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/app/CMakeLists.txt b/tests/app/CMakeLists.txt index 1118f235..f57bddc1 100644 --- a/tests/app/CMakeLists.txt +++ b/tests/app/CMakeLists.txt @@ -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) diff --git a/tests/cpplatest/CMakeLists.txt b/tests/cpplatest/CMakeLists.txt index 22e45cc9..e3e4e904 100644 --- a/tests/cpplatest/CMakeLists.txt +++ b/tests/cpplatest/CMakeLists.txt @@ -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) diff --git a/tests/noexcept/CMakeLists.txt b/tests/noexcept/CMakeLists.txt index 76feacdd..5c3e9b22 100644 --- a/tests/noexcept/CMakeLists.txt +++ b/tests/noexcept/CMakeLists.txt @@ -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) diff --git a/tests/normal/CMakeLists.txt b/tests/normal/CMakeLists.txt index b146e8e4..e1aba834 100644 --- a/tests/normal/CMakeLists.txt +++ b/tests/normal/CMakeLists.txt @@ -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) diff --git a/tests/win7/CMakeLists.txt b/tests/win7/CMakeLists.txt index 8eebc498..7bd0ec57 100644 --- a/tests/win7/CMakeLists.txt +++ b/tests/win7/CMakeLists.txt @@ -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)