Skip to content

Commit

Permalink
Add post build check to verify that the libs on macOS have the right …
Browse files Browse the repository at this point in the history
…architecture
  • Loading branch information
autoantwort committed Oct 6, 2021
1 parent 89d20d4 commit c074b8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
11 changes: 11 additions & 0 deletions include/vcpkg/base/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,15 @@ namespace vcpkg
return !Strings::case_insensitive_ascii_equals(target.extension(), ext);
}
};

struct NotExtensionsCaseInsensitive
{
std::initializer_list<StringView> 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);
});
}
};
}
47 changes: 35 additions & 12 deletions src/vcpkg/postbuildlint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ namespace vcpkg::PostBuildLint
}
#endif

#if defined(_WIN32)
static void print_invalid_architecture_files(const std::string& expected_architecture,
std::vector<FileAndArch> binaries_with_invalid_architecture)
{
Expand All @@ -519,6 +518,8 @@ namespace vcpkg::PostBuildLint
}
}

#if defined(_WIN32)

static LintStatus check_dll_architecture(const std::string& expected_architecture,
const std::vector<Path>& files,
const Filesystem& fs)
Expand Down Expand Up @@ -554,11 +555,9 @@ namespace vcpkg::PostBuildLint
const std::vector<Path>& files,
const Filesystem& fs)
{
#if defined(_WIN32)
std::vector<FileAndArch> binaries_with_invalid_architecture;

for (const Path& file : files)
{
#if defined(_WIN32)
const auto checkLib = [&](const Path& file) {
Checks::check_exit(VCPKG_LINE_INFO,
Strings::case_insensitive_ascii_equals(file.extension(), ".lib"),
"The file extension was not .lib: %s",
Expand All @@ -567,7 +566,7 @@ namespace vcpkg::PostBuildLint

// This is zero for folly's debug library
// TODO: Why?
if (machine_types.empty()) return LintStatus::SUCCESS;
if (machine_types.empty()) return;

Checks::check_exit(
VCPKG_LINE_INFO, machine_types.size() == 1, "Found more than 1 architecture in file %s", file);
Expand All @@ -577,16 +576,30 @@ namespace vcpkg::PostBuildLint
{
binaries_with_invalid_architecture.push_back({file, actual_architecture});
}
}
};
#elif defined(__APPLE__)
const auto requested_arch = expected_architecture == "x64" ? "x86_64" : expected_architecture;
const auto checkLib = [&](const Path& file) {
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});
}
};
#else
const auto checkLib = [&](const Path&) {};
#endif
for (const Path& file : files)
checkLib(file);

if (!binaries_with_invalid_architecture.empty())
{
print_invalid_architecture_files(expected_architecture, binaries_with_invalid_architecture);
return LintStatus::ERROR_DETECTED;
}
#endif
(void)expected_architecture;
(void)files;
(void)fs;
return LintStatus::SUCCESS;
}
Expand Down Expand Up @@ -891,10 +904,20 @@ namespace vcpkg::PostBuildLint
const auto debug_bin_dir = package_dir / "debug/bin";
const auto release_bin_dir = package_dir / "bin";

const auto libFilter = [&pre_build_info]() {
if (pre_build_info.cmake_system_name.empty() || pre_build_info.cmake_system_name == "Windows" ||
pre_build_info.cmake_system_name == "WindowsStore")
{
if (pre_build_info.cmake_system_name == "MinGW") return NotExtensionsCaseInsensitive{{".lib", ".a"}};
return NotExtensionsCaseInsensitive{{".lib"}};
}
return NotExtensionsCaseInsensitive{{".so", ".a", ".dylib"}};
}();

std::vector<Path> 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, libFilter);
std::vector<Path> 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, libFilter);

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);
Expand Down

0 comments on commit c074b8e

Please sign in to comment.