I run a buildroot make and during this build process a text file is created. After this happens another make as part of the build is run. I want to read the contents of the generated file into a variable and use them. This doesn't work as at the start of my buildroot make it sees the file as not existing, so even though it is generated before the code is run make has already decided it doesn't exist.

Format of the file read:

  str1 str2 1.1.0_nightlybuild (1389:1497M@trunk)

I am trying to use eval (this should check the file contents at the time of running). However the problem persists, I do not seem to be able to read from the file after it is created. It seems make might be expanding the variable and so eval does not work.

None of the variables are being set from the file contents. I check and the file has been generated correctly so if I run the make again, when the file has already been generated, everything works fine. Is eval not the correct way to do this?

So the file does not exist at the start of make, comes into existence and then this code is run:

define EXAMPLE
    $(eval s := $(shell cat output/target/version.txt)) 
    $(eval FILENAME_BIN=$(word 1, $(s)).$(word 2, $(s)).$(word 3, $(s)).bin)
    $(eval FILENAME_JFFS2=$(word 1, $(s)).$(word 2, $(s)).$(word 3, $(s)).jffs2)

It does not work, the file is generated and subsequent builds will work, just not the first one where the file does not exist at the start of the process. The first time the files are simply called ...bin and ...jffs2 as the variables read from the file are not populated. When it works the files are called str1.str2.1.1.0_nightlybuild.bin str1.str2. 1.1.0_nightlybuild.jffs2

Here is the real use case makefile http://pastebin.com/6GdKcUg6 TEST_DEFAULT_RENAME is where I am trying to read in a file, I am using buildroot (so a lot of make files), when I hit make during the buildroot process version.txt is created along the way, this code then tries to read this file but it does not work unless version.text is there before I enter the make in the buildroot directory. As you can see I use version.txt elsewhere via cat but that works fine.

  • can't you simply test at the appropriate target for the variable s being empty and if so do a recursive $(MAKE) same-target?
    – meuh
    Jan 11, 2017 at 10:46
  • There is similar question on StackOverflow with some answers: stackoverflow.com/questions/10435490/… Feb 9, 2021 at 9:45

1 Answer 1


The best solution is to change the format of your file to something make can parse directly and then simple -include it. make will know to generate it and automatically rerun itself after it has done so. You can do the conversion in the rule which generates your current file or add a separate rule to generate a different file from it.

For example, put this in Makefile:

-include datafile

    echo VAR is $(VAR)

datafile: Makefile
    echo "VAR=value" > $@

The first time you run make, you will see the command which generates datafile. If you use make -d you will see a line which says Re-executing[1]: make -d which is where make runs itself again and is able to read the contents of datafile.

If you run make once datafile exists, it runs only once. But because it has a depencency on Makefile, if you edit that to change value to some other string, datafile will be correctly regenerated.

  • Thanks, I have updated the question to show a more accurate version of the issue as I feel I misrepresented it. The file does exist at time this make file is run, however it just doesn't exist at the start of the buildroot make process I am doing. (Another part of the process makes the file before this code is run)
    – Paul
    Jan 11, 2017 at 9:44
  • @Paul I don't see the part where other Makefiles are called, is it part of the buildroot system? Your eval code probably runs too early but I can't easily say why without knowing the entire system (ie. how those hooks variables are used). Is there no way you could move creation of your version.txt into the Makefile which uses it?
    – Olivier
    Jan 11, 2017 at 13:14
  • I moved the creation of the file into the make that uses it, but it doesn't work. It seems to read the value of the file when make is run (so file not existing), not the new value. I have simplified the question to the real issue here: unix.stackexchange.com/questions/336797/…
    – Paul
    Jan 12, 2017 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .