0

Im having a problem with a Makefile Im trying to create. I just want to create one by one the .elf file then the dump and lastly the bin file and then with a python script convert it to .hex file. My goal is to actually create the .elf and .hex files with just using the make command.

Despite that because Im very new to Makefiles, Im getting an error after executing the make command:

make
test
make: *** [copy.hex] Error 1

Thank you in advance!

XLEN ?= 32
RISCV_PREFIX ?= riscv$(XLEN)-unknown-elf-
RISCV_GCC ?= $(RISCV_PREFIX)gcc
CFLAGS = -O2
./configure --prefix=/home/opt/Risc_V/my_tests --with-arch=rv32if --with-abi=ilp32d
SCRIPTDIR=/home/opt/Risc_V/tools/
RISCV_TEST_DIR=/home/opt/Risc_V/my_tests/my_software

PROGRAMS ?= copy.c
ALL_HEX = $(PROGRAMS:%.c=%.hex)
PWD := $(shell pwd)

all: $(ALL_HEX) $(PROGRAMS)

RISCV_OPTIONS ?= -o $(PROGRAMS).elf $(CFLAGS) 
RISCV_LINK ?= $(RISCV_GCC) $(PROGRAMS) $(RISCV_OPTIONS)#produces .elf file!
RISCV_OBJDUMP ?= $(RISCV_PREFIX)objdump -D $(PROGRAMS).elf > $(PROGRAMS).dump#produces a dump file to see the assembly code!
RISCV_OBJCOPY ?= $(RISCV_PREFIX)objcopy -O binary $(PROGRAMS).elf $(PROGRAMS).bin#produces a bin file!



test: all

%.elf: %.c
    $(RISCV_LINK)
%.bin: %.elf
    $(RISCV_OBJDUMP)
%.dump: %.elf
    $(RISV_OBJCOPY)
%.hex: %.bin $(SCRIPTDIR)/bin2hex.py
    test

test: $(SCRIPTDIR)/bin2hex.py
    python $(SCRIPTDIR)/bin2hex.py $(PROGRAMS).bin -a 0x0 > $(PROGRAMS).hex || exit -1

clean:
    rm -rf *.elf *.hex *.map *.objdump *.i *.s *.bin *.dump
John Liko
  • 17
  • 5
  • 1
    This Makefile does not seem to have 30 lines, are you sure this is exact and complete file? – raspy May 21 '20 at 11:32
  • Looks like double spaces to me. Is your editor set to insert double space instead of tabs? – Andreas May 21 '20 at 12:14
  • @raspy Hello! The other lines of the files are not included because they are mostly paths etc. – John Liko May 21 '20 at 12:19
  • @Andreas I pressed tab to start the line. I will check my editor right away! – John Liko May 21 '20 at 12:19
  • @Andreas Hi again: yes it was the editors problem. Now I get another error saying: *** No rule to make target `copy.hex', needed by `all'. Stop. – John Liko May 21 '20 at 12:22
  • Well, you don't have a rule telling make how to build `copy.hex` so that error makes perfect sense. – MadScientist May 21 '20 at 12:25
  • @JohnLiko Classic. When writing Makefiles I make sure to use an editor displaying whitespace and non-printables. Most often Notepad++. Leading tabs/spaces is not the only way to break Make. – Andreas May 21 '20 at 12:28
  • If you're tired of fighting with TAB vs. space and you are sure you'll always be using GNU make 3.82 (released in 2010) or higher, you can investigate using the .RECIPEPREFIX feature to pick a different prefix character than TAB. – MadScientist May 21 '20 at 12:42
  • @MadScientist Hello again. I added hex rule and now Im getting an error as shown in the new file I uploaded which doenst provide any information on how to fix it. – John Liko May 21 '20 at 13:22
  • That message means `make` ran the `test` program, and that program didn't print any output and it exited with an exit code of `1`. Make considers any program that it invokes that doesn't exit with exit code `0` (success) to have failed, and once a recipe has a failed command make will stop processing the makefile. – MadScientist May 21 '20 at 13:24

1 Answers1

0

How do you know you "used tabs on each rule"? It's not enough to press the TAB key on your keyboard: you have to be sure that your editor actually inserts a TAB character when you press the TAB key, and that when your editor writes out your file it preserves the TAB character in the written file (some editors will "helpfully" reset whitespace when saving a file).

However, it's quite possible that this is not the problem because, as noted above, you haven't actually shown us the full makefile and we don't know what is on line 30 of the makefile which is where the error happens. It could be some completely different syntax error. It doesn't make sense to ask a question on SO and include part of the file, but elide the actual lines where the error occurs. You say "they are mostly paths etc.", but clearly make thinks there's something wrong with them, so you have to show them to us or we can't help you.

I will say there are various other errors in your makefile (but these are not the cause of your problem you quote here):

%.elf: %.c $(ALL_HEX)
        $$(RISCV_LINK)
%.bin: %.elf
        $$(RISCV_OBJDUMP)
%.dump: %.elf
        $$(RISV_OBJCOPY)

These should not be using $$. You want to expand these variables, they should just be $(RISCV_LINK), $(RISCV_OBJDUMP), and $(RISCV_OBJCOPY) respectively.

test: $SCRIPTDIR/bin2hex.py

You are missing the parens here: this should be $(SCRIPTDIR). What you have is equivalent to this: $(S)CRIPTDIR which is not what you want.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Hey @MadScientist. Thank you for the reply. I will fix what you mentioned. Im quite new to the Makefiles and still practising. Also as said to Andreas it was an editor problem. I fixed it. – John Liko May 21 '20 at 12:32
  • There is no good way to include a TAB character in SO code. Either it contains an actual TAB character and looks really wrong because there's not enough indentation, or you make it look right by choosing spaces instead of TABs. I choose the latter, so that when people look at the example it looks like what they should see on their screen. – MadScientist May 21 '20 at 12:40