From 7931adac99a78fd5488f665578fba858b34bc8a6 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 12 Jun 2019 14:28:16 -0400 Subject: Visual Studio compilation working in test harness (#979) * Work in progress to be able to invoke VS from within code. * First pass at windows version of refactor of OSProcessSpawner * Closer to getting VS path lookup working. * Make OSString assignable/ctor able * Work out program files directory directly, so don't have to expand %%. * WIP: Improve handling of process spawning. * Add support for splitting input by line. * * Correctly locates visual studio install * Added functionality to invoke vs via cmd * Add option to execute the command line. * Handle in ProcessUtil for windows -> WinHandle. * Rename files slang-win-visual-studio-util.cpp/.h and slang-process-util.h * First pass at unix/linux version of ProcessUtil. * Fix reading Visual Studio path from the registry. * Get compiling on linux with. * Fix vcvarsall.bat name * Use ProcessUtil to execute external code. * Remove OSProcessSpawner. * Remove includes for "os.h" where no longer needed. * Fix tabbing issue in premake5.lua Remove test code from slang-test-main.cpp * Fix premake4.lua tabbing issue. * Small fixes to slang-process-util.h Init ExecuteResult on Win execute. * Improve comments. * Fix bug in StringUtil::calcLines - with oddly terminated source input being able to read past end. Make slang-generate use StringUtil over it's own impl. * Fix off by one bug in working out Visual Studio version. * Fix bug in calculating Visual Studio Version * Fix compilation on linux with string parameter being passed to messageFormat. * Remove erroneous use of kOSError codes - use Result. * First effort to generate standard compiler options. * Initial efforts in compiling source code in test framework for VisualStudio. * Testing compiling c code on VisualStudio on Windows. * Fix warning on linux. * Fix clang on linux warning (and therefore failing) returning a StringBuilder as String. * Disable return-std-move on clang. --- source/core/windows/slang-win-process-util.cpp | 63 +++++++++-- .../core/windows/slang-win-visual-studio-util.cpp | 121 +++++++++++++++++++++ source/core/windows/slang-win-visual-studio-util.h | 44 ++++++++ 3 files changed, 220 insertions(+), 8 deletions(-) (limited to 'source/core/windows') diff --git a/source/core/windows/slang-win-process-util.cpp b/source/core/windows/slang-win-process-util.cpp index 424b87128..680864acb 100644 --- a/source/core/windows/slang-win-process-util.cpp +++ b/source/core/windows/slang-win-process-util.cpp @@ -154,10 +154,39 @@ static DWORD WINAPI _readerThreadProc(LPVOID threadParam) return UnownedStringSlice::fromLiteral(".exe"); } -static void _appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) +static bool _isLetter(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +/* Special case handling for command line switches of the form /LINKPATH: that it is immediately followed +potentially by a path. This detects that kind of switch, and returns the index of the first character after +the :. If doesn't have the right format returns -1, to indicate that it's not this style of switch */ +static int _calcIndexOfColonSwitch(const UnownedStringSlice& slice) +{ + if (!slice.startsWith("/")) + { + return -1; + } + int index = slice.indexOf(':'); + if (index > 0) + { + for (int i = 1; i < index; ++i) + { + if (!_isLetter(slice[i])) + { + return -1; + } + } + } + return index + 1; +} + +static void _appendCommandLineEscaped(const UnownedStringSlice& slice, StringBuilder& out) { // TODO(JS): This escaping is not complete... ! - if (slice.indexOf(' ') >= 0 || slice.indexOf('"') >= 0) + + if ((slice.indexOf(' ') >= 0 || slice.indexOf('"') >= 0)) { out << "\""; @@ -166,7 +195,7 @@ static void _appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) while (cur < end) { - char c= *cur++; + char c = *cur++; switch (c) { case '\"': @@ -181,21 +210,39 @@ static void _appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) } out << "\""; - return; } + else + { + out << slice; + } +} - out << slice; +/* static */void ProcessUtil::appendCommandLineEscaped(const UnownedStringSlice& slice, StringBuilder& out) +{ + // Check if starts with a colon ending switch, as we need to special case escaping in this case + const int index = _calcIndexOfColonSwitch(slice); + if (index >= 0) + { + // Append the switch prefix as is (ie /linkpath:) + out << UnownedStringSlice(slice.begin(), index); + // Append the stuff after the prefix escaping if needed + _appendCommandLineEscaped(UnownedStringSlice(slice.begin() + index, slice.end()), out); + } + else + { + _appendCommandLineEscaped(slice, out); + } } /* static */String ProcessUtil::getCommandLineString(const CommandLine& commandLine) { StringBuilder cmd; - _appendEscaped(commandLine.m_executable.getUnownedSlice(), cmd); + appendCommandLineEscaped(commandLine.m_executable.getUnownedSlice(), cmd); for (const auto& arg : commandLine.m_args) { cmd << " "; - _appendEscaped(arg.getUnownedSlice(), cmd); + appendCommandLineEscaped(arg.getUnownedSlice(), cmd); } return cmd.ToString(); } @@ -260,7 +307,7 @@ static void _appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) if (commandLine.m_executableType == CommandLine::ExecutableType::Path) { StringBuilder cmd; - _appendEscaped(commandLine.m_executable.getUnownedSlice(), cmd); + appendCommandLineEscaped(commandLine.m_executable.getUnownedSlice(), cmd); pathBuffer = cmd.toWString(); path = pathBuffer.begin(); diff --git a/source/core/windows/slang-win-visual-studio-util.cpp b/source/core/windows/slang-win-visual-studio-util.cpp index edf33110c..5fc56d091 100644 --- a/source/core/windows/slang-win-visual-studio-util.cpp +++ b/source/core/windows/slang-win-visual-studio-util.cpp @@ -336,4 +336,125 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out } } +/* static */void WinVisualStudioUtil::calcArgs(const CPPCompileOptions& options, CommandLine& cmdLine) +{ + typedef CPPCompileOptions::OptimizationLevel OptimizationLevel; + typedef CPPCompileOptions::TargetType TargetType; + typedef CPPCompileOptions::DebugInfoType DebugInfoType; + + // https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-alphabetically?view=vs-2019 + + cmdLine.addArg("/nologo"); + // Generate complete debugging information + cmdLine.addArg("/Zi"); + // Display full path of source files in diagnostics + cmdLine.addArg("/FC"); + + switch (options.optimizationLevel) + { + case OptimizationLevel::Debug: + { + // No optimization + cmdLine.addArg("/Od"); + + cmdLine.addArg("/MDd"); + break; + } + case OptimizationLevel::Normal: + { + cmdLine.addArg("/O2"); + // Multithreaded DLL + cmdLine.addArg("/MD"); + break; + } + default: break; + } + + // /Fd - followed by name of the pdb file + if (options.debugInfoType != DebugInfoType::None) + { + StringBuilder builder; + builder << "/Fd" << options.modulePath << ".pdb"; + cmdLine.addArg(builder); + } + + switch (options.targetType) + { + case TargetType::SharedLibrary: + { + // Create dynamic link library + if (options.optimizationLevel == OptimizationLevel::Debug) + { + cmdLine.addArg("/LDd"); + } + else + { + cmdLine.addArg("/LD"); + } + + StringBuilder builder; + builder << "/Fe" << options.modulePath << ".dll"; + cmdLine.addArg(builder); + break; + } + case TargetType::Executable: + { + StringBuilder builder; + builder << "/Fe" << options.modulePath << ".exe"; + cmdLine.addArg(builder); + break; + } + default: break; + } + + // Object file specify it's location - needed if we are out + { + StringBuilder builder; + builder << "/Fo" << options.modulePath << ".obj"; + cmdLine.addArg(builder); + } + + // Add defines + for (const auto& define : options.defines) + { + StringBuilder builder; + builder << define.nameWithSig; + if (define.value.getLength()) + { + builder << "=" << define.value; + } + + cmdLine.addArg(builder); + } + + // Add includes + for (const auto& include : options.includePaths) + { + cmdLine.addArg("/I"); + cmdLine.addArg(include); + } + + + // https://docs.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model?view=vs-2019 + // /Eha - Specifies the model of exception handling. (a, s, c, r are options) + + // Files to compile + for (const auto& sourceFile : options.sourceFiles) + { + cmdLine.addArg(sourceFile); + } + + // Link options (parameters past /link go to linker) + cmdLine.addArg("/link"); + + for (const auto& libPath : options.libraryPaths) + { + // Note that any escaping of the path is handled in the ProcessUtil:: + StringBuilder builder; + builder << "/LIBPATH:" << libPath; + + cmdLine.addArg(builder); + } +} + } // namespace Slang diff --git a/source/core/windows/slang-win-visual-studio-util.h b/source/core/windows/slang-win-visual-studio-util.h index aec4493df..fefb9ad0f 100644 --- a/source/core/windows/slang-win-visual-studio-util.h +++ b/source/core/windows/slang-win-visual-studio-util.h @@ -8,6 +8,47 @@ namespace Slang { +struct CPPCompileOptions +{ + enum class OptimizationLevel + { + Normal, ///< Normal optimization + Debug, ///< General has no optimizations + }; + + enum DebugInfoType + { + None, ///< Binary has no debug information + Maximum, ///< Has maximum debug information + Normal, ///< Has normal debug information + }; + enum TargetType + { + Executable, ///< Produce an executable + SharedLibrary, ///< Produce a shared library object/dll + Object, ///< Produce an object file + }; + + struct Define + { + String nameWithSig; ///< If macro takes parameters include in brackets + String value; + }; + + OptimizationLevel optimizationLevel = OptimizationLevel::Debug; + DebugInfoType debugInfoType = DebugInfoType::Normal; + TargetType targetType = TargetType::Executable; + + String modulePath; ///< The path/name of the output module. Should not have the extension, as that will be added for each of the target types + + List defines; + + List sourceFiles; + + List includePaths; + List libraryPaths; +}; + struct WinVisualStudioUtil { enum class Version: uint32_t @@ -43,6 +84,9 @@ struct WinVisualStudioUtil /// Convert a version number into a string static void append(Version version, StringBuilder& outBuilder); + /// Calculate the command line args + static void calcArgs(const CPPCompileOptions& options, CommandLine& cmdLine); + }; } // namespace Slang -- cgit v1.2.3