From fe6248e5ebe7db35a006ba9c7e22b6b13fc52d00 Mon Sep 17 00:00:00 2001 From: "Andrew V. Jones" Date: Sun, 21 Mar 2021 20:55:27 +0000 Subject: [PATCH 1/4] Implementing support for preprocessing large files #9 Signed-off-by: Andrew V. Jones --- tools/CompilerFacade.cpp | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tools/CompilerFacade.cpp b/tools/CompilerFacade.cpp index 9e5fda8f..b93bc2ad 100644 --- a/tools/CompilerFacade.cpp +++ b/tools/CompilerFacade.cpp @@ -23,6 +23,9 @@ #include "Process.h" #include +#include +#include +#include /* for getpid */ using namespace psy; @@ -38,15 +41,35 @@ CompilerFacade::CompilerFacade(const std::string& hostCC, std::pair CompilerFacade::preprocess(const std::string& source) { - std::string in = "cat << 'EOF' | "; - in += hostCC_; + // get the current process id + auto curr_pid = getpid(); + + // Base filename + auto base_name("psyche_" + std::to_string(curr_pid)); + + // Temporary directory name + auto tmp_dir(std::filesystem::temp_directory_path()); + + // Temporary filename + auto tmp_name(tmp_dir / base_name); + + // Write our source to our temporary file + std::ofstream tmp_stream(tmp_name); + tmp_stream << source; + + std::string in = hostCC_; in += macroSetup(); in += " "; in += "-std=" + std_ + " "; - in += "-E -x c -CC -"; - in += "\n" + source + "\nEOF"; + in += "-E -x c -CC "; + in += tmp_name; + + auto ret = Process().execute(in); + + // remove the temporary file + std::remove(tmp_name.c_str()); - return Process().execute(in); + return ret; } std::string CompilerFacade::macroSetup() const From 60fe8886e90e08f6a5d0cd840c16896f97140fec Mon Sep 17 00:00:00 2001 From: "Andrew V. Jones" Date: Mon, 22 Mar 2021 08:39:42 +0000 Subject: [PATCH 2/4] Move preprocessor code to use 'mkstemp' Signed-off-by: Andrew V. Jones --- tools/CompilerFacade.cpp | 47 ++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/tools/CompilerFacade.cpp b/tools/CompilerFacade.cpp index b93bc2ad..7600cf10 100644 --- a/tools/CompilerFacade.cpp +++ b/tools/CompilerFacade.cpp @@ -22,10 +22,10 @@ #include "Process.h" -#include -#include +#include #include -#include /* for getpid */ +#include +#include using namespace psy; @@ -41,22 +41,46 @@ CompilerFacade::CompilerFacade(const std::string& hostCC, std::pair CompilerFacade::preprocess(const std::string& source) { - // get the current process id - auto curr_pid = getpid(); + // Template base filename to pass to 'mkstemp' + const std::string base_template("psyche_XXXXXX"); + + // Default temporary location + const std::string default_dir("/tmp"); + + // Read the environment + const char *env_tmp_dir = std::getenv("TMPDIR"); + + // Calculat temporary directory + const std::string tmp_dir(env_tmp_dir != nullptr ? env_tmp_dir : default_dir); + + // Calculate the full path + const std::string pathed_template(tmp_dir + "/" + base_template); + + // now get a char* to be able to call mkstemp + char *tmp_template = new char[pathed_template.size() + 1]; + std::copy(pathed_template.begin(), pathed_template.end(), tmp_template); + tmp_template[pathed_template.size()] = '\0'; + + // Convert our template into a full-file + int err = mkstemp(tmp_template); - // Base filename - auto base_name("psyche_" + std::to_string(curr_pid)); + // mkstemp returns -1 on error ... + if (err == -1) { + // ... so do we + return std::make_pair(err, ""); + } - // Temporary directory name - auto tmp_dir(std::filesystem::temp_directory_path()); + // Let's get a std::string for convenience + std::string tmp_name(tmp_template); - // Temporary filename - auto tmp_name(tmp_dir / base_name); + // Delete our template + delete[] tmp_template; // Write our source to our temporary file std::ofstream tmp_stream(tmp_name); tmp_stream << source; + // build-up the preprocessor invocation std::string in = hostCC_; in += macroSetup(); in += " "; @@ -64,6 +88,7 @@ std::pair CompilerFacade::preprocess(const std::string& source in += "-E -x c -CC "; in += tmp_name; + // call the preprocessor auto ret = Process().execute(in); // remove the temporary file From 9e5541323843b5d95961e0d1122cea007ea4d8a3 Mon Sep 17 00:00:00 2001 From: "Andrew V. Jones" Date: Mon, 22 Mar 2021 08:54:51 +0000 Subject: [PATCH 3/4] Make sure temporary file is closed and flushed, before calling preprocessor Signed-off-by: Andrew V. Jones --- tools/CompilerFacade.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/CompilerFacade.cpp b/tools/CompilerFacade.cpp index 7600cf10..743bae5f 100644 --- a/tools/CompilerFacade.cpp +++ b/tools/CompilerFacade.cpp @@ -79,6 +79,7 @@ std::pair CompilerFacade::preprocess(const std::string& source // Write our source to our temporary file std::ofstream tmp_stream(tmp_name); tmp_stream << source; + tmp_stream.close(); // build-up the preprocessor invocation std::string in = hostCC_; From ff3d4784d9d0507116237c040d7be4b5fc6de02f Mon Sep 17 00:00:00 2001 From: "Andrew V. Jones" Date: Mon, 22 Mar 2021 08:57:56 +0000 Subject: [PATCH 4/4] Fixing typos in comments around preprocessor invocation Signed-off-by: Andrew V. Jones --- tools/CompilerFacade.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/CompilerFacade.cpp b/tools/CompilerFacade.cpp index 743bae5f..6c424ee2 100644 --- a/tools/CompilerFacade.cpp +++ b/tools/CompilerFacade.cpp @@ -50,18 +50,18 @@ std::pair CompilerFacade::preprocess(const std::string& source // Read the environment const char *env_tmp_dir = std::getenv("TMPDIR"); - // Calculat temporary directory + // Calculate temporary directory const std::string tmp_dir(env_tmp_dir != nullptr ? env_tmp_dir : default_dir); // Calculate the full path const std::string pathed_template(tmp_dir + "/" + base_template); - // now get a char* to be able to call mkstemp + // now get a **non-const** char* to be able to call mkstemp char *tmp_template = new char[pathed_template.size() + 1]; std::copy(pathed_template.begin(), pathed_template.end(), tmp_template); tmp_template[pathed_template.size()] = '\0'; - // Convert our template into a full-file + // Convert our template into a completed filename int err = mkstemp(tmp_template); // mkstemp returns -1 on error ... @@ -73,10 +73,10 @@ std::pair CompilerFacade::preprocess(const std::string& source // Let's get a std::string for convenience std::string tmp_name(tmp_template); - // Delete our template + // Delete our modifiable template delete[] tmp_template; - // Write our source to our temporary file + // Write the input source to our temporary file std::ofstream tmp_stream(tmp_name); tmp_stream << source; tmp_stream.close(); @@ -95,6 +95,7 @@ std::pair CompilerFacade::preprocess(const std::string& source // remove the temporary file std::remove(tmp_name.c_str()); + // return the output from the preprocessor return ret; }