0

I am trying to create a file and set the contents of a file to a variable but the variable is always seen as empty.

An example:

define FILE_VAR
    cat /output/1.txt >> /output/2.txt
    $(eval s := $(shell cat /output/2.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)        
endef

Called via this as a post-gen hook:

# Alter the post-gen hooks, depending on what options are selected
ifneq ($(BR2_TARGET_ROOTFS_EXT2_FILE_VAR),)
    ROOTFS_EXT2_POST_GEN_HOOKS += FILE_VAR
endif

If 2.txt exists before running this the variables will be set to the data prior to running make (not the new redirected data), if 2.txt doesn't exist then the variables are not set. It looks like it is evaluating what the file is like when make is first run, which is not what I want.

2

1 Answer 1

1

You don't specify how FILE_VAR is used but if I try $(eval $(FILE_VAR)), then the problem is that cat does not do what you want outside a rule. Wrapping it in $(shell ) might work better:

define FILE_VAR
    $(shell cat /output/1.txt > /output/2.txt)
    $(eval s := $(shell cat /output/2.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)
endef

$(eval $(FILE_VAR))                                                                     

all:                                                                                    
    echo $(FILENAME_BIN)

And I used > instead of >> so 2.txt does not grow on every run.

2
  • I added how it is called, this all is building a filesystem through buildroot. Your solution did not help unfortunately. It's like the values at time of buildroot make are just set in stone if it's being read from a file. I have no problem with delayed variable expansion for normal variables, just when reading from files.
    – Paul
    Commented Jan 12, 2017 at 15:30
  • 1
    @Paul you still don't show how ROOTFS_EXT2_POST_GEN_HOOKS is used. That's probably key to your problem. Figure this out and you will have a better chance at making it work without guesswork.
    – Olivier
    Commented Jan 12, 2017 at 17:41

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