From de502ef3b12693034653c128e1804d23ff16bb6e Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Wed, 18 Aug 2021 12:55:41 +0200 Subject: [PATCH] Add post build check to verify that the libs on macOS have the right architecture --- include/vcpkg/base/files.h | 11 +++++++++++ src/vcpkg/postbuildlint.cpp | 29 ++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/vcpkg/base/files.h b/include/vcpkg/base/files.h index 7477910720..8a298ed249 100644 --- a/include/vcpkg/base/files.h +++ b/include/vcpkg/base/files.h @@ -394,4 +394,15 @@ namespace vcpkg return !Strings::case_insensitive_ascii_equals(target.extension(), ext); } }; + + struct NotExtensionsCaseInsensitive + { + std::initializer_list exts; + bool operator()(const Path& target) const + { + return !std::any_of(exts.begin(), exts.end(), [extension = target.extension()](const auto& ext) { + return Strings::case_insensitive_ascii_equals(extension, ext); + }); + } + }; } diff --git a/src/vcpkg/postbuildlint.cpp b/src/vcpkg/postbuildlint.cpp index 807688293f..316b7f9e98 100644 --- a/src/vcpkg/postbuildlint.cpp +++ b/src/vcpkg/postbuildlint.cpp @@ -500,7 +500,7 @@ namespace vcpkg::PostBuildLint } #endif -#if defined(_WIN32) +#if defined(_WIN32) or defined(__APPLE__) static void print_invalid_architecture_files(const std::string& expected_architecture, std::vector binaries_with_invalid_architecture) { @@ -517,6 +517,9 @@ namespace vcpkg::PostBuildLint "\n\n"); } } +#endif + +#if defined(_WIN32) static LintStatus check_dll_architecture(const std::string& expected_architecture, const std::vector& files, @@ -578,6 +581,26 @@ namespace vcpkg::PostBuildLint } } + if (!binaries_with_invalid_architecture.empty()) + { + print_invalid_architecture_files(expected_architecture, binaries_with_invalid_architecture); + return LintStatus::ERROR_DETECTED; + } +#elif defined(__APPLE__) + std::vector binaries_with_invalid_architecture; + const auto requested_arch = expected_architecture == "x64" ? "x86_64" : expected_architecture; + for (const Path& file : files) + { + auto cmd_line = Command("lipo").string_arg("-archs").path_arg(file); + ExitCodeAndOutput ec_data = cmd_execute_and_capture_output(cmd_line); + Checks::check_exit( + VCPKG_LINE_INFO, ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line.command_line()); + if (ec_data.output.find(requested_arch) == std::string::npos) + { + binaries_with_invalid_architecture.push_back({file, ec_data.output}); + } + } + if (!binaries_with_invalid_architecture.empty()) { print_invalid_architecture_files(expected_architecture, binaries_with_invalid_architecture); @@ -891,9 +914,9 @@ namespace vcpkg::PostBuildLint const auto release_bin_dir = package_dir / "bin"; std::vector debug_libs = fs.get_regular_files_recursive(debug_lib_dir, IgnoreErrors{}); - Util::erase_remove_if(debug_libs, NotExtensionCaseInsensitive{".lib"}); + Util::erase_remove_if(debug_libs, NotExtensionsCaseInsensitive{{".lib", ".so", ".a", ".dylib"}}); std::vector release_libs = fs.get_regular_files_recursive(release_lib_dir, IgnoreErrors{}); - Util::erase_remove_if(release_libs, NotExtensionCaseInsensitive{".lib"}); + Util::erase_remove_if(release_libs, NotExtensionsCaseInsensitive{{".lib", ".so", ".a", ".dylib"}}); if (!pre_build_info.build_type && !build_info.policies.is_enabled(BuildPolicy::MISMATCHED_NUMBER_OF_BINARIES)) error_count += check_matching_debug_and_release_binaries(debug_libs, release_libs);