From 8dd523fa67f03add766aaf094409d468b20d9542 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Tue, 26 Jun 2012 20:55:33 -0700 Subject: [PATCH] Added support for outputing makefile style dependencies when compiling shaders. --- 3rdparty/fcpp/cpp1.c | 1 + 3rdparty/fcpp/cpp3.c | 7 ++++++ 3rdparty/fcpp/cppadd.h | 2 ++ 3rdparty/fcpp/fpp.h | 3 +++ tools/shaderc.cpp | 52 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/3rdparty/fcpp/cpp1.c b/3rdparty/fcpp/cpp1.c index 4456ea2f..5c8fd813 100644 --- a/3rdparty/fcpp/cpp1.c +++ b/3rdparty/fcpp/cpp1.c @@ -99,6 +99,7 @@ int fppPreProcess(struct fppTag *tags) global->macro=NULL; global->evalue=0; + global->depends=NULL; global->input=NULL; global->output=NULL; global->error=NULL; diff --git a/3rdparty/fcpp/cpp3.c b/3rdparty/fcpp/cpp3.c index 2f77fbc5..7d3cfcf0 100644 --- a/3rdparty/fcpp/cpp3.c +++ b/3rdparty/fcpp/cpp3.c @@ -40,6 +40,10 @@ ReturnCode openfile(struct Global *global, char *filename) else ret=addfile(global, fp, filename); + if(!ret && global->depends) { + global->depends(filename, global->userdata); + } + if(!ret && global->showincluded) { /* no error occured! */ Error(global, "cpp: included \""); @@ -237,6 +241,9 @@ int dooptions(struct Global *global, struct fppTag *tags) strcpy(global->work, tags->data); /* Remember input filename */ global->first_file=tags->data; break; + case FPPTAG_DEPENDS: + global->depends=(void (*)(char *, void *))tags->data; + break; case FPPTAG_INPUT: global->input=(char *(*)(char *, int, void *))tags->data; break; diff --git a/3rdparty/fcpp/cppadd.h b/3rdparty/fcpp/cppadd.h index a088af3d..b65db92e 100644 --- a/3rdparty/fcpp/cppadd.h +++ b/3rdparty/fcpp/cppadd.h @@ -199,6 +199,8 @@ struct Global { int evalue; /* Current value from evallex() */ + void (*depends)(char *filename, void *); /* depends function */ + char *(*input)(char *, int, void *); /* Input function */ char *first_file; /* Preprocessed file. */ diff --git a/3rdparty/fcpp/fpp.h b/3rdparty/fcpp/fpp.h index bf774dca..fe25ac52 100644 --- a/3rdparty/fcpp/fpp.h +++ b/3rdparty/fcpp/fpp.h @@ -156,4 +156,7 @@ struct fppTag { /* Switch on WWW-mode */ #define FPPTAG_WEBMODE 33 +/* Depends function: */ +#define FPPTAG_DEPENDS 34 /* data is an depends funtion */ + int fppPreProcess(struct fppTag *); diff --git a/tools/shaderc.cpp b/tools/shaderc.cpp index edc67496..d4d8122b 100644 --- a/tools/shaderc.cpp +++ b/tools/shaderc.cpp @@ -268,7 +268,7 @@ void printCode(const char* _code) bool compileGLSLShader(CommandLine& _cmdLine, const std::string& _code, const char* _outFilePath) { - const glslopt_shader_type type = (0 == _stricmp(_cmdLine.findOption('\0', "type"), "fragment") ) ? kGlslOptShaderFragment : kGlslOptShaderVertex; + const glslopt_shader_type type = tolower(_cmdLine.findOption('\0', "type")[0]) == 'f' ? kGlslOptShaderFragment : kGlslOptShaderVertex; glslopt_ctx* ctx = glslopt_initialize(false); @@ -500,6 +500,10 @@ struct Preprocessor m_tagptr->data = this; m_tagptr++; + m_tagptr->tag = FPPTAG_DEPENDS; + m_tagptr->data = (void*)fppDepends; + m_tagptr++; + m_tagptr->tag = FPPTAG_INPUT; m_tagptr->data = (void*)fppInput; m_tagptr++; @@ -550,11 +554,22 @@ struct Preprocessor m_default += temp; } + void addDependency(const char* _fileName) + { + m_depends += " \\\n "; + m_depends += _fileName; + } + bool run() { m_fgetsPos = 0; FILE* file = fopen(m_filePath, "r"); + if (NULL == file) + { + return false; + } + long int size = fsize(file); char* input = new char[size+1]; size = fread(input, 1, size, file); @@ -593,6 +608,12 @@ struct Preprocessor return NULL; } + static void fppDepends(char* _fileName, void* _userData) + { + Preprocessor* thisClass = (Preprocessor*)_userData; + thisClass->addDependency(_fileName); + } + static char* fppInput(char* _buffer, int _size, void* _userData) { Preprocessor* thisClass = (Preprocessor*)_userData; @@ -623,6 +644,7 @@ struct Preprocessor fppTag* m_tagptr; char* m_filePath; + std::string m_depends; std::string m_default; std::string m_input; std::string m_preprocessed; @@ -673,6 +695,7 @@ int main(int _argc, const char* _argv[]) return EXIT_FAILURE; } + bool depends = cmdLine.hasArg("depends"); bool preprocessOnly = cmdLine.hasArg("preprocess"); Preprocessor preprocessor(filePath); @@ -788,19 +811,34 @@ int main(int _argc, const char* _argv[]) return EXIT_SUCCESS; } + bool compiled = false; + if (glsl) { - if (compileGLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) ) - { - return EXIT_SUCCESS; - } + compiled = compileGLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath); } else { - if (compileHLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath) ) + compiled = compileHLSLShader(cmdLine, preprocessor.m_preprocessed, outFilePath); + } + + if (compiled + && depends) + { + std::string ofp = outFilePath; + ofp += ".d"; + FILE* out = fopen(ofp.c_str(), "wb"); + if (NULL != out) { - return EXIT_SUCCESS; + Stream stream(out); + stream.write(outFilePath); + stream.write(":"); + stream.write(preprocessor.m_depends.c_str() ); + stream.write("\n"); + stream.close(); + fclose(out); } + return EXIT_SUCCESS; } }