Kconfig: Write tmp files into same directory as target files

This removes the need for COREBOOT_BUILD_DIR in Kconfig. Since the
original files will be replaced with the tmp file, the parent directory
already needs to be writable.

Before this change, the tmp files would be created in the CWD (src) if
COREBOOT_BUILD_DIR was not specified.

BUG=b:112267918
TEST=emerge-grunt coreboot and verified no tmp files were created in the
src directory.

Change-Id: Icdaf2ff3dd1ec98813b75ef55b96e38e1ca19ec7
Signed-off-by: Raul E Rangel <rrangel@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/34244
Reviewed-by: Martin Roth <martinroth@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Raul E Rangel 2019-07-25 15:49:52 -06:00 committed by Patrick Georgi
parent d2f90a0659
commit 7b2deddbb0
2 changed files with 63 additions and 52 deletions

View File

@ -73,9 +73,6 @@ These variables are typically set in the makefiles or on the make command line.
These variables were added to Kconfig specifically for coreboot and are not These variables were added to Kconfig specifically for coreboot and are not
included in the Linux version. included in the Linux version.
- COREBOOT_BUILD_DIR=path for temporary files. This is used by coreboots
abuild tool.
- KCONFIG_STRICT=value. Define to enable warnings as errors. This is enabled - KCONFIG_STRICT=value. Define to enable warnings as errors. This is enabled
in coreboot, and should not be changed. in coreboot, and should not be changed.

View File

@ -764,6 +764,16 @@ next_menu:
return 0; return 0;
} }
int conf_mktemp(const char *path, char *tmpfile)
{
if (snprintf(tmpfile, PATH_MAX, "%s.tmp.XXXXXX", path) >= PATH_MAX) {
errno = EOVERFLOW;
return -1;
}
return mkstemp(tmpfile);
}
int conf_write(const char *name) int conf_write(const char *name)
{ {
FILE *out; FILE *out;
@ -806,7 +816,7 @@ int conf_write(const char *name)
sprintf(newname, "%s%s", dirname, basename); sprintf(newname, "%s%s", dirname, basename);
env = getenv("KCONFIG_OVERWRITECONFIG"); env = getenv("KCONFIG_OVERWRITECONFIG");
if (!env || !*env) { if (!env || !*env) {
sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); conf_mktemp(newname, tmpname);
out = fopen(tmpname, "w"); out = fopen(tmpname, "w");
} else { } else {
*tmpname = 0; *tmpname = 0;
@ -991,7 +1001,6 @@ out:
int conf_write_autoconf(void) int conf_write_autoconf(void)
{ {
struct symbol *sym; struct symbol *sym;
const char *name;
FILE *out, *tristate, *out_h; FILE *out, *tristate, *out_h;
int i; int i;
int print_negatives; int print_negatives;
@ -1008,49 +1017,41 @@ int conf_write_autoconf(void)
if (conf_split_config()) if (conf_split_config())
return 1; return 1;
char *tmpconfig_name = malloc(PATH_MAX); char tmpconfig_name[PATH_MAX];
if (getenv("COREBOOT_BUILD_DIR")) { const char *config_name = conf_get_autoconfig_name();
sprintf(tmpconfig_name, "%s/.tmpconfig.XXXXXX",
getenv("COREBOOT_BUILD_DIR")); i = conf_mktemp(config_name, tmpconfig_name);
} else { if (i == -1)
tmpconfig_name = strdup(".tmpconfig.XXXXXX"); goto error_auto_conf_cmd_tmp;
}
if ((i = mkstemp(tmpconfig_name)) == -1)
return 1;
out = fdopen(i, "w"); out = fdopen(i, "w");
if (!out) if (!out)
return 1; goto error_auto_conf_cmd_open;
char tmpconfig_triname[PATH_MAX];
const char *config_triname = getenv("KCONFIG_TRISTATE");
if (!config_triname)
config_triname = "include/config/tristate.conf";
i = conf_mktemp(config_triname, tmpconfig_triname);
if (i == -1)
goto error_tristate_tmp;
char *tmpconfig_triname = malloc(PATH_MAX);
if (getenv("COREBOOT_BUILD_DIR")) {
sprintf(tmpconfig_triname, "%s/.tmpconfig_tristate.XXXXXX",
getenv("COREBOOT_BUILD_DIR"));
} else {
tmpconfig_triname = strdup(".tmpconfig_tristate.XXXXXX");
}
if ((i = mkstemp(tmpconfig_triname)) == -1)
return 1;
tristate = fdopen(i, "w"); tristate = fdopen(i, "w");
if (!tristate) { if (!tristate)
fclose(out); goto error_tristate_open;
return 1;
} char tmpconfig_h[PATH_MAX];
const char *config_h = getenv("KCONFIG_AUTOHEADER");
if (!config_h)
config_h = "include/generated/autoconf.h";
i = conf_mktemp(config_h, tmpconfig_h);
if (i == -1)
goto error_auto_conf_h_tmp;
char *tmpconfig_h = malloc(PATH_MAX);
if (getenv("COREBOOT_BUILD_DIR")) {
sprintf(tmpconfig_h, "%s/.tmpconfig_tristate.XXXXXX",
getenv("COREBOOT_BUILD_DIR"));
} else {
tmpconfig_h = strdup(".tmpconfig_tristate.XXXXXX");
}
if ((i = mkstemp(tmpconfig_h)) == -1)
return 1;
out_h = fdopen(i, "w"); out_h = fdopen(i, "w");
if (!out_h) { if (!out_h)
fclose(out); goto error_auto_conf_h_open;
fclose(tristate);
return 1;
}
conf_write_heading(out, &kconfig_printer_cb, NULL); conf_write_heading(out, &kconfig_printer_cb, NULL);
@ -1084,25 +1085,38 @@ int conf_write_autoconf(void)
fclose(tristate); fclose(tristate);
fclose(out_h); fclose(out_h);
name = getenv("KCONFIG_AUTOHEADER"); if (rename(tmpconfig_h, config_h))
if (!name)
name = "include/generated/autoconf.h";
if (rename(tmpconfig_h, name))
return 1; return 1;
name = getenv("KCONFIG_TRISTATE");
if (!name) if (rename(tmpconfig_triname, config_triname))
name = "include/config/tristate.conf";
if (rename(tmpconfig_triname, name))
return 1; return 1;
name = conf_get_autoconfig_name();
/* /*
* This must be the last step, kbuild has a dependency on auto.conf * This must be the last step, kbuild has a dependency on auto.conf
* and this marks the successful completion of the previous steps. * and this marks the successful completion of the previous steps.
*/ */
if (rename(tmpconfig_name, name)) if (rename(tmpconfig_name, config_name))
return 1; return 1;
return 0; return 0;
error_auto_conf_h_open:
unlink(tmpconfig_h);
error_auto_conf_h_tmp:
fclose(tristate);
error_tristate_open:
unlink(tmpconfig_triname);
error_tristate_tmp:
fclose(out);
error_auto_conf_cmd_open:
unlink(tmpconfig_name);
error_auto_conf_cmd_tmp:
return 1;
} }
static int sym_change_count; static int sym_change_count;