up vote 2 down vote favorite
share [fb]

I have problems with using pkg-config in a Makefile, while running in sudo mode. I need to have PKG_CONFIG_PATH set for pkg-config to find geany.pc file that is held in /usr/local/lib/pkgconfig.

If I run:

$ echo $PKG_CONFIG_PATH
/usr/local/lib/pkgconfig

If I run with sudo:

$ sudo echo $PKG_CONFIG_PATH
/usr/local/lib/pkgconfig

So I try running my Makefile that looks like this:

PLUGIN_DIR=`pkg-config --variable=libdir geany`/geany

all: pyflakes.o
    gcc pyflakes.o -o geanypyflakes.so -shared `pkg-config --libs geany`

pyflakes.o: pyflakes.c
    gcc -Wall -c pyflakes.c -fPIC `pkg-config --cflags geany` 

install:
    echo ${PKG_CONFIG_PATH}
    cp geanypyflakes.so ${PLUGIN_DIR} --verbose

clean:
    rm geanypyflakes.so pyflakes.o


sudo make install

and I get following ouput:

echo 

cp geanypyflakes.so `pkg-config --variable=libdir geany`/geany --verbose
`geanypyflakes.so' -> `/geany'

Why suddenly PKG_CONFIG_PATH is not set? I can run sudo -E and then it works, but I would like to understand why it suddenly is not working.

EDIT

Running export before didn't help:

$ export PKG_CONFIG_PATH='/usr/local/lib/pkconfig' && sudo make install
echo 

cp geanypyflakes.so `pkg-config --variable=libdir geany`/geany --verbose
`geanypyflakes.so' -> `/geany'
link|improve this question

may be you could try this export PKG_CONFIG_PATH='/usr/local/lib/pkgconfig' && sudo make install – greenmang0 Sep 2 at 13:04
It didn't help. Please see my edit. – gruszczy Sep 2 at 13:10
feedback

2 Answers

up vote 3 down vote accepted

The

sudo echo $PKG_CONFIG_PATH

command is not doing what you expect. The shell variable is expanded on the commandline before sudo is run. Try using single quotes around $PKG_CONFIG_PATH to stop the expansion on the sudo commandline and you will stop the shell expansion before sudo runs

richm@royalcounty:~$ export PKG_CONFIG_PATH=hello
richm@royalcounty:~$ sudo echo $PKG_CONFIG_PATH
hello
richm@royalcounty:~$ sudo echo '$PKG_CONFIG_PATH'
$PKG_CONFIG_PATH

In actual fact your original command is wrong because the echo inside sudo will do no variable expansion. You can force that by running the shell explicitly

richm@royalcounty:~$ sudo -E echo '$PKG_CONFIG_PATH'
$PKG_CONFIG_PATH
richm@royalcounty:~$ sudo sh -c 'echo $PKG_CONFIG_PATH'

richm@royalcounty:~$ sudo -E sh -c 'echo $PKG_CONFIG_PATH'
hello

That demonstrates why your sudo make install does not work without the -E argument to sudo

link|improve this answer
The short version of this is "use sudo -E make install instead". – bahamat Sep 2 at 15:47
I understand now. Thanks a lot. – gruszczy Sep 2 at 16:38
feedback

To answer the question of why make install environment variables are not passed...

By default sudo cleans out the environment before it executes your command. This is a security feature to stop bad guys influencing the operation of commands that are allowed via sudo.

You can specify the -E flag to preserve the environment but you might need to tweak the contents of your sudoers file to give yourself permission.

From man sudoers

   -E          The -E (preserve environment) option will override the
               env_reset option in sudoers(5)).  It is only available when
               either the matching command has the SETENV tag or the
               setenv option is set in sudoers(5).
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.