files: formalize storing "mapped" file names in the filenames system

Formalize the mechanism for storing mapped (non-filesystem) filenames
in the filenames array. These are not compared against input or output
files for overwrite checks.

Add the infrastructure for comparing more than one input filename
(future proofing.)

Move the Makefile dependency target name to the filenames system.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel) 2026-07-06 17:08:01 -07:00
parent 339ee194fc
commit 18c778c590
3 changed files with 54 additions and 23 deletions

View file

@ -112,7 +112,6 @@ static unsigned int operating_mode;
/* Dependency flags */
static bool depend_emit_phony = false;
static bool depend_missing_ok = false;
static char *depend_target = NULL;
struct strlist *depend_list;
static inline bool terminate_after_phase(void)
@ -460,7 +459,7 @@ static void emit_dependencies(struct strlist *list)
deps = stdout;
}
linepos = fprintf(deps, "%s :", depend_target);
linepos = fprintf(deps, "%s :", get_filename(FN_DEPEND_TARGET));
strlist_for_each(l, list) {
char *file = quote_for_make(l->str);
len = strlen(file);
@ -714,10 +713,15 @@ int main(int argc, char **argv)
fix_dependfile_name();
fix_outfile_name();
depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
if (operating_mode & OP_DEPEND) {
depend_list = strlist_alloc(true);
if (!depend_target)
depend_target = quote_for_make(get_filename(FN_OUTFILE));
if (!get_filename(FN_DEPEND_TARGET))
set_filename(FN_DEPEND_TARGET,
quote_for_make(get_filename(FN_OUTFILE)));
} else {
depend_list = NULL;
}
reset_global_defaults(cmd_sb);
@ -842,7 +846,6 @@ int main(int argc, char **argv)
src_free();
strlist_free(&include_path);
cleanup_filenames();
nasm_free(depend_target);
return terminate_after_phase();
}
@ -1349,11 +1352,11 @@ static bool process_arg(char *p, char *q, int pass)
advance = true;
break;
case 'T':
nasm_strdupto(&depend_target, q);
copy_filename(FN_DEPEND_TARGET, q);
advance = true;
break;
case 'Q':
nasm_strto(&depend_target, quote_for_make(q));
set_filename(FN_DEPEND_TARGET, quote_for_make(q));
advance = true;
break;
case 'W':

View file

@ -6,13 +6,20 @@
#include "files.h"
#include "error.h"
/* These must match the constants in "files.h" */
static const char * const filename_names[FN_NFILES] = {
"input",
"mapped-input",
"output",
"error",
"list",
"dependency"
"dependency",
"map",
NULL, /* End of "real" filenames */
"debug mapped input",
"make mapped output"
};
const char *_filenames[FN_NFILES];
@ -40,17 +47,20 @@ const char *set_filename(enum filenames fn, char *src)
void check_overwrite_files(void)
{
enum filenames fn;
const char *inname = get_filename(FN_INFILE);
enum filenames fi, fo; /* No fie or fum */
for (fi = FN_INFILE; fi < FN_OUTFILE; fi++) {
const char *inname = get_filename(fi);
if (!inname)
return;
continue;
for (fn = FN_INFILE+2; fn < FN_NFILES; fn++) {
const char *outname = get_filename(fn);
for (fo = FN_OUTFILE; fo < FN_NFILES_REAL; fo++) {
const char *outname = get_filename(fo);
if (outname && !nasm_compare_paths(inname, outname)) {
nasm_fatal("%s file would overwrite input file",
filename_names[fn]);
nasm_nonfatal("%s file would overwrite %s file `%s'",
filename_names[fo], filename_names[fi], inname);
}
}
}
}

View file

@ -10,17 +10,35 @@
/*
* Primary file names set on the command line etc.
* Wrapped in accessors to make them as constant as possible.
*
* The order of the sections in this enum is critical!
*/
enum filenames {
/* These two entries must be first, in this order */
FN_INFILE, /* Primary input file */
FN_MAPPED_INFILE, /* Debug mapped input file */
FN_OUTFILE, /* Primary output file */
/* Input filenames. FN_INFILE MUST be the first entry. */
FN_INFILE, /* Primary input file - MUST BE FIRST */
/*
* Section: Output filenames.
* FN_OUTFILE MUST be the first entry.
*/
FN_OUTFILE, /* Primary output file - MUST BE FIRST */
FN_ERRFILE, /* Error message file */
FN_LISTFILE, /* Listing file */
FN_DEPENDFILE, /* Dependency file */
FN_MAPFILE, /* Map file (outbin) */
/* Total number of "real" filenames. */
FN_NFILES_REAL,
/*
* Section: Virtual filenames.
* Strings that don't necessarily correspond directly to paths
* in the host filesystem.
*/
FN_MAPPED_INFILE, /* Debug mapped input file name */
FN_DEPEND_TARGET, /* Output target name per -MF, -MD, -MQ */
/* Total fixed filename count */
FN_NFILES
};