-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
154 lines (130 loc) · 4.74 KB
/
Makefile
File metadata and controls
154 lines (130 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Makefile for Awale Project
# Compiles client or server application with shared game logic
# Compiler and flags (can be overridden from command line)
CC ?= gcc
CFLAGS =
# CFLAGS ?= -Wall -Wextra -std=c17 -g
INCLUDES ?= -Ishared
# Linker flags for external libraries
LDFLAGS ?=
LIBS ?=
# Platform-specific libraries (socket libraries)
ifeq ($(OS),Windows_NT)
# Windows - Winsock library
LIBS += -lws2_32
else
# Unix/Linux - usually no additional libraries needed for sockets
# But some systems might need -lsocket -lnsl
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),SunOS)
LIBS += -lsocket -lnsl
endif
endif
# Build mode (can be 'debug' or 'release')
BUILD_MODE ?= debug
# Target name (must be 'client' or 'server')
TARGET_NAME ?= client
# Validate TARGET_NAME
ifneq ($(TARGET_NAME),client)
ifneq ($(TARGET_NAME),server)
$(error ERROR: TARGET_NAME must be 'client' or 'server', but got '$(TARGET_NAME)')
endif
endif
# Additional flags based on build mode
ifeq ($(BUILD_MODE),release)
CFLAGS += -O2 -DNDEBUG
else ifeq ($(BUILD_MODE),debug)
CFLAGS += -g -DDEBUG
endif
# Directories
CLIENT_DIR = client
SHARED_DIR = shared
SERVER_DIR = server
# Set target-specific directories and sources
ifeq ($(TARGET_NAME),client)
TARGET_DIR = $(CLIENT_DIR)
BIN_DIR = $(CLIENT_DIR)/bin
TARGET_SOURCES = $(wildcard $(CLIENT_DIR)/*.c)
else ifeq ($(TARGET_NAME),server)
TARGET_DIR = $(SERVER_DIR)
BIN_DIR = $(SERVER_DIR)/bin
TARGET_SOURCES = $(wildcard $(SERVER_DIR)/*.c)
endif
# Shared sources and headers (always included)
SHARED_SOURCES = $(wildcard $(SHARED_DIR)/*.c)
ALL_SOURCES = $(TARGET_SOURCES) $(SHARED_SOURCES)
# Header files (automatically find all .h files)
SHARED_HEADERS = $(wildcard $(SHARED_DIR)/*.h)
TARGET_HEADERS = $(wildcard $(TARGET_DIR)/*.h)
HEADERS = $(SHARED_HEADERS) $(TARGET_HEADERS)
# Object files (placed in target-specific bin directory)
TARGET_OBJECTS = $(patsubst $(TARGET_DIR)/%.c,$(BIN_DIR)/%.o,$(TARGET_SOURCES))
SHARED_OBJECTS = $(patsubst $(SHARED_DIR)/%.c,$(BIN_DIR)/%.o,$(SHARED_SOURCES))
OBJECTS = $(TARGET_OBJECTS) $(SHARED_OBJECTS)
# Executable name
TARGET = $(BIN_DIR)/$(TARGET_NAME)
# Default target
all: $(TARGET)
# Create bin directory if it doesn't exist
$(BIN_DIR):
mkdir -p $(BIN_DIR)
# Link objects to create executable
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $(TARGET) $(LIBS)
# Compile target source files (client or server)
$(BIN_DIR)/%.o: $(TARGET_DIR)/%.c $(HEADERS) | $(BIN_DIR)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
# Compile shared source files
$(BIN_DIR)/%.o: $(SHARED_DIR)/%.c $(HEADERS) | $(BIN_DIR)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
# Clean compiled files
clean:
rm -rf $(BIN_DIR)
# Clean all (both client and server bin directories)
clean-all:
rm -rf $(CLIENT_DIR)/bin $(SERVER_DIR)/bin
# Rebuild everything
rebuild: clean all
# Phony targets
.PHONY: all clean clean-all rebuild show help
# Show variables (for debugging)
show:
@echo "CC: $(CC)"
@echo "CFLAGS: $(CFLAGS)"
@echo "LDFLAGS: $(LDFLAGS)"
@echo "LIBS: $(LIBS)"
@echo "INCLUDES: $(INCLUDES)"
@echo "BUILD_MODE: $(BUILD_MODE)"
@echo "TARGET_NAME: $(TARGET_NAME)"
@echo "TARGET_DIR: $(TARGET_DIR)"
@echo "BIN_DIR: $(BIN_DIR)"
@echo "TARGET_SOURCES: $(TARGET_SOURCES)"
@echo "SHARED_SOURCES: $(SHARED_SOURCES)"
@echo "ALL_SOURCES: $(ALL_SOURCES)"
@echo "OBJECTS: $(OBJECTS)"
@echo "TARGET: $(TARGET)"
# Help target to show available parameters
help:
@echo "Available targets:"
@echo " all (default) - Build the specified target (client or server)"
@echo " clean - Remove compiled files for current target"
@echo " clean-all - Remove compiled files for both client and server"
@echo " rebuild - Clean and build current target"
@echo " show - Show makefile variables"
@echo " help - Show this help"
@echo ""
@echo "Available parameters:"
@echo " CC=<compiler> - Compiler to use (default: gcc)"
@echo " CFLAGS=<flags> - Compiler flags"
@echo " LDFLAGS=<flags> - Linker flags"
@echo " LIBS=<libraries> - Additional libraries to link"
@echo " BUILD_MODE=<mode> - debug or release (default: debug)"
@echo " TARGET_NAME=<name> - MUST be 'client' or 'server' (default: client)"
@echo " INCLUDES=<paths> - Include paths"
@echo ""
@echo "Examples:"
@echo " make # Build client (default)"
@echo " make TARGET_NAME=server # Build server"
@echo " make TARGET_NAME=client BUILD_MODE=release"
@echo " make TARGET_NAME=server CC=clang"
@echo " make clean-all # Clean both client and server"