Fixes for compiling on 64bit and outside the source dir
[AROS.git] / tools / collect-aros / collect-aros.c
blobac81715a7eb462cedcf5db897de1a0bfbf14a209
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <sys/stat.h>
7 #include "misc.h"
8 #include "docommand.h"
9 #include "backend.h"
10 #include "ldscript.h"
11 #include "gensets.h"
12 #include "env.h"
14 static char *ldscriptname, *tempoutput, *ld_name, *strip_name;
15 static FILE *ldscriptfile;
17 static void exitfunc(void)
19 if (ldscriptfile != NULL)
20 fclose(ldscriptfile);
22 if (ldscriptname != NULL)
23 remove(ldscriptname);
25 if (tempoutput != NULL)
26 remove(tempoutput);
29 int main(int argc, char *argv[])
31 int cnt;
32 char *output, **ldargs;
33 /* incremental = 1 -> don't do final linking.
34 incremental = 2 -> don't do final linking AND STILL produce symbol sets. */
35 int incremental = 0, ignore_undefined_symbols = 0;
36 int strip_all = 0;
37 char *do_verbose = NULL;
39 setnode *setlist = NULL;
41 program_name = argv[0];
42 ld_name = LD_NAME;
43 strip_name = STRIP_NAME;
45 /* Do some stuff with the arguments */
46 output = "a.out";
47 for (cnt = 1; argv[cnt]; cnt++)
49 /* We've encountered an option */
50 if (argv[cnt][0]=='-')
52 /* Get the output file name */
53 if (argv[cnt][1]=='o')
54 output = argv[cnt][2]?&argv[cnt][2]:argv[++cnt];
55 else
56 /* Incremental linking is requested */
57 if ((argv[cnt][1]=='r' || argv[cnt][1]=='i') && argv[cnt][2]=='\0')
58 incremental = 1;
59 else
60 /* Incremental, but produce the symbol sets */
61 if (strncmp(&argv[cnt][1], "Ur", 3) == 0)
63 incremental = 2;
65 argv[cnt][1] = 'r'; /* Just some non-harming option... */
66 argv[cnt][2] = '\0';
68 else
69 /* Ignoring of missing symbols is requested */
70 if (strncmp(&argv[cnt][1], "ius", 4) == 0)
72 ignore_undefined_symbols = 1;
73 argv[cnt][1] = 'r'; /* Just some non-harming option... */
74 argv[cnt][2] = '\0';
76 else
77 /* Complete stripping is requested, but we do it our own way */
78 if (argv[cnt][1]=='s' && argv[cnt][2]=='\0')
80 strip_all = 1;
81 argv[cnt][1] = 'r'; /* Just some non-harming option... */
83 else
84 /* The user just requested help info, don't do anything else */
85 if (strncmp(&argv[cnt][1], "-help", 6) == 0)
87 /* I know, it's not incremental linking we're after, but the end result
88 is the same */
89 incremental = 1;
90 break;
92 else
93 /* verbose output */
94 if (strncmp(&argv[cnt][1], "-verbose", 9) == 0)
96 do_verbose = argv[cnt];
97 break;
102 ldargs = xmalloc(sizeof(char *) * (argc+2 + 2*(incremental != 1)));
104 ldargs[0] = ld_name;
105 ldargs[1] = "-r";
107 for (cnt = 1; cnt < argc; cnt++)
108 ldargs[cnt+1] = argv[cnt];
110 if (incremental != 1)
112 atexit(exitfunc);
115 !(tempoutput = make_temp_file(NULL)) ||
116 !(ldscriptname = make_temp_file(NULL)) ||
117 !(ldscriptfile = fopen(ldscriptname, "w"))
120 fatal(ldscriptname ? ldscriptname : "make_temp_file()", strerror(errno));
123 ldargs[cnt + 1] = "-o";
124 ldargs[cnt + 2] = tempoutput;
125 cnt += 2;
128 ldargs[cnt+1] = NULL;
130 docommandvp(ld_name, ldargs);
132 if (incremental == 1)
133 return EXIT_SUCCESS;
135 collect_sets(tempoutput, &setlist);
137 if (setlist != NULL)
138 fprintf(ldscriptfile, "EXTERN(__this_program_requires_symbol_sets_handling)\n");
140 fwrite(LDSCRIPT_PART1, sizeof(LDSCRIPT_PART1) - 1, 1, ldscriptfile);
141 emit_sets(setlist, ldscriptfile);
142 fwrite(LDSCRIPT_PART2, sizeof(LDSCRIPT_PART2) - 1, 1, ldscriptfile);
143 if (incremental == 0)
144 fputs("PROVIDE(SysBase = 0x515BA5E);\n", ldscriptfile);
145 fwrite(LDSCRIPT_PART3, sizeof(LDSCRIPT_PART3) - 1, 1, ldscriptfile);
147 fclose(ldscriptfile);
148 ldscriptfile = NULL;
150 docommandlp(ld_name, ld_name, "-r", "-o", output, tempoutput, "-T", ldscriptname, do_verbose, NULL);
152 if (incremental != 0)
153 return EXIT_SUCCESS;
155 if (!ignore_undefined_symbols && check_and_print_undefined_symbols(output))
157 remove(output);
158 return EXIT_FAILURE;
161 chmod(output, 0766);
163 if (strip_all)
165 docommandlp(strip_name, strip_name, "--strip-unneeded", output, NULL);
168 return 0;