1

I'm using gyp to generate Visual Studio projects, make files, and Xcode projects.

I would like to have a pre-build step that calls a command line tool which generates some code that I later compile in, is this possible?

Incidentally, in cmake I do it using a post build step on a project that I depend on as a kind of workaround for there being no pre build there, so a post build step would also be fine.

I can see that I can call arbitrary commands at generation time using <!() syntax but I'd really prefer to generate the projects once and then on compile have the code generation step occur.

I got somewhere using actions...

I have this little application:

main.c

#include <stdio.h>

int main() {
    #include "some_output"
    return 0;
}

And this input file

some_input

printf("Hello World!\n");

And so I can fake up some, albeit Windows specific, code generation in my .gyp file like so (i.e. printing the file to standard out and redirecting back into a file, yes, silly but it illustrates the point hopefully):

gypping.gyp

{
    'targets': [
    {
        'target_name': 'gypping',
        'type': 'executable',
        'sources': [
            'main.c',
            '<(INTERMEDIATE_DIR)/some_output',
        ],
        'actions': [{
                'action_name': 'create_something_generated',
                'inputs': [ 
                    'some_input'
                ],
                'outputs': [
                    '<(INTERMEDIATE_DIR)/some_output',
                ],
                'action': ['type', '<@(_inputs)', '>', '<@(_outputs)'],
            },
        ],
      },
    ],
  }

This seems to nearly work in so much as when I build in Visual Studio (having run gyp --depth=.) I get the following error:

1>------ Build started: Project: gypping, Configuration: Default Win32 ------
1>  create_something_generated
1>  '"C:\dev\code\Sandpit\gypping\.\setup_env.bat"' is not recognized as an internal or external command,
1>  operable program or batch file.
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(171,5): error MSB6006: "cmd.exe" exited with code 1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I can't find setup_env.bat in my path.

I created an empty setup_env.bat file and I simplified the action by removing the > (I guess the escaping goes wrong with this). Now I get this:

'bash' is not recognized as an internal or external command,

I do not have bash as I'm on Windows.

I think Gyp is finished for me then, I guess I'll stick to cmake.

kmp
  • 10,535
  • 11
  • 75
  • 125

1 Answers1

1

actions are exactly what you want. They are run pre-compile and the Git docs show them being used to generate files to be used during the compile step.

I have Git for Windows installed and use that, not cygwin, for running the generated actions via Visual Studio. Here is my setenv.bat:

REM Set up to use Git Bash for running "cygwin" actions.

REM CD to where this file lives to avoid path relativization problems.
cd %~dp0

set PATH=%PATH%;C:\Program Files\Git\bin

I have also seen Git for Windows installed in %USERPROFILE%\AppData\Local\Programs\Git. I have no idea what causes the differing locations. Change the above PATH as necessary.

I am looking for the answer to how to generate a post build step

msc
  • 1,549
  • 2
  • 12
  • 19
  • 1
    Thanks very much! I discovered how to stop it needing cygwin - you need to specify 'msvs_cygwin_shell': 0, inside the action. – kmp Mar 07 '17 at 08:19
  • Yes indeed. I came back to your question add a comment about that. Sorry I originally forgot to mention it in my answer. – msc Mar 09 '17 at 08:06