Makefile 8.1 KB

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