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 verbose;
extern bool warnings; // True to enable warnings, false to disable them. extern bool warnings; // True to enable warnings, false to disable them.
extern FILE *dependfile; extern FILE *dependFile;
extern std::string targetFileName; extern std::string targetFileName;
extern bool generatedMissingIncludes; extern bool generatedMissingIncludes;
extern bool failedOnMissingInclude; extern bool failedOnMissingInclude;

View File

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

View File

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