vcpkg/ports/vcpkg-msbuild/vcpkg_msbuild_install.cmake
Alexander Neumann 95c0444591
[scripts] add vcpkg-msbuild (#28773)
* add vcpkg-msbuild

* v db

* Apply some suggestions from code review

* revert v db and format manifest

* v db

* fix missing rename

* v db

* revert back to USE_MSVC_SYNTAX_ON_WINDOWS

* v db

* Fix logic

* v db

* Update versions/v-/vcpkg-msbuild.json

* Apply suggestions from code review

Co-authored-by: Javier Matos Denizac <javier.matosd@gmail.com>

* remove include destination since it wasn't used any way

* v db

---------

Co-authored-by: Jonliu1993 <13720414433@163.com>
Co-authored-by: Javier Matos Denizac <javier.matosd@gmail.com>
2023-06-23 12:07:47 -07:00

153 lines
6.3 KiB
CMake

function(vcpkg_msbuild_install)
cmake_parse_arguments(
PARSE_ARGV 0
"arg"
"CLEAN;NO_TOOLCHAIN_PROPS;NO_INSTALL"
"SOURCE_PATH;PROJECT_SUBPATH;RELEASE_CONFIGURATION;DEBUG_CONFIGURATION;PLATFORM;TARGET"
"OPTIONS;OPTIONS_RELEASE;OPTIONS_DEBUG;DEPENDENT_PKGCONFIG;ADDITIONAL_LIBS;ADDITIONAL_LIBS_DEBUG;ADDITIONAL_LIBS_RELEASE"
)
if(DEFINED arg_UNPARSED_ARGUMENTS)
message(WARNING "${CMAKE_CURRENT_FUNCTION} was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
if(NOT DEFINED arg_RELEASE_CONFIGURATION)
set(arg_RELEASE_CONFIGURATION Release)
endif()
if(NOT DEFINED arg_DEBUG_CONFIGURATION)
set(arg_DEBUG_CONFIGURATION Debug)
endif()
if(NOT DEFINED arg_PLATFORM)
if(VCPKG_TARGET_ARCHITECTURE STREQUAL "x64")
set(arg_PLATFORM x64)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "x86")
set(arg_PLATFORM Win32)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm")
set(arg_PLATFORM ARM)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64")
set(arg_PLATFORM arm64)
else()
message(FATAL_ERROR "Unsupported target architecture")
endif()
endif()
if(NOT DEFINED arg_PLATFORM_TOOLSET)
set(arg_PLATFORM_TOOLSET "${VCPKG_PLATFORM_TOOLSET}")
endif()
if(NOT DEFINED arg_TARGET)
set(arg_TARGET Rebuild)
endif()
if(DEFINED arg_ADDITIONAL_LIBS)
list(APPEND arg_ADDITIONAL_LIBS_DEBUG ${arg_ADDITIONAL_LIBS})
list(APPEND arg_ADDITIONAL_LIBS_RELEASE ${arg_ADDITIONAL_LIBS})
endif()
vcpkg_get_windows_sdk(arg_TARGET_PLATFORM_VERSION)
if(NOT arg_NO_TOOLCHAIN_PROPS)
file(RELATIVE_PATH project_root "${arg_SOURCE_PATH}/${arg_PROJECT_SUBPATH}" "${arg_SOURCE_PATH}") # required by z_vcpkg_msbuild_create_props
z_vcpkg_msbuild_create_props(OUTPUT_PROPS props_file
OUTPUT_TARGETS target_file
RELEASE_CONFIGURATION "${arg_RELEASE_CONFIGURATION}"
DEBUG_CONFIGURATION "${arg_DEBUG_CONFIGURATION}"
DEPENDENT_PKGCONFIG ${arg_DEPENDENT_PKGCONFIG}
ADDITIONAL_LIBS_DEBUG ${arg_ADDITIONAL_LIBS_DEBUG}
ADDITIONAL_LIBS_RELEASE ${arg_ADDITIONAL_LIBS_RELEASE})
list(APPEND arg_OPTIONS
"/p:ForceImportAfterCppProps=${props_file}"
"/p:ForceImportAfterCppTargets=${target_file}"
)
endif()
list(APPEND arg_OPTIONS
"/t:${arg_TARGET}"
"/p:UseMultiToolTask=true"
"/p:MultiProcMaxCount=${VCPKG_CONCURRENCY}"
"/p:EnforceProcessCountAcrossBuilds=true"
"/m:${VCPKG_CONCURRENCY}"
"-maxCpuCount:${VCPKG_CONCURRENCY}"
# other Properties
"/p:Platform=${arg_PLATFORM}"
"/p:PlatformToolset=${arg_PLATFORM_TOOLSET}"
"/p:WindowsTargetPlatformVersion=${arg_TARGET_PLATFORM_VERSION}"
# vcpkg properties
"/p:VcpkgApplocalDeps=false"
"/p:VcpkgManifestInstall=false"
"/p:VcpkgManifestEnabled=false"
"/p:VcpkgEnabled=false"
"/p:VcpkgTriplet=${TARGET_TRIPLET}"
"/p:VcpkgInstalledDir=${_VCPKG_INSTALLED_DIR}"
)
if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
# Disable LTCG for static libraries because this setting introduces ABI incompatibility between minor compiler versions
# TODO: Add a way for the user to override this if they want to opt-in to incompatibility
list(APPEND arg_OPTIONS "/p:WholeProgramOptimization=false")
endif()
if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release")
message(STATUS "Building ${arg_PROJECT_SUBPATH} for Release")
set(working_dir "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")
file(REMOVE_RECURSE "${working_dir}")
file(MAKE_DIRECTORY "${working_dir}")
file(COPY "${arg_SOURCE_PATH}/" DESTINATION "${working_dir}")
vcpkg_execute_required_process(
COMMAND msbuild "${working_dir}/${arg_PROJECT_SUBPATH}"
"/p:Configuration=${arg_RELEASE_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_RELEASE}
WORKING_DIRECTORY "${working_dir}"
LOGNAME "build-${TARGET_TRIPLET}-rel"
)
if(NOT arg_NO_INSTALL)
file(GLOB_RECURSE libs "${working_dir}/*.lib")
file(GLOB_RECURSE dlls "${working_dir}/*.dll")
file(GLOB_RECURSE exes "${working_dir}/*.exe")
if(NOT libs STREQUAL "")
file(COPY ${libs} DESTINATION "${CURRENT_PACKAGES_DIR}/lib")
endif()
if(NOT dlls STREQUAL "")
file(COPY ${dlls} DESTINATION "${CURRENT_PACKAGES_DIR}/bin")
endif()
if(NOT exes STREQUAL "")
file(COPY ${exes} DESTINATION "${CURRENT_PACKAGES_DIR}/tools/${PORT}")
vcpkg_copy_tool_dependencies("${CURRENT_PACKAGES_DIR}/tools/${PORT}")
endif()
endif()
endif()
if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
message(STATUS "Building ${arg_PROJECT_SUBPATH} for Debug")
set(working_dir "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")
file(REMOVE_RECURSE "${working_dir}")
file(MAKE_DIRECTORY "${working_dir}")
file(COPY "${arg_SOURCE_PATH}/" DESTINATION "${working_dir}")
vcpkg_execute_required_process(
COMMAND msbuild "${working_dir}/${arg_PROJECT_SUBPATH}"
"/p:Configuration=${arg_DEBUG_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_DEBUG}
WORKING_DIRECTORY "${working_dir}"
LOGNAME "build-${TARGET_TRIPLET}-dbg"
)
if(NOT arg_NO_INSTALL)
file(GLOB_RECURSE libs "${working_dir}/*.lib")
file(GLOB_RECURSE dlls "${working_dir}/*.dll")
if(NOT libs STREQUAL "")
file(COPY ${libs} DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib")
endif()
if(NOT dlls STREQUAL "")
file(COPY ${dlls} DESTINATION "${CURRENT_PACKAGES_DIR}/debug/bin")
endif()
endif()
endif()
vcpkg_copy_pdbs()
if(arg_CLEAN)
vcpkg_clean_msbuild()
endif()
endfunction()