Compiling

From NetHackWiki
Revision as of 23:22, 22 December 2015 by Elronnd (talk | contribs) (SLASH'EM: : capitalization)
Jump to navigation Jump to search

This page contains help on compiling NetHack from source. Compiling is necessary if you want to apply a patch, e.g. from the bilious database.

Decoding the Source Distribution

On Linux, Mac OS X or other Unixes, you will likely have a "tar" program and a "gzip" program. The source distribution was bundled by these two programs working together, forming a file format often called a "tarball." The current GNU version of tar knows about gzip and can decode the source distribution thus:

tar xvf nethack-343-src.tgz

Older GNU tars need a "z" (tar xzvf nethack-343-src.tgz) to tell them to use gzip. Non-GNU versions of tar usually do not know about gzip, and it must be run separately. If all else fails, do this:

gzip -cd nethack-343-src.tgz | tar xvf -

On Windows, tar and gzip are available if you have installed MinGW or Cygwin. Some might prefer a graphical interface, and 7-Zip provides this. Running 7-Zip once on the source distribution only undoes gzip, leaving an uncompressed tar file; but 7-Zip knows the tar format also, and double-clicking on the tar file will reveal its contents.

On Windows

This was tested on Win2k with MinGW-5.0.3 and WinXP with MinGW-5.1.1 and win7 with MinGW-5

  • Install MinGW. If you use the 'minimal' installation, remember to also select the optional 'MinGW make' -component, so it gets installed. Use, say, C:\mingw\ as the path to install to.
  • Unpack the NetHack source package somewhere, say, c:\nh343\. After unpacking, that directory should contain several subdirectories, such as src, include, win and sys. You will need WinZip or some other utility that can uncompress the package.
  • Go to c:\nh343\sys\winnt\ and run nhsetup.bat
  • If you want to run the graphical windows GUI, edit c:\nh343\src\makefile.gcc and change the line
#GRAPHICAL=Y

to

GRAPHICAL=Y

NOTE: The latest MinGW (5.1.3 as of writing) has issues compiling the GUI version of the program. To remedy this, copy rpcndr.h from c:\MinGW\include\ to c:\nh343\include\. Edit c:\nh343\include\rpcndr.h by removing or commenting out line 52: "typedef unsigned char boolean;". This should allow the compile to commence with minimal errors.

  • Go to c:\nh343\ and create a new file called 'install.bat' with the following contents:
PATH=%PATH%;c:\mingw\bin
cd src
mingw32-make -f makefile.gcc install
  • Run the install.bat you just created. This takes about thirty seconds.
  • The NetHack binary should now be in c:\nh343\binary\
  • If you want to patch your NetHack, you should next see the article on patching.

Cygwin

Useful instructions for compiling under Cygwin can be found at this external page. Instructions for compiling under Cygwin and MinGW/MSYS (so the game won't need Cygwin to run) can also be found here.

On Linux

TTY

The following compiles the TTY-version of NetHack, with all the default settings.

  • Make sure you have the development packages for ncurses installed. On debian these are called libncurses5-dev. You will also need make, gcc, yacc (or something similar, for example bison), and lex (or something similar, such as flex).
  • Open a terminal, and cd to the directory where you downloaded the source package to.
  • Unpack the NetHack sources and move to the NetHack source root directory:
tar xvzf nethack-343-src.tgz
cd nethack-3.4.3
  • Execute the script that creates symbolic links to the Makefiles in the correct places:
sh sys/unix/setup.sh x
  • Edit include/unixconf.h by changing
/* #define LINUX */

to

#define LINUX
  • Edit src/Makefile by commenting the following like so
#WINTTYLIB = -ltermlib

and uncommenting this line so it looks like

WINTTYLIB = -lncurses
  • If you're using something else than yacc and lex, edit util/Makefile so that YACC and LEX defines are correct.
  • If you are using cygwin, edit include/config.h and uncomment the following like so
#define STRNCMPI        /* compiler/library has the strncmpi function */
  • Typing make in the NetHack source root dir should compile the binary executable. Typing make all will compile everything, including special levels.
  • To install NetHack to /usr/games/, type make install while you're logged in as root. If you want to change the installation directory, you need to edit the top Makefile.
  • For more information about compiling NetHack, see the sys/unix/Install.unx text file.
  • If you want to patch your NetHack, you should next see the article on patching.

X11

The following compiles the X11 windowport with default settings. First follow the instructions in the TTY section, then continue this.

  • Make sure you have the X11 Athena Widget libraries and headers installed. On Debian the packages are libxaw, libxaw-dev and libxaw-headers.
  • Edit include/config.h by changing
/* #define X11_GRAPHICS */

to

#define X11_GRAPHICS

and

/* # define USE_XPM */

to

# define USE_XPM
  • Edit src/Makefile:
    • add $(WINX11SRC) to WINSRC
    • add $(WINX11OBJ) to WINOBJ
    • add $(WINX11LIB) to WINLIB
    • Change WINX11LIB so that it has -lXaw -lXmu -lXext -lXt -lXpm -lX11 -lm
    • At least on Debian, WINX11LIB also needs -L/usr/X11R6/lib
  • Edit Makefile so that VARDATND includes x11tiles NetHack.ad pet_mark.xbm rip.xpm
  • Type make in the NetHack source root dir.
  • To install NetHack to /usr/games/, type make install while you're logged in as root. If you want to change the installation directory, you need to edit the top Makefile.
  • For more information about compiling NetHack, see the sys/unix/Install.unx text file. For X11 compilation, see win/X11/Install.X11
  • If you want to patch your NetHack, you should next see the article on patching.
  • If you want to use tiles on X11, or change the window colors, see the article on X11 configuration.

On Mac OS X

These are the instructions for compiling a TTY port of NetHack with default settings on Mac OS X. You will need to have the Xcode developer tools installed to be able to compile NetHack (check the CDs that came with your copy of Mac OS X). These instructions assume a minimal working knowledge of C and Terminal.

This has worked on at least OSX 10.6.1, 10.6.8.

  1. Download NetHack's sources from http://nethack.org/v343/download-src.html and unpack them.
  2. Open Terminal and cd to the sys/unix directory in the source folder. Run sh setup.sh. New files named Makefile will now be present in the src and root folders. Return to the root folder.
  3. If you are installing "vanilla" Nethack, read about the problems described at this page on the Nethack wiki and install patch-ac. (Do not apply this patch on Snow Leopard, it is not needed and breaks the compilation.)
  4. Edit the file include/unixconf.h to make three changes (Do not do this on Snow Leopard):
    • Uncomment the BSD #define on line 22,
    • Comment out the #defines for SYSV on line 28, and
    • Comment out the #defines for SVR4 on line 35.
  5. In the file win/tty/termcap.c, change line 838 from #ifndef LINUX to #if !defined(LINUX) && !defined(__APPLE__)
  6. Make three changes to src/Makefile, which was created in step 2:
    • Uncomment line 233
    • Comment out line 234
    • On line 154, change CFLAGS = -O -I../include to CFLAGS = -O2 -I../include. (This prevents a fatal compilation error ( mkmaze.c:281: error: unable to find a register to spill in class 'Q_REGS') in mkmaze.c.)
  7. To prevent errors on startup and exit ("No such file or directory. Exec to compress save/501XXX failed"), edit include/config.h, line 172, change to specify the actual location of 'gzip' (find it using 'where gzip'. I don't know if it exists on stock OSX, maybe you have to install gzip first):
    • #define COMPRESS "/usr/bin/gzip"
  8. cd to the src directory and run make to compile the bulk of NetHack. This takes a minute or two. If you needed to install the pkgsrc patch at step 3, you will receive an error here about a conflicting definition of srandom.
  9. Edit the Makefile in the root source directory to fix the game permissions. Here is one solution:
    • Change 'games' on line 20 to 'root',
    • Change 'games' on line 21 to 'staff',
    • Change the value of GAMEPERM on line 25 to 0755
    • Change the value of FILEPERM on line 26 to 0664, and
    • Change the value of DIRPERM on line 28 to 0775.
  10. In Terminal, at the top source directory, run make all to create the auxiliary files, and then run sudo make install to install the game. This installs the executable to /usr/games/nethack, and your save/score/bones files are created in /usr/games/lib/nethackdir.
  11. To install the manpages, edit doc/Makefile by changing the path on line 44 to wherever you wish to install them (create the directory if necessary), and then run make install (possibly with sudo) from the doc directory.
  12. Different versions of Mac OS X may have different issues with file permission settings on various components of /usr/games/lib/nethackdir. The following works for Snow Leopard:
    • $ sudo chmod 775 /usr/games/lib/nethackdir
    • $ sudo chmod 775 /usr/games/lib/nethackdir/save/
    • $ sudo chmod 775 /usr/games/lib/nethackdir/perm
    • $ sudo chmod 775 /usr/games/lib/nethackdir/record
  13. Play! For example, open the terminal version by typing open src/nethack from the top source directory in Terminal. Different versions of Mac OS X may have different issues with file permission settings on various components of /usr/games/lib/nethackdir.


See also

On iPhone/iPod Touch

This is a tricky setup. The iPhone/iPod Touch (as of firmware 2.1), due to distribution terms, terminal sandboxing, and other odds and ends associated with a highly proprietary product, tends make things difficult.

So, we'll assume that you're using a jailbroken iPhone or iPod Touch for this procedure. We'll also assume that you're looking to play the game locally on the device on the go, as anyone can use the free terminal and telnet software to connect to NAO or equivalent.

We additionally assume that you're compiling for terminal mode; for tiles or native window manager support, far more work will be required in the future.

For compiling on the device itself, you will need:

  • MobileTerminal or OpenSSH
  • GCC, Make, and/or Toolchain 2.0
  • Bison
  • Flex

All of which may be found in Cydia's package repositories or, if you do not have Cydia, various places online.

Due to the rapid state of flux associated with commercial offerings, this list shall not be comprehensive. However, it WILL cover the highlights:

  • SCP or otherwise download the latest sources onto your device, and perform the necessary tar -xzf. We use /usr/games as a candidate location, and /usr/games/nethack-3.4.3 as our top level directory for the remaining steps.
  • Browse into the sys/unix directory and read the associated documentation, to get a good feel for what you'll be doing with your system.
  • (Optional) Alter settings in include/config.h such as compression options, wizard mode, etc. No changes need be made in here, though changing compression options will suppress a rather annoying warning.
  • Return to sys/unix. Open Makefile.top and alter PREFIX, GAME, GAMEUID, GAMEGRP, GAMEDIR, VARDIR, SHELLDIR, and anything else you feel is necessary.

For convenience, here are tested values:

PREFIX   = /usr/games
GAME     = nethack
# GAME     = nethack.prg
GAMEUID  = mobile
GAMEGRP  = wheel
...
GAMEDIR  = /usr/games/lib/$(GAME)dir
VARDIR  = $(GAMEDIR)
SHELLDIR = /bin
  • For the remaining makefiles in sys/unix, add CC = gcc or your compiler to the top of the file. In this example, CC = arm-apple-darwin9-gcc is used. Only Makefile.src and Makefile.utl should be required for this step.
Note: It is suggested, but not currently required, to also include items like:

CFLAGS = -O -I../include -I/var/include
LFLAGS = -L"/usr/lib" -F"/System/Library/Frameworks" -F"/System/Library/PrivateFrameworks" -bind_at_load

... by the maintainer of Toolchain 2.0. 

However, this does not appear to be necessary; nor does alteration of the code of main().
  • From the main directory, specific source edits are required for localization in this environment:
    • win/tty/termcap.c:839: Comment out: // extern char *tparm();
    • sys/unix/Makefile.src:234: Make sure WINTTYLIB is defined as -lncurses instead of -ltermlib or similar.
    • sys/unix/Makefile.utl:108: Set YACC and LEX to use bison and flex, respectively. Example lines for these changes are given within this file.
    • sys/unix/Makefile.doc:23: Set GUIDECMD = cat Guidebook.txt instead of the default.
Note: flex and bison may not work in the current build, stating "Bus error".

If this happens to you, it is advised to make the .c and .h files for this step
on a different system and copy them into /util and /include manually.
  • (2.x firmware) At several steps in the build process, you will need to run ldid to sign each binary file, as unsigned files will be automatically "Killed" by the system with no further information.

To do this, either alter the make files or perform the following incremental steps manually:

ldid -S makedefs  (in util)
ldid -S recover   (in util)
ldid -S dgn_comp  (in util)
ldid -S lev_comp  (in util)
ldid -S nethack   (in src or the final install location (we use /bin))

(ldid should be located in /usr/bin)
  • (Optional) Create your own .nethackrc in your system's user directory (usually /private/var/mobile) and enjoy! :)

Other steps may be required, but these are the major steps that result in a functional, working build of nethack. Of special note, color and lit_corridor does not work in certain local terminals, but otherwise, all Unix options appear to be supported.

Additionally, mobile and root seem to conflict if both users are used interchangeably, causing deaths by trickery. Only one user is advised (preferably the unprivileged mobile) with this current setup.

This test was performed with vanilla NetHack, though there should be no reason for this procedure to fail with many mods, patches, or slashem.

Getting color to work on iPhone/iPod Touch

Of special note is the fact that this build supports color, but this does not appear to enable color within MobileTerminal.app (vt100).

However, color support can be verified by ssh in xterm, or in other terminal software for the iPhone and iPod Touch.

Using Autotools

There's a patch for Vanilla NetHack that uses the GNU build system (aka Autotools).

It is known to have worked on Linux, Mac OSX, Fedora8, and FreeBSD 10.1 for building a TTY version and on Windows XP for building a TTY and Windows GUI version.

Notes

  • If using flex and 'make' fails with 'Broken Pipe', you'll need to downgrade your version of flex. Version 2.5.4a works with NetHack.
  • By default, the Guidebook isn't copied to the game's directory. You'll need to copy it manually.
  • If you want NetHack to reside inside chroot, see Chroot jail for instructions.

SLASH'EM

Linux

In general, in order to compile SLASH'EM on linux you need to make these changes.

  • Download the stuff you'll need.
sudo apt-get install libncurses5-dev bison flex checkinstall make
  • From with in the source directory. Run the setup script.
sh ./sys/unix/setup.sh
  • You'll need to further edit 4 files.
gedit ./include/config.h

comment out lines 230, 231 with "/*" at the start of the line, and with "*/" at the end.

replace line 234 with "# define COMPRESS "/bin/gzip" /* FSF gzip compression */"

replace line 235 with "# define COMPRESS_EXTENSION ".gz" /* normal gzip extension */"

gedit ./src/Makefile

Comment out line 340. This "WINTTYLIB = -ltermlib", should become this "#WINTTYLIB = -ltermlib".

uncomment line 339. This"#WINTTYLIB = -lncurses", should become this "WINTTYLIB = -lncurses".

gedit ./util/Makefile

uncomment line 108. comment out line 105

gedit ./win/tty/termcap.c

line 839, Replace the entire line with:

extern char *tparm(NCURSES_CONST char *, ...);
  • Compile and install
make ; sudo checkinstall
  • Easy uninstall with your regular package manager.

This page may need to be updated for the current version of NetHack.

It may contain text specific to NetHack 3.4.3. Information on this page may be out of date.

Editors: After reviewing this page and making necessary edits, please change the {{nethack-343}} tag to the current version's tag or {{noversion}} as appropriate.