~aritra1911/mc_server

7cd0a0d363f1a8844d8cc5f4b593ac539fa02ec6 — Aritra Sarkar 1 year, 11 months ago a6661f5
Apply Nico's makefile-template
5 files changed, 338 insertions(+), 19 deletions(-)

M .gitignore
M Makefile
A Makefile.gmake
A autodetect.sh
A default.mk
M .gitignore => .gitignore +3 -1
@@ 1,4 1,6 @@
server
*.o
*.d
*.swp
*.core
server
config.mk

M Makefile => Makefile +16 -18
@@ 1,22 1,20 @@
LIBS = -pthread
# See https://gitlab.com/herrhotzenplotz/makefile-template

CFLAGS=	-Wall -Wextra -pedantic -Werror-implicit-function-declaration \
	-g -O0 -std=c99
CPPFLAGS = -D_XOPEN_SOURCE=600
LDFLAGS = $(LIBS)
PROGS				=	server

SRCS = main.c session.c response.c tpool.c events.c helpers.c
OBJS = $(SRCS:.c=.o)
CFLAGS				=	-std=iso9899:1999 -pedantic -pedantic-errors -Wall -Wextra \
					-Werror-implicit-function-declaration -Wno-reserved-id-macro \
					-Wno-missing-prototypes -g -O0 -fno-fast-math -fno-builtin
CFLAGS_x86_64-linux-gcc		=	-m64
CFLAGS_i686-linux-gcc		=	-m32
CFLAGS_x86_64-linux-clang	=	-Weverything -m64
CFLAGS_i686-linux-clang		=	-Weverything -m32
CFLAGS_amd64-freebsd-clang	=	-Weverything -m64
CFLAGS_i686-freebsd-clang	=	-Weverything -m32
CPPFLAGS			=	-D_XOPEN_SOURCE=600 -D_POSIX_PTHREAD_SEMANTICS \
					-D_LARGEFILE64_SOURCE
LDFLAGS				=	-pthread

.PHONY: all clean
server_SRCS			=	main.c session.c response.c tpool.c events.c helpers.c

all: server

server: $(OBJS)
	$(CC) $(LDFLAGS) -o server $(OBJS)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

clean:
	rm -f $(OBJS) server
include default.mk

A Makefile.gmake => Makefile.gmake +22 -0
@@ 0,0 1,22 @@
CFLAGS		=	-std=iso9899:1999 -pedantic -pedantic-errors -Wall -Wextra \
			-Werror-implicit-function-declaration -Wno-reserved-id-macro \
			-Wno-missing-prototypes -g -O0 -fno-fast-math -fno-builtin
CPPFLAGS	=	-D_XOPEN_SOURCE=600 -D_POSIX_PTHREAD_SEMANTICS \
			-D_LARGEFILE64_SOURCE
LDFLAGS		=	-pthread

SRCS = main.c session.c response.c tpool.c events.c helpers.c
OBJS = $(SRCS:.c=.o)

.PHONY: all clean

all: server

server: $(OBJS)
	$(CC) $(LDFLAGS) -o server $(OBJS)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

clean:
	rm -f $(OBJS) server

A autodetect.sh => autodetect.sh +177 -0
@@ 0,0 1,177 @@
#!/bin/sh

# set -x
info() {
    echo "$*" 1>&2
}

die() {
    info "Config Error: $*"
    exit 1
}

dump() {
    echo "$*" >> config.mk
}

checking() {
    printf -- "   > Checking $*..." 1>&2
}

checking_result() {
    info $*
}

target_triplet() {
    HOSTOS=`uname | awk '{ print(tolower($0)) }'`
    HOSTCPU=`uname -p | awk '{ print(tolower($0)) }'`

    # Because GNU messes with us
    if [ "${HOSTCPU}" = "unknown" ]; then
        HOSTCPU=`uname -m | awk '{ print(tolower($0)) }'`
    fi

    c_compiler

    HOSTCC=${CC}

    checking "Target Triplet"
    TARGET="${HOSTCPU}-${HOSTOS}-${CCNAME}"
    checking_result "${TARGET}"

    dump "CC=${CC}"
    dump "TARGET=\"${TARGET}\""
}

compiler_flags() {

    checking "compilation flags"

    case "${CCNAME}" in
        sunstudio)
            COMPILER_FLAGS="\${CFLAGS} \${CFLAGS_${TARGET}} -errfmt=error -erroff=%none -errshort=full -xstrconst -xildoff -xmemalign=8s -xnolibmil -xcode=pic32 -xregs=no%appl -xlibmieee -ftrap=%none -xbuiltin=%none -xunroll=1 -Qy -xdebugformat=dwarf \${CPPFLAGS} -D_POSIX_PTHREAD_SEMANTICS -D_LARGEFILE64_SOURCE -D_TS_ERRNO -D_FILE_OFFSET_BITS=64 \${LDFLAGS} -m64"
            MKDEPS_FLAGS="\${COMPILE_FLAGS} -xM1"
            ;;
        *)
            COMPILER_FLAGS="\${CFLAGS} \${CPPFLAGS} \${CFLAGS_${TARGET}}"
            MKDEPS_FLAGS="\${COMPILE_FLAGS} -MM -MT \${@:.d=.o}"
            ;;
    esac

    dump "COMPILE_FLAGS=${COMPILER_FLAGS} \${\${@}_CFLAGS} \${LIB_CFLAGS}"
    dump "MKDEPS_FLAGS=${MKDEPS_FLAGS}"
    checking_result "ok"
}

linker_flags() {
    checking "link stage flags"

    case "${CCNAME}" in
        sunstudio)
            LINK_FLAGS="\${COMPILE_FLAGS}"
            ;;
        *)
            LINK_FLAGS="\${CFLAGS} \${CPPFLAGS}"
            ;;
    esac

    dump "LINK_FLAGS=${LINK_FLAGS} \${LDFLAGS} \${LDFLAGS_${TARGET}} \${LIB_LDFLAGS} \${\${@}_LDFLAGS}"
    checking_result "ok"
}

c_compiler() {
    checking "C compiler"
    if [ ! "${CC}" ]; then
        for compiler in /opt/developerstudio*/bin/cc /usr/bin/cc /usr/bin/gcc* /usr/bin/clang* /usr/local/bin/gcc*; do
            which ${compiler} 2>/dev/null >/dev/null
            [ $? -eq 0 ] || continue

            CC=${compiler}
            break
        done
    fi

    [ ${CC} ] || die "I have no C Compiler"

    # Here is a hack to detect some C compilers I know and use
    # Sun Studio
    foo=`${CC} -V 2>&1 | grep "Studio"`
    if [ $? -eq 0 ] && [ "${foo}" ]; then
        CCNAME="sunstudio"
        checking_result "${CC} is ${CCNAME}"
        return
    fi

    # GCC
    foo=`${CC} -v 2>&1 | grep "gcc version"`
    if [ $? -eq 0 ] && [ "${foo}" ]; then
        CCNAME="gcc"
        checking_result "${CC} is ${CCNAME}"
        return
    fi

    # Clang
    foo=`${CC} -v 2>&1 | grep "clang version"`
    if [ $? -eq 0 ] && [ "${foo}" ]; then
        CCNAME="clang"
        checking_result "${CC} is ${CCNAME}"
        return
    fi
}

linker() {
    checking "linker"
    LD=${LD-${CC}}

    [ $LD ] || die "I have no linker"

    dump "LD=${LD}"
    checking_result "${LD}"
}

PKG_CONFIG=${PKG_CONFIG-`which pkg-config 2>/dev/null`}
[ ${PKG_CONFIG} ] || die "Cannot find pkg-config"

check_library() {
    checking "for ${1}"

    cflags=`${PKG_CONFIG} --cflags ${1}`
    ldflags=`${PKG_CONFIG} --libs ${1}`

    [ $? -eq 0 ] || die "Cannot find ${1}"

    dump "LIB_CFLAGS	+=	${cflags}"
    dump "LIB_LDFLAGS	+=	${ldflags}"

    checking_result "found"
}

warn_gnu_make() {
    barf="`${MAKE} -v 2>&1 | grep GNU`"
    if [ "${barf}" != "" ]; then
        info "/!\\ You appear to be using GNU make to build this software."
        info "/!\\ In case you just saw a whole bunch of compiler errors,"
        info "/!\\ this is because of a buggy make implementation found in GNU."
        info "/!\\ PLEASE USE A BETTER MAKE INSTEAD."
    fi
}

main() {
    warn_gnu_make
    target_triplet
    linker
    compiler_flags
    linker_flags

    # Provide -s flag because GNU make is behaving abnormally yet
    # again and prints out that it is going to do a recursion even tho
    # the .SILENT target is defined...This does not break bmake and
    # smake.
    for lib in `${MAKE} -s snmk-libdeps`; do
        check_library "${lib}"
    done
}

MAKE="${1}"

main

A default.mk => default.mk +120 -0
@@ 0,0 1,120 @@
# Copyright 2021 Nico Sonack <nsonack@outlook.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

.SILENT:
.DEFAULT_TARGET: all

PROGS	?=	${PROG}
SRCS	?=	${${PROGS:=_SRCS}} ${${LIBS:=_SRCS}}
OBJS	=	${SRCS:.c=.o}
DEPS	=	${SRCS:.c=.d}

# Unfortunately GNU make is a buggy piece of crap, so we have to do a
# dance around it and include stuff in this specific order (yes,
# otherwise GNU make is too fucking stupid to run the makefile at
# all). Even worse: it now tries to generate the dependency files
# first (which I didn't instruct it to do, but who cares right?) and
# then afterwards it runs the config.mk script which spits out the
# rules on how to properly generate the dependencies. At least it
# builds. This is the only way to do things without complete build
# failures and still maintain compatibility to OpenBSD make, as it
# does things seemingly correct. Mind you, it builds on all kinds of
# different make implementations. Only GNU make tries to be
# special. Thank you! </rant>
-include config.mk
-include ${DEPS}

all: Makefile config.mk
	${MAKE} -f Makefile depend

config.mk: autodetect.sh
	rm -f config.mk
	@echo " ==> Performing autodetection of system variables"
	./autodetect.sh "${MAKE}"

.PHONY: build clean install depend ${MAN:=-install}
build: default.mk ${PROGS} ${LIBS}
	@echo " ==> Compilation finished"

depend: ${DEPS}
	@echo " ==> Starting build"
	${MAKE} -f Makefile build

.SUFFIXES: .c .d
.c.d:
	@echo " ==> Generating dependencies for $<"
	${CC} ${MKDEPS_FLAGS} $< > $@

.c.o:
	@echo " ==> Compiling $<"
	${CC} -c ${COMPILE_FLAGS} -o $@ $<

${PROGS}: ${OBJS} ${LIBS}
	@echo " ==> Linking $@"
	${LD} -o ${@} ${${@}_SRCS:.c=.o} ${LINK_FLAGS}

${LIBS}: ${OBJS}
	@echo " ==> Archiving $@"
	${AR} -rc ${@} ${${@}_SRCS:.c=.o}

clean:
	@echo " ==> Cleaning"
	rm -f ${PROGS} ${LIBS} ${OBJS} ${DEPS} config.mk

PREFIX	?=	/usr/local
BINDIR	?=	${DESTDIR}${PREFIX}/bin
LIBDIR	?=	${DESTDIR}${PREFIX}/lib
MANDIR	?=	${DESTDIR}${PREFIX}/man

${PROGS:=-install}:
	@echo " ==> Installing program ${@:-install=}"
	install -d ${BINDIR}
	install ${@:-install=} ${BINDIR}

${LIBS:=-install}:
	@echo " ==> Installing library {@:-install=}"
	install -d ${LIBDIR}
	install ${@:-install=} ${LIBDIR}

${MAN:=-install}:
	@echo " ==> Installing man page ${@:-install=}"
	install -d ${MANDIR}/man`echo "${@:-install=}" | sed 's/.*\.\([1-9]\)$$/\1/g'`
	gzip -c ${@:-install=} > ${MANDIR}/man`echo "${@:-install=}" | sed 's/.*\.\([1-9]\)$$/\1/g'`/`basename ${@:-install=}`.gz

install: all ${PROGS:=-install} ${LIBS:=-install} ${MAN:=-install}
	@echo " ==> Installation finished"

snmk-libdeps:
	@echo ${LIBADD}

snmk-update:
	@echo " ==> Updating to the most recent version of SN Makefiles"
	curl -L -O --url 'https://gitlab.com/herrhotzenplotz/makefile-template/-/raw/trunk/default.mk'
	curl -L -O --url 'https://gitlab.com/herrhotzenplotz/makefile-template/-/raw/trunk/autodetect.sh'
	chmod 755 autodetect.sh
	@echo " ==> Updated. Rebuilding everything..."
	${MAKE} clean
	${MAKE} all