From 74f2f47cb63b02638270beecd20acea1a0f5665e Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 27 Sep 2017 11:17:39 -0700 Subject: First attempt at a Linux build (#193) * First attempt at a Linux build - Fix up places where C++ idioms were written assuming lenient behavior of Microsoft's compiler - Add a few more alternatives for platform-specific behavior where Windows was the only platform accounted for. - Add a basic Makefile that can at least invoke our build, even if it isn't going good dependency tracking, etc. - Build `libslang.so` and `slangc` that depends on it, using a relative `RPATH` to make the binary portable (I hope) - Add an initial `.travis.yml` to see if we can trigger their build process. * Fixup: const bug in `List::Sort` I'm not clear why this gets picked up by the gcc *and* clang that Travis uses, but not the (newer) gcc I'm using on Ubuntu here, but I'm hoping it is just some missing `const` qualifiers. * Fixup: reorder specialization of "class info" Clang complains about things being specialized after being instantiated (implicilty), and I hope it is just the fact that I generate the class info for the roots of the hierarchy after the other cases. We'll see. * Fixup: add `platform.cpp` to unified/lumped build * Fixup: Windows uses `FreeLibrary` and not `UnloadLibrary` * Fixup: fix Windows project file to include new source file This obviously points to the fact that we are going to need to be generating these files sooner or later. --- source/slang/compiler.cpp | 117 ++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 62 deletions(-) (limited to 'source/slang/compiler.cpp') diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp index b8830c0b4..d7de5c42e 100644 --- a/source/slang/compiler.cpp +++ b/source/slang/compiler.cpp @@ -1,6 +1,7 @@ // Compiler.cpp : Defines the entry point for the console application. // #include "../core/basic.h" +#include "../core/platform.h" #include "../core/slang-io.h" #include "compiler.h" #include "lexer.h" @@ -12,21 +13,43 @@ #include "reflection.h" #include "emit.h" -// Utilities for pass-through modes -#include "../slang-glslang/slang-glslang.h" +// Enable calling through to `fxc` to +// generate code on Windows. +#ifdef _WIN32 + #define WIN32_LEAN_AND_MEAN + #define NOMINMAX + #include + #undef WIN32_LEAN_AND_MEAN + #undef NOMINMAX + #include + #ifndef SLANG_ENABLE_DXBC_SUPPORT + #define SLANG_ENABLE_DXBC_SUPPORT 1 + #endif +#endif +// +// Otherwise, don't enable DXBC by default: +#ifndef SLANG_ENABLE_DXBC_SUPPORT + #define SLANG_ENABLE_DXBC_SUPPORT 0 +#endif +// Enable calling through to `glslang` on +// all platforms. +#ifndef SLANG_ENABLE_GLSLANG_SUPPORT + #define SLANG_ENABLE_GLSLANG_SUPPORT 1 +#endif -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX -#include -#undef WIN32_LEAN_AND_MEAN -#undef NOMINMAX -#include +#if SLANG_ENABLE_GLSLANG_SUPPORT +#include "../slang-glslang/slang-glslang.h" #endif -#include +// Includes to allow us to control console +// output when writing assembly dumps. #include +#ifdef _WIN32 +#include +#else +#include +#endif #ifdef _MSC_VER #pragma warning(disable: 4996) @@ -189,7 +212,7 @@ namespace Slang } } -#ifdef _WIN32 +#if SLANG_ENABLE_DXBC_SUPPORT HMODULE loadD3DCompilerDLL(CompileRequest* request) { char const* libraryName = "d3dcompiler_47"; @@ -264,28 +287,6 @@ namespace Slang return data; } -#if 0 - List EmitDXBytecode( - ExtraContext& context) - { - if(context.getTranslationUnitOptions().entryPoints.Count() != 1) - { - if(context.getTranslationUnitOptions().entryPoints.Count() == 0) - { - // TODO(tfoley): need to write diagnostics into this whole thing... - fprintf(stderr, "no entry point specified\n"); - } - else - { - fprintf(stderr, "multiple entry points specified\n"); - } - return List(); - } - - return EmitDXBytecodeForEntryPoint(context, context.getTranslationUnitOptions().entryPoints[0]); - } -#endif - String dissassembleDXBC( CompileRequest* compileRequest, void const* data, @@ -345,32 +346,16 @@ namespace Slang return result; } - -#if 0 - String EmitDXBytecodeAssembly( - ExtraContext& context) - { - if(context.getTranslationUnitOptions().entryPoints.Count() == 0) - { - // TODO(tfoley): need to write diagnostics into this whole thing... - fprintf(stderr, "no entry point specified\n"); - return ""; - } - - StringBuilder sb; - for (auto entryPoint : context.getTranslationUnitOptions().entryPoints) - { - sb << EmitDXBytecodeAssemblyForEntryPoint(context, entryPoint); - } - return sb.ProduceString(); - } #endif - HMODULE loadGLSLCompilerDLL(CompileRequest* request) +#if SLANG_ENABLE_GLSLANG_SUPPORT + + SharedLibrary loadGLSLCompilerDLL(CompileRequest* request) { char const* libraryName = "slang-glslang"; // TODO(tfoley): let user specify version of glslang DLL to use. - HMODULE glslCompiler = LoadLibraryA(libraryName); + + SharedLibrary glslCompiler = SharedLibrary::load(libraryName); if (!glslCompiler) { request->mSink.diagnose(SourceLoc(), Diagnostics::failedToLoadDynamicLibrary, libraryName); @@ -378,9 +363,9 @@ namespace Slang return glslCompiler; } - HMODULE getGLSLCompilerDLL(CompileRequest* request) + SharedLibrary getGLSLCompilerDLL(CompileRequest* request) { - static HMODULE glslCompiler = loadGLSLCompilerDLL(request); + static SharedLibrary glslCompiler = loadGLSLCompilerDLL(request); return glslCompiler; } @@ -393,11 +378,11 @@ namespace Slang static glslang_CompileFunc glslang_compile = nullptr; if (!glslang_compile) { - HMODULE glslCompiler = getGLSLCompilerDLL(slangCompileRequest); + SharedLibrary glslCompiler = getGLSLCompilerDLL(slangCompileRequest); if (!glslCompiler) return 1; - glslang_compile = (glslang_CompileFunc)GetProcAddress(glslCompiler, "glslang_compile"); + glslang_compile = (glslang_CompileFunc) glslCompiler.findFuncByName("glslang_compile"); if (!glslang_compile) return 1; } @@ -532,6 +517,7 @@ namespace Slang } break; +#if SLANG_ENABLE_DXBC_SUPPORT case CodeGenTarget::DXBytecode: { List code = EmitDXBytecodeForEntryPoint(entryPoint); @@ -547,6 +533,7 @@ namespace Slang result = CompileResult(code); } break; +#endif case CodeGenTarget::SPIRV: { @@ -712,6 +699,7 @@ namespace Slang switch (compileRequest->Target) { + #if SLANG_ENABLE_DXBC_SUPPORT case CodeGenTarget::DXBytecode: { String assembly = dissassembleDXBC(compileRequest, @@ -720,6 +708,7 @@ namespace Slang writeOutputToConsole(compileRequest, assembly); } break; + #endif case CodeGenTarget::SPIRV: { @@ -738,7 +727,9 @@ namespace Slang else { // Redirecting stdout to a file, so do the usual thing + #ifdef _WIN32 _setmode(stdoutFileDesc, _O_BINARY); + #endif writeOutputFile( compileRequest, stdout, @@ -955,10 +946,6 @@ namespace Slang dumpIntermediateText(compileRequest, data, size, ".spv.asm"); break; - case CodeGenTarget::DXBytecodeAssembly: - dumpIntermediateText(compileRequest, data, size, ".dxbc.asm"); - break; - case CodeGenTarget::SlangIRAssembly: dumpIntermediateText(compileRequest, data, size, ".slang-ir.asm"); break; @@ -971,6 +958,11 @@ namespace Slang } break; + #if SLANG_ENABLE_DXBC_SUPPORT + case CodeGenTarget::DXBytecodeAssembly: + dumpIntermediateText(compileRequest, data, size, ".dxbc.asm"); + break; + case CodeGenTarget::DXBytecode: dumpIntermediateBinary(compileRequest, data, size, ".dxbc"); { @@ -978,6 +970,7 @@ namespace Slang dumpIntermediateText(compileRequest, dxbcAssembly.begin(), dxbcAssembly.Length(), ".dxbc.asm"); } break; + #endif case CodeGenTarget::SlangIR: dumpIntermediateBinary(compileRequest, data, size, ".slang-ir"); -- cgit v1.2.3