-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
107 lines (91 loc) · 3.25 KB
/
Copy pathMakefile
File metadata and controls
107 lines (91 loc) · 3.25 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
# ============================================================
# Cross‑platform Makefile for the Tube Renderer
#
# Requirements:
# - Linux/macOS: GCC or Clang (pthreads is built‑in)
# - Windows: MinGW‑w64 (pthreads comes with MinGW)
# or MSVC + vcpkg (use `cl` command directly)
# ============================================================
# ---------- Toolchain ----------
CC ?= gcc
RM ?= rm -f
MKDIR ?= mkdir -p
# ---------- Target ----------
TARGET := renderer
ifeq ($(OS),Windows_NT)
TARGET := $(TARGET).exe
endif
# ---------- Directories (adjust if your layout differs) ----------
INC_DIRS := files/include files/src include
SRC_DIRS := files/src
VPATH := $(SRC_DIRS)
BUILD_DIR := build
# ---------- Source files ----------
SRCS := main.c scene.c renderer.c geometry.c math_utils.c
OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
# ---------- Compiler flags ----------
CFLAGS := -O3 -march=native -ffast-math -funroll-loops -pthread \
-Wall -Wextra -std=c11
LDFLAGS := -pthread -lm
# ---------- Cross‑platform detection ----------
ifeq ($(OS),Windows_NT)
# MinGW already has pthreads, nothing extra needed
# For MSVC the user should use the `cl` command shown in the help
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
# macOS – everything is built‑in
endif
endif
# ---------- Include paths ----------
CFLAGS += $(addprefix -I,$(INC_DIRS))
# ---------- Automatic dependency files ----------
DEPS := $(OBJS:.o=.d)
CFLAGS += -MMD -MP
# ---------- Default target ----------
.PHONY: all
all: check_pthread $(TARGET)
# ---------- Pre‑build check for pthreads ----------
.PHONY: check_pthread
check_pthread:
@echo "Checking for pthreads..."
@$(CC) -x c -pthread -o /dev/null - <<< 'int main(){return 0;}' 2>/dev/null || \
(echo "ERROR: pthreads not found!" && \
echo "On Windows (MinGW): install MinGW‑w64 (pthreads is included)." && \
echo "On MSVC: install vcpkg and run 'vcpkg install pthreads:x64-windows'." && \
exit 1)
@echo "pthreads OK."
# ---------- Linking ----------
$(TARGET): $(OBJS)
@echo "Linking $@ ..."
$(CC) $(OBJS) -o $@ $(LDFLAGS)
# ---------- Compilation (with auto dependency) ----------
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Create build directory if it doesn't exist
$(BUILD_DIR):
$(MKDIR) $(BUILD_DIR)
# ---------- Include auto‑generated dependency rules ----------
-include $(DEPS)
# ---------- Clean ----------
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR) # remove all objects and dependency files
$(RM) *.o # remove any .o files in the project root (if any)
$(RM) $(TARGET) # remove the executable
@echo "Cleaned."
# ---------- Help ----------
.PHONY: help
help:
@echo "Usage:"
@echo " make - build the renderer (default)"
@echo " make clean - remove all build artifacts"
@echo " make help - show this help"
@echo ""
@echo "For MSVC (Windows) without MinGW, use:"
@echo " cl /O2 /Fe:render.exe *.c /I files/include /I files/src /I include /link pthreads.lib"
@echo " (requires vcpkg with pthreads installed)"
@echo ""
@echo "Environment variables:"
@echo " CC - compiler (default: gcc)"
@echo " BUILD_DIR - where objects go (default: build)"