Makefile 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # =============================================================================
  2. # MPPT Solar Charge Controller Monitor - Makefile
  3. # =============================================================================
  4. # Compatible with: Ubuntu 24.04 (x86_64) / Raspberry Pi 5 8G (aarch64)
  5. #
  6. # Libraries:
  7. # - mongoose-7.23 (HTTP server)
  8. # - cJSON-1.7.19 (JSON serialization)
  9. # - SQLite3 (historical data storage)
  10. # - OpenSSL 3.x (TLS support, optional)
  11. #
  12. # Usage:
  13. # make - Build the project
  14. # make clean - Remove build artifacts
  15. # make run - Build and run
  16. # make debug - Build with debug symbols and ASAN
  17. # make install - Install to /usr/local/bin
  18. # make systemd - Install systemd service file
  19. # =============================================================================
  20. # ── Compiler & Flags ─────────────────────────────────────────────
  21. CC ?= gcc
  22. CFLAGS := -Wall -Wextra -Wno-unused-parameter -O2 -g
  23. LDFLAGS := -lpthread -lm -lsqlite3
  24. # ── Directories ──────────────────────────────────────────────────
  25. SRCDIR := src
  26. LIBDIR := lib
  27. BUILDDIR := build
  28. # ── Library paths ────────────────────────────────────────────────
  29. CJSON_DIR := $(LIBDIR)/cJSON-1.7.19
  30. MONGOOSE_DIR := $(LIBDIR)/mongoose-7.23
  31. # ── Source files ─────────────────────────────────────────────────
  32. APP_SRCS := $(SRCDIR)/main.c \
  33. $(SRCDIR)/modbus_client.c \
  34. $(SRCDIR)/web_server.c \
  35. $(SRCDIR)/config.c \
  36. $(SRCDIR)/db.c
  37. LIB_SRCS := $(CJSON_DIR)/cJSON.c \
  38. $(MONGOOSE_DIR)/mongoose.c
  39. ALL_SRCS := $(APP_SRCS) $(LIB_SRCS)
  40. # ── Include paths ────────────────────────────────────────────────
  41. INCLUDES := -I$(SRCDIR) \
  42. -I$(CJSON_DIR) \
  43. -I$(MONGOOSE_DIR)
  44. # ── OpenSSL support ─────────────────────────────────────────────
  45. # Auto-detect OpenSSL availability
  46. OPENSSL_AVAILABLE := $(shell pkg-config --exists openssl 2>/dev/null && echo yes || echo no)
  47. ifeq ($(OPENSSL_AVAILABLE),yes)
  48. CFLAGS += -DMG_ENABLE_OPENSSL=1 $(shell pkg-config --cflags openssl)
  49. LDFLAGS += $(shell pkg-config --libs openssl)
  50. else
  51. # Try manual paths (for Raspberry Pi or custom installs)
  52. OPENSSL_MANUAL := $(shell test -f /usr/include/openssl/ssl.h && echo yes || echo no)
  53. ifeq ($(OPENSSL_MANUAL),yes)
  54. CFLAGS += -DMG_ENABLE_OPENSSL=1
  55. LDFLAGS += -lssl -lcrypto
  56. else
  57. # Check custom path from user's reference: /opt/openAI/Lib64U/
  58. OPENSSL_CUSTOM := $(shell test -f /opt/openAI/Lib64U/include/openssl/ssl.h && echo yes || echo no)
  59. ifeq ($(OPENSSL_CUSTOM),yes)
  60. CFLAGS += -DMG_ENABLE_OPENSSL=1 -I/opt/openAI/Lib64U/include
  61. LDFLAGS += -L/opt/openAI/Lib64U/lib -lssl -lcrypto
  62. else
  63. $(warning OpenSSL not found - building without TLS support)
  64. $(warning Install with: sudo apt-get install libssl-dev)
  65. endif
  66. endif
  67. endif
  68. # ── Target ───────────────────────────────────────────────────────
  69. TARGET := $(BUILDDIR)/mppt_monitor
  70. # ── Object files ─────────────────────────────────────────────────
  71. OBJS := $(patsubst %.c,$(BUILDDIR)/%.o,$(notdir $(ALL_SRCS)))
  72. # ── Rules ────────────────────────────────────────────────────────
  73. .PHONY: all clean run debug install systemd help
  74. all: $(TARGET)
  75. $(TARGET): $(OBJS)
  76. @mkdir -p $(BUILDDIR)
  77. $(CC) $(OBJS) -o $@ $(LDFLAGS)
  78. @echo "============================================"
  79. @echo " Build complete: $(TARGET)"
  80. @echo "============================================"
  81. # Compile source files - handle both src/ and lib/ directories
  82. $(BUILDDIR)/main.o: $(SRCDIR)/main.c | $(BUILDDIR)
  83. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  84. $(BUILDDIR)/modbus_client.o: $(SRCDIR)/modbus_client.c | $(BUILDDIR)
  85. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  86. $(BUILDDIR)/web_server.o: $(SRCDIR)/web_server.c | $(BUILDDIR)
  87. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  88. $(BUILDDIR)/config.o: $(SRCDIR)/config.c | $(BUILDDIR)
  89. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  90. $(BUILDDIR)/db.o: $(SRCDIR)/db.c | $(BUILDDIR)
  91. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  92. $(BUILDDIR)/cJSON.o: $(CJSON_DIR)/cJSON.c | $(BUILDDIR)
  93. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  94. $(BUILDDIR)/mongoose.o: $(MONGOOSE_DIR)/mongoose.c | $(BUILDDIR)
  95. $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
  96. $(BUILDDIR):
  97. @mkdir -p $(BUILDDIR)
  98. # ── Debug build ──────────────────────────────────────────────────
  99. debug: CFLAGS += -DDEBUG -fsanitize=address -fno-omit-frame-pointer
  100. debug: LDFLAGS += -fsanitize=address
  101. debug: clean all
  102. # ── Run ──────────────────────────────────────────────────────────
  103. run: all
  104. ./$(TARGET)
  105. # ── Clean ────────────────────────────────────────────────────────
  106. clean:
  107. rm -rf $(BUILDDIR)
  108. # ── Install ──────────────────────────────────────────────────────
  109. install: all
  110. install -Dm755 $(TARGET) /usr/local/bin/mppt_monitor
  111. @echo "Installed to /usr/local/bin/mppt_monitor"
  112. # ── Systemd service ──────────────────────────────────────────────
  113. systemd: install
  114. @echo "Creating systemd service file..."
  115. @echo "[Unit]" > /etc/systemd/system/mppt-monitor.service
  116. @echo "Description=MPPT Solar Charge Controller Monitor" >> /etc/systemd/system/mppt-monitor.service
  117. @echo "After=network-online.target" >> /etc/systemd/system/mppt-monitor.service
  118. @echo "Wants=network-online.target" >> /etc/systemd/system/mppt-monitor.service
  119. @echo "" >> /etc/systemd/system/mppt-monitor.service
  120. @echo "[Service]" >> /etc/systemd/system/mppt-monitor.service
  121. @echo "Type=simple" >> /etc/systemd/system/mppt-monitor.service
  122. @echo "ExecStart=/usr/local/bin/mppt_monitor" >> /etc/systemd/system/mppt-monitor.service
  123. @echo "Restart=always" >> /etc/systemd/system/mppt-monitor.service
  124. @echo "RestartSec=5" >> /etc/systemd/system/mppt-monitor.service
  125. @echo "StandardOutput=journal" >> /etc/systemd/system/mppt-monitor.service
  126. @echo "StandardError=journal" >> /etc/systemd/system/mppt-monitor.service
  127. @echo "SyslogIdentifier=mppt-monitor" >> /etc/systemd/system/mppt-monitor.service
  128. @echo "" >> /etc/systemd/system/mppt-monitor.service
  129. @echo "[Install]" >> /etc/systemd/system/mppt-monitor.service
  130. @echo "WantedBy=multi-user.target" >> /etc/systemd/system/mppt-monitor.service
  131. @systemctl daemon-reload
  132. @systemctl enable mppt-monitor
  133. @echo "Systemd service installed and enabled."
  134. @echo " Start: sudo systemctl start mppt-monitor"
  135. @echo " Status: sudo systemctl status mppt-monitor"
  136. @echo " Logs: sudo journalctl -u mppt-monitor -f"
  137. # ── Help ─────────────────────────────────────────────────────────
  138. help:
  139. @echo "MPPT Monitor Build System"
  140. @echo ""
  141. @echo " make - Build the project"
  142. @echo " make clean - Remove build artifacts"
  143. @echo " make run - Build and run"
  144. @echo " make debug - Build with debug + ASAN"
  145. @echo " make install - Install to /usr/local/bin"
  146. @echo " make systemd - Install + setup systemd service"
  147. @echo " make help - Show this help"
  148. @echo ""
  149. @echo "OpenSSL: $(OPENSSL_AVAILABLE)"
  150. @echo "CC: $(CC)"