Defer closing of depend file

This commit is contained in:
Rangi42
2024-03-27 11:44:26 -04:00
parent 2ef5e807f8
commit 912a1504ec
5 changed files with 22 additions and 27 deletions

View File

@@ -16,7 +16,7 @@ extern bool warnOnLdOpt;
extern bool verbose;
extern bool warnings; // True to enable warnings, false to disable them.
extern FILE *dependfile;
extern FILE *dependFile;
extern std::string targetFileName;
extern bool generatedMissingIncludes;
extern bool failedOnMissingInclude;

View File

@@ -142,10 +142,10 @@ void fstk_SetPreIncludeFile(std::string const &path) {
}
static void printDep(std::string const &path) {
if (dependfile) {
fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), path.c_str());
if (dependFile) {
fprintf(dependFile, "%s: %s\n", targetFileName.c_str(), path.c_str());
if (generatePhonyDeps)
fprintf(dependfile, "%s:\n", path.c_str());
fprintf(dependFile, "%s:\n", path.c_str());
}
}

View File

@@ -10,6 +10,7 @@
#include "error.hpp"
#include "extern/getopt.hpp"
#include "helpers.hpp" // Defer
#include "parser.hpp"
#include "version.hpp"
@@ -20,7 +21,7 @@
#include "asm/symbol.hpp"
#include "asm/warning.hpp"
FILE *dependfile = nullptr;
FILE *dependFile = nullptr;
bool generatedMissingIncludes = false;
bool failedOnMissingInclude = false;
bool generatePhonyDeps = false;
@@ -37,7 +38,6 @@ bool warnings; // True to enable warnings, false to disable them.
static std::string make_escape(std::string &str) {
std::string escaped;
size_t pos = 0;
for (;;) {
// All dollars needs to be doubled
size_t nextPos = str.find("$", pos);
@@ -114,18 +114,20 @@ static void printUsage() {
int main(int argc, char *argv[]) {
time_t now = time(nullptr);
char const *sourceDateEpoch = getenv("SOURCE_DATE_EPOCH");
// Support SOURCE_DATE_EPOCH for reproducible builds
// https://reproducible-builds.org/docs/source-date-epoch/
if (sourceDateEpoch)
if (char const *sourceDateEpoch = getenv("SOURCE_DATE_EPOCH"); sourceDateEpoch)
now = (time_t)strtoul(sourceDateEpoch, nullptr, 0);
Defer closeDependFile{[&] {
if (dependFile)
fclose(dependFile);
}};
// Perform some init for below
sym_Init(now);
// Set defaults
opt_B("01");
opt_G("0123");
opt_P(0);
@@ -140,7 +142,6 @@ int main(int argc, char *argv[]) {
uint32_t maxDepth = DEFAULT_MAX_DEPTH;
char const *dependFileName = nullptr;
std::string newTarget;
// Maximum of 100 errors only applies if rgbasm is printing errors to a terminal.
if (isatty(STDERR_FILENO))
maxErrors = 100;
@@ -219,16 +220,16 @@ int main(int argc, char *argv[]) {
break;
case 'M':
if (dependfile)
if (dependFile)
warnx("Overriding dependfile %s", dependFileName);
if (!strcmp("-", musl_optarg)) {
dependfile = stdout;
dependFile = stdout;
dependFileName = "<stdout>";
} else {
dependfile = fopen(musl_optarg, "w");
dependFile = fopen(musl_optarg, "w");
dependFileName = musl_optarg;
}
if (dependfile == nullptr)
if (dependFile == nullptr)
err("Failed to open dependfile \"%s\"", dependFileName);
break;
@@ -357,12 +358,12 @@ int main(int argc, char *argv[]) {
if (verbose)
printf("Assembling %s\n", mainFileName.c_str());
if (dependfile) {
if (dependFile) {
if (targetFileName.empty())
errx("Dependency files can only be created if a target file is specified with either "
"-o, -MQ or -MT");
fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), mainFileName.c_str());
fprintf(dependFile, "%s: %s\n", targetFileName.c_str(), mainFileName.c_str());
}
charmap_New(DEFAULT_CHARMAP_NAME, nullptr);
@@ -375,9 +376,6 @@ int main(int argc, char *argv[]) {
if (parser.parse() != 0 && nbErrors == 0)
nbErrors = 1;
if (dependfile)
fclose(dependfile);
sect_CheckUnionClosed();
if (nbErrors != 0)

View File

@@ -302,7 +302,7 @@ static void writeFileStackNode(FileStackNode const &node, FILE *file) {
}
}
// Write an objectfile
// Write an object file
void out_WriteObject() {
FILE *file;
if (objectName != "-") {
@@ -352,7 +352,7 @@ void out_WriteObject() {
writeassert(assert, file);
}
// Set the objectfilename
// Set the object filename
void out_SetFileName(std::string const &name) {
if (!objectName.empty())
warnx("Overriding output filename %s", objectName.c_str());

View File

@@ -426,7 +426,7 @@ Symbol *sym_AddLabel(std::string const &symName) {
return sym;
}
static uint32_t anonLabelID;
static uint32_t anonLabelID = 0;
// Add an anonymous label
Symbol *sym_AddAnonLabel() {
@@ -533,7 +533,7 @@ void sym_SetExportAll(bool set) {
exportAll = set;
}
// Initialize the symboltable
// Define the built-in symbols
void sym_Init(time_t now) {
PCSymbol = &createSymbol("@"s);
PCSymbol->type = SYM_LABEL;
@@ -598,7 +598,4 @@ void sym_Init(time_t now) {
sym_AddEqu("__UTC_HOUR__"s, time_utc->tm_hour)->isBuiltin = true;
sym_AddEqu("__UTC_MINUTE__"s, time_utc->tm_min)->isBuiltin = true;
sym_AddEqu("__UTC_SECOND__"s, time_utc->tm_sec)->isBuiltin = true;
labelScope = std::nullopt;
anonLabelID = 0;
}