Search code examples
makefilestm32

I'm trying to port a make file that was used in linux to be able to work in a windows, confused about some errors


At some point in the make I'm doing this:

@copy $(OUTPUT_HEX) $(BUILD_DIR)$(OUTPUT_NAME)_$(VARIANT_MODE)_new.hex

As this wasn't working I tried the following command from the same directory:

copy /Y build\debug\0821_0822_CON_debug.hex build\debug\hello_new.hex

This command is working on cmd, no problem. When I try the same exactly command in the makefile I've got an error saying that the system cannot find the specific route.

So my guess was that for an unknown reason the build is being created from another route, so before running the command I do this:

@cd C:/Users/john/git/0821_0822/01_Control/app
@copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex

Same directory I'm using the copy command in cmd, and it's working there, I don't understand why it's not working.

I've tried some

@echo $(CURDIR)

Before & after doing the cd, and the directory seems correct, but cd actually doesn't seem to be working, as the diretory is still the same if I change it to another.

I'm really confused about this.

By the way, I'm doing this in ST32CubeIDE, no alternative from what I have to do.

Thanks!


Solution

  • To start, pmacfarlane is 100% right. Using @, certainly before your makefile is working 100% correctly, is a bad idea. You wouldn't try to debug your program by first redirecting all its output to /dev/null: this is the equivalent.

    The first problem is that this doesn't do what you think it does:

    @cd C:/Users/john/git/0821_0822/01_Control/app
    @copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex
    

    Every logical line in a makefile recipe is run in a different shell which means any changes to the local shell environment (such as setting environment variables, or changing the working directory) are lost whenever the command exits. So the first command here is a no-op: it starts a shell, changes the directory, exits the shell whereupon the directory change is gone.

    Second, writing makefiles in Windows is very tricky because there are so many different possible ways to do things. Make invokes a shell, but what shell does it invoke? Is it invoking cmd.exe? Is it invoking Powershell? Is it invoking a POSIX shell you installed (say as part of Git for Windows)?

    That matters because unlike on UNIX systems, many of the programs you run in a Windows shell are built-in to the shell; that includes copy for example. That means that you can't run them as stand-alone programs if you start a different shell.

    So it looks to me like your version of make is finding UNIX shell sh.exe on your PATH, and so it's using that as the shell to invoke commands, and of course that won't allow copy to work.

    The simplest way around this is to explicitly set the shell you want to run. For example you can add this to your makefile:

    SHELL := cmd.exe