annotate sources/toys/ccwrap.c @ 1665:98311ea2e4c2

Fix --print-file-name=blah.a
author Rob Landley <rob@landley.net>
date Wed, 02 Jul 2014 20:42:45 -0500
parents 35b8949e9d9c
children f63305aa4961
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
1 /* by Rob Landley <rob@landley.net>
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * C compiler wrapper. Parses command line, supplies path information for
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * headers and libraries.
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
5 *
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
6 * This file is hereby released into the public domain.
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 */
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 #undef _FORTIFY_SOURCE
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
1662
970b405bd6e6 Fix thinkos in new ccwrap.
Rob Landley <rob@landley.net>
parents: 1661
diff changeset
11 #include <errno.h>
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 #include <libgen.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 #include <stdarg.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 #include <stdio.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 #include <stdlib.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 #include <string.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 #include <sys/stat.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 #include <sys/types.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #include <unistd.h>
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
20
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 // Default to musl
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #ifndef DYNAMIC_LINKER
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #define DYNAMIC_LINKER "/lib/libc.so"
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 #endif
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 // Some plumbing from toybox
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 void *xmalloc(long len)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 void *ret = malloc(len);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 if (!ret) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 fprintf(stderr, "bad malloc\n");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 exit(1);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 }
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
36
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
37 return ret;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
39
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 // Die unless we can allocate enough space to sprintf() into.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 char *xmprintf(char *format, ...)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 va_list va, va2;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 int len;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 char *ret;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 va_start(va, format);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 va_copy(va2, va);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 // How long is it?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 len = vsnprintf(0, 0, format, va);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 len++;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 va_end(va);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
54
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 // Allocate and do the sprintf()
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 ret = xmalloc(len);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 vsnprintf(ret, len, format, va2);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 va_end(va2);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
59
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 return ret;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 // Confirm that a regular file exists, and (optionally) has the executable bit.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 int is_file(char *filename, int has_exe)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 // Confirm it has the executable bit set, if necessary.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 if (!has_exe || !access(filename, X_OK)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 struct stat st;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
69
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 // Confirm it exists and is not a directory.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 if (!stat(filename, &st) && S_ISREG(st.st_mode)) return 1;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 return 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
75
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 // Find a file in a colon-separated path
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 char *find_in_path(char *path, char *filename, int has_exe)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 {
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
79 char *cwd;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
80
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 if (index(filename, '/') && is_file(filename, has_exe))
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 return realpath(filename, 0);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
83
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
84 if (!path || !(cwd = getcwd(0, 0))) return 0;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 while (path) {
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
86 char *str, *next = index(path, ':');
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 int len = next ? next-path : strlen(path);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
88
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 if (!len) str = xmprintf("%s/%s", cwd, filename);
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
90 else str = xmprintf("%.*s/%s", len, path, filename);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
91
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 // If it's not a directory, return it.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 if (is_file(str, has_exe)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 char *s = realpath(str, 0);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 free(str);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 free(cwd);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
98
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 return s;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 } else free(str);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
101
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 if (next) next++;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 path = next;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 free(cwd);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
106
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 return NULL;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
109
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 struct dlist {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 struct dlist *next, *prev;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 char *str;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 };
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
114
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 // Append to end of doubly linked list (in-order insertion)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 void dlist_add(struct dlist **list, char *str)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 struct dlist *new = xmalloc(sizeof(struct dlist));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
119
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
120 new->str = str;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 if (*list) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 new->next = *list;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 new->prev = (*list)->prev;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 (*list)->prev->next = new;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 (*list)->prev = new;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 } else *list = new->next = new->prev = new;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
127
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 *list = new;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
130
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 // Some compiler versions don't provide separate T and S versions of begin/end,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 // so fall back to the base version if they're not there.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
133
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 char *find_TSpath(char *base, char *top, int use_shared, int use_static_linking)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 int i;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 char *temp;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
138
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 temp = xmprintf(base, top,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 use_shared ? "S.o" : use_static_linking ? "T.o" : ".o");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
141
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 if (!is_file(temp, 0)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 free(temp);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 temp = xmprintf(base, top, ".o");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
146
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 return temp;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
149
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
150
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 enum {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 Clibccso, Clink, Cprofile, Cshared, Cstart, Cstatic, Cstdinc, Cstdlib,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 Cverbose, Cx, Cdashdash,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
154
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 CPctordtor, CP, CPstdinc
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 };
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
157
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 #define MASK_BIT(X) (1<<X)
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
159 #define SET_FLAG(X) (flags |= MASK_BIT(X))
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
160 #define CLEAR_FLAG(X) (flags &= ~MASK_BIT(X))
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
161 #define GET_FLAG(X) (flags & MASK_BIT(X))
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
162
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 // Read the command line arguments and work out status
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 int main(int argc, char *argv[])
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 char *topdir, *ccprefix, *dynlink, *cc, *temp, **keepv, **hdr, **outv;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 int i, keepc, srcfiles, flags, outc;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 struct dlist *libs = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
169
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 keepv = xmalloc(argc*sizeof(char *));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 flags = MASK_BIT(Clink)|MASK_BIT(Cstart)|MASK_BIT(Cstdinc)|MASK_BIT(Cstdlib)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 |MASK_BIT(CPctordtor);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 keepc = srcfiles = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
174
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 if (getenv("CCWRAP_DEBUG")) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 SET_FLAG(Cverbose);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 fprintf(stderr, "incoming: ");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 for (i=0; i<argc; i++) fprintf(stderr, "%s ", argv[i]);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 fprintf(stderr, "\n\n");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
181
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
182 // figure out cross compiler prefix
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
183 i = strlen(ccprefix = basename(*argv));
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
184 if (i<2) {
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
185 fprintf(stderr, "Bad name '%s'\n", ccprefix);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
186 exit(1);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
187 }
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
188 if (!strcmp("++", ccprefix+i-2)) {
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
189 cc = "raw++";
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
190 SET_FLAG(CP);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
191 SET_FLAG(CPstdinc);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
192 if (i<3) exit(1);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
193 i -= 3;
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
194 } else if (!strcmp("gcc", ccprefix+i-3)) i -= 3; // TODO: yank
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
195 else if (!strcmp("cc", ccprefix+i-2)) i-=2;
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
196 else if (!strcmp("cpp", ccprefix+i-3)) {
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
197 i -= 3;
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
198 CLEAR_FLAG(Clink);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
199 } else return 1; // TODO: wrap ld
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
200 if (!(ccprefix = strndup(ccprefix, i))) exit(1);
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
201
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 // Find the cannonical path to the directory containing our executable
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 topdir = find_in_path(getenv("PATH"), *argv, 1);
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
204
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
205 if (!topdir || !(temp = rindex(topdir, '/')) || strlen(*argv)<2) {
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 fprintf(stderr, "Can't find %s in $PATH (did you export it?)\n", *argv);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 exit(1);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 }
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
209 // We want to strip off the bin/ but the path we followed can end with
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
210 // a symlink, so append .. instead.
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
211 strcpy(++temp, "..");
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
212
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 // Add our binary's directory and the tools directory to $PATH so gcc's
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 // convulsive flailing probably blunders through here first.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 // Note: do this before reading topdir from environment variable, because
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 // toolchain binaries go with wrapper even if headers/libraries don't.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 temp = getenv("PATH");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 if (!temp) temp = "";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 temp = xmprintf("PATH=%s/bin:%s/tools/bin:%s", topdir, topdir, temp);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 putenv(temp);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
221
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 // Override header/library search path with environment variable?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 temp = getenv("CCWRAP_TOPDIR");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 if (!temp) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 cc = xmprintf("%sCCWRAP_TOPDIR", ccprefix);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
226
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 for (i=0; cc[i]; i++) if (cc[i] == '-') cc[i]='_';
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 temp = getenv(cc);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 free(cc);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 if (temp) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 free(topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 topdir = temp;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 }
1664
35b8949e9d9c Very basic musl support. Still needs debugging and to be enabled per-target.
Rob Landley <rob@landley.net>
parents: 1662
diff changeset
235
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 // Name of the C compiler we're wrapping.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 cc = getenv("CCWRAP_CC");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 if (!cc) cc = "rawcc";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
239
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 // Does toolchain have a shared libcc?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 temp = xmprintf("%s/lib/libgcc_s.so", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 if (is_file(temp, 0)) SET_FLAG(Clibccso);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 free(temp);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
244
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 // Where's the dynamic linker?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 temp = getenv("CCWRAP_DYNAMIC_LINKER");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 if (!temp) temp = DYNAMIC_LINKER;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 dynlink = xmprintf("-Wl,--dynamic-linker,%s", temp);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
249
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 // Fallback library search path, these will wind up at the end
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 dlist_add(&libs, xmprintf("%s/lib", topdir));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 dlist_add(&libs, xmprintf("%s/cc/lib", topdir));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
253
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 // Parse command line arguments
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
255 for (i=1; i<argc; i++) {
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 char *c = keepv[keepc++] = argv[i];
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
257
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 if (!strcmp(c, "--")) SET_FLAG(Cdashdash);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
259
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 // is this an option?
1658
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
261 if (*c == '-' && c[1] && !GET_FLAG(Cdashdash)) c++;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 else {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 srcfiles++;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 continue;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
266
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 // Second dash?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 if (*c == '-') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 // Passthrough double dash versions of single-dash options.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 if (!strncmp(c, "-print-", 7) || !strncmp(c, "-static", 7)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 || !strncmp(c, "-shared", 7)) c++;
1658
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
272 else {
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
273 if (!strcmp(c, "-no-ctors")) {
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
274 CLEAR_FLAG(CPctordtor);
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
275 keepc--;
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
276 }
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
277 continue;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
280
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 // -M and -MM imply -E and thus no linking.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 // Other -M? options don't, including -MMD
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 if (*c == 'M' && c[1] && (c[1] != 'M' || c[2])) continue;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
284
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
285 // compile, preprocess, assemble... options that suppress linking.
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
286 if (strchr("cEMS", *c)) CLEAR_FLAG(Clink);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 else if (*c == 'L') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 if (c[1]) dlist_add(&libs, c+1);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
289 else if (!argv[++i]) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 fprintf(stderr, "-L at end of args\n");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 exit(1);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 } else dlist_add(&libs, argv[i]);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
293 keepc--;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 } else if (*c == 'f') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
295 if (!strcmp(c, "fprofile-arcs")) SET_FLAG(Cprofile);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 } else if (*c == 'n') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 keepc--;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 if (!strcmp(c, "nodefaultlibs")) CLEAR_FLAG(Cstdlib);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 else if (!strcmp(c, "nostartfiles")) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 CLEAR_FLAG(CPctordtor);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 CLEAR_FLAG(Cstart);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 } else if (!strcmp(c, "nostdinc")) CLEAR_FLAG(Cstdinc);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 else if (!strcmp(c, "nostdinc++")) CLEAR_FLAG(CPstdinc);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 else if (!strcmp(c, "nostdlib")) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 CLEAR_FLAG(Cstdlib);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 CLEAR_FLAG(Cstart);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 CLEAR_FLAG(CPctordtor);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 } else keepc++;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 } else if (*c == 'p') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 if (!strncmp(c, "print-", 6)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 struct dlist *dl;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 int show = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
313
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 // Just add prefix to prog-name
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 if (!strncmp(c += 6, "prog-name=", 10)) {
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
316 printf("%s%s\n", ccprefix, c+10);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 exit(0);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
319
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
320 if (!strncmp(c, "file-name=", 10)) {
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
321 c += 10;
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
322 if (!strcmp(c, "include")) {
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
323 printf("%s/cc/include\n", topdir);
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
324 exit(0);
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
325 }
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
326 } else if (!strcmp(c, "search-dirs")) {
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 c = "";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 show = 1;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 printf("install: %s/\nprograms: %s\nlibraries:",
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 topdir, getenv("PATH"));
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
331 } else if (!strcmp(c, "libgcc-file-name")) {
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
332 printf("%s/cc/lib/libgcc.a\n", topdir);
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
333 exit(0);
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
334 } else break;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
335
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 // Adjust dlist before traversing (move fallback to end, break circle)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 libs = libs->next->next;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 libs->prev->next = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
339
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 // Either display the list, or find first hit.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 for (dl = libs; dl; dl = dl->next) {
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
342 temp = dl->str;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 if (show) printf(":%s" + (dl==libs), dl->str);
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
344 else {
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
345 if (*c) temp = xmprintf("%s/%s", dl->str, c);
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
346 if (!access(temp, F_OK)) break;
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
347 if (*c) free(temp);
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
348 }
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 }
1665
98311ea2e4c2 Fix --print-file-name=blah.a
Rob Landley <rob@landley.net>
parents: 1664
diff changeset
350 if (dl) printf("%s", temp);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 printf("\n");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
352
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 return 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 } else if (!strcmp(c, "pg")) SET_FLAG(Cprofile);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 } else if (*c == 's') {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
356 keepc--;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
357 if (!strcmp(c, "shared")) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 CLEAR_FLAG(Cstart);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
359 SET_FLAG(Cshared);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
360 } else if (!strcmp(c, "static")) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 SET_FLAG(Cstatic);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
362 CLEAR_FLAG(Clibccso);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 } else if (!strcmp(c, "shared-libgcc")) SET_FLAG(Clibccso);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 else if (!strcmp(c, "static-libgcc")) CLEAR_FLAG(Clibccso);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 else keepc++;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
366 } else if (*c == 'v' && !c[1]) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
367 SET_FLAG(Cverbose);
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
368 printf("ccwrap: %s\n", topdir);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 } else if (!strncmp(c, "Wl,", 3)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 temp = strstr(c, ",-static");
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
371 if (temp && (!temp[8] || temp[8]==',')) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 SET_FLAG(Cstatic);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 CLEAR_FLAG(Clibccso);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
374 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 // This argument specifies dynamic linker, so we shouldn't.
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
376 if (strstr(c, "--dynamic-linker")) dynlink = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 } else if (*c == 'x') SET_FLAG(Cx);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
379
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 // Initialize argument list for exec call
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
381
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 // what's a good outc size?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
383
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
384 outc = (argc+keepc+64)*sizeof(char *);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
385 memset(outv = xmalloc(outc), 0, outc);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
386 outc = 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 outv[outc++] = cc;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
388
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
389 // Are we linking?
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 if (srcfiles) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
391 outv[outc++] = "-nostdinc";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 if (GET_FLAG(CP)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
393 outv[outc++] = "-nostdinc++";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
394 if (GET_FLAG(CPstdinc)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
395 outv[outc++] = "-isystem";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
396 outv[outc++] = xmprintf("%s/c++/include", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
398 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 if (GET_FLAG(Cstdinc)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
400 outv[outc++] = "-isystem";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 outv[outc++] = xmprintf("%s/include", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
402 outv[outc++] = "-isystem";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
403 outv[outc++] = xmprintf("%s/cc/include", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
405 if (GET_FLAG(Clink)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 // Zab defaults, add dynamic linker
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
407 outv[outc++] = "-nostdlib";
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 outv[outc++] = GET_FLAG(Cstatic) ? "-static" : dynlink;
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
409 if (GET_FLAG(Cshared)) outv[outc++] = "-shared";
1658
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
410
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
411 // Copy libraries to output (first move fallback to end, break circle)
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 libs = libs->next->next;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 libs->prev->next = 0;
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
414 for (; libs; libs = libs->next)
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
415 outv[outc++] = xmprintf("-L%s", libs->str);
1658
38b18a4707dd More ccwrap debugging. "-" is an argument, not an option. Actually fall through to process --doubledash version of -singledash options. The -rpath-link line has the same visiblity as -L since it's link time path modification.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
416 outv[outc++] = xmprintf("-Wl,-rpath-link,%s/lib", topdir); // TODO: in?
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
417
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 // TODO: -fprofile-arcs
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 if (GET_FLAG(Cprofile)) xmprintf("%s/lib/gcrt1.o", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 if (GET_FLAG(CPctordtor)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 outv[outc++] = xmprintf("%s/lib/crti.o", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
422 outv[outc++] = find_TSpath("%s/cc/lib/crtbegin%s", topdir,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 GET_FLAG(Cshared), GET_FLAG(Cstatic));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 if (!GET_FLAG(Cprofile) && GET_FLAG(Cstart))
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
426 outv[outc++] = xmprintf("%s/lib/%scrt1.o", topdir,
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
427 GET_FLAG(Cshared) ? "S" : "");
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
429 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
430
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
431 // Copy unclaimed arguments
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 memcpy(outv+outc, keepv, keepc*sizeof(char *));
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
433 outc += keepc;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
434
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
435 if (srcfiles && GET_FLAG(Clink)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
436 if (GET_FLAG(Cx)) outv[outc++] = "-xnone";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 if (GET_FLAG(Cstdlib)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
438 if (GET_FLAG(CP)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
439 outv[outc++] = "-lstdc++";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 //outv[outc++] = "-lm";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
441 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
442
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
443 // libgcc can call libc which can call libgcc
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
444 outv[outc++] = "-Wl,--start-group,--as-needed";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
445 outv[outc++] = "-lgcc";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
446 if (GET_FLAG(Clibccso)) outv[outc++] = "-lgcc_s";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
447 else outv[outc++] = "-lgcc_eh";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 outv[outc++] = "-lc";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 outv[outc++] = "-Wl,--no-as-needed,--end-group";
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
450 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
451 if (GET_FLAG(CPctordtor)) {
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
452 outv[outc++] = find_TSpath("%s/cc/lib/crtend%s", topdir,
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
453 GET_FLAG(Cshared), GET_FLAG(Cstatic));
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
454 outv[outc++] = xmprintf("%s/lib/crtn.o", topdir);
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
455 }
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
456 }
1657
84e47eac0f28 First round of bug fixes for new ccwrap.
Rob Landley <rob@landley.net>
parents: 1656
diff changeset
457 outv[outc] = 0;
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
458
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
459 if (getenv("CCWRAP_DEBUG")) {
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
460 fprintf(stderr, "outgoing:");
1662
970b405bd6e6 Fix thinkos in new ccwrap.
Rob Landley <rob@landley.net>
parents: 1661
diff changeset
461 for(i=0; i<outc; i++) fprintf(stderr, " \"%s\"", outv[i]);
1660
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
462 fprintf(stderr, "\n");
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
463 }
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
464
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
465 execvp(*outv, outv);
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
466 fprintf(stderr, "%s: %s\n", *outv, strerror(errno));
c1addb6ebb84 More fixes from testing. Swap testing code for exec code.
Rob Landley <rob@landley.net>
parents: 1658
diff changeset
467 exit(1);
1656
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
468
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
469 return 0;
00928b6d7895 First pass at ccwrap rewrite for (among other things) musl support.
Rob Landley <rob@landley.net>
parents:
diff changeset
470 }