Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pretty format for Makefile variables #900

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ struct BuildConfig {
all = dependsOn;
}

void emitVariable(std::ostream& os, const String& varName) const;
void emitMakefile(std::ostream& os) const;
void emitCompdb(StringRef baseDir, std::ostream& os) const;
};
Expand All @@ -191,7 +192,7 @@ emitDep(std::ostream& os, usize& offset, const StringRef dep) {
os << std::setw(static_cast<int>(MAX_LINE_LEN + 3 - offset)) << " \\\n ";
offset = 2;
}
os << " " << dep;
os << ' ' << dep;
offset += dep.size() + 1; // space
}

Expand All @@ -202,7 +203,7 @@ emitTarget(
) {
usize offset = 0;

os << target << ":";
os << target << ':';
offset += target.size() + 2; // : and space

if (sourceFile.has_value()) {
Expand All @@ -223,11 +224,43 @@ emitTarget(
os << '\n';
}

void
BuildConfig::emitVariable(std::ostream& os, const String& varName) const {
std::ostringstream oss; // TODO: implement an elegant way to get type size.
oss << varName << ' ' << variables.at(varName).type;
const String left = oss.str();
os << left << ' ';

constexpr usize MAX_LINE_LEN = 80; // TODO: share across sources?
usize offset = left.size() + 1; // space
String value;
for (const char c : variables.at(varName).value) {
if (c == ' ') {
// Emit value
if (offset + value.size() + 2 > MAX_LINE_LEN) { // 2 for space and '\'
os << std::setw(static_cast<int>(MAX_LINE_LEN + 3 - offset))
<< "\\\n ";
offset = 2;
}
os << value << ' ';
offset += value.size() + 1;
value.clear();
} else {
value.push_back(c);
}
}

if (!value.empty()) {
os << value;
}
os << '\n';
}

void
BuildConfig::emitMakefile(std::ostream& os) const {
const Vec<String> sortedVars = topoSort(variables, varDeps);
for (const String& varName : sortedVars) {
os << varName << ' ' << variables.at(varName) << '\n';
emitVariable(os, varName);
}
if (!sortedVars.empty() && !targets.empty()) {
os << '\n';
Expand Down