Skip to content

Commit

Permalink
[scripts-audit] simple msbuild-buildsystem changes (microsoft#17779)
Browse files Browse the repository at this point in the history
* [scripts-audit] simple msbuild-buildsystem changes

* warn on extra args in vcpkg_clean_msbuild

* fix variable name, and quotes expansion

* ROOT_INCLUDES -> root_includes
  • Loading branch information
strega-nil authored May 17, 2021
1 parent 253c561 commit cae98be
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 132 deletions.
93 changes: 48 additions & 45 deletions scripts/cmake/vcpkg_build_msbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,86 +63,89 @@ Additional options passed to msbuild for Debug builds. These are in addition to
#]===]

function(vcpkg_build_msbuild)
# parse parameters such that semicolons in options arguments to COMMAND don't get erased
cmake_parse_arguments(
PARSE_ARGV 0
_csc
arg
"USE_VCPKG_INTEGRATION"
"PROJECT_PATH;RELEASE_CONFIGURATION;DEBUG_CONFIGURATION;PLATFORM;PLATFORM_TOOLSET;TARGET_PLATFORM_VERSION;TARGET"
"OPTIONS;OPTIONS_RELEASE;OPTIONS_DEBUG"
)

if(NOT DEFINED _csc_RELEASE_CONFIGURATION)
set(_csc_RELEASE_CONFIGURATION Release)
if(DEFINED arg_UNPARSED_ARGUMENTS)
message(WARNING "vcpkg_build_msbuild was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
if(NOT DEFINED _csc_DEBUG_CONFIGURATION)
set(_csc_DEBUG_CONFIGURATION Debug)

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 _csc_PLATFORM)
set(_csc_PLATFORM ${TRIPLET_SYSTEM_ARCH})
if(NOT DEFINED arg_PLATFORM)
set(arg_PLATFORM "${TRIPLET_SYSTEM_ARCH}")
endif()
if(NOT DEFINED _csc_PLATFORM_TOOLSET)
set(_csc_PLATFORM_TOOLSET ${VCPKG_PLATFORM_TOOLSET})
if(NOT DEFINED arg_PLATFORM_TOOLSET)
set(arg_PLATFORM_TOOLSET "${VCPKG_PLATFORM_TOOLSET}")
endif()
if(NOT DEFINED _csc_TARGET_PLATFORM_VERSION)
vcpkg_get_windows_sdk(_csc_TARGET_PLATFORM_VERSION)
if(NOT DEFINED arg_TARGET_PLATFORM_VERSION)
vcpkg_get_windows_sdk(arg_TARGET_PLATFORM_VERSION)
endif()
if(NOT DEFINED _csc_TARGET)
set(_csc_TARGET Rebuild)
if(NOT DEFINED arg_TARGET)
set(arg_TARGET Rebuild)
endif()

list(APPEND _csc_OPTIONS
/t:${_csc_TARGET}
/p:Platform=${_csc_PLATFORM}
/p:PlatformToolset=${_csc_PLATFORM_TOOLSET}
/p:VCPkgLocalAppDataDisabled=true
/p:UseIntelMKL=No
/p:WindowsTargetPlatformVersion=${_csc_TARGET_PLATFORM_VERSION}
/p:VcpkgManifestInstall=false
/p:VcpkgManifestEnabled=false
/m
list(APPEND arg_OPTIONS
"/t:${arg_TARGET}"
"/p:Platform=${arg_PLATFORM}"
"/p:PlatformToolset=${arg_PLATFORM_TOOLSET}"
"/p:VCPkgLocalAppDataDisabled=true"
"/p:UseIntelMKL=No"
"/p:WindowsTargetPlatformVersion=${arg_TARGET_PLATFORM_VERSION}"
"/p:VcpkgManifestInstall=false"
"/p:VcpkgManifestEnabled=false"
"/m"
)

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 _csc_OPTIONS /p:WholeProgramOptimization=false)
list(APPEND arg_OPTIONS "/p:WholeProgramOptimization=false")
endif()

if(_csc_USE_VCPKG_INTEGRATION)
if(arg_USE_VCPKG_INTEGRATION)
list(
APPEND _csc_OPTIONS
/p:ForceImportBeforeCppTargets=${SCRIPTS}/buildsystems/msbuild/vcpkg.targets
APPEND arg_OPTIONS
"/p:ForceImportBeforeCppTargets=${SCRIPTS}/buildsystems/msbuild/vcpkg.targets"
"/p:VcpkgTriplet=${TARGET_TRIPLET}"
"/p:VcpkgInstalledDir=${_VCPKG_INSTALLED_DIR}"
)
else()
list(APPEND _csc_OPTIONS "/p:VcpkgEnabled=false")
list(APPEND arg_OPTIONS "/p:VcpkgEnabled=false")
endif()

if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release")
message(STATUS "Building ${_csc_PROJECT_PATH} for Release")
file(MAKE_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel)
message(STATUS "Building ${arg_PROJECT_PATH} for Release")
file(MAKE_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")
vcpkg_execute_required_process(
COMMAND msbuild ${_csc_PROJECT_PATH}
/p:Configuration=${_csc_RELEASE_CONFIGURATION}
${_csc_OPTIONS}
${_csc_OPTIONS_RELEASE}
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel
LOGNAME build-${TARGET_TRIPLET}-rel
COMMAND msbuild "${arg_PROJECT_PATH}"
"/p:Configuration=${arg_RELEASE_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_RELEASE}
WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel"
LOGNAME "build-${TARGET_TRIPLET}-rel"
)
endif()

if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
message(STATUS "Building ${_csc_PROJECT_PATH} for Debug")
file(MAKE_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg)
message(STATUS "Building ${arg_PROJECT_PATH} for Debug")
file(MAKE_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")
vcpkg_execute_required_process(
COMMAND msbuild ${_csc_PROJECT_PATH}
/p:Configuration=${_csc_DEBUG_CONFIGURATION}
${_csc_OPTIONS}
${_csc_OPTIONS_DEBUG}
WORKING_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg
LOGNAME build-${TARGET_TRIPLET}-dbg
COMMAND msbuild "${arg_PROJECT_PATH}"
"/p:Configuration=${arg_DEBUG_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_DEBUG}
WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg"
LOGNAME "build-${TARGET_TRIPLET}-dbg"
)
endif()
endfunction()
7 changes: 5 additions & 2 deletions scripts/cmake/vcpkg_clean_msbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ vcpkg_clean_msbuild()
#]===]

function(vcpkg_clean_msbuild)
if(NOT ARGC EQUAL 0)
message(WARNING "vcpkg_clean_msbuild was passed extra arguments: ${ARGV}")
endif()
file(REMOVE_RECURSE
${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg
${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel
"${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg"
"${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel"
)
endfunction()
181 changes: 96 additions & 85 deletions scripts/cmake/vcpkg_install_msbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,144 +91,155 @@ Additional options passed to msbuild for Debug builds. These are in addition to
* [libimobiledevice](https://github.com/Microsoft/vcpkg/blob/master/ports/libimobiledevice/portfile.cmake)
#]===]

include(vcpkg_clean_msbuild)

function(vcpkg_install_msbuild)
# parse parameters such that semicolons in options arguments to COMMAND don't get erased
cmake_parse_arguments(
PARSE_ARGV 0
_csc
"arg"
"USE_VCPKG_INTEGRATION;ALLOW_ROOT_INCLUDES;REMOVE_ROOT_INCLUDES;SKIP_CLEAN"
"SOURCE_PATH;PROJECT_SUBPATH;INCLUDES_SUBPATH;LICENSE_SUBPATH;RELEASE_CONFIGURATION;DEBUG_CONFIGURATION;PLATFORM;PLATFORM_TOOLSET;TARGET_PLATFORM_VERSION;TARGET"
"OPTIONS;OPTIONS_RELEASE;OPTIONS_DEBUG"
)

if(NOT DEFINED _csc_RELEASE_CONFIGURATION)
set(_csc_RELEASE_CONFIGURATION Release)
if(DEFINED arg_UNPARSED_ARGUMENTS)
message(WARNING "vcpkg_install_msbuild was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()

if(NOT DEFINED arg_RELEASE_CONFIGURATION)
set(arg_RELEASE_CONFIGURATION Release)
endif()
if(NOT DEFINED _csc_DEBUG_CONFIGURATION)
set(_csc_DEBUG_CONFIGURATION Debug)
if(NOT DEFINED arg_DEBUG_CONFIGURATION)
set(arg_DEBUG_CONFIGURATION Debug)
endif()
if(NOT DEFINED _csc_PLATFORM)
if(VCPKG_TARGET_ARCHITECTURE STREQUAL x64)
set(_csc_PLATFORM x64)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL x86)
set(_csc_PLATFORM Win32)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL ARM)
set(_csc_PLATFORM ARM)
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL arm64)
set(_csc_PLATFORM arm64)
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 _csc_PLATFORM_TOOLSET)
set(_csc_PLATFORM_TOOLSET ${VCPKG_PLATFORM_TOOLSET})
if(NOT DEFINED arg_PLATFORM_TOOLSET)
set(arg_PLATFORM_TOOLSET "${VCPKG_PLATFORM_TOOLSET}")
endif()
if(NOT DEFINED _csc_TARGET_PLATFORM_VERSION)
vcpkg_get_windows_sdk(_csc_TARGET_PLATFORM_VERSION)
if(NOT DEFINED arg_TARGET_PLATFORM_VERSION)
vcpkg_get_windows_sdk(arg_TARGET_PLATFORM_VERSION)
endif()
if(NOT DEFINED _csc_TARGET)
set(_csc_TARGET Rebuild)
if(NOT DEFINED arg_TARGET)
set(arg_TARGET Rebuild)
endif()

list(APPEND _csc_OPTIONS
/t:${_csc_TARGET}
/p:Platform=${_csc_PLATFORM}
/p:PlatformToolset=${_csc_PLATFORM_TOOLSET}
/p:VCPkgLocalAppDataDisabled=true
/p:UseIntelMKL=No
/p:WindowsTargetPlatformVersion=${_csc_TARGET_PLATFORM_VERSION}
/p:VcpkgTriplet=${TARGET_TRIPLET}
list(APPEND arg_OPTIONS
"/t:${arg_TARGET}"
"/p:Platform=${arg_PLATFORM}"
"/p:PlatformToolset=${arg_PLATFORM_TOOLSET}"
"/p:VCPkgLocalAppDataDisabled=true"
"/p:UseIntelMKL=No"
"/p:WindowsTargetPlatformVersion=${arg_TARGET_PLATFORM_VERSION}"
"/p:VcpkgTriplet=${TARGET_TRIPLET}"
"/p:VcpkgInstalledDir=${_VCPKG_INSTALLED_DIR}"
/p:VcpkgManifestInstall=false
/m
"/p:VcpkgManifestInstall=false"
"/m"
)

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 _csc_OPTIONS /p:WholeProgramOptimization=false)
list(APPEND arg_OPTIONS "/p:WholeProgramOptimization=false")
endif()

if(_csc_USE_VCPKG_INTEGRATION)
list(APPEND _csc_OPTIONS /p:ForceImportBeforeCppTargets=${SCRIPTS}/buildsystems/msbuild/vcpkg.targets /p:VcpkgApplocalDeps=false)
if(arg_USE_VCPKG_INTEGRATION)
list(APPEND arg_OPTIONS
"/p:ForceImportBeforeCppTargets=${SCRIPTS}/buildsystems/msbuild/vcpkg.targets"
"/p:VcpkgApplocalDeps=false"
)
endif()

get_filename_component(SOURCE_PATH_SUFFIX "${_csc_SOURCE_PATH}" NAME)
get_filename_component(source_path_suffix "${arg_SOURCE_PATH}" NAME)
if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release")
message(STATUS "Building ${_csc_PROJECT_SUBPATH} for Release")
file(REMOVE_RECURSE ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel)
file(MAKE_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel)
file(COPY ${_csc_SOURCE_PATH} DESTINATION ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel)
set(SOURCE_COPY_PATH ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel/${SOURCE_PATH_SUFFIX})
message(STATUS "Building ${arg_PROJECT_SUBPATH} for Release")
file(REMOVE_RECURSE "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")
file(MAKE_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")
file(COPY "${arg_SOURCE_PATH}" DESTINATION "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")
set(source_copy_path "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel/${source_path_suffix}")
vcpkg_execute_required_process(
COMMAND msbuild ${SOURCE_COPY_PATH}/${_csc_PROJECT_SUBPATH}
/p:Configuration=${_csc_RELEASE_CONFIGURATION}
${_csc_OPTIONS}
${_csc_OPTIONS_RELEASE}
WORKING_DIRECTORY ${SOURCE_COPY_PATH}
LOGNAME build-${TARGET_TRIPLET}-rel
COMMAND msbuild "${source_copy_path}/${arg_PROJECT_SUBPATH}"
"/p:Configuration=${arg_RELEASE_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_RELEASE}
WORKING_DIRECTORY "${source_copy_path}"
LOGNAME "build-${TARGET_TRIPLET}-rel"
)
file(GLOB_RECURSE LIBS ${SOURCE_COPY_PATH}/*.lib)
file(GLOB_RECURSE DLLS ${SOURCE_COPY_PATH}/*.dll)
file(GLOB_RECURSE EXES ${SOURCE_COPY_PATH}/*.exe)
if(LIBS)
file(COPY ${LIBS} DESTINATION ${CURRENT_PACKAGES_DIR}/lib)
file(GLOB_RECURSE libs "${source_copy_path}/*.lib")
file(GLOB_RECURSE dlls "${source_copy_path}/*.dll")
file(GLOB_RECURSE exes "${source_copy_path}/*.exe")
if(NOT libs STREQUAL "")
file(COPY ${libs} DESTINATION "${CURRENT_PACKAGES_DIR}/lib")
endif()
if(DLLS)
file(COPY ${DLLS} DESTINATION ${CURRENT_PACKAGES_DIR}/bin)
if(NOT dlls STREQUAL "")
file(COPY ${dlls} DESTINATION "${CURRENT_PACKAGES_DIR}/bin")
endif()
if(EXES)
file(COPY ${EXES} DESTINATION ${CURRENT_PACKAGES_DIR}/tools/${PORT})
vcpkg_copy_tool_dependencies(${CURRENT_PACKAGES_DIR}/tools/${PORT})
if(NOT exes STREQUAL "")
file(COPY ${exes} DESTINATION "${CURRENT_PACKAGES_DIR}/tools/${PORT}")
vcpkg_copy_tool_dependencies("${CURRENT_PACKAGES_DIR}/tools/${PORT}")
endif()
endif()

if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
message(STATUS "Building ${_csc_PROJECT_SUBPATH} for Debug")
file(REMOVE_RECURSE ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg)
file(MAKE_DIRECTORY ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg)
file(COPY ${_csc_SOURCE_PATH} DESTINATION ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg)
set(SOURCE_COPY_PATH ${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg/${SOURCE_PATH_SUFFIX})
message(STATUS "Building ${arg_PROJECT_SUBPATH} for Debug")
file(REMOVE_RECURSE "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")
file(MAKE_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")
file(COPY "${arg_SOURCE_PATH}" DESTINATION "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")
set(source_copy_path "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg/${source_path_suffix}")
vcpkg_execute_required_process(
COMMAND msbuild ${SOURCE_COPY_PATH}/${_csc_PROJECT_SUBPATH}
/p:Configuration=${_csc_DEBUG_CONFIGURATION}
${_csc_OPTIONS}
${_csc_OPTIONS_DEBUG}
WORKING_DIRECTORY ${SOURCE_COPY_PATH}
LOGNAME build-${TARGET_TRIPLET}-dbg
COMMAND msbuild "${source_copy_path}/${arg_PROJECT_SUBPATH}"
"/p:Configuration=${arg_DEBUG_CONFIGURATION}"
${arg_OPTIONS}
${arg_OPTIONS_DEBUG}
WORKING_DIRECTORY "${source_copy_path}"
LOGNAME "build-${TARGET_TRIPLET}-dbg"
)
file(GLOB_RECURSE LIBS ${SOURCE_COPY_PATH}/*.lib)
file(GLOB_RECURSE DLLS ${SOURCE_COPY_PATH}/*.dll)
if(LIBS)
file(COPY ${LIBS} DESTINATION ${CURRENT_PACKAGES_DIR}/debug/lib)
file(GLOB_RECURSE libs "${source_copy_path}/*.lib")
file(GLOB_RECURSE dlls "${source_copy_path}/*.dll")
if(NOT libs STREQUAL "")
file(COPY ${libs} DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib")
endif()
if(DLLS)
file(COPY ${DLLS} DESTINATION ${CURRENT_PACKAGES_DIR}/debug/bin)
if(NOT dlls STREQUAL "")
file(COPY ${dlls} DESTINATION "${CURRENT_PACKAGES_DIR}/debug/bin")
endif()
endif()

vcpkg_copy_pdbs()

if(NOT _csc_SKIP_CLEAN)
if(NOT arg_SKIP_CLEAN)
vcpkg_clean_msbuild()
endif()

if(DEFINED _csc_INCLUDES_SUBPATH)
file(COPY ${_csc_SOURCE_PATH}/${_csc_INCLUDES_SUBPATH}/ DESTINATION ${CURRENT_PACKAGES_DIR}/include/)
file(GLOB ROOT_INCLUDES LIST_DIRECTORIES false ${CURRENT_PACKAGES_DIR}/include/*)
if(ROOT_INCLUDES)
if(_csc_REMOVE_ROOT_INCLUDES)
file(REMOVE ${ROOT_INCLUDES})
elseif(_csc_ALLOW_ROOT_INCLUDES)
if(DEFINED arg_INCLUDES_SUBPATH)
file(COPY "${arg_SOURCE_PATH}/${arg_INCLUDES_SUBPATH}/"
DESTINATION "${CURRENT_PACKAGES_DIR}/include/"
)
file(GLOB root_includes
LIST_DIRECTORIES false
"${CURRENT_PACKAGES_DIR}/include/*")
if(NOT root_includes STREQUAL "")
if(arg_REMOVE_ROOT_INCLUDES)
file(REMOVE ${root_includes})
elseif(arg_ALLOW_ROOT_INCLUDES)
else()
message(FATAL_ERROR "Top-level files were found in ${CURRENT_PACKAGES_DIR}/include; this may indicate a problem with the call to `vcpkg_install_msbuild()`.\nTo avoid conflicts with other libraries, it is recommended to not put includes into the root `include/` directory.\nPass either ALLOW_ROOT_INCLUDES or REMOVE_ROOT_INCLUDES to handle these files.\n")
endif()
endif()
endif()

if(DEFINED _csc_LICENSE_SUBPATH)
file(INSTALL ${_csc_SOURCE_PATH}/${_csc_LICENSE_SUBPATH} DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT} RENAME copyright)
if(DEFINED arg_LICENSE_SUBPATH)
file(INSTALL "${arg_SOURCE_PATH}/${arg_LICENSE_SUBPATH}"
DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}"
RENAME copyright
)
endif()
endfunction()

0 comments on commit cae98be

Please sign in to comment.