0
votes

In a makefile I'm reading, there are many references to a variable, projdir, defined and exported in the shell script that calls make; there are many instances of $(projdir) in the makefile.

From the make manual at http://www.gnu.org/software/make/manual/make.html#Reference, there are only two types of variables: recursively expanded variables and simply expanded variables. It seems projdir is neither one.

Question 1: Since the makefile works, it must be true that makefiles can access shell variables defined in the parent shell environment. Why is this not documented (or perhaps I did not find the correct documentation)?

Question 2: Unrelated to question 1, I see in Section 6.2 the line all:;echo $(foo). Why is the semicolon ; needed here?

3
See gnu.org/software/make/manual/make.html#Environment: Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value.Barmar

3 Answers

2
votes

Question 1. Environment variables automatically become make variables. This is explained in section 6.10:

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value.

Question 2: Putting a semicolon after a rule name and optional prerequisites allows you to start the recipe on the same line. This is explained in section 4.2:

The first recipe line may appear on the line after the prerequisites, with a tab character, or may appear on the same line, with a semicolon.

They used this syntax in the example in 6.2 for brevity, so they could show the whole rule inline in the sentence; I think it's rarely used in practice.

1
votes

From the make manual:

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value.

As to your second question:

Why is the semicolon ; needed here?

Without the semicolon the right-hand side of : would specify the list of dependencies. The semicolon terminates that (empty) list.

1
votes

Variables have both a flavor and an origin.

Expansion type is a flavor.

make contains a flavor function which can be used to find the type of variable in question. It will return one of undefined, recursive or simple.

Variables also have an origin which is where their value has come from. What you are seeing here is one of those possible origins.

The origin function will tell you the origin of a variable.

There are seven possible origins: undefined, default, environment, environment override, file, command line, override and automatic.

So your projdir variable has a flavor of simple and an origin of environment.