#
# Makefile.in/Makefile, build rules for pseudo
#
# Copyright (c) 2008-2015 Wind River Systems, Inc.
#
# SPDX-License-Identifier: LGPL-2.1-only
#

# configuration flags
PREFIX=@PREFIX@
LIBDIR=@LIBDIR@
SUFFIX=@SUFFIX@
SQLITE=@SQLITE@
SQLITE_LIB=@SQLITE_LIB@
SQLITE_MEMORY=@SQLITE_MEMORY@
FORCE_ASYNC=@FORCE_ASYNC@
EPOLL=@EPOLL@
XATTR=@XATTR@
XATTRDB=@XATTRDB@
PROFILING=@PROFILING@
ifeq (true,$(PROFILING))
  PSEUDO_PROFILE=$(BIN)/pseudo_profile
else
  PSEUDO_PROFILE=
endif
define cond
  $(if $(filter true,$($(1))),-D$(2))
endef
OPTDEFS=$(call cond,XATTR,PSEUDO_XATTR_SUPPORT) \
        $(call cond,XATTRDB,PSEUDO_XATTRDB) \
        $(call cond,PROFILING,PSEUDO_PROFILING)
PASSWD_FALLBACK=@PASSWD_FALLBACK@
BITS=@BITS@
ARCH_FLAGS=@ARCH_FLAGS@
MARK64=@MARK64@
RPATH=@RPATH@
VERSION=1.9.0

LIB=@LIB@
BIN=bin
LOCALSTATE=var/pseudo
BINDIR=$(PREFIX)/$(BIN)
LOCALSTATEDIR=$(PREFIX)/$(LOCALSTATE)

CFLAGS_BASE=-pipe -std=gnu99 -Wall -W -Wextra -Wno-deprecated-declarations
CFLAGS_CODE=-fPIC -D_LARGEFILE64_SOURCE -D_ATFILE_SOURCE $(ARCH_FLAGS)
CFLAGS_DEFS=-DPSEUDO_PREFIX='"$(PREFIX)"' -DPSEUDO_SUFFIX='"$(SUFFIX)"' -DPSEUDO_BINDIR='"$(BIN)"' -DPSEUDO_LIBDIR='"$(LIB)"' -DPSEUDO_LOCALSTATEDIR='"$(LOCALSTATE)"' -DPSEUDO_VERSION='"$(VERSION)"' $(SQLITE_MEMORY) $(FORCE_ASYNC) -DPSEUDO_PASSWD_FALLBACK='$(PASSWD_FALLBACK)' $(OPTDEFS) $(EPOLL)
CFLAGS_DEBUG=-O2 -g
@DEFAULT_SQLITE@CFLAGS_SQL=-L$(SQLITE)/$(SQLITE_LIB) -I$(SQLITE)/include $(RPATH)
CFLAGS_PSEUDO=$(CFLAGS_BASE) $(CFLAGS_CODE) $(CFLAGS_DEFS) \
	$(CFLAGS_DEBUG) $(CFLAGS_SQL)

GLOB_PATTERN=guts/*.c
GUTS=$(filter-out "$(GLOB_PATTERN)",$(wildcard $(GLOB_PATTERN)))

SOURCES=$(wildcard *.c)
OBJS=$(subst .c,.o,$(SOURCES))
TESTS=$(patsubst %.c,%,$(wildcard test/*.c))

SHOBJS=pseudo_tables.o pseudo_util.o
DBOBJS=pseudo_db.o
WRAPOBJS=pseudo_wrappers.o

# needed for anything that links with pseduo_client.o, pretty much
CLIENT_LDFLAGS=-ldl -lpthread
DB_LDFLAGS=@SQLITE_LDARG@ -lpthread

PSEUDO=$(BIN)/pseudo
PSEUDODB=$(BIN)/pseudodb
PSEUDOLOG=$(BIN)/pseudolog
LIBPSEUDO=$(LIB)/libpseudo.so

TEMPLATES=templates/guts templates/wrapfuncs.c templates/wrapfuncs.h templates/wrapper_table
TABLES=table_templates/pseudo_tables.c table_templates/pseudo_tables.h

all: $(LIBPSEUDO) $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) $(PSEUDO_PROFILE)

test: all $(TESTS) | $(BIN) $(LIB)
	./run_tests.sh -v

test/%: test/%.c
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $@ $<

install-lib: $(LIBPSEUDO)
	mkdir -p $(DESTDIR)$(LIBDIR)
	cp $(LIBPSEUDO) $(DESTDIR)$(LIBDIR)
	$(if $(SUFFIX),cp $(LIBPSEUDO) $(DESTDIR)$(LIBDIR)/libpseudo$(SUFFIX).so,:)

install-bin: $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) $(PSEUDO_PROFILE)
	mkdir -p $(DESTDIR)$(BINDIR)
	cp $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) $(PSEUDO_PROFILE) $(DESTDIR)$(BINDIR)

install-data:
	mkdir -p $(DESTDIR)$(LOCALSTATEDIR)

install: all install-lib install-bin install-data

$(BIN) $(LIB):
	mkdir -p $@

pseudo: $(PSEUDO)

$(PSEUDO): pseudo.o $(SHOBJS) $(DBOBJS) pseudo_client.o pseudo_server.o pseudo_ipc.o | $(BIN)
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $(PSEUDO) \
		pseudo.o pseudo_server.o pseudo_client.o pseudo_ipc.o \
		$(DBOBJS) $(SHOBJS) $(LDFLAGS) $(DB_LDFLAGS) $(CLIENT_LDFLAGS)

pseudolog: $(PSEUDOLOG)

$(PSEUDOLOG): pseudolog.o $(SHOBJS) $(DBOBJS) pseudo_client.o pseudo_ipc.o | $(BIN)
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $(PSEUDOLOG) pseudolog.o pseudo_client.o pseudo_ipc.o \
		$(DBOBJS) $(SHOBJS) $(LDFLAGS) $(DB_LDFLAGS) $(CLIENT_LDFLAGS)

pseudodb: $(PSEUDODB)

$(PSEUDODB): pseudodb.o $(SHOBJS) $(DBOBJS) pseudo_ipc.o | $(BIN)
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $(PSEUDODB) pseudodb.o \
		$(DBOBJS) $(SHOBJS) pseudo_ipc.o $(LDFLAGS) $(DB_LDFLAGS) $(CLIENT_LDFLAGS)

libpseudo: $(LIBPSEUDO)

$(LIBPSEUDO): $(WRAPOBJS) pseudo_client.o pseudo_ipc.o $(SHOBJS) | $(LIB)
	$(CC) $(CFLAGS)  -Lprebuilt/$(shell uname -m)-linux/lib/ $(CFLAGS_PSEUDO) -shared -o $(LIBPSEUDO) \
		pseudo_client.o pseudo_ipc.o \
		$(WRAPOBJS) $(SHOBJS) $(LDFLAGS) $(CLIENT_LDFLAGS)

# *everything* now relies on stuff that's generated in the
# wrapper process.
%.o: %.c pseudo_wrapfuncs.h
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $(CFLAGS_PSEUDO) $<

$(OBJS): pseudo_tables.h

pseudo_client.o pseudo_server.o pseudo_ipc.o: pseudo_ipc.h

pseudo_client.o: pseudo_client.h

pseudo_server.o: pseudo_server.h

tables: enums/*.in maketables templatefile.py $(TABLES)
	./maketables enums/*.in

wrappers: makewrappers templatefile.py $(TEMPLATES) ports/*/wrapfuncs.in
	CC="$(CC) $(CFLAGS) $(CFLAGS_PSEUDO)" ./makewrappers "xattr=$(XATTR)"

.SECONDARY: tables wrappers

pseudo_wrapfuncs.c pseudo_wrapfuncs.h: wrappers

pseudo_tables.c pseudo_tables.h: tables

pseudo_tables.o: pseudo_tables.c
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -c -o pseudo_tables.o pseudo_tables.c

# no-strict-aliasing is needed for the function pointer trickery.
pseudo_wrappers.o: $(GUTS) pseudo_wrappers.c pseudo_wrapfuncs.c pseudo_wrapfuncs.h pseudo_tables.h
	$(CC) -fno-strict-aliasing $(CFLAGS) $(CFLAGS_PSEUDO) -D_GNU_SOURCE -c -o pseudo_wrappers.o pseudo_wrappers.c

offsets32:
	$(CC) -m32 -o offsets32 offsets.c

offsets64:
	$(CC) -m64 -o offsets64 offsets.c

$(PSEUDO_PROFILE): pseudo_profile | $(BIN)
	cp pseudo_profile $(BIN)

pseudo_profile: Makefile pseudo_profile.c tables wrappers
	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o pseudo_profile pseudo_profile.c

clean:
	rm -f *.o $(LIBPSEUDO) $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) \
		$(TESTS) \
		pseudo_wrapfuncs.h pseudo_wrapfuncs.c \
		pseudo_wrapper_table.c \
		pseudo_tables.c pseudo_tables.h \
		pseudo_ports.h port_wrappers.c \
		offsets32 offsets64 \
		pseudo_profile \
		port_deps.mk func_deps.mk
	touch port_deps.mk func_deps.mk

distclean: clean
	rm -f Makefile port_deps.mk func_deps.mk
	rm -rf ./$(BIN) ./$(LIB) ./$(LOCALSTATE) ./__pycache__
	@echo "WARNING: Makefile has been removed.  You must reconfigure to do anything else."

nuke: distclean
	case "$(PREFIX)" in "`pwd`"/*) rm -rf "$(PREFIX)";; esac
	@echo "WARNING: Removed $(PREFIX)."

tarball:
	@test -d .git || ( echo >&2 "Tarball can only be made from git tree."; exit 1)
	rm -rf pseudo-$(VERSION)
	mkdir -p pseudo-$(VERSION)
	( parent=$(PWD); \
	  cd pseudo-$(VERSION) && \
	  git clone $$parent && \
	  mv pseudo/* . && \
	  rm -rf pseudo/.git* && \
	  rmdir pseudo \
	)
	tar cjf pseudo-$(VERSION).tar.bz2 pseudo-$(VERSION)

# Note when we need to rebuild pseudo_wrappers.o
include port_deps.mk
include func_deps.mk

