diff options
Diffstat (limited to 'source/core/unix')
| -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/unix/slang-unix-process-util.cpp | 4 |
3 files changed, 191 insertions, 0 deletions
diff --git a/source/core/unix/slang-unix-cpp-compiler-util.cpp b/source/core/unix/slang-unix-cpp-compiler-util.cpp new file mode 100644 index 000000000..a5459b665 --- /dev/null +++ b/source/core/unix/slang-unix-cpp-compiler-util.cpp @@ -0,0 +1,163 @@ +#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 CPPCompileOptions& options, CommandLine& cmdLine) +{ + typedef CPPCompileOptions::OptimizationLevel OptimizationLevel; + typedef CPPCompileOptions::TargetType TargetType; + typedef CPPCompileOptions::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 new file mode 100644 index 000000000..03c1b5e2e --- /dev/null +++ b/source/core/unix/slang-unix-cpp-compiler-util.h @@ -0,0 +1,24 @@ +#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 CPPCompileOptions& options, CommandLine& cmdLine); +}; + +} // namespace Slang + +#endif diff --git a/source/core/unix/slang-unix-process-util.cpp b/source/core/unix/slang-unix-process-util.cpp index 0447dd499..400f4f773 100644 --- a/source/core/unix/slang-unix-process-util.cpp +++ b/source/core/unix/slang-unix-process-util.cpp @@ -20,7 +20,11 @@ namespace Slang { /* static */UnownedStringSlice ProcessUtil::getExecutableSuffix() { +#if __CYGWIN__ + return UnownedStringSlice::fromLiteral(".exe"); +#else return UnownedStringSlice::fromLiteral(""); +#endif } /* static */void ProcessUtil::appendCommandLineEscaped(const UnownedStringSlice& slice, StringBuilder& out) |
