V
V
Vlad Zaitsev2017-10-19 03:35:32
linux
Vlad Zaitsev, 2017-10-19 03:35:32

Duplicate code for makefile purposes, how to avoid?

ubrige: 
 make -j5 TARGET=unwired BOARD=$(BOARD)/$(CPU) -f makefiles/Makefile.[email protected]
 ../bootloader/generate-metadata/generate-metadata [email protected].bin 0x0 0x01A00001 1
 srec_cat firmware-metadata.bin -binary [email protected].bin -binary -offset 0x100 -o [email protected]-ota-image.bin -binary
 rm [email protected].hex

ubrige-fb: ubrige-CLEAN
  ../../tools/backdoor-bootloader.py -e -w -v ubrige-firmware.hex

ubrige-f: ubrige-CLEAN
 /Applications/ti/Uniflash/flash_cc1310.sh ubrige-firmware.hex 

ubrige-ota: ubrige
 scp ubrige-ota-image.bin [email protected]:/root/unwired_smarthome

It is easy to see that the name of the target is often repeated there (in this case, ubrige). This is the name of the project. If I create a new project I have to copy these four targets and then replace ubrige with the new project name. It would be a joke with a replacement, but I don’t like that there is a bunch of the same code in the file.
For the first target, I found [email protected], no longer required to replace all project names in commands.
But it does not reduce the amount of code - you still need to copy several targets for each project. In the example above, the contents of ubrige, ubrige-fb, ubrige-f, ubrige-ota will be the same for all projects, only the ubrige name will differ.
How do I get rid of duplicating code that is the same for several targets, but differs in filenames (let the filenames be the same as the target name)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-10-19
@vvzvlad

How do I get rid of duplicating code that is the same for several targets, but differs in filenames (let the filenames be the same as the target name)?

You can write a macro target generator. Changing parameters can be passed as macro parameters, internally they can be referenced by number. For example like this:
define generate-targets
$(1): 
 make -j5 TARGET=unwired BOARD=$$(BOARD)/$$(CPU) -f makefiles/[email protected]
 ../bootloader/generate-metadata/generate-metadata [email protected] 0x0 0x01A00001 1
 srec_cat firmware-metadata.bin -binary [email protected] -binary -offset 0x100 -o [email protected] -binary
 rm [email protected]

$(1)-fb: $(1)-CLEAN
  ../../tools/backdoor-bootloader.py -e -w -v $(1)-firmware.hex

$(1)-f: $(1)-CLEAN
 /Applications/ti/Uniflash/flash_cc1310.sh $(1)-firmware.hex 

$(1)-ota: $(1)
 scp $(1)-ota-image.bin [email protected]:/root/unwired_smarthome

endef

Inside the macro, you need to escape $ by replacing it with $$.
This macro can be called at the desired location as follows:
The code generated at this location will be equivalent to the code in the question.

R
res2001, 2017-10-19
@res2001

Use variables:

PROJECT=ubridge

$(PROJECT)-fb: $(PROJECT)e-CLEAN
  ../../tools/backdoor-bootloader.py -e -w -v $(PROJECT)-firmware.hex

$(PROJECT)-f: $(PROJECT)-CLEAN
 /Applications/ti/Uniflash/flash_cc1310.sh $(PROJECT)-firmware.hex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question