summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/compiler-core/slang-downstream-compiler.h4
-rw-r--r--source/compiler-core/slang-gcc-compiler-util.cpp21
-rw-r--r--source/compiler-core/slang-gcc-compiler-util.h18
-rw-r--r--source/compiler-core/windows/slang-win-visual-studio-util.cpp18
-rw-r--r--source/core/slang-command-line.cpp90
-rw-r--r--source/core/slang-command-line.h75
-rw-r--r--source/core/slang-io.h5
-rw-r--r--source/core/unix/slang-unix-process.cpp17
-rw-r--r--source/core/windows/slang-win-process.cpp18
9 files changed, 195 insertions, 71 deletions
diff --git a/source/compiler-core/slang-downstream-compiler.h b/source/compiler-core/slang-downstream-compiler.h
index 58b7c5be0..dc9e29185 100644
--- a/source/compiler-core/slang-downstream-compiler.h
+++ b/source/compiler-core/slang-downstream-compiler.h
@@ -419,10 +419,10 @@ public:
virtual SlangResult calcArgs(const CompileOptions& options, CommandLine& cmdLine) = 0;
virtual SlangResult parseOutput(const ExecuteResult& exeResult, DownstreamDiagnostics& output) = 0;
- CommandLineDownstreamCompiler(const Desc& desc, const String& exeName) :
+ CommandLineDownstreamCompiler(const Desc& desc, const ExecutableLocation& exe) :
Super(desc)
{
- m_cmdLine.setExecutableFilename(exeName);
+ m_cmdLine.setExecutableLocation(exe);
}
CommandLineDownstreamCompiler(const Desc& desc, const CommandLine& cmdLine) :
diff --git a/source/compiler-core/slang-gcc-compiler-util.cpp b/source/compiler-core/slang-gcc-compiler-util.cpp
index 034f349e0..5d72e675a 100644
--- a/source/compiler-core/slang-gcc-compiler-util.cpp
+++ b/source/compiler-core/slang-gcc-compiler-util.cpp
@@ -86,10 +86,10 @@ static Index _findVersionEnd(const UnownedStringSlice& in)
return SLANG_FAIL;
}
-SlangResult GCCDownstreamCompilerUtil::calcVersion(const String& exeName, DownstreamCompiler::Desc& outDesc)
+SlangResult GCCDownstreamCompilerUtil::calcVersion(const ExecutableLocation& exe, DownstreamCompiler::Desc& outDesc)
{
CommandLine cmdLine;
- cmdLine.setExecutableFilename(exeName);
+ cmdLine.setExecutableLocation(exe);
cmdLine.addArg("-v");
ExecuteResult exeRes;
@@ -635,19 +635,13 @@ static SlangResult _parseGCCFamilyLine(const UnownedStringSlice& line, LineParse
return SLANG_OK;
}
-/* static */SlangResult GCCDownstreamCompilerUtil::createCompiler(const String& path, const String& inExeName, RefPtr<DownstreamCompiler>& outCompiler)
+/* static */SlangResult GCCDownstreamCompilerUtil::createCompiler(const ExecutableLocation& exe, RefPtr<DownstreamCompiler>& outCompiler)
{
- String exeName(inExeName);
- if (path.getLength() > 0)
- {
- exeName = Path::combine(path, inExeName);
- }
-
DownstreamCompiler::Desc desc;
- SLANG_RETURN_ON_FAIL(GCCDownstreamCompilerUtil::calcVersion(exeName, desc));
+ SLANG_RETURN_ON_FAIL(GCCDownstreamCompilerUtil::calcVersion(exe, desc));
RefPtr<CommandLineDownstreamCompiler> compiler(new GCCDownstreamCompiler(desc));
- compiler->m_cmdLine.setExecutableFilename(exeName);
+ compiler->m_cmdLine.setExecutableLocation(exe);
outCompiler = compiler;
return SLANG_OK;
@@ -656,8 +650,9 @@ static SlangResult _parseGCCFamilyLine(const UnownedStringSlice& line, LineParse
/* static */SlangResult GCCDownstreamCompilerUtil::locateGCCCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
{
SLANG_UNUSED(loader);
+
RefPtr<DownstreamCompiler> compiler;
- if (SLANG_SUCCEEDED(createCompiler(path, "g++", compiler)))
+ if (SLANG_SUCCEEDED(createCompiler(ExecutableLocation(path, "g++"), compiler)))
{
set->addCompiler(compiler);
}
@@ -669,7 +664,7 @@ static SlangResult _parseGCCFamilyLine(const UnownedStringSlice& line, LineParse
SLANG_UNUSED(loader);
RefPtr<DownstreamCompiler> compiler;
- if (SLANG_SUCCEEDED(createCompiler(path, "clang", compiler)))
+ if (SLANG_SUCCEEDED(createCompiler(ExecutableLocation(path, "clang"), compiler)))
{
set->addCompiler(compiler);
}
diff --git a/source/compiler-core/slang-gcc-compiler-util.h b/source/compiler-core/slang-gcc-compiler-util.h
index b97144e35..b1ff791b0 100644
--- a/source/compiler-core/slang-gcc-compiler-util.h
+++ b/source/compiler-core/slang-gcc-compiler-util.h
@@ -12,28 +12,30 @@ struct GCCDownstreamCompilerUtil : public DownstreamCompilerBaseUtil
/// Extracts version number into desc from text (assumes gcc/clang -v layout with a line with version)
static SlangResult parseVersion(const UnownedStringSlice& text, const UnownedStringSlice& prefix, DownstreamCompiler::Desc& outDesc);
- /// Runs the exeName, and extracts the version info into outDesc
- static SlangResult calcVersion(const String& exeName, DownstreamCompiler::Desc& outDesc);
+ /// Runs the exe, and extracts the version info into outDesc
+ static SlangResult calcVersion(const ExecutableLocation& exe, DownstreamCompiler::Desc& outDesc);
/// Calculate gcc family compilers (including clang) cmdLine arguments from options
static SlangResult calcArgs(const CompileOptions& options, CommandLine& cmdLine);
- /// Parse ExecuteResult into Output
- static SlangResult parseOutput(const ExecuteResult& exeRes, DownstreamDiagnostics& outOutput);
+ /// Parse ExecuteResult into diagnostics
+ static SlangResult parseOutput(const ExecuteResult& exeRes, DownstreamDiagnostics& out);
/// Calculate the output module filename
static SlangResult calcModuleFilePath(const CompileOptions& options, StringBuilder& outPath);
- /// Given options, calculate paths to products produced for a compilation
+ /// Given options, calculate paths to products/files produced for a compilation
static SlangResult calcCompileProducts(const CompileOptions& options, ProductFlags flags, List<String>& outPaths);
- /// Given a path and an exe name, detects if compiler is present, and if so adds to compiler set.
- static SlangResult createCompiler(const String& path, const String& inExeName, RefPtr<DownstreamCompiler>& outCompiler);
+ /// Given the exe location, creates a DownstreamCompiler.
+ /// Note! Invoke/s the compiler to determine the compiler version number.
+ static SlangResult createCompiler(const ExecutableLocation& exe, RefPtr<DownstreamCompiler>& outCompiler);
+ /// Finds GCC compiler/s and adds them to the set
static SlangResult locateGCCCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
+ /// Finds clang compiler/s and adds them to the set
static SlangResult locateClangCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
-
};
class GCCDownstreamCompiler : public CommandLineDownstreamCompiler
diff --git a/source/compiler-core/windows/slang-win-visual-studio-util.cpp b/source/compiler-core/windows/slang-win-visual-studio-util.cpp
index dd9656c23..e1a0f6109 100644
--- a/source/compiler-core/windows/slang-win-visual-studio-util.cpp
+++ b/source/compiler-core/windows/slang-win-visual-studio-util.cpp
@@ -200,7 +200,7 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out
String vswherePath = programFilesPath;
vswherePath.append("\\Microsoft Visual Studio\\Installer\\vswhere");
- cmd.setExecutableFilename(vswherePath);
+ cmd.setExecutableLocation(ExecutableLocation(vswherePath));
StringBuilder versionName;
WinVisualStudioUtil::append(version, versionName);
@@ -305,22 +305,20 @@ static SlangResult _find(int versionIndex, WinVisualStudioUtil::VersionPath& out
// 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
- CommandLine cmdLine;
+ // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
+ // To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the
+ // following arguments: /c plus the name of the batch file.
- cmdLine.setExecutableFilename("cmd.exe");
+ CommandLine cmdLine;
+ cmdLine.setExecutableLocation(ExecutableLocation(ExecutableLocation::Type::Name, "cmd.exe"));
+
{
String options[] = { "/q", "/c", "@prompt", "$" };
cmdLine.addArgs(options, SLANG_COUNT_OF(options));
}
cmdLine.addArg("&&");
-
- {
- StringBuilder path;
- path << versionPath.vcvarsPath;
- path << "\\vcvarsall.bat";
- cmdLine.addArg(path);
- }
+ cmdLine.addArg(Path::combine(versionPath.vcvarsPath, "vcvarsall.bat"));
#if SLANG_PTR_IS_32
cmdLine.addArg("x86");
diff --git a/source/core/slang-command-line.cpp b/source/core/slang-command-line.cpp
index 419c031ab..973bb46d0 100644
--- a/source/core/slang-command-line.cpp
+++ b/source/core/slang-command-line.cpp
@@ -11,6 +11,63 @@
namespace Slang {
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ExecutableLocation !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
+void ExecutableLocation::set(const String& dir, const String& name)
+{
+ if (dir.getLength() == 0)
+ {
+ set(name);
+ }
+ else
+ {
+ set(Path::combine(dir, name));
+ }
+}
+
+void ExecutableLocation::set(const String& nameOrPath)
+{
+ // See if input looks like a path
+ if (Path::hasPath(nameOrPath))
+ {
+ // If it is a path we may want to add a suffix
+ const auto suffix = Process::getExecutableSuffix();
+
+ if (suffix.getLength() == 0 || nameOrPath.endsWith(suffix))
+ {
+ setPath(nameOrPath);
+ }
+ else
+ {
+ // If on target that has suffix make sure name has the suffix
+ StringBuilder builder;
+ builder << nameOrPath;
+ builder << suffix;
+ setPath(builder.ProduceString());
+ }
+ }
+ else
+ {
+ // If we don't have a parent, we assume it is just a naem
+ setName(nameOrPath);
+ }
+}
+
+void ExecutableLocation::append(StringBuilder& out) const
+{
+ if (m_type == Type::Unknown)
+ {
+ out << "(unknown)";
+ }
+ else
+ {
+ auto escapeHandler = Process::getEscapeHandler();
+ StringEscapeUtil::appendMaybeQuoted(escapeHandler, m_pathOrName.getUnownedSlice(), out);
+ }
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CommandLine !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
void CommandLine::addPrefixPathArg(const char* prefix, const String& path, const char* pathPostfix)
{
StringBuilder builder;
@@ -23,23 +80,29 @@ void CommandLine::addPrefixPathArg(const char* prefix, const String& path, const
addArg(builder.ProduceString());
}
-void CommandLine::setExecutable(const String& dir, const String& name)
+void CommandLine::append(StringBuilder& out) const
{
- StringBuilder builder;
- Path::combineIntoBuilder(dir.getUnownedSlice(), name.getUnownedSlice(), builder);
- builder << Process::getExecutableSuffix();
- setExecutablePath(builder.ProduceString());
+ m_executableLocation.append(out);
+
+ if (m_args.getCount())
+ {
+ out << " ";
+ appendArgs(out);
+ }
}
-void CommandLine::append(StringBuilder& out) const
+void CommandLine::appendArgs(StringBuilder& out) const
{
auto escapeHandler = Process::getEscapeHandler();
- StringEscapeUtil::appendMaybeQuoted(escapeHandler, m_executable.getUnownedSlice(), out);
-
- for (const auto& arg : m_args)
+ const Int argCount = m_args.getCount();
+ for (Index i = 0; i < argCount; ++i)
{
- out << " ";
+ const auto& arg = m_args[i];
+ if (i > 0)
+ {
+ out << " ";
+ }
StringEscapeUtil::appendMaybeQuoted(escapeHandler, arg.getUnownedSlice(), out);
}
}
@@ -51,4 +114,11 @@ String CommandLine::toString() const
return buf.ProduceString();
}
+String CommandLine::toStringArgs() const
+{
+ StringBuilder buf;
+ appendArgs(buf);
+ return buf.ProduceString();
+}
+
} // namespace Slang
diff --git a/source/core/slang-command-line.h b/source/core/slang-command-line.h
index 85ba1a331..f2007865f 100644
--- a/source/core/slang-command-line.h
+++ b/source/core/slang-command-line.h
@@ -7,15 +7,57 @@
namespace Slang {
-struct CommandLine
+struct ExecutableLocation
{
- enum class ExecutableType
+ typedef ExecutableLocation ThisType;
+
+ enum Type
{
- Unknown, ///< The executable is not specified
+ Unknown, ///< Not specified
Path, ///< The executable is set as a path (ie won't be searched for)
- Filename, ///< The executable is set as a filename
+ Name, ///< The executable is passed as a name which will be searched for
};
+ /// Set the executable path.
+ /// NOTE! On some targets the executable path *must* include an extension to be able to start as a process
+ void setPath(const String& path) { m_type = Type::Path; m_pathOrName = path; }
+
+ /// Set a filename (such that the path will be looked up)
+ void setName(const String& filename) { m_type = Type::Name; m_pathOrName = filename; }
+
+ void set(Type type, const String& pathOrName) { m_type = type; m_pathOrName = pathOrName; }
+
+ /// Set the executable path from a base directory and an executable name (no suffix such as '.exe' needed)
+ void set(const String& dir, const String& name);
+
+ /// Determines if it's a name or a path when it sets
+ void set(const String& nameOrPath);
+
+ /// Append as text to out.
+ void append(StringBuilder& out) const;
+
+ /// Reset state to be same as ctor
+ void reset() { m_type = Type::Unknown; m_pathOrName = String(); }
+
+ /// Equality means exactly the same definition.
+ /// *NOT* that exactly the same executable is specified
+ bool operator==(const ThisType& rhs) const { return m_type == rhs.m_type && m_pathOrName == rhs.m_pathOrName; }
+ bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
+
+ ExecutableLocation() {}
+ ExecutableLocation(const String& dir, const String& name) { set(dir, name); }
+ ExecutableLocation(Type type, const String& pathOrName) : m_type(type), m_pathOrName(pathOrName) {}
+
+ explicit ExecutableLocation(const String& nameOrPath) { set(nameOrPath); }
+
+ Type m_type = Type::Unknown;
+ String m_pathOrName;
+};
+
+struct CommandLine
+{
+ typedef CommandLine ThisType;
+
/// Add args - assumed unescaped
void addArg(const String& in) { m_args.add(in); }
void addArgs(const String* args, Int argsCount) { for (Int i = 0; i < argsCount; ++i) addArg(args[i]); }
@@ -23,16 +65,6 @@ struct CommandLine
/// Find the index of an arg which is exact match for slice
SLANG_INLINE Index findArgIndex(const UnownedStringSlice& slice) const { return m_args.indexOf(slice); }
- /// Set the executable path.
- /// NOTE! On some targets the executable path *must* include an extension to be able to start as a process
- void setExecutablePath(const String& path) { m_executableType = ExecutableType::Path; m_executable = path; }
-
- /// Set the executable path from a base directory and an executable name (no suffix such as '.exe' needed)
- void setExecutable(const String& dir, const String& name);
-
- /// Set a filename (such that the path will be looked up
- void setExecutableFilename(const String& filename) { m_executableType = ExecutableType::Filename; m_executable = filename; }
-
/// For handling args where the switch is placed directly in front of the path
void addPrefixPathArg(const char* prefix, const String& path, const char* pathPostfix = nullptr);
@@ -42,17 +74,22 @@ struct CommandLine
/// Reset to the initial state
void reset() { *this = CommandLine(); }
+ /// Append the args
+ void appendArgs(StringBuilder& out) const;
+
/// Append the command line to out
void append(StringBuilder& out) const;
/// convert into a string
String toString() const;
- /// Ctor
- CommandLine():m_executableType(ExecutableType::Unknown) {}
+ /// Convert just the args to string
+ String toStringArgs() const;
+
+ /// Set an executable location
+ void setExecutableLocation(const ExecutableLocation& loc) { m_executableLocation = loc; }
- ExecutableType m_executableType; ///< How the executable is specified
- String m_executable; ///< Executable to run. Note that the executable is never escaped.
- List<String> m_args; ///< The arguments (Stored *unescaped*)
+ ExecutableLocation m_executableLocation; ///< The executable location
+ List<String> m_args; ///< The arguments (Stored *unescaped*)
};
}
diff --git a/source/core/slang-io.h b/source/core/slang-io.h
index 50ce67784..de761cf4b 100644
--- a/source/core/slang-io.h
+++ b/source/core/slang-io.h
@@ -74,6 +74,11 @@ namespace Slang
static Index findExtIndex(String const& path) { return findExtIndex(path.getUnownedSlice()); }
static Index findExtIndex(UnownedStringSlice const& path);
+ /// True if isn't just a name (ie has any path separator)
+ /// Note this is no the same as having a 'parent' as '/thing' 'has a path', but it doesn't have a parent.
+ static bool hasPath(const UnownedStringSlice& path) { return findLastSeparatorIndex(path) >= 0; }
+ static bool hasPath(const String& path) { return findLastSeparatorIndex(path) >= 0; }
+
static String replaceExt(const String& path, const char* newExt);
static String getFileName(const String& path);
static String getPathWithoutExt(const String& path);
diff --git a/source/core/unix/slang-unix-process.cpp b/source/core/unix/slang-unix-process.cpp
index 2c84ef08f..1f58f5725 100644
--- a/source/core/unix/slang-unix-process.cpp
+++ b/source/core/unix/slang-unix-process.cpp
@@ -355,8 +355,10 @@ static const int kCannotExecute = 126;
{
List<char const*> argPtrs;
+ const auto& exe = commandLine.m_executableLocation;
+
// Add the command
- argPtrs.add(commandLine.m_executable.getBuffer());
+ argPtrs.add(exe.m_pathOrName.getBuffer());
// Add all the args - they don't need any explicit escaping
for (auto arg : commandLine.m_args)
@@ -413,9 +415,16 @@ static const int kCannotExecute = 126;
// TODO(JS): Strictly speaking if m_executableType is 'Path' then we shouldn't be searching. Ie which
// exec we use here should be dependent on the executable type.
- // Path = execvp
- // Filename = execv
- ::execvp(argPtrs[0], (char* const*)&argPtrs[0]);
+ if (exe.m_type == ExecutableLocation::Type::Path)
+ {
+ // Use the specified path (ie don't search)
+ ::execv(argPtrs[0], (char* const*)&argPtrs[0]);
+ }
+ else
+ {
+ // Search for the executable
+ ::execvp(argPtrs[0], (char* const*)&argPtrs[0]);
+ }
// If we get here, then `exec` failed
diff --git a/source/core/windows/slang-win-process.cpp b/source/core/windows/slang-win-process.cpp
index e0e38f2d4..215904938 100644
--- a/source/core/windows/slang-win-process.cpp
+++ b/source/core/windows/slang-win-process.cpp
@@ -453,12 +453,12 @@ void WinProcess::kill(int32_t returnCode)
OSString pathBuffer;
LPCWSTR path = nullptr;
- if (commandLine.m_executableType == CommandLine::ExecutableType::Path)
+ const auto& exe = commandLine.m_executableLocation;
+ if (exe.m_type == ExecutableLocation::Type::Path)
{
- StringBuilder cmd;
- StringEscapeUtil::appendMaybeQuoted(getEscapeHandler(), commandLine.m_executable.getUnownedSlice(), cmd);
-
- pathBuffer = cmd.toWString();
+ // If it 'Path' specified we pass in as the lpApplicationName to limit
+ // searching.
+ pathBuffer = exe.m_pathOrName.toWString();
path = pathBuffer.begin();
}
@@ -479,6 +479,14 @@ void WinProcess::kill(int32_t returnCode)
createFlags |= CREATE_SUSPENDED;
}
+ // From docs:
+ // If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName
+ // specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line.
+
+ // JS:
+ // Somewhat confusingly this means that even if lpApplicationName is specified, it muse *ALSO* be included as the first
+ // whitespace delimited arg must *also* be the (possibly) quoted executable
+
// https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa
// `CreateProcess` requires write access to this, for some reason...
BOOL success = CreateProcessW(