linuxmakefileinstallationhome-directory

How do i get the user name in a Makefile?


I'm making a simple game for Ubuntu and to update the highscore list, it needs a single file at runtime, called 'highscores.bin'.

I wish to put this file at

/home/(USER)/.game_name

I've researched a little and found that from inside a Makefile i can get the environment variable $USER. So in the Makefile, at the 'install' target, i've added:

mkdir -p $(DESTDIR)home/$$USER/.game_name

But when i run 'sudo make install', the Makefile installs it as:

/home/root/.game_name

How can i get the (non-root) user name in a Makefile?

P.S.: I'm writing the Makefile by hand. No ./configure

P.S.2: I dont want to do

mkdir -p ~/.game_name

because i want to be able to change DESTDIR if i want to install to a temporary directory.


Solution

  • Well, if you want that file during runtime I would recommend that you create it during runtime. The Makefile is considered only during compile time. This is obviously a problem if the program is compiled and then executed by different users, some of which may not even exist at compile time.

    During runtime you can get the home directory as shown here (if you are using C/C++). Then you can check for existance of the highscore folder/file and create it if you need to. If you want a highscore file for all users, you should put it in the home directory of a special user, but rather somewhere else in the file system. This is for example done by the game xjump.

    And about your Makefile, the $USER variable is translated to root because of the sudo command on

    sudo make install

    The user actually running make is the superuser: root.