How to sync specific file with makefile


How to sync specific file with makefile



I have a makefile which I generate according to a JSON file structure that I have. I.e. user will provide some JSON file in his project and by executing some CLI tool (which I will provide) it will generate a makefile based on the project.json file.


project.json



So far its working as expected, but here comes the tricky point. In case user changes the project.json file and the makefile has already been generated it will not catch the latest changes from the project.json file, is there a way to solve it with make file? I need them to be synced...


project.json


makefile


project.json



update



lets this is my make file called gmake


gmake


include gmake
gmake: project.json
rtr init $< $@

DIR := $(shell rtr execute start)
all: app1 app2

.PHONY: app1
App1:
@echo “run app 1"

.PHONY: app2
App2:
@echo "run app2”

Done:
rtr clean $(DIR)





Not sure I fully understand the question, but it sounds like... either always run the CLI tool provided to regenerate the Makefile? Or it might actually also work to generate a rule into to that Makefile to have make do it.
– Ondrej K.
Jul 3 at 9:17





You are including the file named gmake in itself? Sorry, but this does not make sense with make. Create a file named Makefile. In this file (Makefile) add the rule that generates file gmake, plus the line include gmake. And, by the way, gmake is not a very good choice, there are systems where the GNU make utility is named gmake. What about Makefile.generated, instead?
– Renaud Pacalet
Jul 5 at 6:09


gmake


Makefile


Makefile


gmake


include gmake


gmake


gmake


Makefile.generated





@RaynD Just to be sure: are you sure that your rtr init command works the way you use it? I mean, if you type rtr init project.json gmake in the command line, it works as expected? The arguments are the correct ones and in the correct order?
– Renaud Pacalet
Jul 5 at 6:20


rtr init


rtr init project.json gmake




2 Answers
2



If you are using GNU make, you could simply include this generated makefile in a top makefile:


include Makefile.generated

Makefile.generated: project.json
json2makefile $< $@



make always tries to rebuild missing or out of date makefiles. And if it does it parses the makefiles again. From How Makefiles Are Remade of the GNU make documentation:



To this end, after reading in all makefiles, make will consider each
as a goal target and attempt to update it. If a makefile has a rule
which says how to update it (found either in that very makefile or in
another one) or if an implicit rule applies to it (see Using Implicit
Rules), it will be updated if necessary. After all makefiles have been
checked, if any have actually been changed, make starts with a clean
slate and reads all the makefiles over again. (It will also attempt to
update each of them over again, but normally this will not change them
again, since they are already up to date.)





thanks, can you please explain why its better/diff then the first answer ,( I really want to understand which approach to take ) :)
– Rayn D
Jul 3 at 14:01






@RaynD: Sure. Several reasons: 1) it's a built-in feature of make that has been designed exactly for this purpose. 2) One single make invocation instead of two, that is, better performance. 3) Simple, clean...
– Renaud Pacalet
Jul 3 at 17:14





Can you explain what is the host> cat Makefile include Makefile.generated , what is nost and why I need to cat makefile? thanks
– Rayn D
Jul 4 at 11:19


host> cat Makefile include Makefile.generated


nost


cat makefile





host> is the prompt of my shell. I use it to distinguish the commands I type in the terminal from the result they print. cat Makefile is the command that prints the content of file Makefile. I used it to show you the content of the Makefile I propose. But as this looks overcomplicated to you, let me modify my answer and remove this.
– Renaud Pacalet
Jul 4 at 12:22



host>


cat Makefile


Makefile


Makefile





Well , I know what is cat means but I was confused with the context :) ..., so in my makefile I should put only Makefile.generated: project.json json2makefile $< $@ and add it like all: Makefile.generated and this should "listen" to the projecet.json changes , is that correct ? if not can you please show sample of makefile with this functionality?
– Rayn D
Jul 4 at 16:12


cat


Makefile.generated: project.json json2makefile $< $@


all: Makefile.generated


projecet.json



You can operate with a top-level and a generated makefile. In the top-level makefile, you might have nothing but


makefile


all: makefile.gen
@$(MAKE) -f $<

.PHONY: all

makefile.gen: project.json
@yourCommand > $@



And the generated makefile (named makefile.gen here) is built whenever project.json changes. Make sure you change the last build rule to your needs such that that makefile.gen is generated by your command line tool.


makefile.gen


project.json


makefile.gen





Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Jul 4 at 1:43






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?