From 7461e95210e7420d0ddf681279813f394a6fd0d8 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 14 Jun 2019 10:02:04 -0400 Subject: Abstract CPPCompiler (#983) * 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. * CommandLine arguments are now tagged if they are escaped or not. That it is the clients responsibility to escape command lines that cannot be automatically escaped. * Add checks on unix/linux that command line args are all unescaped. * WIP getting runtime GCC to work. * First pass compiler working on unix-like targets. * Added File::remove function. * Enable c-compile.c test on 'smoke'. * WIP abstracting the CPP compiler concept. * CPPCompilerSet and CPPCompilerUtil working on windows. Problem on unix. * Used stdError for parsing of invoke of compiler to figure out verison. * Removed some code that was no longer needed from slang-cpp-compiler.cpp --- source/core/slang-cpp-compiler.h | 146 ++++++++++++++++++++++++++++++++++----- 1 file changed, 130 insertions(+), 16 deletions(-) (limited to 'source/core/slang-cpp-compiler.h') diff --git a/source/core/slang-cpp-compiler.h b/source/core/slang-cpp-compiler.h index 644d3d85b..8794463d1 100644 --- a/source/core/slang-cpp-compiler.h +++ b/source/core/slang-cpp-compiler.h @@ -4,29 +4,45 @@ #include "slang-common.h" #include "slang-string.h" +#include "slang-process-util.h" + namespace Slang { -class CPPCompiler +class CPPCompiler: public RefObject { public: + typedef RefObject Super; enum class Type { + Unknown, VisualStudio, GCC, Clang, + SNC, + GHS, + CountOf, }; - struct Version + struct Desc { - Type type; ///< The compiler type - Int major; ///< The major version number - Int minor; ///< The minor version number + typedef Desc ThisType; + + UInt GetHashCode() const { return combineHash(int(type), combineHash(int(majorVersion), int(minorVersion))); } + bool operator==(const ThisType& rhs) const { return type == rhs.type && majorVersion == rhs.majorVersion && minorVersion == rhs.minorVersion; } + bool operator!=(const ThisType& rhs) const { return !(*this == rhs); } + + /// Get the version as a value + Int getVersionValue() const { return majorVersion * 100 + minorVersion; } + + /// Ctor + Desc(Type inType = Type::Unknown, Int inMajorVersion = 0, Int inMinorVersion = 0):type(inType), majorVersion(inMajorVersion), minorVersion(inMinorVersion) {} + + Type type; ///< The type of the compiler + Int majorVersion; ///< Major version (interpretation is type specific) + Int minorVersion; ///< Minor version }; -}; -struct CPPCompileOptions -{ enum class OptimizationLevel { Normal, ///< Normal optimization @@ -52,20 +68,118 @@ struct CPPCompileOptions String value; }; - OptimizationLevel optimizationLevel = OptimizationLevel::Debug; - DebugInfoType debugInfoType = DebugInfoType::Normal; - TargetType targetType = TargetType::Executable; + struct CompileOptions + { + 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; + }; + + /// Get the desc of this compiler + const Desc& getDesc() const { return m_desc; } + /// Compile using the specified options. The result is in resOut + virtual SlangResult compile(const CompileOptions& options, ExecuteResult& outResult) = 0; - 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 +protected: - List defines; + CPPCompiler(const Desc& desc) : + m_desc(desc) + {} - List sourceFiles; + Desc m_desc; +}; - List includePaths; - List libraryPaths; +class GenericCPPCompiler : public CPPCompiler +{ +public: + typedef CPPCompiler Super; + + typedef void(*CalcArgsFunc)(const CPPCompiler::CompileOptions& options, CommandLine& cmdLine); + + virtual SlangResult compile(const CompileOptions& options, ExecuteResult& outResult) SLANG_OVERRIDE; + + GenericCPPCompiler(const Desc& desc, const String& exeName, CalcArgsFunc func) : + Super(desc), + m_exeName(exeName), + m_func(func) + {} + + CalcArgsFunc m_func; + String m_exeName; }; +class CPPCompilerSet : public RefObject +{ +public: + typedef RefObject Super; + + + /// Find all the available compilers + void getCompilerDescs(List& outCompilerDescs) const; + /// Returns list of all compilers + void getCompilers(List& outCompilers) const; + + /// Get a compiler + CPPCompiler* getCompiler(const CPPCompiler::Desc& compilerDesc) const; + + /// Will replace if there is one with same desc + void addCompiler(CPPCompiler* compiler); + + /// Get a default compiler + CPPCompiler* getDefaultCompiler() const { return m_defaultCompiler; } + /// Set the default compiler + void setDefaultCompiler(CPPCompiler* compiler) { m_defaultCompiler = compiler; } + +protected: + + Index _findIndex(const CPPCompiler::Desc& desc) const; + + RefPtr m_defaultCompiler; + // This could be a dictionary/map - but doing a linear search is going to be fine and it makes + // somethings easier. + List> m_compilers; +}; + +struct CPPCompilerUtil +{ + enum class MatchType + { + MinGreaterEqual, + MinAbsolute, + 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); + + /// Runs the exeName, and extracts the version info into outDesc + static SlangResult calcGccFamilyVersion(const String& exeName, const UnownedStringSlice& versionPrefix, CPPCompiler::Desc& outDesc); + + /// Find a compiler + static CPPCompiler* findCompiler(const CPPCompilerSet* set, MatchType matchType, const CPPCompiler::Desc& desc); + static CPPCompiler* findCompiler(const List& compilers, MatchType matchType, const CPPCompiler::Desc& desc); + + /// Find the compiler closest to the desc + static CPPCompiler* findClosestCompiler(const List& compilers, const CPPCompiler::Desc& desc); + static CPPCompiler* findClosestCompiler(const CPPCompilerSet* set, const CPPCompiler::Desc& desc); + + /// Get the information on the compiler used to compile this source + static const CPPCompiler::Desc& getCompiledWithDesc(); + + /// Given a set, registers compilers found through standard means and determines a reasonable default compiler if possible + static SlangResult initializeSet(CPPCompilerSet* set); +}; + + } #endif -- cgit v1.2.3