Files
moloapp/example/example.mk
T

118 lines
4.4 KiB
Makefile
Raw Normal View History

# example/example.mk: living example part (plan 10-workflow-example.md).
# Included by the root Makefile through per-part subdir .mk includes, or run
# standalone from the repo root:
# make -f example/example.mk example # build lib + server
# make -f example/example.mk example-test # build + run example suite
# make -f example/example.mk example-clean # remove build/example
# Portable make: variables plus sh loops only, no GNU-only functions.
CXX ?= g++
EXAMPLE_CXXFLAGS ?= -std=c++23 -Wall -Wextra -O1
EXAMPLE_DIR ?= example
EXAMPLE_BUILD ?= build/example
EXAMPLE_TESTDIR ?= tests/example
EXAMPLE_SRCS = \
$(EXAMPLE_DIR)/fw/Http.cpp \
$(EXAMPLE_DIR)/fw/Router.cpp \
$(EXAMPLE_DIR)/fw/App.cpp \
$(EXAMPLE_DIR)/fw/View.cpp \
$(EXAMPLE_DIR)/fw/Validate.cpp \
$(EXAMPLE_DIR)/fw/Templates.cpp \
$(EXAMPLE_DIR)/fw/Negotiate.cpp \
$(EXAMPLE_DIR)/fw/Cookies.cpp \
$(EXAMPLE_DIR)/fw/StaticFiles.cpp \
$(EXAMPLE_DIR)/fw/Tls.cpp \
$(EXAMPLE_DIR)/fw/Kv.cpp \
$(EXAMPLE_DIR)/fw/Stats.cpp \
$(EXAMPLE_DIR)/fw/Observ.cpp \
$(EXAMPLE_DIR)/fw/Data.cpp \
$(EXAMPLE_DIR)/fw/Jobs.cpp \
$(EXAMPLE_DIR)/fw/Net.cpp \
$(EXAMPLE_DIR)/fw/Workflow.cpp \
$(EXAMPLE_DIR)/fw/TestClient.cpp \
$(EXAMPLE_DIR)/fw/Server.cpp \
$(EXAMPLE_DIR)/app/App.cpp \
$(EXAMPLE_DIR)/app/Seed.cpp \
$(EXAMPLE_DIR)/app/Templates.cpp \
$(EXAMPLE_DIR)/app/Embedded.cpp \
$(EXAMPLE_DIR)/app/Index.cpp \
$(EXAMPLE_DIR)/app/RoutingDemo.cpp \
$(EXAMPLE_DIR)/app/DataDemo.cpp \
$(EXAMPLE_DIR)/app/SecurityDemo.cpp \
$(EXAMPLE_DIR)/app/FormatsDemo.cpp \
$(EXAMPLE_DIR)/app/RealtimeDemo.cpp \
$(EXAMPLE_DIR)/app/OpsDemo.cpp \
$(EXAMPLE_DIR)/app/StatsDemo.cpp
# Tests are discovered: every tests/example/**/*_test.cpp is one route test.
EXAMPLE_TEST_JOBS ?= 8
EXAMPLE_GEN = $(EXAMPLE_BUILD)/gen
EXAMPLE_EMBED_SRCS = \
src/cli/embed/asset_scanner.cpp \
src/cli/embed/embed_emitter.cpp \
src/cli/embed/gzip_stored.cpp \
src/cli/embed/mime.cpp \
src/cli/embed/sha256.cpp
EXAMPLE_ASSETS = \
$(EXAMPLE_DIR)/assets/app.css \
$(EXAMPLE_DIR)/assets/chart.umd.min.js \
$(EXAMPLE_DIR)/assets/hello.txt \
$(EXAMPLE_DIR)/assets/stats.js
EXAMPLE_INC = -I$(EXAMPLE_DIR) -I$(EXAMPLE_BUILD)
.PHONY: example example-assets example-test example-clean
# Asset embedding is part of the build: example/assets (Chart.js included)
# becomes generated C++ under build/example/gen via the MoloApp embedder.
example-assets: $(EXAMPLE_GEN)/assets.cpp
$(EXAMPLE_GEN)/assets.cpp: $(EXAMPLE_ASSETS) $(EXAMPLE_DIR)/tools/embed_assets.cpp \
$(EXAMPLE_EMBED_SRCS)
mkdir -p $(EXAMPLE_GEN)
$(CXX) $(EXAMPLE_CXXFLAGS) -Isrc/cli -Isrc \
$(EXAMPLE_DIR)/tools/embed_assets.cpp $(EXAMPLE_EMBED_SRCS) \
-o $(EXAMPLE_BUILD)/embed_assets
$(EXAMPLE_BUILD)/embed_assets $(EXAMPLE_DIR)/assets \
$(EXAMPLE_GEN)/assets.cpp $(EXAMPLE_GEN)/assets.hpp molo::example::gen
example: example-assets
mkdir -p $(EXAMPLE_BUILD)/obj
for s in $(EXAMPLE_SRCS); do \
o=$(EXAMPLE_BUILD)/obj/`echo $$s | tr '/' '_'`.o; \
$(CXX) $(EXAMPLE_CXXFLAGS) $(EXAMPLE_INC) -c $$s -o $$o || exit 1; \
done
$(CXX) $(EXAMPLE_CXXFLAGS) -c $(EXAMPLE_GEN)/assets.cpp \
-o $(EXAMPLE_BUILD)/obj/gen_assets.o
ar rcs $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_BUILD)/obj/*.o
$(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) $(EXAMPLE_DIR)/main.cpp \
$(EXAMPLE_BUILD)/libexample.a -o $(EXAMPLE_BUILD)/living-server
example-test: example
rm -rf $(EXAMPLE_BUILD)/tests
mkdir -p $(EXAMPLE_BUILD)/tests
find $(EXAMPLE_TESTDIR) -name '*_test.cpp' | sort > $(EXAMPLE_BUILD)/tests/list.txt
xargs -P $(EXAMPLE_TEST_JOBS) -I{} sh -c ' \
t={}; n=`echo $$t | sed "s|$(EXAMPLE_TESTDIR)/||; s|/|_|g; s|\\.cpp$$||"`; \
b=$(EXAMPLE_BUILD)/tests/$$n; \
if $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -I$(EXAMPLE_TESTDIR) \
$$t $(EXAMPLE_BUILD)/libexample.a -o $$b > $$b.log 2>&1; \
then $$b >> $$b.log 2>&1; else echo "COMPILE FAILED: $$t" >> $$b.log; false; fi; \
echo $$? > $$b.rc' < $(EXAMPLE_BUILD)/tests/list.txt
pass=0; fail=0; \
for l in `ls $(EXAMPLE_BUILD)/tests/*.log | sort`; do \
if [ "`cat $${l%.log}.rc`" = 0 ]; then pass=$$((pass+1)); \
else fail=$$((fail+1)); echo "FAIL: $$l"; cat $$l; fi; \
done; \
echo "example suite: $$pass passed, $$fail failed"; \
test $$fail -eq 0
example-clean:
rm -rf $(EXAMPLE_BUILD)
# Root Makefile aggregation points (double-colon: safe with other parts).
all:: example
test:: example-test
clean:: example-clean