You are not logged in.

#1 2015-02-11 14:33:43

xedchou
Member
Registered: 2014-02-27
Posts: 23

[Solved]How does makepkg program append env variables to makefile

Usually GNU make will just override the environment variables if you specify
them in Makefile itself or pass it as argument, however, when I use
PKGBUILD  and specify a variable in package() like this:   

package() {
    cd "$pkgname"
    CFLAGS+=' -fPIC'
    make all
}

and with a trival Makefile like this:

CFLAGS += '-pipe -g'

all:
        @echo $(CFLAGS)

I got the output like this:

-fPIC -pipe -g

The problem is when I defined the CFLAGS as env variable, it didn't work coz Makefile overrided it, so why Makefile didn't override the CFLAGS
that defined in PKGBUILD?

I guess it is not the problem of make, since the manual
said this would cause different users to get different results from the same makefile. This is against the whole purpose of most makefiles.

So how did makepkg program sucessfully append a variable without modefiying Makefile?

Last edited by xedchou (2015-02-12 02:31:47)

Offline

#2 2015-02-11 15:17:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,459
Website

Re: [Solved]How does makepkg program append env variables to makefile

What?  Why is that not the expected result?  You added -fPIC in the PKGBUILD and -pipe -g in the Makefile, and the output included both of those.

If you are wondering why the flags specified in makepkg.conf aren't there it's because you put that in the package function rather than the build function.  You should build in build().  EDIT: makepkg variables seem to be availble to package as well.  But is this what you are asking about?

Last edited by Trilby (2015-02-11 15:19:40)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2015-02-11 15:29:35

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Solved]How does makepkg program append env variables to makefile

xedchou wrote:

I guess it is not the problem of make, since the manual
said this would cause different users to get different results from the same makefile. This is against the whole purpose of most makefiles.

Re-read that page carefully.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2015-02-11 15:34:18

xedchou
Member
Registered: 2014-02-27
Posts: 23

Re: [Solved]How does makepkg program append env variables to makefile

Trilby wrote:

What?  Why is that not the expected result?  You added -fPIC in the PKGBUILD and -pipe -g in the Makefile, and the output included both of those.

If you are wondering why the flags specified in makepkg.conf aren't there it's because you put that in the package function rather than the build function.  You should build in build().  EDIT: makepkg variables seem to be availble to package as well.  But is this what you are asking about?

Do I always have the expressing problem?  The problem is when I specified CFLAGS as environment variables, it didn't work, becasue Makefile overrided it. Why Makefile didn't override the CFLAGS that I defined in PKGBUILD

Offline

#5 2015-02-11 16:00:53

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,459
Website

Re: [Solved]How does makepkg program append env variables to makefile

xedchou wrote:

The problem is when I specified CFLAGS as environment variables, it didn't work, becasue Makefile overrided it. Why Makefile didn't override the CFLAGS that I defined in PKGBUILD

You didn't show us either of these - and these even contradict each other.  You specified an environment variable for CFLAGS in the PKGBUILD, and clearly it was used as it is shown in the output you provided - the Makefile didn't override it.

The reason the Makefile didn't override the environment variable is two-fold: first, makefiles shouldn't override these flags.  Second, if there is a good reason that a Makefile must override these flags, the Makefile would need to set the variable, not append to it.  If you change your Makefile to the following, it would override the environment variable setting (but generally this should not be done):

CFLAGS = '-pipe -g'

EDIT: I just read the edit you made to your inital post.  So the problem is you are trying to set CFLAGS as an environment variable outside of the PKGBUILD?  If this is the problem, you should have shown this rather than something else unrelated.  Makepkg does not use variables inherited from the environment it is launched from - you must specify these in /etc/makepkg.conf (or you can specify them on the makepkg command line).

Last edited by Trilby (2015-02-11 16:04:44)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2015-02-12 02:15:43

xedchou
Member
Registered: 2014-02-27
Posts: 23

Re: [Solved]How does makepkg program append env variables to makefile

The reason the Makefile didn't override the environment variable is two-fold: first, makefiles shouldn't override these flags.  Second, if there is a good reason that a Makefile must override these flags, the Makefile would need to set the variable, not append to it.  If you change your Makefile to the following, it would override the environment variable setting (but generally this should not be done):

Appreciate you guys' patience. I always have the expressing problem.

I fixed it. It turned out I was stupid again, forgot to export the variables.

Last edited by xedchou (2015-02-12 02:24:46)

Offline

#7 2015-02-12 02:22:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,459
Website

Re: [Solved]How does makepkg program append env variables to makefile

Now thist has nothing to do with make or makepkg or PKGBUILDs.  Will you please describe what your actual problem is?  We've now been through three layers of X-Y problem.  In this brand new version that you've presented, the problem is simply that you didn't export the CFLAGS from this mystery script you will not share.  No processes will inherit shell variables that you have not exported - this is not unique to make.

$ cat Makefile

CFLAGS += '-g -pipe'

all:
	echo ${CFLAGS}

$ CFLAGS=something

$ make
echo '-g -pipe'
-g -pipe

$ export CFLAGS

$ make
echo something '-g -pipe'
something -g -pipe

----

xedchou wrote:

Appreciate you guys' patience. I always have the expressing problem.

I for one would find it much easier to remain patient with future questions if you just state what you want to happen rather than trying to take all these indirect approaches.  And please just spend more time asking and less time blaming - several of your posts seem to just conclude that the problem is in PKGBUILD, or Makefiles, or the make binary - none of those were the case.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2015-02-12 02:28:07

xedchou
Member
Registered: 2014-02-27
Posts: 23

Re: [Solved]How does makepkg program append env variables to makefile

Trilby wrote:

Now thist has nothing to do with make or makepkg or PKGBUILDs.  Will you please describe what your actual problem is?  We've now been through three layers of X-Y problem.  In this brand new version that you've presented, the problem is simply that you didn't export the CFLAGS from this mystery script you will not share.  No processes will inherit shell variables that you have not exported - this is not unique to make.

$ cat Makefile

CFLAGS += '-g -pipe'

all:
	echo ${CFLAGS}

$ CFLAGS=something

$ make
echo '-g -pipe'
-g -pipe

$ export CFLAGS

$ make
echo something '-g -pipe'
something -g -pipe

Yeah, I know that now, I dont have much experience with bash script, so the sub env will not inherits the variable if you didn't export it.
Thanks very much for your patience.

Offline

Board footer

Powered by FluxBB