make: File Function

1 
1 8.6 The 'file' Function
1 =======================
1 
1 The 'file' function allows the makefile to write to or read from a file.
1 Two modes of writing are supported: overwrite, where the text is written
1 to the beginning of the file and any existing content is lost, and
1 append, where the text is written to the end of the file, preserving the
1 existing content.  In both cases the file is created if it does not
1 exist.  It is a fatal error if the file cannot be opened for writing, or
1 if the write operation fails.  The 'file' function expands to the empty
1 string when writing to a file.
1 
1    When reading from a file, the 'file' function expands to the verbatim
1 contents of the file, except that the final newline (if there is one)
1 will be stripped.  Attempting to read from a non-existent file expands
1 to the empty string.
1 
1    The syntax of the 'file' function is:
1 
1      $(file OP FILENAME[,TEXT])
1 
1    When the 'file' function is evaluated all its arguments are expanded
1 first, then the file indicated by FILENAME will be opened in the mode
1 described by OP.
1 
1    The operator OP can be '>' to indicate the file will be overwritten
1 with new content, '>>' to indicate the current contents of the file will
1 be appended to, or '<' to indicate the contents of the file will be read
1 in.  The FILENAME specifies the file to be written to or read from.
1 There may optionally be whitespace between the operator and the file
1 name.
1 
1    When reading files, it is an error to provide a TEXT value.
1 
1    When writing files, TEXT will be written to the file.  If TEXT does
1 not already end in a newline a final newline will be written (even if
1 TEXT is the empty string).  If the TEXT argument is not given at all,
1 nothing will be written.
1 
1    For example, the 'file' function can be useful if your build system
1 has a limited command line size and your recipe runs a command that can
1 accept arguments from a file as well.  Many commands use the convention
1 that an argument prefixed with an '@' specifies a file containing more
1 arguments.  Then you might write your recipe in this way:
1 
1      program: $(OBJECTS)
1              $(file >$@.in,$^)
1              $(CMD) $(CMDFLAGS) @$@.in
1              @rm $@.in
1 
1    If the command required each argument to be on a separate line of the
1 input file, you might write your recipe like this:
1 
1      program: $(OBJECTS)
1              $(file >$@.in) $(foreach O,$^,$(file >>$@.in,$O))
1              $(CMD) $(CMDFLAGS) @$@.in
1              @rm $@.in
1