Skip to content

Commit

Permalink
CLI: Add --platform option
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Nov 4, 2023
1 parent 4de313f commit dd99fe3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ int main(int argc, char *argv[])
{
"version", { "--version" },
"Print version and exit", 0 },
{
"platform", { "--platform" },
"Print platform information and exit", 0 },
{
"no-timing", { "--no-timing" },
"Do not print out timing measurements", 0 },
Expand Down Expand Up @@ -183,6 +186,12 @@ int main(int argc, char *argv[])
exit(0);
}

if (args["platform"])
{
stdutils::platform::print_platform_info(std::cout);
exit(0);
}

const bool validation_mode = args["validation-mode"];
const bool verbose_mode = args["verbose"];
const std::chrono::seconds timeout_duration(args["timeout"].as<unsigned int>(0u));
Expand Down
15 changes: 14 additions & 1 deletion src/stdutils/include/stdutils/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ namespace stdutils
namespace platform
{

enum class OS
{
UNKNOWN = 0,
LINUX,
MACOS,
WINDOWS
};

constexpr OS os();
std::ostream& operator<<(std::ostream& out, OS os);

enum class Compiler
{
UNKNOWN = 0,
Expand All @@ -35,11 +46,13 @@ enum class Arch
constexpr Arch architecture();
std::ostream& operator<<(std::ostream& out, Arch arch);

void print_os(std::ostream& out);
void print_cpp_standard(std::ostream& out);
void print_compiler_info(std::ostream& out);
void print_compilation_date(std::ostream& out);

void print_platform_info(std::ostream& out);
void print_compiler_all_info(std::ostream& out);

}
}
}
68 changes: 62 additions & 6 deletions src/stdutils/src/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ namespace stdutils
namespace platform
{

constexpr OS os()
{
// See: https://github.com/cpredef/predef/blob/master/OperatingSystems.md
#if defined(__linux__)
return OS::LINUX;
#elif defined(__APPLE__)
return OS::MACOS;
#elif defined(_WIN32)
return OS::WINDOWS;
#else
return OS::UNKNOWN;
#endif
}

std::ostream& operator<<(std::ostream& out, OS os)
{
switch(os)
{
case OS::UNKNOWN:
out << "Unknown";
break;

case OS::LINUX:
out << "linux";
break;

case OS::MACOS:
out << "macos";
break;

case OS::WINDOWS:
out << "windows";
break;

default:
assert(0);
out << "Unknown enum";
break;
}
return out;
}

constexpr Compiler compiler()
{
// NB: Test __GNUC__ last, because that macro is sometimes defined by other compilers than the "true" GCC
Expand Down Expand Up @@ -53,7 +95,7 @@ std::ostream& operator<<(std::ostream& out, Compiler compiler)

default:
assert(0);
out << "Unknown Id";
out << "Unknown enum";
break;
}
return out;
Expand Down Expand Up @@ -94,7 +136,7 @@ std::string compiler_version()

default:
assert(0);
out << "Unknown Id";
out << "Unknown enum";
break;
}
return out.str();
Expand Down Expand Up @@ -136,12 +178,18 @@ std::ostream& operator<<(std::ostream& out, Arch arch)

default:
assert(0);
out << "Unknown Id";
out << "Unknown enum";
break;
}
return out;
}

void print_os(std::ostream& out)
{
constexpr auto os_id = os();
out << "OS: " << os_id << std::endl;
}

void print_cpp_standard(std::ostream& out)
{
out << "C++ Standard: " << __cplusplus << std::endl;
Expand All @@ -167,13 +215,21 @@ void print_compilation_date(std::ostream& out)
out << "Compilation date: " << __DATE__ << " " << __TIME__ << std::endl;
}

void print_compiler_all_info(std::ostream& out)
void print_platform_info(std::ostream& out)
{
print_cpp_standard(out);
print_os(out);
print_architecture_info(out);
print_compiler_info(out);
}

void print_compiler_all_info(std::ostream& out)
{
print_os(out);
print_architecture_info(out);
print_compiler_info(out);
print_cpp_standard(out);
print_compilation_date(out);
}

} // namespace platform
} // namespace stdutils
} // namespace stdutils

0 comments on commit dd99fe3

Please sign in to comment.