mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Only open files when necessary
This commit is contained in:
@@ -16,11 +16,11 @@
|
|||||||
|
|
||||||
/* Variables related to CLI options */
|
/* Variables related to CLI options */
|
||||||
extern bool isDmgMode;
|
extern bool isDmgMode;
|
||||||
extern FILE *linkerScript;
|
extern char const *linkerScriptName;
|
||||||
extern FILE *mapFile;
|
extern char const *mapFileName;
|
||||||
extern FILE *symFile;
|
extern char const *symFileName;
|
||||||
extern FILE *overlayFile;
|
extern char const *overlayFileName;
|
||||||
extern FILE *outputFile;
|
extern char const *outputFileName;
|
||||||
extern uint8_t padValue;
|
extern uint8_t padValue;
|
||||||
extern bool is32kMode;
|
extern bool is32kMode;
|
||||||
extern bool beVerbose;
|
extern bool beVerbose;
|
||||||
@@ -32,4 +32,18 @@ extern bool isWRA0Mode;
|
|||||||
fprintf(stderr, __VA_ARGS__); \
|
fprintf(stderr, __VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a file if specified, and aborts on error.
|
||||||
|
* @param fileName The name of the file to open; if NULL, no file will be opened
|
||||||
|
* @param mode The mode to open the file with
|
||||||
|
* @return A pointer to a valid FILE structure, or NULL if fileName was NULL
|
||||||
|
*/
|
||||||
|
FILE *openFile(char const *fileName, char const *mode);
|
||||||
|
|
||||||
|
#define closeFile(file) do { \
|
||||||
|
FILE *tmp = file; \
|
||||||
|
if (tmp) \
|
||||||
|
fclose(tmp); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#endif /* RGBDS_LINK_MAIN_H */
|
#endif /* RGBDS_LINK_MAIN_H */
|
||||||
|
|||||||
@@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
extern FILE *linkerScript;
|
||||||
|
|
||||||
struct SectionPlacement {
|
struct SectionPlacement {
|
||||||
struct Section *section;
|
struct Section *section;
|
||||||
uint16_t org;
|
uint16_t org;
|
||||||
|
|||||||
@@ -58,10 +58,12 @@ static void initFreeSpace(void)
|
|||||||
*/
|
*/
|
||||||
static void processLinkerScript(void)
|
static void processLinkerScript(void)
|
||||||
{
|
{
|
||||||
if (!linkerScript)
|
if (!linkerScriptName)
|
||||||
return;
|
return;
|
||||||
verbosePrint("Reading linker script...\n");
|
verbosePrint("Reading linker script...\n");
|
||||||
|
|
||||||
|
linkerScript = openFile(linkerScriptName, "r");
|
||||||
|
|
||||||
/* Modify all sections according to the linker script */
|
/* Modify all sections according to the linker script */
|
||||||
struct SectionPlacement *placement;
|
struct SectionPlacement *placement;
|
||||||
|
|
||||||
@@ -86,6 +88,8 @@ static void processLinkerScript(void)
|
|||||||
section->bank = placement->bank;
|
section->bank = placement->bank;
|
||||||
section->isAlignFixed = false; /* The alignment is satisfied */
|
section->isAlignFixed = false; /* The alignment is satisfied */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(linkerScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -385,7 +389,7 @@ void assign_AssignSections(void)
|
|||||||
|
|
||||||
/* Overlaying requires only fully-constrained sections */
|
/* Overlaying requires only fully-constrained sections */
|
||||||
verbosePrint("Assigning other sections...\n");
|
verbosePrint("Assigning other sections...\n");
|
||||||
if (overlayFile)
|
if (overlayFileName)
|
||||||
errx(1, "All sections must be fixed when using an overlay file; %u, %sn't",
|
errx(1, "All sections must be fixed when using an overlay file; %u, %sn't",
|
||||||
nbSectionsToAssign, nbSectionsToAssign == 1 ? "is" : "are");
|
nbSectionsToAssign, nbSectionsToAssign == 1 ? "is" : "are");
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -22,16 +24,29 @@
|
|||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
bool isDmgMode; /* -d */
|
bool isDmgMode; /* -d */
|
||||||
FILE *linkerScript; /* -l */
|
char const *linkerScriptName; /* -l */
|
||||||
FILE *mapFile; /* -m */
|
char const *mapFileName; /* -m */
|
||||||
FILE *symFile; /* -n */
|
char const *symFileName; /* -n */
|
||||||
FILE *overlayFile; /* -O */
|
char const *overlayFileName; /* -O */
|
||||||
FILE *outputFile; /* -o */
|
char const *outputFileName; /* -o */
|
||||||
uint8_t padValue; /* -p */
|
uint8_t padValue; /* -p */
|
||||||
bool is32kMode; /* -t */
|
bool is32kMode; /* -t */
|
||||||
bool beVerbose; /* -v */
|
bool beVerbose; /* -v */
|
||||||
bool isWRA0Mode; /* -w */
|
bool isWRA0Mode; /* -w */
|
||||||
|
|
||||||
|
FILE *openFile(char const *fileName, char const *mode)
|
||||||
|
{
|
||||||
|
if (!fileName)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
FILE *file = fopen(fileName, mode);
|
||||||
|
|
||||||
|
if (!file)
|
||||||
|
err(1, "Could not open file \"%s\"", fileName);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints the program's usage to stdout.
|
* Prints the program's usage to stdout.
|
||||||
@@ -42,42 +57,12 @@ static void printUsage(void)
|
|||||||
" [-o outfile] [-p pad_value] [-s symbol] file [...]");
|
" [-o outfile] [-p pad_value] [-s symbol] file [...]");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function for `main`'s argument parsing.
|
|
||||||
* For use with options which take a file name to operate on
|
|
||||||
* If the file fails to be opened, an error message will be printed,
|
|
||||||
* and the function `exit`s.
|
|
||||||
* @param mode The mode to open the file in
|
|
||||||
* @param failureMessage A format string that will be printed on failure.
|
|
||||||
* A single (string) argument is given, the file name.
|
|
||||||
* @return What `fopen` returned; this cannot be NULL.
|
|
||||||
*/
|
|
||||||
static FILE *openArgFile(char const *mode, char const *failureMessage)
|
|
||||||
{
|
|
||||||
FILE *file = fopen(optarg, mode);
|
|
||||||
|
|
||||||
if (!file)
|
|
||||||
err(1, failureMessage, optarg);
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up what has been done
|
* Cleans up what has been done
|
||||||
* Mostly here to please tools such as `valgrind` so actual errors can be seen
|
* Mostly here to please tools such as `valgrind` so actual errors can be seen
|
||||||
*/
|
*/
|
||||||
static void cleanup(void)
|
static void cleanup(void)
|
||||||
{
|
{
|
||||||
if (linkerScript)
|
|
||||||
fclose(linkerScript);
|
|
||||||
if (mapFile)
|
|
||||||
fclose(mapFile);
|
|
||||||
if (symFile)
|
|
||||||
fclose(symFile);
|
|
||||||
if (overlayFile)
|
|
||||||
fclose(overlayFile);
|
|
||||||
if (outputFile)
|
|
||||||
fclose(outputFile);
|
|
||||||
|
|
||||||
obj_Cleanup();
|
obj_Cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,19 +80,19 @@ int main(int argc, char *argv[])
|
|||||||
isWRA0Mode = true;
|
isWRA0Mode = true;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
linkerScript = openArgFile("r", "Could not open linker script file \"%s\"");
|
linkerScriptName = optarg;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
mapFile = openArgFile("w", "Could not open map file \"%s\"");
|
mapFileName = optarg;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
symFile = openArgFile("w", "Could not open sym file \"%s\"");
|
symFileName = optarg;
|
||||||
break;
|
break;
|
||||||
case 'O':
|
case 'O':
|
||||||
overlayFile = openArgFile("r+b", "Could not open overlay file \"%s\"");
|
overlayFileName = optarg;
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
outputFile = openArgFile("wb", "Could not open output file \"%s\"");
|
outputFileName = optarg;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
value = strtoul(optarg, &endptr, 0);
|
value = strtoul(optarg, &endptr, 0);
|
||||||
|
|||||||
@@ -9,6 +9,11 @@
|
|||||||
|
|
||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|
||||||
|
FILE *outputFile;
|
||||||
|
FILE *overlayFile;
|
||||||
|
FILE *symFile;
|
||||||
|
FILE *mapFile;
|
||||||
|
|
||||||
struct SortedSection {
|
struct SortedSection {
|
||||||
struct Section const *section;
|
struct Section const *section;
|
||||||
struct SortedSection *next;
|
struct SortedSection *next;
|
||||||
@@ -158,6 +163,9 @@ static void writeBank(struct SortedSection *bankSections, uint16_t baseOffset,
|
|||||||
*/
|
*/
|
||||||
static void writeROM(void)
|
static void writeROM(void)
|
||||||
{
|
{
|
||||||
|
outputFile = openFile(outputFileName, "wb");
|
||||||
|
overlayFile = openFile(overlayFileName, "rb");
|
||||||
|
|
||||||
checkOverlay();
|
checkOverlay();
|
||||||
|
|
||||||
if (outputFile) {
|
if (outputFile) {
|
||||||
@@ -177,6 +185,9 @@ static void writeROM(void)
|
|||||||
funlockfile(overlayFile);
|
funlockfile(overlayFile);
|
||||||
funlockfile(outputFile);
|
funlockfile(outputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closeFile(outputFile);
|
||||||
|
closeFile(overlayFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -310,7 +321,7 @@ static void writeMapBank(struct SortedSections const *sectList,
|
|||||||
*/
|
*/
|
||||||
static void writeSymAndMap(void)
|
static void writeSymAndMap(void)
|
||||||
{
|
{
|
||||||
if (!symFile && !mapFile)
|
if (!symFileName && !mapFileName)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
enum SectionType typeMap[SECTTYPE_INVALID] = {
|
enum SectionType typeMap[SECTTYPE_INVALID] = {
|
||||||
@@ -324,8 +335,12 @@ static void writeSymAndMap(void)
|
|||||||
SECTTYPE_HRAM
|
SECTTYPE_HRAM
|
||||||
};
|
};
|
||||||
|
|
||||||
if (symFile)
|
symFile = openFile(symFileName, "w");
|
||||||
|
mapFile = openFile(mapFileName, "w");
|
||||||
|
|
||||||
|
if (symFileName) {
|
||||||
fputs("; File generated by rgblink\n", symFile);
|
fputs("; File generated by rgblink\n", symFile);
|
||||||
|
}
|
||||||
|
|
||||||
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
|
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
|
||||||
enum SectionType type = typeMap[i];
|
enum SectionType type = typeMap[i];
|
||||||
@@ -339,6 +354,9 @@ static void writeSymAndMap(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closeFile(symFile);
|
||||||
|
closeFile(mapFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanupSections(struct SortedSection *section)
|
static void cleanupSections(struct SortedSection *section)
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|
||||||
|
FILE *linkerScript;
|
||||||
|
|
||||||
static inline bool isWhiteSpace(int c)
|
static inline bool isWhiteSpace(int c)
|
||||||
{
|
{
|
||||||
return c == ' ' || c == '\t';
|
return c == ' ' || c == '\t';
|
||||||
|
|||||||
Reference in New Issue
Block a user