18 //#define GDC_DEPS_DEBUG
21 extern LIST
*var_get (const char *symbol
);
24 static LIST
*processDepFile (const char *fname
) {
26 FILE *fl
= fopen(fname
, "r");
27 if (fl
== NULL
) return NULL
;
30 fseek(fl
, 0, SEEK_END
);
32 if (fsize
<= 0) { fclose(fl
); return NULL
; }
33 fseek(fl
, 0, SEEK_SET
);
34 fcont
= calloc(fsize
+2, 1);
35 fread(fcont
, fsize
, 1, fl
);
37 uint8_t *tmp
= (uint8_t *)fcont
;
40 printf("{{%s}}\n", tmp
);
42 while (*tmp
&& *tmp
!= ':') ++tmp
;
43 if (tmp
[0] && tmp
[1]) {
46 while (*tmp
&& (tmp
[0] <= 32 || tmp
[0] == '\\')) ++tmp
;
47 if (!tmp
[0]) goto quit
;
49 printf("[%s]\n", tmp
);
52 while (*e
&& e
[0] > ' ' && e
[0] != '\\') ++e
;
54 res
= list_new(res
, (void *)tmp
, 0); // don't copy it
56 printf(" {%s}\n", tmp
);
68 #define ADDARG(a_) do { \
69 if (argCount >= 510) goto error_quit; \
70 args[argCount++] = (a_); \
74 #define ADDARGPF(...) do { \
75 if (argCount >= 510) goto error_quit; \
76 asprintf(&args[argCount], __VA_ARGS__); \
82 #define ADDINCS(vname_) do { \
83 for (const LIST *hdrs = var_get(vname_); hdrs != NULL; hdrs = hdrs->next) { \
84 if (hdrs->string[0]) { \
85 ADDARGPF("-I%s", hdrs->string); \
86 ADDARGPF("-J%s", hdrs->string); \
93 // NULL: can't, do text scan
94 LIST
*gdcDeps (const char *srcfname
) {
96 const char *compilerBin
= NULL
;
97 static char *args
[512];
100 strcpy(tmpFName
, "/tmp/jamgdctmp_XXXXXX");
101 // get out if here if D compiler is not 'gdc'
103 const LIST
*gdc
= var_get("GDC");
104 if (gdc
== NULL
|| gdc
->string
== NULL
) return NULL
;
105 if (strcmp(gdc
->string
, "gdc") == 0) {
110 gdc
= var_get("ENABLE_GDC_DEPS");
111 if (gdc
== NULL
|| gdc
->string
== NULL
|| !gdc
->string
[0]) return NULL
;
115 int fd
= mkstemp(tmpFName
);
116 if (fd
< 0) return NULL
;
119 ADDARG(strdup(compilerBin
));
120 ADDARG(strdup("-c"));
121 ADDARG(strdup("-o"));
122 ADDARG(strdup("/dev/null"));
123 // copy '-fdebug' flags
124 for (const LIST
*flg
= var_get("GDCFLAGS.all"); flg
!= NULL
; flg
= flg
->next
) {
125 if (strncmp(flg
->string
, "-fdebug", 7) == 0) ADDARG(strdup(flg
->string
));
128 for (const LIST
*hdrs
= var_get("HDRS"); hdrs
!= NULL
; hdrs
= hdrs
->next
) {
129 if (hdrs
->string
[0]) {
130 ADDARGPF("-I%s", hdrs
->string
);
131 ADDARGPF("-J%s", hdrs
->string
);
135 static char zpath
[8192];
137 for (const LIST
*hdrs
= var_get("SUBDIR_TOKENS"); hdrs
!= NULL
; hdrs
= hdrs
->next
) {
138 if (hdrs
->string
[0]) {
140 strcat(zpath
, hdrs
->string
);
141 ADDARGPF("-I%s", zpath
);
142 ADDARGPF("-J%s", zpath
);
145 // add input file path to includes
146 strcpy(zpath
, srcfname
);
148 char *t
= strrchr(zpath
, '/');
151 ADDARGPF("-I%s", zpath
);
152 ADDARGPF("-J%s", zpath
);
156 ADDARGPF("-fmake-mdeps=%s", tmpFName
);
158 ADDARG(strdup(srcfname
));
160 args
[argCount
] = NULL
;
161 #ifdef GDC_DEPS_DEBUG
163 for (int f
= 0; f
< argCount
; ++f
) printf("%d: [%s]\n", f
, args
[f
]);
164 for (int f
= 0; f
< argCount
; ++f
) printf("%s ", args
[f
]); printf("\n");
167 pid_t child
= fork();
168 if (child
== (pid_t
)-1) goto error_quit
;
171 // close stdout and stderr
176 //fprintf(stderr, "SHIT!!!\n");
180 waitpid(child
, &status
, 0);
181 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) goto error_quit
;
182 #ifdef GDC_DEPS_DEBUG
183 printf("OK! %s\n", tmpFName
);
185 res
= processDepFile(tmpFName
);
189 while (--argCount
>= 0) free(args
[argCount
]);