Force coreboot mconf to create temp files in the output directory
[coreboot.git] / util / kconfig / confdata.c
index 3bbad3010a390c08777cd5526f0faf6b3e9dd9cf..df39fcc2431a773deeaff814762c3203600d3622 100644 (file)
@@ -674,6 +674,33 @@ out:
        return res;
 }
 
+/*
+ * Return malloced string the contents of which are a concatenation of the
+ * directory name of the first argument and the second argument.
+ */
+static char* get_tmp_file_name(const char* base, const char *tmp_name)
+{
+        char *file_name, *p;
+        /* for sure a few bytes longer than needed */
+        int file_name_size = strlen(base) + sizeof(file_name) + 10;
+
+        file_name = malloc(file_name_size);
+        if (!file_name) {
+                fprintf(stderr, "%s:%d: failed to allocate %d bytes\n",
+                        __FILE__, __LINE__, file_name_size);
+               return 0;
+        }
+
+        strcpy(file_name, base);
+        p = strrchr(file_name, '/');
+        if (p)
+                strcpy(p + 1, tmp_name);
+        else
+                strcpy(file_name, tmp_name);
+
+        return file_name;
+}
+
 int conf_write_autoconf(void)
 {
        struct symbol *sym;
@@ -682,9 +709,19 @@ int conf_write_autoconf(void)
        FILE *out, *out_h;
        time_t now;
        int i, l;
+       char *tmp_conf, *tmp_conf_h;
 
        sym_clear_all_valid();
 
+       name = getenv("KCONFIG_AUTOHEADER");
+       if (!name)
+               name = "include/linux/autoconf.h";
+
+       tmp_conf = get_tmp_file_name(name, ".tmpconfig");
+       tmp_conf_h = get_tmp_file_name(name, ".tmpconfig.h");
+       if (!tmp_conf || !tmp_conf_h)
+               return 1;
+
        file_write_dep("build/auto.conf.cmd");
 
 #if 0
@@ -692,11 +729,11 @@ int conf_write_autoconf(void)
                return 1;
 #endif
 
-       out = fopen(".tmpconfig", "w");
+       out = fopen(tmp_conf, "w");
        if (!out)
                return 1;
 
-       out_h = fopen(".tmpconfig.h", "w");
+       out_h = fopen(tmp_conf_h, "w");
        if (!out_h) {
                fclose(out);
                return 1;
@@ -721,8 +758,15 @@ int conf_write_autoconf(void)
 
        for_all_symbols(i, sym) {
                sym_calc_value(sym);
-               if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
+               if (!sym->name)
+                       continue;
+               if (!(sym->flags & SYMBOL_WRITE)) {
+                       if (sym->type == S_BOOLEAN || sym->type == S_HEX
+                           || sym->type == S_INT)
+                               fprintf(out_h, "#define CONFIG_%s 0\n",
+                                       sym->name);
                        continue;
+               }
                switch (sym->type) {
                case S_BOOLEAN:
                case S_TRISTATE:
@@ -780,11 +824,8 @@ int conf_write_autoconf(void)
        fclose(out);
        fclose(out_h);
 
-       name = getenv("KCONFIG_AUTOHEADER");
-       if (!name)
-               name = "include/linux/autoconf.h";
        UNLINK_IF_NECESSARY(name);
-       if (rename(".tmpconfig.h", name))
+       if (rename(tmp_conf_h, name))
                return 1;
        name = getenv("KCONFIG_AUTOCONFIG");
        if (!name)
@@ -794,9 +835,11 @@ int conf_write_autoconf(void)
         * and this marks the successful completion of the previous steps.
         */
        UNLINK_IF_NECESSARY(name);
-       if (rename(".tmpconfig", name))
+       if (rename(tmp_conf, name))
                return 1;
 
+       free(tmp_conf_h);
+       free(tmp_conf);
        return 0;
 }