visual-studiomsbuildvisual-studio-2017visual-studio-2019pre-build-event

Running SPIR-V compiler as pre-build event in VS2017 if only the shader code was modified


So I've look at a few questions here which popped up after a search, but I still haven't managed to get this to work.

My project has two files, main.cpp and shader.comp.

The shader needs to be compiled before the main program is ran and I have a small .bat script that does just that. It's set to run as a pre-build event.

However, if I edit shader.comp and leave main.cpp unmodified since I last ran the project, there is no need to rebuild the project (according to VS anyway), so there is no need to run any pre-build events and my shader doesn't get compiled.

Is there a way to tell VS2017 (or VS2019) that if some file is modified, then run something, or at least a way to add an arbitrary file to list of files that VS checks against when deciding whether to run the build or not?

I've tried setting "Exclude from build" to "No" in the file properties, but no matter what "Item type" I choose, editing just the shader won't trigger the rebuild.


Solution

  • It's possible to define the shader that needs to be compiled as Custom Build Tool in the properties of the file (as Item Type). This will open another menu in the properties where cmd script and similar can be written.

    In this particular case, value for Command Line was:

    glslangValidator.exe -V -o "%(RootDir)%(Directory)%(Filename).spv" "%(FullPath)"
    

    And the Outputs:

    %(RootDir)%(Directory)%(Filename).spv
    

    In short, if file defined in Outputs doesn't exists or is older than the owner of this property (the file that needs to be compiled), the Command Line argument will be ran in cmd.

    Official documentation has more info on this.