diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/core/slang-cpp-compiler.cpp | 312 | ||||
| -rw-r--r-- | source/core/slang-cpp-compiler.h | 28 | ||||
| -rw-r--r-- | source/core/slang-io.cpp | 5 | ||||
| -rw-r--r-- | source/core/slang-process-util.h | 3 | ||||
| -rw-r--r-- | source/core/unix/slang-unix-cpp-compiler-util.cpp | 163 | ||||
| -rw-r--r-- | source/core/unix/slang-unix-cpp-compiler-util.h | 24 | ||||
| -rw-r--r-- | source/core/windows/slang-win-visual-studio-util.cpp | 148 | ||||
| -rw-r--r-- | source/core/windows/slang-win-visual-studio-util.h | 3 |
8 files changed, 325 insertions, 361 deletions
diff --git a/source/core/slang-cpp-compiler.cpp b/source/core/slang-cpp-compiler.cpp index ef679d780..4bcdd068c 100644 --- a/source/core/slang-cpp-compiler.cpp +++ b/source/core/slang-cpp-compiler.cpp @@ -5,10 +5,11 @@ #include "../../slang-com-helper.h" #include "slang-string-util.h" +#include "slang-io.h" +#include "slang-shared-library.h" + #if SLANG_VC # include "windows/slang-win-visual-studio-util.h" -#else -# include "unix/slang-unix-cpp-compiler-util.h" #endif namespace Slang @@ -18,14 +19,12 @@ namespace Slang SlangResult GenericCPPCompiler::compile(const CompileOptions& options, ExecuteResult& outResult) { - CommandLine cmdLine; + // Copy the command line options + CommandLine cmdLine(m_cmdLine); - // Calculate the command line options + // Append command line args to the end of cmdLine using the target specific function for the specified options m_func(options, cmdLine); - // Set the executable - cmdLine.setExecutableFilename(m_exeName); - #if 0 // Test { @@ -49,16 +48,17 @@ static bool _isWhiteSpace(char c) return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } -/* static */SlangResult CPPCompilerUtil::parseGccFamilyVersion(const UnownedStringSlice& text, const UnownedStringSlice& versionPrefix, CPPCompiler::Desc& outDesc) +/* static */SlangResult CPPCompilerUtil::parseGCCFamilyVersion(const UnownedStringSlice& text, const UnownedStringSlice& prefix, CPPCompiler::Desc& outDesc) { List<UnownedStringSlice> lines; StringUtil::calcLines(text, lines); for (auto line : lines) { - if (String(line).startsWith(versionPrefix)) + // TODO(JS): Ugh - having to turn into a string to do this test isn't great. + if (String(line).startsWith(prefix)) { - UnownedStringSlice versionSlice(line.begin() + versionPrefix.size(), line.end()); + UnownedStringSlice versionSlice(line.begin() + prefix.size(), line.end()); List<Int> digits; @@ -99,7 +99,6 @@ static bool _isWhiteSpace(char c) outDesc.majorVersion = digits[0]; outDesc.minorVersion = digits[1]; - return SLANG_OK; } } @@ -107,7 +106,7 @@ static bool _isWhiteSpace(char c) return SLANG_FAIL; } -SlangResult CPPCompilerUtil::calcGccFamilyVersion(const String& exeName, const UnownedStringSlice& versionPrefix, CPPCompiler::Desc& outDesc) +SlangResult CPPCompilerUtil::calcGCCFamilyVersion(const String& exeName, CPPCompiler::Desc& outDesc) { CommandLine cmdLine; cmdLine.setExecutableFilename(exeName); @@ -115,7 +114,258 @@ SlangResult CPPCompilerUtil::calcGccFamilyVersion(const String& exeName, const U ExecuteResult exeRes; SLANG_RETURN_ON_FAIL(ProcessUtil::execute(cmdLine, exeRes)); - return parseGccFamilyVersion(exeRes.standardError.getUnownedSlice(), versionPrefix, outDesc); + + const UnownedStringSlice prefixes[] = + { + UnownedStringSlice::fromLiteral("clang version"), + UnownedStringSlice::fromLiteral("gcc version"), + UnownedStringSlice::fromLiteral("Apple LLVM version"), + }; + const CPPCompiler::Type types[] = + { + CPPCompiler::Type::Clang, + CPPCompiler::Type::GCC, + CPPCompiler::Type::Clang, + }; + + SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(prefixes) == SLANG_COUNT_OF(types)); + + for (Index i = 0; i < SLANG_COUNT_OF(prefixes); ++i) + { + // Set the type + outDesc.type = types[i]; + // Extract the version + if (SLANG_SUCCEEDED(parseGCCFamilyVersion(exeRes.standardError.getUnownedSlice(), prefixes[i], outDesc))) + { + return SLANG_OK; + } + } + return SLANG_FAIL; +} + +/* static */void CPPCompilerUtil::calcVisualStudioArgs(const CompileOptions& options, CommandLine& cmdLine) +{ + // 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) + { + cmdLine.addPrefixPathArg("/Fd", options.modulePath, ".pdb"); + } + + switch (options.targetType) + { + case TargetType::SharedLibrary: + { + // Create dynamic link library + if (options.optimizationLevel == OptimizationLevel::Debug) + { + cmdLine.addArg("/LDd"); + } + else + { + cmdLine.addArg("/LD"); + } + + cmdLine.addPrefixPathArg("/Fe", options.modulePath, ".dll"); + break; + } + case TargetType::Executable: + { + cmdLine.addPrefixPathArg("/Fe", options.modulePath, ".exe"); + break; + } + default: break; + } + + // Object file specify it's location - needed if we are out + cmdLine.addPrefixPathArg("/Fo", options.modulePath, ".obj"); + + // 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:: + cmdLine.addPrefixPathArg("/LIBPATH:", libPath); + } +} + +/* static */void CPPCompilerUtil::calcGCCFamilyArgs(const CompileOptions& options, CommandLine& cmdLine) +{ + cmdLine.addArg("-fvisibility=hidden"); + // Use shared libraries + //cmdLine.addArg("-shared"); + + switch (options.optimizationLevel) + { + case OptimizationLevel::Debug: + { + // No optimization + cmdLine.addArg("-O0"); + break; + } + case OptimizationLevel::Normal: + { + cmdLine.addArg("-Os"); + break; + } + default: break; + } + + if (options.debugInfoType != DebugInfoType::None) + { + cmdLine.addArg("-g"); + } + + switch (options.targetType) + { + case TargetType::SharedLibrary: + { + // Position independent + cmdLine.addArg("-fPIC"); + + String sharedLibraryPath; + + // Work out the shared library name + { + String moduleDir = Path::getParentDirectory(options.modulePath); + String moduleFilename = Path::getFileName(options.modulePath); + + StringBuilder sharedLibraryFilename; + SharedLibrary::appendPlatformFileName(moduleFilename.getUnownedSlice(), sharedLibraryFilename); + + if (moduleDir.getLength() > 0) + { + sharedLibraryPath = Path::combine(moduleDir, sharedLibraryFilename); + } + else + { + sharedLibraryPath = sharedLibraryFilename; + } + } + + cmdLine.addArg("-o"); + cmdLine.addArg(sharedLibraryPath); + break; + } + case TargetType::Executable: + { + cmdLine.addArg("-o"); + + StringBuilder builder; + builder << options.modulePath; + builder << ProcessUtil::getExecutableSuffix(); + + cmdLine.addArg(options.modulePath); + break; + } + case TargetType::Object: + { + // Don't link, just produce object file + cmdLine.addArg("-c"); + break; + } + default: break; + } + + // 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); + } + + // Link options + if (0) + { + StringBuilder linkOptions; + linkOptions << "Wl,"; + cmdLine.addArg(linkOptions); + } + + // Files to compile + for (const auto& sourceFile : options.sourceFiles) + { + cmdLine.addArg(sourceFile); + } + + for (const auto& libPath : options.libraryPaths) + { + // Note that any escaping of the path is handled in the ProcessUtil:: + cmdLine.addArg("-L"); + cmdLine.addArg(libPath); + cmdLine.addArg("-F"); + cmdLine.addArg(libPath); + } } static CPPCompiler::Desc _calcCompiledWithDesc() @@ -126,12 +376,16 @@ static CPPCompiler::Desc _calcCompiledWithDesc() desc = WinVisualStudioUtil::getDesc(WinVisualStudioUtil::getCompiledVersion()); #elif SLANG_CLANG desc.type = CPPCompiler::Type::Clang; + desc.majorVersion = Int(__clang_major__); + desc.minorVersion = Int(__clang_minor__); #elif SLANG_SNC desc.type = CPPCompiler::Type::SNC; #elif SLANG_GHS desc.type = CPPCompiler::Type::GHS; #elif SLANG_GCC desc.type = CPPCompiler::Type::GCC; + desc.majorVersion = Int(__GNUC__); + desc.minorVersion = Int(__GNUC_MINOR__); #else desc.type = CPPCompiler::Type::Unknown; #endif @@ -246,6 +500,19 @@ const CPPCompiler::Desc& CPPCompilerUtil::getCompiledWithDesc() return nullptr; } +// Have to do this conditionally because unreferenced static functions are a warning on VC, and warnings are errors. +#if !SLANG_WINDOWS_FAMILY +static void _addGCCFamilyCompiler(const String& exeName, CPPCompilerSet* compilerSet) +{ + CPPCompiler::Desc desc; + if (SLANG_SUCCEEDED(CPPCompilerUtil::calcGCCFamilyVersion(exeName, desc))) + { + RefPtr<CPPCompiler> compiler(new GenericCPPCompiler(desc, exeName, &CPPCompilerUtil::calcGCCFamilyArgs)); + compilerSet->addCompiler(compiler); + } +} +#endif + /* static */CPPCompiler* CPPCompilerUtil::findClosestCompiler(const CPPCompilerSet* set, const CPPCompiler::Desc& desc) { CPPCompiler* compiler = set->getCompiler(desc); @@ -258,28 +525,13 @@ const CPPCompiler::Desc& CPPCompilerUtil::getCompiledWithDesc() return findClosestCompiler(compilers, desc); } - /* static */SlangResult CPPCompilerUtil::initializeSet(CPPCompilerSet* set) { #if SLANG_WINDOWS_FAMILY WinVisualStudioUtil::find(set); #else - { - CPPCompiler::Desc desc(CPPCompiler::Type::Clang); - if (SLANG_SUCCEEDED(calcGccFamilyVersion("clang", UnownedStringSlice::fromLiteral("clang version"), desc))) - { - RefPtr<CPPCompiler> compiler(new GenericCPPCompiler(desc, "clang", &UnixCPPCompilerUtil::calcArgs)); - set->addCompiler(compiler); - } - } - { - CPPCompiler::Desc desc(CPPCompiler::Type::GCC); - if (SLANG_SUCCEEDED(calcGccFamilyVersion("g++", UnownedStringSlice::fromLiteral("gcc version"), desc))) - { - RefPtr<CPPCompiler> compiler(new GenericCPPCompiler(desc, "g++", &UnixCPPCompilerUtil::calcArgs)); - set->addCompiler(compiler); - } - } + _addGCCFamilyCompiler("clang", set); + _addGCCFamilyCompiler("g++", set); #endif // Set the default to the compiler closest to how this source was compiled diff --git a/source/core/slang-cpp-compiler.h b/source/core/slang-cpp-compiler.h index 8794463d1..82930afd6 100644 --- a/source/core/slang-cpp-compiler.h +++ b/source/core/slang-cpp-compiler.h @@ -109,12 +109,19 @@ public: GenericCPPCompiler(const Desc& desc, const String& exeName, CalcArgsFunc func) : Super(desc), - m_exeName(exeName), + m_func(func) + { + m_cmdLine.setExecutableFilename(exeName); + } + + GenericCPPCompiler(const Desc& desc, const CommandLine& cmdLine, CalcArgsFunc func) : + Super(desc), + m_cmdLine(cmdLine), m_func(func) {} CalcArgsFunc m_func; - String m_exeName; + CommandLine m_cmdLine; }; class CPPCompilerSet : public RefObject @@ -151,6 +158,11 @@ protected: struct CPPCompilerUtil { + typedef CPPCompiler::CompileOptions CompileOptions; + typedef CPPCompiler::OptimizationLevel OptimizationLevel; + typedef CPPCompiler::TargetType TargetType; + typedef CPPCompiler::DebugInfoType DebugInfoType; + enum class MatchType { MinGreaterEqual, @@ -158,11 +170,17 @@ struct CPPCompilerUtil Newest, }; - /// Extracts version number into desc from text (assumes gcc/slang type layout with a line with version starting with versionPrefix) - static SlangResult parseGccFamilyVersion(const UnownedStringSlice& text, const UnownedStringSlice& versionPrefix, CPPCompiler::Desc& outDesc); + /// Extracts version number into desc from text (assumes gcc/clang -v layout with a line with version) + static SlangResult parseGCCFamilyVersion(const UnownedStringSlice& text, const UnownedStringSlice& prefix, CPPCompiler::Desc& outDesc); /// Runs the exeName, and extracts the version info into outDesc - static SlangResult calcGccFamilyVersion(const String& exeName, const UnownedStringSlice& versionPrefix, CPPCompiler::Desc& outDesc); + static SlangResult calcGCCFamilyVersion(const String& exeName, CPPCompiler::Desc& outDesc); + + /// Calculate gcc family compilers (including clang) cmdLine arguments from options + static void calcGCCFamilyArgs(const CompileOptions& options, CommandLine& cmdLine); + + /// Calculate Visual Studio family compilers cmdLine arguments from options + static void calcVisualStudioArgs(const CompileOptions& options, CommandLine& cmdLine); /// Find a compiler static CPPCompiler* findCompiler(const CPPCompilerSet* set, MatchType matchType, const CPPCompiler::Desc& desc); diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp index a144c5892..83f21d5dd 100644 --- a/source/core/slang-io.cpp +++ b/source/core/slang-io.cpp @@ -42,9 +42,8 @@ namespace Slang } return SLANG_FAIL; #else - // https://linux.die.net/man/2/unlink - - if (unlink(fileName.getBuffer()) == 0) + // https://linux.die.net/man/3/remove + if (::remove(fileName.getBuffer()) == 0) { return SLANG_OK; } diff --git a/source/core/slang-process-util.h b/source/core/slang-process-util.h index 03265c1cb..89e3400a0 100644 --- a/source/core/slang-process-util.h +++ b/source/core/slang-process-util.h @@ -52,6 +52,9 @@ struct CommandLine /// Get the total number of args SLANG_FORCE_INLINE Index getArgCount() const { return m_args.getCount(); } + /// Reset to the initial state + void reset() { *this = CommandLine(); } + /// Ctor CommandLine():m_executableType(ExecutableType::Unknown) {} diff --git a/source/core/unix/slang-unix-cpp-compiler-util.cpp b/source/core/unix/slang-unix-cpp-compiler-util.cpp deleted file mode 100644 index 9217c6d52..000000000 --- a/source/core/unix/slang-unix-cpp-compiler-util.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "slang-unix-cpp-compiler-util.h" - -#include "../slang-common.h" -#include "../slang-process-util.h" -#include "../slang-string-util.h" - -#include "../slang-shared-library.h" - -#include "../slang-io.h" - -// The method used to invoke VS was originally inspired by some ideas in -// https://github.com/RuntimeCompiledCPlusPlus/RuntimeCompiledCPlusPlus/ - -namespace Slang { - - -/* static */void UnixCPPCompilerUtil::calcArgs(const CPPCompiler::CompileOptions& options, CommandLine& cmdLine) -{ - typedef CPPCompiler::OptimizationLevel OptimizationLevel; - typedef CPPCompiler::TargetType TargetType; - typedef CPPCompiler::DebugInfoType DebugInfoType; - - cmdLine.addArg("-fvisibility=hidden"); - // Use shared libraries - //cmdLine.addArg("-shared"); - - switch (options.optimizationLevel) - { - case OptimizationLevel::Debug: - { - // No optimization - cmdLine.addArg("-O0"); - break; - } - case OptimizationLevel::Normal: - { - cmdLine.addArg("-Os"); - break; - } - default: break; - } - - if (options.debugInfoType != DebugInfoType::None) - { - cmdLine.addArg("-g"); - } - - switch (options.targetType) - { - case TargetType::SharedLibrary: - { - // Position independent - cmdLine.addArg("-fPIC"); - - String sharedLibraryPath; - - // Work out the shared library name - { - String moduleDir = Path::getParentDirectory(options.modulePath); - String moduleFilename = Path::getFileName(options.modulePath); - - StringBuilder sharedLibraryFilename; - SharedLibrary::appendPlatformFileName(moduleFilename.getUnownedSlice(), sharedLibraryFilename); - - if (moduleDir.getLength() > 0) - { - sharedLibraryPath = Path::combine(moduleDir, sharedLibraryFilename); - } - else - { - sharedLibraryPath = sharedLibraryFilename; - } - } - - cmdLine.addArg("-o"); - cmdLine.addArg(sharedLibraryPath); - break; - } - case TargetType::Executable: - { - cmdLine.addArg("-o"); - - StringBuilder builder; - builder << options.modulePath; - builder << ProcessUtil::getExecutableSuffix(); - - cmdLine.addArg(options.modulePath); - break; - } - case TargetType::Object: - { - // Don't link, just produce object file - cmdLine.addArg("-c"); - break; - } - default: break; - } - - // 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); - } - - // Link options - if (0) - { - StringBuilder linkOptions; - linkOptions << "Wl,"; - cmdLine.addArg(linkOptions); - } - - // Files to compile - for (const auto& sourceFile : options.sourceFiles) - { - cmdLine.addArg(sourceFile); - } - - for (const auto& libPath : options.libraryPaths) - { - // Note that any escaping of the path is handled in the ProcessUtil:: - cmdLine.addArg("-L"); - cmdLine.addArg(libPath); - cmdLine.addArg("-F"); - cmdLine.addArg(libPath); - } -} - -/* static */SlangResult UnixCPPCompilerUtil::executeCompiler(const CommandLine& commandLine, ExecuteResult& outResult) -{ - CommandLine cmdLine; - // We'll assume g++ for now - cmdLine.setExecutableFilename("g++"); - - // Append the command line options - cmdLine.addArgs(commandLine.m_args.getBuffer(), commandLine.m_args.getCount()); - -#if 0 - // Test - { - String line = ProcessUtil::getCommandLineString(cmdLine); - printf("%s", line.getBuffer()); - } -#endif - - return ProcessUtil::execute(cmdLine, outResult); -} - -} // namespace Slang diff --git a/source/core/unix/slang-unix-cpp-compiler-util.h b/source/core/unix/slang-unix-cpp-compiler-util.h deleted file mode 100644 index 42886e5eb..000000000 --- a/source/core/unix/slang-unix-cpp-compiler-util.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef SLANG_UNIX_CPP_COMPILER_UTIL_H -#define SLANG_UNIX_CPP_COMPILER_UTIL_H - -#include "../slang-list.h" -#include "../slang-string.h" - -#include "../slang-process-util.h" - -#include "../slang-cpp-compiler.h" - -namespace Slang { - -struct UnixCPPCompilerUtil -{ - /// Run compiler with command line (typically generated by calcArgs). Output placed in outResult. - static SlangResult executeCompiler(const CommandLine& commandLine, ExecuteResult& outResult); - - /// Calculate the command line args - static void calcArgs(const CPPCompiler::CompileOptions& options, CommandLine& cmdLine); -}; - -} // namespace Slang - -#endif diff --git a/source/core/windows/slang-win-visual-studio-util.cpp b/source/core/windows/slang-win-visual-studio-util.cpp index 2fdf7e400..81d77c710 100644 --- a/source/core/windows/slang-win-visual-studio-util.cpp +++ b/source/core/windows/slang-win-visual-studio-util.cpp @@ -40,33 +40,8 @@ struct VersionInfo const char* name; ///< The name of the registry key }; - - -class WinVisualStudioCompiler : public CPPCompiler -{ -public: - typedef CPPCompiler Super; - - Result WinVisualStudioCompiler::compile(const CompileOptions& options, ExecuteResult& outRes) SLANG_OVERRIDE - { - CommandLine cmdLine; - WinVisualStudioUtil::calcArgs(options, cmdLine); - return WinVisualStudioUtil::executeCompiler(m_versionPath, cmdLine, outRes); - } - /// Ctor - WinVisualStudioCompiler(const Desc& desc, const WinVisualStudioUtil::VersionPath& versionPath) : - Super(desc), - m_versionPath(versionPath) - { - } - - WinVisualStudioUtil::VersionPath m_versionPath; -}; - } // anonymous - - static SlangResult _readRegistryKey(const char* path, const char* keyName, String& outString) { // https://docs.microsoft.com/en-us/windows/desktop/api/winreg/nf-winreg-regopenkeyexa @@ -313,7 +288,10 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out VersionPath versionPath; if (!set->getCompiler(desc) && SLANG_SUCCEEDED(_find(i, versionPath))) { - RefPtr<WinVisualStudioCompiler> compiler = new WinVisualStudioCompiler(desc, versionPath); + CommandLine cmdLine; + calcExecuteCompilerArgs(versionPath, cmdLine); + + RefPtr<GenericCPPCompiler> compiler = new GenericCPPCompiler(desc, cmdLine, &CPPCompilerUtil::calcVisualStudioArgs); set->addCompiler(compiler); } } @@ -321,12 +299,11 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out return SLANG_OK; } -/* static */SlangResult WinVisualStudioUtil::executeCompiler(const VersionPath& versionPath, const CommandLine& commandLine, ExecuteResult& outResult) +/* static */void WinVisualStudioUtil::calcExecuteCompilerArgs(const VersionPath& versionPath, CommandLine& outCmdLine) { // To invoke cl we need to run the suitable vcvars. In order to run this we have to have MS CommandLine. // So here we build up a cl command line that is run by first running vcvars, and then executing cl with the parameters as passed to commandLine - StringBuilder builder; CommandLine cmdLine; cmdLine.setExecutableFilename("cmd.exe"); @@ -353,9 +330,15 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out cmdLine.addArg("&&"); cmdLine.addArg("cl"); + outCmdLine = cmdLine; +} + +/* static */SlangResult WinVisualStudioUtil::executeCompiler(const VersionPath& versionPath, const CommandLine& commandLine, ExecuteResult& outResult) +{ + CommandLine cmdLine; + calcExecuteCompilerArgs(versionPath, cmdLine); // Append the command line options cmdLine.addArgs(commandLine.m_args.getBuffer(), commandLine.m_args.getCount()); - return ProcessUtil::execute(cmdLine, outResult); } @@ -380,111 +363,4 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out } } -/* static */void WinVisualStudioUtil::calcArgs(const CPPCompiler::CompileOptions& options, CommandLine& cmdLine) -{ - typedef CPPCompiler::OptimizationLevel OptimizationLevel; - typedef CPPCompiler::TargetType TargetType; - typedef CPPCompiler::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) - { - cmdLine.addPrefixPathArg("/Fd", options.modulePath, ".pdb"); - } - - switch (options.targetType) - { - case TargetType::SharedLibrary: - { - // Create dynamic link library - if (options.optimizationLevel == OptimizationLevel::Debug) - { - cmdLine.addArg("/LDd"); - } - else - { - cmdLine.addArg("/LD"); - } - - cmdLine.addPrefixPathArg("/Fe", options.modulePath, ".dll"); - break; - } - case TargetType::Executable: - { - cmdLine.addPrefixPathArg("/Fe", options.modulePath, ".exe"); - break; - } - default: break; - } - - // Object file specify it's location - needed if we are out - cmdLine.addPrefixPathArg("/Fo", options.modulePath, ".obj"); - - // 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:: - cmdLine.addPrefixPathArg("/LIBPATH:", libPath); - } -} - } // 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 b7e1c8a85..76e1cc710 100644 --- a/source/core/windows/slang-win-visual-studio-util.h +++ b/source/core/windows/slang-win-visual-studio-util.h @@ -33,6 +33,9 @@ struct WinVisualStudioUtil /// Find and add to the set (if not already there) static SlangResult find(CPPCompilerSet* set); + /// Create the cmdLine to start compiler for specified path + static void calcExecuteCompilerArgs(const VersionPath& versionPath, CommandLine& outCmdLine); + /// Run visual studio on specified path with the parameters specified on the command line. Output placed in outResult. static SlangResult executeCompiler(const VersionPath& versionPath, const CommandLine& commandLine, ExecuteResult& outResult); |
