Skip to content

Commit

Permalink
Cleanup OS specific coding (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Feb 9, 2025
1 parent ff10bdc commit 48643ec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
9 changes: 6 additions & 3 deletions src/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ void Driver::init() {
if (cliOptions.targetArch == TARGET_WASM32 || cliOptions.targetArch == TARGET_WASM64) {
cliOptions.outputPath.replace_extension("wasm");
} else {
#if OS_WINDOWS
#if OS_UNIX
cliOptions.outputPath.replace_extension("");
#elif OS_WINDOWS
cliOptions.outputPath.replace_extension("exe");
#else
cliOptions.outputPath.replace_extension("");
#error "Unsupported platform"
#endif
}

Expand Down Expand Up @@ -364,7 +366,8 @@ void Driver::addCompileSubcommandOptions(CLI::App *subCmd) {
// --dump-object-file
subCmd->add_flag<bool>("--dump-object-file,-obj", cliOptions.dumpSettings.dumpObjectFile, "Dump object file");
// --dump-dependency-graph
subCmd->add_flag<bool>("--dump-dependency-graph,-dep", cliOptions.dumpSettings.dumpDependencyGraph, "Dump compile unit dependency graph");
subCmd->add_flag<bool>("--dump-dependency-graph,-dep", cliOptions.dumpSettings.dumpDependencyGraph,
"Dump compile unit dependency graph");

// Source file
subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
Expand Down
19 changes: 9 additions & 10 deletions src/util/CommonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
#include "CommonUtil.h"

#include <cxxabi.h>

#include <SourceFile.h>

#ifdef OS_WINDOWS
#include <windows.h>
#elif OS_UNIX
#if OS_UNIX
#include <unistd.h>
#elif OS_WINDOWS
#include <windows.h>
#else
#error "Unsupported platform"
#endif

#include <SourceFile.h>

namespace spice::compiler {

/**
Expand Down Expand Up @@ -81,12 +80,12 @@ std::vector<std::string> CommonUtil::split(const std::string &input) {
* @return Page size in bytes
*/
size_t CommonUtil::getSystemPageSize() {
#ifdef OS_WINDOWS
#if OS_UNIX
return static_cast<size_t>(sysconf(_SC_PAGESIZE));
#elif OS_WINDOWS
SYSTEM_INFO si;
GetSystemInfo(&si);
return static_cast<size_t>(si.dwPageSize);
#elif OS_UNIX
return static_cast<size_t>(sysconf(_SC_PAGESIZE));
#else
#error "Unsupported platform"
#endif
Expand Down Expand Up @@ -130,7 +129,7 @@ std::string CommonUtil::demangleTypeName(const char *mangledName) {
}

/**
* Check if the given string is a valid mangled name
* Check if the given string is a valid, mangled name
*
* @return
*/
Expand Down
40 changes: 24 additions & 16 deletions src/util/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ size_t FileUtil::getLineCount(const std::filesystem::path &filePath) {
* @return Result struct
*/
ExecResult FileUtil::exec(const std::string &command, bool redirectStdErrToStdOut) {
#ifdef _WIN32
#if OS_UNIX
std::string redirectedCommand = command;
if (redirectStdErrToStdOut)
redirectedCommand += " 2>&1"; // Redirect stderr to stdout
FILE *pipe = popen(redirectedCommand.c_str(), "r");
#elif OS_WINDOWS
std::string redirectedCommand = command;
if (redirectStdErrToStdOut)
redirectedCommand = "\"" + command + " 2>&1\""; // Redirect stderr to stdout
FILE *pipe = _popen(redirectedCommand.c_str(), "r");
#else
std::string redirectedCommand = command;
if (redirectStdErrToStdOut)
redirectedCommand += " 2>&1"; // Redirect stderr to stdout
FILE *pipe = popen(redirectedCommand.c_str(), "r");
#error "Unsupported platform"
#endif

if (!pipe) // GCOV_EXCL_LINE
Expand All @@ -90,10 +92,12 @@ ExecResult FileUtil::exec(const std::string &command, bool redirectStdErrToStdOu
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
result << buffer.data();

#ifdef _WIN32
#if OS_UNIX
const int exitCode = pclose(pipe) / 256;
#elif OS_WINDOWS
const int exitCode = _pclose(pipe);
#else
const int exitCode = pclose(pipe) / 256;
#error "Unsupported platform"
#endif
return {result.str(), exitCode};
}
Expand All @@ -105,10 +109,10 @@ ExecResult FileUtil::exec(const std::string &command, bool redirectStdErrToStdOu
* @return Present or not
*/
bool FileUtil::isCommandAvailable(const std::string &cmd) {
#if OS_WINDOWS
#if OS_UNIX
const std::string checkCmd = "which " + cmd + " > /dev/null 2>&1";
#elif OS_WINDOWS
const std::string checkCmd = "where " + cmd + " > nul 2>&1";
#elif OS_UNIX
const std::string checkCmd = "which " + cmd + " > /dev/null 2>&1";
#else
#error "Unsupported platform"
#endif
Expand All @@ -129,7 +133,7 @@ bool FileUtil::isGraphvizInstalled() { return std::system("dot -V") == 0; }
* @return Name of path to the linker invoker executable
*/
ExternalBinaryFinderResult FileUtil::findLinkerInvoker() {
#ifdef OS_UNIX
#if OS_UNIX
for (const char *linkerInvokerName : {"clang", "gcc"})
for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
if (std::filesystem::exists(path + linkerInvokerName))
Expand All @@ -138,6 +142,8 @@ ExternalBinaryFinderResult FileUtil::findLinkerInvoker() {
for (const char *linkerInvokerName : {"clang", "gcc"})
if (isCommandAvailable(std::string(linkerInvokerName) + " -v"))
return ExternalBinaryFinderResult{linkerInvokerName, linkerInvokerName};
#else
#error "Unsupported platform"
#endif
const auto msg = "No supported linker invoker was found on the system. Supported are: clang and gcc"; // LCOV_EXCL_LINE
throw LinkerError(LINKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
Expand All @@ -150,7 +156,7 @@ ExternalBinaryFinderResult FileUtil::findLinkerInvoker() {
* @return Name of path to the linker executable
*/
ExternalBinaryFinderResult FileUtil::findLinker(const CliOptions &cliOptions) {
#ifdef OS_UNIX
#if OS_UNIX
std::vector<const char *> linkerList;
linkerList.reserve(5);
// mold does only support linking for unix and darwin
Expand All @@ -169,6 +175,8 @@ ExternalBinaryFinderResult FileUtil::findLinker(const CliOptions &cliOptions) {
for (const char *linkerName : {"lld", "ld"})
if (isCommandAvailable(std::string(linkerName) + " -v"))
return ExternalBinaryFinderResult{linkerName, linkerName};
#else
#error "Unsupported platform"
#endif
const auto msg = "No supported linker was found on the system. Supported are: mold, lld, gold and ld"; // LCOV_EXCL_LINE
throw LinkerError(LINKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
Expand All @@ -181,7 +189,7 @@ ExternalBinaryFinderResult FileUtil::findLinker(const CliOptions &cliOptions) {
* @return Std directory
*/
std::filesystem::path FileUtil::getStdDir() {
#ifdef OS_UNIX
#if OS_UNIX
if (exists(std::filesystem::path("/usr/lib/spice/std/")))
return {"/usr/lib/spice/std/"};
#endif
Expand Down Expand Up @@ -211,10 +219,10 @@ std::filesystem::path FileUtil::getBootstrapDir() {
* @return Installation directory
*/
std::filesystem::path FileUtil::getSpiceBinDir() {
#if OS_WINDOWS
#if OS_UNIX
return "/usr/local/bin/";
#elif OS_WINDOWS
return std::filesystem::path(std::getenv("USERPROFILE")) / "spice" / "bin";
#elif OS_UNIX
return "/usr/local/bin/";
#else
#error "Unsupported platform"
#endif
Expand Down

0 comments on commit 48643ec

Please sign in to comment.