+static void WriteDep (FILE* F, FileType Types)
+/* Helper function. Writes all file names that match Types to the output */
+{
+ unsigned I;
+
+ /* Loop over all files */
+ for (I = 0; I < CollCount (&FileTab); ++I) {
+
+ const StrBuf* Filename;
+
+ /* Get the next input file */
+ const FileEntry* E = (const FileEntry*) CollAt (&FileTab, I);
+
+ /* Ignore it if it is not of the correct type */
+ if ((E->Type & Types) == 0) {
+ continue;
+ }
+
+ /* If this is not the first file, add a space */
+ if (I > 0) {
+ fputc (' ', F);
+ }
+
+ /* Print the dependency */
+ Filename = GetStrBuf (E->Name);
+ fprintf (F, "%*s", SB_GetLen (Filename), SB_GetConstBuf (Filename));
+ }
+}
+
+
+
+static void CreateDepFile (const char* Name, FileType Types)
+/* Create a dependency file with the given name and place dependencies for
+ * all files with the given types there.
+ */
+{
+ /* Open the file */
+ FILE* F = fopen (Name, "w");
+ if (F == 0) {
+ Fatal ("Cannot open dependency file `%s': %s", Name, strerror (errno));
+ }
+
+ /* Print the output file followed by a tab char */
+ fprintf (F, "%s:\t", OutFile);
+
+ /* Write out the dependencies for the output file */
+ WriteDep (F, Types);
+ fputs ("\n\n", F);
+
+ /* Write out a phony dependency for the included files */
+ WriteDep (F, Types);
+ fputs (":\n\n", F);
+
+ /* Close the file, check for errors */
+ if (fclose (F) != 0) {
+ remove (Name);
+ Fatal ("Cannot write to dependeny file (disk full?)");
+ }
+}
+
+
+
+void CreateDependencies (void)
+/* Create dependency files requested by the user */
+{
+ if (SB_NotEmpty (&DepName)) {
+ CreateDepFile (SB_GetConstBuf (&DepName),
+ FT_MAIN | FT_INCLUDE | FT_BINARY);
+ }
+ if (SB_NotEmpty (&FullDepName)) {
+ CreateDepFile (SB_GetConstBuf (&FullDepName),
+ FT_MAIN | FT_INCLUDE | FT_BINARY | FT_DBGINFO);
+ }
+}
+
+