Spherical forums

Creations => Programming => Topic started by: N E O on September 19, 2013, 02:15:41 pm

Title: Using byuu's nall/phoenix libraries on MinGW
Post by: N E O on September 19, 2013, 02:15:41 pm
nall and phoenix (http://byuu.org/programming/phoenix/) are abstraction libraries written in C++0x by byuu, the author of the bsnes & higan emulators. nall is meant to be an STL replacement, while phoenix is meant to be a GUI abstraction library that uses as native an API as possible. On Windows, phoenix will compile with Win32; OS X will use Cocoa; Linux will use GTK+2 or Qt4 depending on what the desktop environment is.

Compilation customarily uses GNU Make and is optimized for it. Simply write your Makefile similar to the following:

Code: [Select]

ifeq ($(platform),)
platform = unix
ifeq ($(shell uname -a),)
platform = win
else ifneq ($(findstring MINGW,$(shell uname -a)),)
platform = win
else ifneq ($(findstring Darwin,$(shell uname -a)),)
platform = osx
endif
endif

# platform
ifeq ($(platform),unix)
include Makefile.unix
else ifeq ($(platform),osx)
include Makefile.osx
else ifeq ($(platform),win)
include Makefile.win
else
unknown_platform: help;
endif

# You'll need to customize your own help label
help:;


...and you can put all the OS-specific stuff in Makefile.win, Makefile.osx, Makefile.unix, and any other Makefile.* you wish to write. Below is a sample Makefile.win that should work with compiling a nall/phoenix project on Windows using MinGW+MSYS:

Code: [Select]

# point these variables to the directory locations on your system relative to your project
PH  = ./phoenix
NALL= ./nall
OUT = ./out

# APP is the filename (without extension) of your app in question; there should be a file named $(APP).cpp in the project's folder
APP = trk
BIN = $(APP).exe

# standard helper files;
# in bigger projects the "ui" directory would contain the phoenix code to create the interfaces,
# the "formats" directory would likely contain the nall code to load/save file formats
CXXFILES_WIN = $(wildcard ui/*.cpp)
CXXFILES_BASE = $(wildcard *.cpp)
CXXFILES   = $(CXXFILES_BASE)
HEADERS_FMT = $(wildcard formats/*.hpp)
HEADERS_WIN = $(wildcard ui/*.hpp)
HEADERS_BASE = $(wildcard *.hpp)
HEADERS = $(HEADERS_FMT) $(HEADERS_WIN) $(HEADERS_BASE)

# any necessary libraries; include SDL here, for example
LIBS =

# any necessary flags; include SDL cflags here, for example
FLAGS =

# the required libraries to compile on Windows; you can remove -mconsole here with it if you don't plan to write to STDOUT
WIN32_LIBS = -mwindows -mconsole -lkernel32 -luser32 -lgdi32 -ladvapi32 -lcomctl32 -lcomdlg32 -lshell32 -lole32 -lshlwapi

# use top line if you have a phoenix GUI, the bottom line if you don't
OBJ      = $(DIR)/resource.o $(DIR)/phoenix.o $(addprefix $(DIR)/, $(CXXFILES:.cpp=.o))
# OBJ      = $(addprefix $(DIR)/, $(CXXFILES:.cpp=.o))

WINDRES  = windres
CXX      = g++
RM       = rm -f
DIR = $(OUT)

# force 32/64-bit compilation if you define ARCH on your `make` command line
ifeq ($(ARCH),32)
FORCE_ARCH = -m32
FORCE_WIND = --target=pe-i386
DIR = $(OUT)32
BIN = $(APP)-32.exe
else ifeq ($(ARCH),64)
FORCE_ARCH = -m64
FORCE_WIND = --target=pe-x86-64
DIR = $(OUT)64
BIN = $(APP)-64.exe
endif

CXXINCS  = -I"$(NALL)" -I"$(PH)" -I.
CXXFLAGS = $(FORCE_ARCH) $(CXXINCS) -std=gnu++0x -DPHOENIX_WINDOWS -g $(FLAGS)

.PHONY: all clean

all: mkout $(DIR)/$(BIN)

clean:
$(shell if [ -d "$(DIR)" ]; then $(RM) -r "$(DIR)"; fi)
$(RM) *.o

mkout:
$(shell if [ ! -d "$(DIR)" ]; then mkdir "$(DIR)"; fi)

# build phoenix; comment out if not using phoenix
# resource.rc is a standard Windows-format resource file and app.ico in "res" directory is the app icon
$(DIR)/resource.o: resource.rc res/app.ico
$(WINDRES) $(FORCE_WIND) $< $@
$(DIR)/phoenix.o: $(PH)/phoenix.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<

# build app
$(DIR)/%.o: %.cpp $(HEADERS) $(CXXFILES_WIN)
$(CXX) $(CXXFLAGS) -c -o $@ $< $(LIBS)
$(DIR)/%.o: %.cc
$(CXX) $(CXXFLAGS) -c -o $@ $< $(LIBS)
$(DIR)/$(BIN): $(OBJ) $(wildcard res/*.png)
$(CXX) $(FORCE_ARCH) -s -o $@ $(OBJ) $(LIBS) $(WIN32_LIBS)


byuu himself provides most of his projects in source form (http://byuu.org/programming/) and the latest ones (higan v093, beat v03, and mosaic v07 at the time of this writing) should have the latest versions of nall and/or phoenix included. If you still need help, view the source and makefiles included in those projects.
Title: Re: Using byuu's nall/phoenix libraries on MinGW
Post by: Flying Jester on September 19, 2013, 02:40:33 pm
Very awesome! I will have to try this out!
Title: Re: Using byuu's nall/phoenix libraries on MinGW
Post by: N E O on September 19, 2013, 02:52:02 pm
Eventually I'll split out lines common to all the makefiles (like the phoenix and nall dirs, the CXXFILES vars, etc) into a Makefile.common and update the first post.
Title: Re: Using byuu's nall/phoenix libraries on MinGW
Post by: Flying Jester on September 19, 2013, 05:03:12 pm
Thanks Neo, I think I can actually use Phoenix now--I at least got it to compile after hacking up a unix makefile based on what you posted. The GTK build for phoenix seems to assume an awful lot about my path, I had to add a lot of include directories to the flags, but it actually compiled.

I wish there was an actual tutorial or example still around for using phoenix, but I guess I'll just study the source higan to get an idea of how it works.
Title: Re: Using byuu's nall/phoenix libraries on MinGW
Post by: N E O on September 19, 2013, 05:14:53 pm
The mosaic source might be easier to follow along as it's already ready for Linux and assumes the separate nall and phoenix makefiles take care of the platform abstraction. I may eventually change my own makefiles accordingly as well.