# 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 # make -f example/example.mk example-locust # headless locust run # 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/SecurityExtra.cpp \ $(EXAMPLE_DIR)/fw/Platform.cpp \ $(EXAMPLE_DIR)/fw/Enterprise.cpp \ $(EXAMPLE_DIR)/fw/Resilience.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 \ $(EXAMPLE_DIR)/app/SecurityExtraDemo.cpp \ $(EXAMPLE_DIR)/app/PlatformDemo.cpp \ $(EXAMPLE_DIR)/app/EnterpriseDemo.cpp \ $(EXAMPLE_DIR)/app/ResilienceDemo.cpp \ $(EXAMPLE_DIR)/app/VoiceDemo.cpp \ $(sort $(wildcard $(EXAMPLE_DIR)/app/agent/*.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 # Every file in example/assets is embedded (agent modules add agent_.js). EXAMPLE_ASSETS = $(sort $(wildcard $(EXAMPLE_DIR)/assets/*)) EXAMPLE_INC = -I$(EXAMPLE_DIR) -I$(EXAMPLE_BUILD) -Isrc # Real framework parts the example links: Google Cloud (src/google) over the # HTTP client (src/clients, wolfSSL TLS) and the operability breaker. EXAMPLE_REAL_LIBS = build/google/libgoogle.a build/clients/libclients.a \ build/liboperability.a .PHONY: example example-real-libs example-assets example-test example-clean example-locust # 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-real-libs: $(MAKE) -f src/operability.mk build/liboperability.a $(MAKE) -f src/clients/clients.mk clients $(MAKE) -f src/google/google.mk google example: example-assets example-real-libs 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 $(MAKE) -f src/supervisor/supervisor.mk build/supervisor/libsupervisor.a $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -Isrc $(EXAMPLE_DIR)/main.cpp \ $(EXAMPLE_BUILD)/libexample.a build/supervisor/libsupervisor.a \ $(EXAMPLE_REAL_LIBS) -lwolfssl -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) -Isrc \ $$t $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_REAL_LIBS) -lwolfssl -o $$b > $$b.log 2>&1; \ then MOLO_ENV_FILES=off $$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-locust: python3 $(EXAMPLE_DIR)/check_locust_coverage.py locust -f $(EXAMPLE_DIR)/locustfile.py --host $${LOCUST_HOST:-http://127.0.0.1:8080} --headless -u $${LOCUST_USERS:-10} -r $${LOCUST_RATE:-2} -t $${LOCUST_TIME:-30s} example-clean: rm -rf $(EXAMPLE_BUILD) # Root Makefile aggregation points (double-colon: safe with other parts). all:: example test:: example-test clean:: example-clean