Improve CMakeLists.txt generation to not touch the file when there are no changes

vcpkg-wip
Duncan Ogilvie 4 years ago
parent b6d629b9f4
commit fdd9a3d134

@ -72,5 +72,3 @@ install(
COMPONENT cmkr COMPONENT cmkr
) )

@ -281,7 +281,7 @@ int generate_cmake(const char *path) {
ss << "\")\n\n"; ss << "\")\n\n";
} }
// TODO: support cmake.toml subdirectory generation // generate_cmake is called on these recursively later
if (!cmake.subdirs.empty()) { if (!cmake.subdirs.empty()) {
for (const auto &dir : cmake.subdirs) { for (const auto &dir : cmake.subdirs) {
cmd("add_subdirectory")(dir); cmd("add_subdirectory")(dir);
@ -404,7 +404,7 @@ int generate_cmake(const char *path) {
target_type = ""; target_type = "";
target_scope = "PUBLIC"; target_scope = "PUBLIC";
} else { } else {
throw std::runtime_error("Unknown binary type " + target.type + throw std::runtime_error("Unknown target type " + target.type +
"! Supported types are: executable, library, shared, static, interface"); "! Supported types are: executable, library, shared, static, interface");
} }
@ -513,15 +513,30 @@ int generate_cmake(const char *path) {
} }
} }
ss << "\n\n"; auto list_path = fs::path(path) / "CMakeLists.txt";
// printf("%s\n", ss.str().c_str());
std::ofstream ofs(fs::path(path) / "CMakeLists.txt", std::ios_base::binary); auto should_regenerate = [&list_path, &ss]()
if (ofs.is_open()) { {
ofs << ss.rdbuf(); if(!fs::exists(list_path))
return true;
std::ifstream ifs(list_path, std::ios_base::binary);
if(!ifs.is_open()) {
throw std::runtime_error("Failed to read " + list_path.string());
}
std::string data((std::istreambuf_iterator<char>(ifs)),std::istreambuf_iterator<char>());
return data != ss.str();
}();
if(should_regenerate) {
std::ofstream ofs(list_path, std::ios_base::binary);
if (ofs.is_open()) {
ofs << ss.str();
} else {
throw std::runtime_error("Failed to write " + list_path.string());
}
} }
ofs.flush();
ofs.close();
for (const auto &sub : cmake.subdirs) { for (const auto &sub : cmake.subdirs) {
if (fs::exists(fs::path(sub) / "cmake.toml")) if (fs::exists(fs::path(sub) / "cmake.toml"))

Loading…
Cancel
Save