cmSystemTools: Add EnvDiff class to hold ENVIRONMENT_MODIFICATION logic

Prepare to re-use this logic when enhancing `cmake -E env`.
This commit is contained in:
Alex Reinking
2022-08-14 02:28:24 -04:00
committed by Brad King
parent a0b1c4ee90
commit bfa1c5285b
3 changed files with 154 additions and 121 deletions
+27
View File
@@ -6,9 +6,12 @@
#include <cstddef>
#include <functional>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <cm/optional>
#include <cm/string_view>
#include "cmsys/Process.h"
@@ -377,6 +380,30 @@ public:
/** Append multiple variables to the current environment. */
static void AppendEnv(std::vector<std::string> const& env);
/**
* Helper class to represent an environment diff directly. This is to avoid
* repeated in-place environment modification (i.e. via setenv/putenv), which
* could be slow.
*/
class EnvDiff
{
public:
/**
* Apply an ENVIRONMENT_MODIFICATION operation to this diff. Returns
* false and issues an error on parse failure.
*/
bool ParseOperation(const std::string& envmod);
/**
* Apply this diff to the actual environment, optionally writing out the
* modifications to a CTest-compatible measurement stream.
*/
void ApplyToCurrentEnv(std::ostringstream* measurement = nullptr);
private:
std::map<std::string, cm::optional<std::string>> diff;
};
/** Helper class to save and restore the environment.
Instantiate this class as an automatic variable on
the stack. Its constructor saves a copy of the current