aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile146
-rwxr-xr-xconfigure190
2 files changed, 336 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7479f46
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,146 @@
+# vim:ts=4:sw=4:noexpandtab
+MAKEFLAGS += --no-builtin-rules
+
+# Load configuration
+-include $(O)/.config.mk
+
+# Default output path. Can be changed by enviroment to compile to different folder
+# than default.
+O ?= .
+# This variable can be overwritten to show executed commands
+Q = @
+
+# TODO add CROSS_COMPILE support
+BISON ?= bison
+CC ?= gcc
+# TODO also other tools
+
+
+.PHONY: all
+all: $(O)/sgp
+
+# TODO modules
+
+ifeq ($(DEBUG),yes)
+CFLAGS += -ggdb -DDEBUG
+endif
+CFLAGS += -Wall
+CFLAGS += -Iinclude -include $(O)/build/config.h
+
+### Source files list ###########################
+SRC = sgp.c \
+ utils.c \
+ parser.c \
+ io.c \
+ command.c
+### End of source files list ####################
+
+CSRC = $(patsubst %,src/%,$(filter %.c,$(SRC)))
+YSRC = $(patsubst %,src/%,$(filter %.y,$(SRC)))
+CYSRC = $(patsubst %,%.c,$(YSRC))
+HYSRC = $(patsubst %,%.h,$(YSRC))
+CSRC += $(CYSRC)
+GPERFSRC = $(patsubst %,src/%,$(filter %.gperf,$(SRC)))
+HGPERFSRC = $(patsubst %,%.h,$(GPERFSRC))
+
+OBJ = $(patsubst src/%.c,$(O)/build/%.o,$(CSRC))
+DEP = $(patsubst src/%.c,$(O)/build/%.d,$(CSRC))
+
+.PHONY: help
+help:
+ @echo "Simple general preprocessor make targets:"
+ @echo " all|sgp - Build sgp executable"
+ @echo " help - Prints this text help."
+ @echo " install - Install sgp to you system"
+ @echo " uninstall - Revert install target actions on your system"
+ @echo " clean - Cleans builded files"
+ @echo " distclean - Same as clean but also removes distributed generated files"
+ @echo " docs - Build html documentation"
+ @echo " serve-docs - Start html server with documentation on localhost:4000"
+ @echo " clean-docs - Removes generated documentation"
+ @echo "Some enviroment variables to be defined:"
+ @echo " CROSS_COMPILE - Specifies installation prefix. Default is /usr/local"
+ @echo " Q - Define emty to show executed commands"
+
+.PHONY: install
+install:
+ #TODO
+
+.PHONY: uninstall
+uninstall:
+ #TODO
+
+# Cleaning
+.PHONY: clean
+clean::
+ @echo " CLEAN build"
+ $(Q)$(RM) -r $(O)/build
+ @echo " CLEAN sgp"
+ $(Q)$(RM) $(O)/sgp
+.PHONY: distclean
+distclean:: clean
+ @echo " CLEAN distributed"
+ $(Q)$(RM) $(CYSRC)
+ $(Q)$(RM) $(HYSRC)
+ $(Q)$(RM) $(HGPERFSRC)
+ @echo " CLEAN configuration"
+ $(Q)$(RM) $(O)/.config
+
+.PHONY: docs
+docs:
+ @echo " DOC $@"
+ $(Q)mkdocs build
+
+.PHONY: serve-docs
+serve-docs:
+ $(Q)mkdocs serve
+
+.PHONY: clean-docs
+clean-docs:
+ @echo " CLEAN docs"
+ $(Q)$(RM) -r site
+
+ifeq (,$(filter clean distclean help docs serve-docs clean-docs \
+ ,$(MAKECMDGOALS))) # Ignore build targets if goal is not building
+ifeq ($(DEBUG),yes)
+-include $(DEP) # If developing, use dependencies from source files
+.PHONY: dependency dep
+dependency dep:: $(DEP)
+$(DEP): $(O)/build/%.d: src/%.c
+ @mkdir -p "$(@D)"
+ @echo " DEP $@"
+ $(Q)$(CC) -MM -MG -MT '$*.o $@' $(CFLAGS) $< -MF $@
+endif # DEBUG
+
+$(O)/sgp: $(OBJ)
+ @echo " LD $@"
+ $(Q)$(CC) $(CFLAGS) $^ -o $@
+
+$(OBJ): $(O)/build/%.o: src/%.c $(O)/build/config.h
+ @mkdir -p "$(@D)"
+ @echo " CC $@"
+ $(Q)$(CC) -c $(CFLAGS) $< -o $@
+
+$(O)/build/config.h: $(O)/.config
+ @mkdir -p "$(@D)"
+ @echo " CONF $@"
+ $(Q)./configure --op-h > $@
+
+$(HGPERFSRC): %.y.h: %.y.c
+ @
+$(CYSRC): %.y.c: %.y
+ @echo " BISON $@"
+ $(Q)$(BISON) -d -o $@ $<
+
+$(HGPERFSRC): %.h: %
+ @echo " GPERF $@"
+ $(Q)$(GPERF) --output-file=$@ $<
+endif
+
+# Configuation files
+$(O)/.config:
+ $(error Please run configure script first)
+
+$(O)/.config.mk: $(O)/.config
+ @echo " CONF $@"
+ $(Q)./configure --op-makefile > $@
diff --git a/configure b/configure
new file mode 100755
index 0000000..1cf35d3
--- /dev/null
+++ b/configure
@@ -0,0 +1,190 @@
+#!/bin/sh
+# vim:ft=sh:ts=4:sw=4:expandtab
+
+# Configured variables #####################################################
+# Described in following format:
+# name:initial value:type:if exported to makefile:if exported to C header
+# Where type can be just a string (in C in "") or plain. None implies plain.
+CNFS="SGP_VERSION:0.1:string:F:T
+SGP_CONTACT:cynerd@email.cz:string:F:T
+PREFIX:/usr/local:string:T:F
+MOD_LUA:true::T:F
+MOD_PYTHON:true::T:F
+DEBUG:false::T:F
+CFLAGS:::T:F
+LFLAGS:::T:F"
+############################################################################
+
+print_help() {
+ echo "Usage: ./configure [OPTION]..."
+ echo "SGP configuration script. Execute this script to configure project"
+ echo "and prepare it for building."
+ echo
+ echo " --help, -h Give this help list"
+ echo " --prefix=PATH Set PATH as installation prefix."
+ echo " In default /usr/local"
+ echo " --debug, -d Enable development features."
+ echo " --release, -r Disable development features for project."
+ echo " --dep-check Force depencency check for this machine."
+ echo
+ echo " --mod-lua-enable, --mod-lua-disable"
+ echo " Set if Lua module should be compiled. In default is enabled."
+ echo " --mod-python-enable, --mod-python-disable"
+ echo " Set if Python module should be compiled. In default is enabled."
+ echo
+ echo "Environment variables:"
+ echo " CROSS_COMPILE Prefix for compilation tools."
+ echo
+ echo "SGP version $SGP_VERSION"
+ echo "Report bugs to <$SGP_CONTACT>."
+}
+
+CONFIG_FILE=.config
+CONFIGURED_FILE=.configured # TODO
+
+# TODO support for values with escaped colons
+# Load default configuration
+eval `echo "$CNFS" | grep -o -E '^[^:]*:[^:]*' | sed 's/:/=/'`
+
+# Load existing configuration
+if [ -f "$CONFIG_FILE" ]; then
+ source ./"$CONFIG_FILE"
+fi
+
+# Requested operation.
+# c - Configure, default configuration behavior
+# m - Prints output for Makefile
+# h - Prints C header file (machine validation is skipped)
+OP=c
+
+# Parse arguments
+while [ "$#" -gt 0 ]; do
+ case $1 in
+ -h|-?|--help)
+ print_help
+ exit 0
+ ;;
+ --prefix=*)
+ # TODO
+ ;;
+ -d|--debug)
+ DEBUG=True
+ ;;
+ -r|--release)
+ DEBUG=False
+ ;;
+ --mod-lua-enable)
+ MOD_LUA=true
+ ;;
+ --mod-lua-disable)
+ MOD_LUA=false
+ ;;
+ --mod-python-enable)
+ MOD_PYTHON=true
+ ;;
+ --mod-python-disable)
+ MOD_PYTHON=false
+ ;;
+ --op-makefile)
+ OP=m
+ ;;
+ --op-h)
+ OP=h
+ ;;
+ *)
+ echo Unknown option $1 1>&2
+ exit 2
+ ;;
+ esac
+ shift
+done
+
+# Basically save configuration to file
+configure() {
+ echo "# SGP configuration file" > "$CONFIG_FILE"
+ echo "# Please do not edit this file directly." >> "$CONFIG_FILE"
+ echo "$CNFS" | while read L; do
+ NAME=`echo "$L" | grep -o -E '^[^:]*'`
+ eval "VALUE=\$$NAME"
+ echo "$NAME=$VALUE" >> "$CONFIG_FILE"
+ done
+ echo "Configuration written to \"$CONFIG_FILE\""
+}
+
+# Generate Makefile and configure them if they doesn't exists in PWD
+doext() {
+ if [ -f "Makefile" ] || [ -f configure ]; then
+ return
+ fi
+ SGPDIR=`dirname "$0"`
+
+ echo "# This is external Makefile for SGP." > Makefile
+ echo "export CROSS_COMPILE=$CROSS_COMPILE" >> Makefile
+ echo >> Makefile
+ echo "SGP_PATH = $SGPDIR" >> Makefile
+ echo >> Makefile
+ echo "MAKEARGS := -C \"\$(SGP_PATH)\"" >> Makefile
+ echo "MAKEARGS += O=\"\$(shell pwd)\"" >> Makefile
+ echo >> Makefile
+ echo "MAKEFLAGS += --no-print-directory" >> Makefile
+ echo >> Makefile
+ echo "Q = @" >> Makefile
+ echo ".PHONY: all \$(MAKECMDGOALS)" >> Makefile
+ echo "all \$(MAKECMDGOALS):" >> Makefile
+ echo " \$(Q)\$(MAKE) \$(MAKEARGS) \$@" >> Makefile
+
+ echo Created local Makefile
+
+ echo "# This is external configure script for SGP." > configure
+ echo "SGP_PATH=$SGPDIR" >> configure
+ echo "\$SGP_PATH/configure \$@" >> configure
+ chmod +x configure
+
+ echo Created local configure script
+}
+
+makefile() {
+ echo "$CNFS" | while read L; do
+ if [ `echo $L | awk -F ':' '{ print $4 }'` != "T" ]; then
+ continue
+ fi
+ NAME=`echo "$L" | grep -o -E '^[^:]*'`
+ eval "VALUE=\$$NAME"
+ echo "$NAME = $VALUE"
+ done
+}
+
+cheader() {
+ echo "$CNFS" | while read L; do
+ if [ `echo $L | awk -F ':' '{ print $5 }'` != "T" ]; then
+ continue
+ fi
+ NAME=`echo "$L" | grep -o -E '^[^:]*'`
+ eval "VALUE=\$$NAME"
+ if [ "`echo $L | awk -F ':' '{ print $3 }'`" = "string" ]; then
+ echo "#define $NAME \"$VALUE\""
+ else
+ echo "#define $NAME $VALUE"
+ fi
+ done
+}
+
+validate() {
+ echo -n
+ # TODO check dependencies and programs
+}
+
+case $OP in
+ c)
+ validate
+ doext
+ configure
+ ;;
+ m)
+ validate
+ makefile
+ ;;
+ h)
+ cheader
+ ;;
+esac