diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index eacfda480..7da1f79c6 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -1367,8 +1367,16 @@ rule FORTRAN_DEP_HACK infilename = os.path.join(self.build_to_src, curfile) outfiles = genlist.get_outputs_for(curfile) outfiles = [os.path.join(self.get_target_private_dir(target), of) for of in outfiles] + if generator.depfile is None: + rulename = 'CUSTOM_COMMAND' + args = base_args + else: + rulename = 'CUSTOM_COMMAND_DEP' + depfilename = generator.get_dep_outname(infilename) + depfile = os.path.join(self.get_target_private_dir(target), depfilename) + args = [x.replace('@DEPFILE@', depfile) for x in base_args] args = [x.replace("@INPUT@", infilename).replace('@OUTPUT@', sole_output)\ - for x in base_args] + for x in args] args = self.replace_outputs(args, self.get_target_private_dir(target), outfilelist) # We have consumed output files, so drop them from the list of remaining outputs. if sole_output == '': @@ -1377,7 +1385,9 @@ rule FORTRAN_DEP_HACK args = [x.replace("@SOURCE_DIR@", self.build_to_src).replace("@BUILD_DIR@", relout) for x in args] cmdlist = exe_arr + self.replace_extra_args(args, genlist) - elem = NinjaBuildElement(self.all_outputs, outfiles, 'CUSTOM_COMMAND', infilename) + elem = NinjaBuildElement(self.all_outputs, outfiles, rulename, infilename) + if generator.depfile is not None: + elem.add_item('DEPFILE', depfile) if len(extra_dependencies) > 0: elem.add_dep(extra_dependencies) elem.add_item('DESC', 'Generating $out') diff --git a/mesonbuild/build.py b/mesonbuild/build.py index c92c99f59..2d6840afc 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -612,6 +612,7 @@ class Generator(): if not isinstance(exe, Executable) and not isinstance(exe, dependencies.ExternalProgram): raise InvalidArguments('First generator argument must be an executable.') self.exe = exe + self.depfile = None self.process_kwargs(kwargs) def __repr__(self): @@ -633,7 +634,6 @@ class Generator(): if not isinstance(a, str): raise InvalidArguments('A non-string object in "arguments" keyword argument.') self.arglist = args - if 'output' not in kwargs: raise InvalidArguments('Generator must have "output" keyword argument.') outputs = kwargs['output'] @@ -651,12 +651,26 @@ class Generator(): if '@OUTPUT@' in o: raise InvalidArguments('Tried to use @OUTPUT@ in a rule with more than one output.') self.outputs = outputs + if 'depfile' in kwargs: + depfile = kwargs['depfile'] + if not isinstance(depfile, str): + raise InvalidArguments('Depfile must be a string.') + if os.path.split(depfile)[1] != depfile: + raise InvalidArguments('Depfile must be a plain filename without a subdirectory.') + self.depfile = depfile def get_base_outnames(self, inname): plainname = os.path.split(inname)[1] basename = plainname.split('.')[0] return [x.replace('@BASENAME@', basename).replace('@PLAINNAME@', plainname) for x in self.outputs] + def get_dep_outname(self, inname): + if self.depfile is None: + raise InvalidArguments('Tried to get dep name for rule that does not have dependency file defined.') + plainname = os.path.split(inname)[1] + basename = plainname.split('.')[0] + return self.depfile.replace('@BASENAME@', basename).replace('@PLAINNAME@', plainname) + def get_arglist(self): return self.arglist diff --git a/test cases/common/29 pipeline/meson.build b/test cases/common/29 pipeline/meson.build index 8418381a4..200a6d8f2 100644 --- a/test cases/common/29 pipeline/meson.build +++ b/test cases/common/29 pipeline/meson.build @@ -6,8 +6,9 @@ e1 = executable('srcgen', 'srcgen.c', native : true) # Generate a source file that needs to be included in the build. gen = generator(e1, \ - output : '@BASENAME@.c', # Line continuation inside arguments should work without needing a "\". - arguments : ['@INPUT@', '@OUTPUT@']) + depfile : '@BASENAME@.d', + output : '@BASENAME@.c', # Line continuation inside arguments should work without needing a "\". + arguments : ['@INPUT@', '@OUTPUT@', '@DEPFILE@']) generated = gen.process(['input_src.dat']) diff --git a/test cases/common/29 pipeline/srcgen.c b/test cases/common/29 pipeline/srcgen.c index 8095724ab..ceb9eccee 100644 --- a/test cases/common/29 pipeline/srcgen.c +++ b/test cases/common/29 pipeline/srcgen.c @@ -1,5 +1,6 @@ #include #include +#include #define ARRSIZE 80 @@ -7,17 +8,20 @@ int main(int argc, char **argv) { char arr[ARRSIZE]; char *ofilename; char *ifilename; + char *dfilename; FILE *ifile; FILE *ofile; + FILE *depfile; size_t bytes; + int i; - if(argc != 3) { - fprintf(stderr, "%s \n", argv[0]); + if(argc != 4) { + fprintf(stderr, "%s \n", argv[0]); return 1; } ifilename = argv[1]; ofilename = argv[2]; - printf("%s\n", ifilename); + dfilename = argv[3]; ifile = fopen(argv[1], "r"); if(!ifile) { fprintf(stderr, "Could not open source file %s.\n", argv[1]); @@ -34,7 +38,32 @@ int main(int argc, char **argv) { assert(bytes > 0); fwrite(arr, 1, bytes, ofile); + depfile = fopen(dfilename, "w"); + if(!depfile) { + fprintf(stderr, "Could not open depfile %s\n", ofilename); + fclose(ifile); + fclose(ofile); + return 1; + } + for(i=0; i