Skip to content

Commit

Permalink
Avoid linker errors on Windows with long command lines
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Aug 6, 2024
1 parent d455102 commit 0723a83
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
coverage database file instead of a top-level unit name.
- The `--force-init` command which was deprecated in the 1.7 release has
been removed.
- Elaboration of large designs on Windows no longer fail with linker
errors due to excessive command line length.

## Version 1.13.1 - 2024-07-25
- Windows installer was missing some standard library files.
Expand Down
33 changes: 31 additions & 2 deletions src/cgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static A(char *) link_args;
static A(char *) cleanup_files = AINIT;

#define UNITS_PER_JOB 25
#define LARGE_OBJECT_COUNT 10

static void cgen_find_children(vcode_unit_t root, unit_list_t *units)
{
Expand Down Expand Up @@ -215,8 +216,29 @@ static void cgen_link(const char *module_name, char **objs, int nobjs)
cgen_link_arg("-o");
cgen_link_arg("%s", so_path);

for (int i = 0; i < nobjs; i++)
cgen_link_arg("%s", objs[i]);
char *tmplist = NULL;
if (nobjs >= LARGE_OBJECT_COUNT) {
// If there are a large number of objects to link then pass them
// in a file rather than individually to avoid command line length
// limits on Windows

tmplist = nvc_temp_file();

FILE *f = fopen(tmplist, "w");
if (f == NULL)
fatal_errno("%s", tmplist);

for (int i = 0; i < nobjs; i++)
fprintf(f, "%s\n", objs[i]);

fclose(f);

cgen_link_arg("@%s", tmplist);
}
else {
for (int i = 0; i < nobjs; i++)
cgen_link_arg("%s", objs[i]);
}

#if defined LINKER_PATH && defined __OpenBSD__
// Extra linker arguments to make constructors work on OpenBSD
Expand Down Expand Up @@ -250,6 +272,13 @@ static void cgen_link(const char *module_name, char **objs, int nobjs)
fatal_errno("unlink: %s", objs[i]);
}

if (tmplist != NULL) {
if (unlink(tmplist) != 0)
fatal_errno("unlink: %s", tmplist);

free(tmplist);
}

progress("linking shared library");

for (size_t i = 0; i < link_args.count; i++)
Expand Down

0 comments on commit 0723a83

Please sign in to comment.