spiderface
spiderface

Reputation: 1135

Why does indenting if-else with tabs break a Makefile?

Here's a Makefile:

.PHONY: all

ifeq ($(OS),Windows_NT)
    DETECTED_OS := Windows

else
    DETECTED_OS := $(shell uname -s)

endif

$(info DETECTED_OS is set to '$(DETECTED_OS)')

When indented with tabs it prints

DETECTED_OS is set to ''

But when indented with 0 or more spaces it prints

DETECTED_OS is set to 'Linux'

But if you delete the first line .PHONY: all it prints

DETECTED_OS is set to 'Linux'

regardless of tabs or spaces.

So the first version is broken because it sets DETECTED_OS to nothing. Why is that?

My make version:

GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Upvotes: 2

Views: 1311

Answers (1)

MadScientist
MadScientist

Reputation: 100946

When you indent a line with a TAB, make assumes it's part of a recipe for the previous target if there is one. In this case, there's the .PHONY target, so make is assuming that these two lines are part of the recipe for the .PHONY target (which is never used). Because of that, these make variable assignments are not run.

The simple rule for makefiles is, always indent recipe lines with TAB, and never indent any other lines with TAB.

Upvotes: 7

Related Questions