Use generated code in bazel assembly

$ python gencpp.py 

This command creates the cpp file foo.cpp in the working directory.

I want to run this command in bazel before creation to include foo.cpp in the cc_binary srcs attribute.

What I tried:

 genrule( name = 'foo', outs = ['foo.cpp'], cmd = 'python gencpp.py', ) cc_library( srcs = ['foo.cpp'], # tried also with :foo ... ) 

the declared output of 'external / somelib / foo.cpp' was not created by genrule. This is probably due to the fact that genrule did not actually generate this output, or because the output was a directory, and genrule was started remotely (note that only the contents of the declared files unloaded from genrules are started remotely).

I know that there is a solution that requires gencpp.py be slightly modified, but that is not what I am looking for.

+5
source share
2 answers

Thanks @kristina for the answer .

I need to copy the foo.cpp directory to outs after creating it.

 genrule( name = 'foo', outs = ['foo.cpp'], cmd = """ python gencpp.py cp foo.cpp $@ """, ) 
+4
source

This command generates the cpp file foo.cpp in the working directory.

I would recommend you change this to either:

  • You write the output to the file indicated by the command line flag
  • You write output to standard output.

Then your genrule command can be either:

 python gencpp.py --outputFile=$@ 

or

 python gencpp.py > $@ 

respectively.

As Ulf Adams points out:

Bazel performs several actions in parallel, and if the same rule is dependent on the tool, as well as on the application, it may try to run both at the same time, and they overwrite each other, with potentially very bad results.

Therefore, it is best to avoid writing output files that the basel does not know directly.

+2
source

Source: https://habr.com/ru/post/1264247/


All Articles