diff --git a/README.md b/README.md index 9df5a55..f6c20c9 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,12 @@ gcc -fplugin=externis -fplugin-arg-externis-trace=SOME_PATH/ # Otherwise: gcc -fplugin=/PATH/TO/build/externis.so -fplugin-arg-externis-trace=SOME_PATH/trace.json ``` -If a trace output path is not given, a temporary file with the name +Alternatively, you can specify a directory to write the files to: +```bash +gcc -fplugin=/PATH/TO/build/externis.so -fplugin-arg-externis-trace-dir=SOME_PATH/ +``` +In which case the output will be written to SOME_PATH/trace_XXXXXX.json +If a trace output path or directory is not given, a temporary file with the name `/tmp/trace_XXXXXX.json` will be used instead. ## License & Copyright diff --git a/externis.cc b/externis.cc index a5864aa..43df4ff 100644 --- a/externis.cc +++ b/externis.cc @@ -102,6 +102,7 @@ static const char *PLUGIN_NAME = "externis"; bool setup_output(int argc, plugin_argument *argv) { const char *flag_name = "trace"; + const char *dir_flag_name = "trace-dir"; // TODO: Maybe make the default filename related to the source filename. // TODO: Validate we only compile one TU at a time. FILE *trace_file = nullptr; @@ -113,22 +114,25 @@ bool setup_output(int argc, plugin_argument *argv) { return false; } trace_file = fdopen(fd, "w"); - } else if (argc == 1) { - if (strcmp(argv[0].key, flag_name)) { - fprintf(stderr, - "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME", - PLUGIN_NAME, flag_name); - return false; - } + } else if (argc == 1 && !strcmp(argv[0].key, flag_name)) { trace_file = fopen(argv[0].value, "w"); if (!trace_file) { fprintf(stderr, "Externis Error! Couldn't open %s for writing\n", argv[0].value); } + } else if (argc == 1 && !strcmp(argv[0].key, dir_flag_name)) { + std::string file_template{argv[0].value}; + file_template += "/trace_XXXXXX.json"; + int fd = mkstemps(file_template.data(), 5); + if (fd == -1) { + perror("Externis mkstemps error: "); + return false; + } + trace_file = fdopen(fd, "w"); } else { fprintf(stderr, - "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME\n", - PLUGIN_NAME, flag_name); + "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME or -fplugin-arg-%s-%s=DIRECTORY\n", + PLUGIN_NAME, flag_name, PLUGIN_NAME, dir_flag_name); return false; } if (trace_file) {