Home > Net >  Makefile compute new version of each folder
Makefile compute new version of each folder

Time:01-16

I have got a tree structure like this :

upperfolder 
│
└───1
│   │   output_v1.xml
│   │   output_v2.xml
│   
└───2
│   │   output_v1.xml
│   
└───3

and i would like to goes to this :

upperfolder 
│
└───1
│   │   output_v1.xml
│   │   output_v2.xml
│   │   output_v3.xml
│   
└───2
│   │   output_v1.xml
│   │   output_v2.xml
│   
└───3
│   │   output_v1.xml

I use a makefile to do this and it looks like this :

./upperfolder/%:
    --- here i would like to grab the last version of each folder % , incremente it and ---
    --- compute next version name file to put it in parameters of my python script      ---
    python3 writer.py <previous_version_path_script> $@_v?.xml


all: ./upperfolder/1/output ./upperfolder/2/output ./upperfolder/3/output ./upperfolder/4/output 

Indeed my python script need 2 entries : 1) The path of previous version xml file 2) The path of new version xml file

I tried to eval variables fetching the last char of pwd of target (that is the number of folder since we can't grab the % value in target (grrrrrrrrrr)) like this :

$(eval number := $(shell basename $@))

But i'm not really satisfied because let's imagine the folder name is not a simple number but something number, i would need to fetch the last char of it. So i'm pretty sure there exists a better way to do it. Concerning the last version of each folder, to be honest i don't have any idea how i could do it.

Help or ints would be really appreciated

CodePudding user response:

You shouldn't use make to do this.

Makefiles require that you know what the final thing you want to build is. That final file is the target that make will try to build: it starts with that target and works backward find the things needed to build it. It can visualize what kinds of things you might need to build a given target, but you have to give it a target: it can't visualize forward from an existing file to some new file that it doesn't know about.

In your case you can't write down in your makefile what the final targets you want to build are, because they aren't static: the name of final target depends on the existing files.

Also, make's entire reason for existing is to avoid updating files if they already exist and are up to date. In your case you always want to create a new file, so it's not of any use to use make to try to avoid building something.

You would have to write a script that figures out what the current version is, then generates a new version from that. At that point it seems useless to even involve make: just have your script run the python command to generate the file.

In short, make is simply the wrong tool for the job you want to do.

  • Related