/************************************************************************* * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * See LICENSE for license information. ************************************************************************/ #ifndef TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_ #define TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_ #include #include #include #include #include #include #include "logging.h" namespace transformer_engine { namespace detail { /*! \brief Template specialization to get the env var for numeric data types */ template inline typename std::enable_if::value, T>::type getenv_helper( const char *variable, const T &default_value) { // Implementation for numeric types const char *env = std::getenv(variable); if (env == nullptr || env[0] == '\0') { return default_value; } T value; std::istringstream iss(env); iss >> value; NVTE_CHECK(iss, "Invalid environment variable value"); return value; } /*! \brief Template specialization to get the env var for string-like data types */ template inline typename std::enable_if::value, T>::type getenv_helper( const char *variable, const T &default_value) { // Implementation for string-like types const char *env = std::getenv(variable); if (env == nullptr || env[0] == '\0') { return default_value; } else { return env; } } /*! \brief Template specialization to get the default values for different * numeric data types */ template inline T getenv_default_value() { return 0; } /*! \brief Template specialization to get the default values for bool */ template <> inline bool getenv_default_value() { return false; } /*! \brief Template specialization to get the default values for string */ template <> inline std::string getenv_default_value() { return std::string(); } /*! \brief Template specialization to get the default values for filesystem * path data type */ template <> inline std::filesystem::path getenv_default_value() { return std::filesystem::path(); } } // namespace detail /*! \brief Get environment variable and convert to type * * If the environment variable is unset or empty, a falsy value is * returned. */ template inline T getenv(const char *variable) { return detail::getenv_helper(variable, detail::getenv_default_value()); } /*! \brief Get environment variable and convert to type */ template inline T getenv(const char *variable, const T &default_value) { return detail::getenv_helper(variable, default_value); } inline bool file_exists(const std::string &path) { return std::filesystem::exists(path) && std::filesystem::is_regular_file(path); } } // namespace transformer_engine #endif // TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_