Skip to content

Commit

Permalink
[vcpkg] Add compiler info to nuspec description (#13571)
Browse files Browse the repository at this point in the history
* Add compiler info to nuspec description

* Run clang-format on some files

* Fix the unit tests

* [vcpkg] Clarify NuGet description to note 'CXX compiler'

* [vcpkg] Fix tests

Co-authored-by: frivard <frivard@coveo.com>
Co-authored-by: Robert Schumacher <roschuma@microsoft.com>
  • Loading branch information
3 people committed Sep 28, 2020
1 parent 64a281b commit 741c8cb
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
3 changes: 2 additions & 1 deletion scripts/detect_compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ if(CMAKE_GENERATOR STREQUAL "Ninja" AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(CMAKE_C_COMPILER_ID_RUN 1)
set(CMAKE_C_COMPILER_FORCED 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_COMPILER_FORCED 1)
endif()

Expand All @@ -18,3 +17,5 @@ file(SHA1 "${CMAKE_C_COMPILER}" C_HASH)
string(SHA1 COMPILER_HASH "${C_HASH}${CXX_HASH}")

message("#COMPILER_HASH#${COMPILER_HASH}")
message("#COMPILER_CXX_VERSION#${CMAKE_CXX_COMPILER_VERSION}")
message("#COMPILER_CXX_ID#${CMAKE_CXX_COMPILER_ID}")
10 changes: 10 additions & 0 deletions toolsrc/include/vcpkg/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,21 @@ namespace vcpkg::Build
fs::path tag_file;
};

struct CompilerInfo
{
std::string id;
std::string version;
std::string hash;
};

struct AbiInfo
{
std::unique_ptr<PreBuildInfo> pre_build_info;
Optional<const Toolset&> toolset;
Optional<const std::string&> triplet_abi;
std::string package_abi;
Optional<fs::path> abi_tag_file;
Optional<const CompilerInfo&> compiler_info;
};

void compute_all_abis(const VcpkgPaths& paths,
Expand All @@ -325,12 +333,14 @@ namespace vcpkg::Build

const System::Environment& get_action_env(const VcpkgPaths& paths, const AbiInfo& abi_info);
const std::string& get_triplet_info(const VcpkgPaths& paths, const AbiInfo& abi_info);
const CompilerInfo& get_compiler_info(const VcpkgPaths& paths, const AbiInfo& abi_info);

private:
struct TripletMapEntry
{
std::string hash;
Cache<std::string, std::string> compiler_hashes;
Cache<std::string, CompilerInfo> compiler_info;
};
Cache<fs::path, TripletMapEntry> m_triplet_cache;
Cache<fs::path, std::string> m_toolchain_cache;
Expand Down
2 changes: 2 additions & 0 deletions toolsrc/include/vcpkg/vcpkgpaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace vcpkg
{
struct PreBuildInfo;
struct AbiInfo;
struct CompilerInfo;
}

namespace System
Expand Down Expand Up @@ -121,6 +122,7 @@ namespace vcpkg

const System::Environment& get_action_env(const Build::AbiInfo& abi_info) const;
const std::string& get_triplet_info(const Build::AbiInfo& abi_info) const;
const Build::CompilerInfo& get_compiler_info(const Build::AbiInfo& abi_info) const;
bool manifest_mode_enabled() const { return get_manifest().has_value(); }

void track_feature_flag_metrics() const;
Expand Down
14 changes: 14 additions & 0 deletions toolsrc/src/vcpkg-test/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ Build-Depends: bzip
ipa.abi_info.get()->package_abi = "packageabi";
std::string tripletabi("tripletabi");
ipa.abi_info.get()->triplet_abi = tripletabi;
Build::CompilerInfo compiler_info;
compiler_info.hash = "compilerhash";
compiler_info.id = "compilerid";
compiler_info.version = "compilerversion";
ipa.abi_info.get()->compiler_info = compiler_info;

NugetReference ref(ipa);

Expand All @@ -110,6 +115,9 @@ Build-Depends: bzip
a spiffy compression library wrapper
Version: 1.5
Triplet: x64-windows
CXX Compiler id: compilerid
CXX Compiler version: compilerversion
Triplet/Compiler hash: tripletabi
Features: a, b
Dependencies:
Expand Down Expand Up @@ -139,6 +147,9 @@ Features: a, b
a spiffy compression library wrapper
Version: 1.5
Triplet: x64-windows
CXX Compiler id: compilerid
CXX Compiler version: compilerversion
Triplet/Compiler hash: tripletabi
Features: a, b
Dependencies:
Expand Down Expand Up @@ -168,6 +179,9 @@ Features: a, b
a spiffy compression library wrapper
Version: 1.5
Triplet: x64-windows
CXX Compiler id: compilerid
CXX Compiler version: compilerversion
Triplet/Compiler hash: tripletabi
Features: a, b
Dependencies:
Expand Down
10 changes: 9 additions & 1 deletion toolsrc/src/vcpkg/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,13 +1016,21 @@ std::string vcpkg::generate_nuspec(const VcpkgPaths& paths,
auto& spec = action.spec;
auto& scf = *action.source_control_file_location.value_or_exit(VCPKG_LINE_INFO).source_control_file;
auto& version = scf.core_paragraph->version;
const auto& abi_info = action.abi_info.value_or_exit(VCPKG_LINE_INFO);
const auto& compiler_info = abi_info.compiler_info.value_or_exit(VCPKG_LINE_INFO);
std::string description =
Strings::concat("NOT FOR DIRECT USE. Automatically generated cache package.\n\n",
Strings::join("\n ", scf.core_paragraph->description),
"\n\nVersion: ",
version,
"\nTriplet: ",
spec.triplet().to_string(),
"\nCXX Compiler id: ",
compiler_info.id,
"\nCXX Compiler version: ",
compiler_info.version,
"\nTriplet/Compiler hash: ",
action.abi_info.value_or_exit(VCPKG_LINE_INFO).triplet_abi.value_or_exit(VCPKG_LINE_INFO),
abi_info.triplet_abi.value_or_exit(VCPKG_LINE_INFO),
"\nFeatures:",
Strings::join(",", action.feature_list, [](const std::string& s) { return " " + s; }),
"\nDependencies:\n");
Expand Down
70 changes: 58 additions & 12 deletions toolsrc/src/vcpkg/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,33 @@ namespace vcpkg::Build
}
#endif

static std::string load_compiler_hash(const VcpkgPaths& paths, const AbiInfo& abi_info);
static CompilerInfo load_compiler_info(const VcpkgPaths& paths, const AbiInfo& abi_info);

const CompilerInfo& EnvCache::get_compiler_info(const VcpkgPaths& paths, const AbiInfo& abi_info)
{
const auto& fs = paths.get_filesystem();
Checks::check_exit(VCPKG_LINE_INFO, abi_info.pre_build_info != nullptr);
const fs::path triplet_file_path = paths.get_triplet_file_path(abi_info.pre_build_info->triplet);

auto tcfile = abi_info.pre_build_info->toolchain_file();
auto&& toolchain_hash = m_toolchain_cache.get_lazy(
tcfile, [&]() { return Hash::get_file_hash(VCPKG_LINE_INFO, fs, tcfile, Hash::Algorithm::Sha1); });

auto&& triplet_entry = m_triplet_cache.get_lazy(triplet_file_path, [&]() -> TripletMapEntry {
return TripletMapEntry{Hash::get_file_hash(VCPKG_LINE_INFO, fs, triplet_file_path, Hash::Algorithm::Sha1)};
});

return triplet_entry.compiler_info.get_lazy(toolchain_hash, [&]() -> CompilerInfo {
if (m_compiler_tracking)
{
return load_compiler_info(paths, abi_info);
}
else
{
return CompilerInfo{};
}
});
}

const std::string& EnvCache::get_triplet_info(const VcpkgPaths& paths, const AbiInfo& abi_info)
{
Expand All @@ -366,8 +392,17 @@ namespace vcpkg::Build
return triplet_entry.compiler_hashes.get_lazy(toolchain_hash, [&]() -> std::string {
if (m_compiler_tracking)
{
auto compiler_hash = load_compiler_hash(paths, abi_info);
return Strings::concat(triplet_entry.hash, '-', toolchain_hash, '-', compiler_hash);
auto& compiler_info = triplet_entry.compiler_info.get_lazy(toolchain_hash, [&]() -> CompilerInfo {
if (m_compiler_tracking)
{
return load_compiler_info(paths, abi_info);
}
else
{
return CompilerInfo{};
}
});
return Strings::concat(triplet_entry.hash, '-', toolchain_hash, '-', compiler_info.hash);
}
else
{
Expand Down Expand Up @@ -464,7 +499,7 @@ namespace vcpkg::Build
}
}

static std::string load_compiler_hash(const VcpkgPaths& paths, const AbiInfo& abi_info)
static CompilerInfo load_compiler_info(const VcpkgPaths& paths, const AbiInfo& abi_info)
{
auto triplet = abi_info.pre_build_info->triplet;
System::print2("Detecting compiler hash for triplet ", triplet, "...\n");
Expand Down Expand Up @@ -499,14 +534,24 @@ namespace vcpkg::Build
auto stdoutlog = buildpath / ("stdout-" + triplet.canonical_name() + ".log");
std::ofstream out_file(stdoutlog.native().c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
Checks::check_exit(VCPKG_LINE_INFO, out_file, "Failed to open '%s' for writing", fs::u8string(stdoutlog));
std::string compiler_hash;
CompilerInfo compiler_info;
System::cmd_execute_and_stream_lines(
command,
[&](const std::string& s) {
static const StringLiteral s_marker = "#COMPILER_HASH#";
if (Strings::starts_with(s, s_marker))
static const StringLiteral s_hash_marker = "#COMPILER_HASH#";
if (Strings::starts_with(s, s_hash_marker))
{
compiler_info.hash = s.data() + s_hash_marker.size();
}
static const StringLiteral s_version_marker = "#COMPILER_CXX_VERSION#";
if (Strings::starts_with(s, s_version_marker))
{
compiler_info.version = s.data() + s_version_marker.size();
}
static const StringLiteral s_id_marker = "#COMPILER_CXX_ID#";
if (Strings::starts_with(s, s_id_marker))
{
compiler_hash = s.data() + s_marker.size();
compiler_info.id = s.data() + s_id_marker.size();
}
Debug::print(s, '\n');
out_file.write(s.data(), s.size()).put('\n');
Expand All @@ -516,7 +561,7 @@ namespace vcpkg::Build
env);
out_file.close();

if (compiler_hash.empty())
if (compiler_info.hash.empty())
{
Debug::print("Compiler information tracking can be disabled by passing --",
VcpkgCmdArguments::FEATURE_FLAGS_ARG,
Expand All @@ -525,11 +570,11 @@ namespace vcpkg::Build
"\n");
}
Checks::check_exit(VCPKG_LINE_INFO,
!compiler_hash.empty(),
!compiler_info.hash.empty(),
"Error occurred while detecting compiler information. Pass `--debug` for more information.");

Debug::print("Detecting compiler hash for triplet ", triplet, ": ", compiler_hash, "\n");
return compiler_hash;
Debug::print("Detecting compiler hash for triplet ", triplet, ": ", compiler_info.hash, "\n");
return compiler_info;
}

static std::vector<System::CMakeVariable> get_cmake_build_args(const VcpkgPaths& paths,
Expand Down Expand Up @@ -957,6 +1002,7 @@ namespace vcpkg::Build
abi_info.pre_build_info = std::make_unique<PreBuildInfo>(
paths, action.spec.triplet(), var_provider.get_tag_vars(action.spec).value_or_exit(VCPKG_LINE_INFO));
abi_info.toolset = paths.get_toolset(*abi_info.pre_build_info);
abi_info.compiler_info = paths.get_compiler_info(abi_info);
abi_info.triplet_abi = paths.get_triplet_info(abi_info);

auto maybe_abi_tag_and_file = compute_abi_tag(paths, action, dependency_abis);
Expand Down
5 changes: 5 additions & 0 deletions toolsrc/src/vcpkg/vcpkgpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ If you wish to silence this error and use classic mode, you can:
return m_pimpl->m_env_cache.get_triplet_info(*this, abi_info);
}

const Build::CompilerInfo& VcpkgPaths::get_compiler_info(const Build::AbiInfo& abi_info) const
{
return m_pimpl->m_env_cache.get_compiler_info(*this, abi_info);
}

Files::Filesystem& VcpkgPaths::get_filesystem() const { return *m_pimpl->fs_ptr; }

void VcpkgPaths::track_feature_flag_metrics() const
Expand Down

0 comments on commit 741c8cb

Please sign in to comment.