summaryrefslogtreecommitdiff
path: root/Makefile
blob: e2f0c54caaa414d0b497f0ff3a6b9a149a22dc9c (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Simplified Makefile for glamac - SDL3 version
# Supports: Linux native, Windows cross-compilation

# Detect OS and set platform-specific variables
UNAME_S := $(shell uname -s 2>/dev/null || echo Windows_NT)
ifeq ($(UNAME_S),Linux)
    PLATFORM := linux
    EXE_EXT :=
    MKDIR := mkdir -p
    RM := rm -f
    RMDIR := rm -rf
    CC := gcc
    MINGW_CC := x86_64-w64-mingw32-gcc
    SDL3_LIBS := $(shell pkg-config --libs sdl3 SDL3_ttf 2>/dev/null || echo -lSDL3 -lSDL3_ttf) -lm
    CROSS_PREFIX := /usr/x86_64-w64-mingw32
else
    PLATFORM := windows
    EXE_EXT := .exe
    MKDIR := mkdir
    RM := del /Q
    RMDIR := rmdir /s /q
    CC := gcc
    SDL3_LIBS := -lSDL3 -lSDL3_ttf -mwindows
endif

# Directories
SRCDIR := src
BINDIR := bin
BINDIR_WIN := bin/win
INCDIR := include
DLL_CACHE := $(HOME)/.cache/glamac-dlls

# Base flags
CFLAGS_BASE := -I$(INCDIR) -O2 -flto
# Security and warning flags
SECURITY_FLAGS := -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE
WARNING_FLAGS := -Wall -Wextra -Wformat=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wvla

# Compiler flags
CFLAGS := $(CFLAGS_BASE) $(SECURITY_FLAGS) $(WARNING_FLAGS)
CFLAGS_NATIVE := $(CFLAGS) -march=native
# Windows cross-compilation flags (without stack protector to avoid libssp dependency)
WARNING_FLAGS_WIN := -Wall -Wextra -Wformat=2 -Wformat-security -Wnull-dereference -Wvla
CFLAGS_CROSS := $(CFLAGS_BASE) $(WARNING_FLAGS_WIN) -I$(CROSS_PREFIX)/include

# Source files
GLAMAC_SRCS := $(wildcard $(SRCDIR)/glamac/*.c)
GLAUTILS_SRCS := $(wildcard $(SRCDIR)/glautils/*.c)
GLAUTILS_BINS := $(patsubst $(SRCDIR)/glautils/%.c, $(BINDIR)/%$(EXE_EXT), $(GLAUTILS_SRCS))
GLAUTILS_BINS_WIN := $(patsubst $(SRCDIR)/glautils/%.c, $(BINDIR_WIN)/%.exe, $(GLAUTILS_SRCS))

# Glass data dependencies for fgla
GLASS_DATA_SRCS := $(SRCDIR)/glamac/glass_data.c $(SRCDIR)/glamac/glamac_errors.c

# Default target
all: glamac glautils

# Native build targets
glamac: $(BINDIR)/glamac$(EXE_EXT)

glautils: $(GLAUTILS_BINS)

$(BINDIR)/glamac$(EXE_EXT): $(GLAMAC_SRCS) | $(BINDIR)
	@echo "Building glamac..."
	$(CC) $^ $(CFLAGS_NATIVE) $(SDL3_LIBS) -o $@

# Special rule for fgla which needs glass_data dependencies
$(BINDIR)/fgla$(EXE_EXT): $(SRCDIR)/glautils/fgla.c $(GLASS_DATA_SRCS) | $(BINDIR)
	$(CC) $^ $(CFLAGS_NATIVE) -o $@

# General rule for other glautils (excluding fgla)
$(BINDIR)/%$(EXE_EXT): $(SRCDIR)/glautils/%.c | $(BINDIR)
	$(CC) $< $(CFLAGS_NATIVE) -o $@

$(BINDIR):
	$(MKDIR) $(BINDIR)

# Windows cross-compilation (Linux only)
ifeq ($(PLATFORM),linux)

win: $(BINDIR_WIN)/glamac.exe win-dlls win-data

win-all: win $(GLAUTILS_BINS_WIN)

$(BINDIR_WIN)/glamac.exe: $(GLAMAC_SRCS) | $(BINDIR_WIN)
	@echo "Cross-compiling glamac for Windows..."
	@which $(MINGW_CC) >/dev/null 2>&1 || (echo "ERROR: Install mingw-w64-gcc first" && exit 1)
	$(MINGW_CC) $^ $(CFLAGS_CROSS) -L$(CROSS_PREFIX)/lib -lmingw32 -lSDL3 -lSDL3_ttf -mwindows -static-libgcc -o $@

$(BINDIR_WIN)/%.exe: $(SRCDIR)/glautils/%.c | $(BINDIR_WIN)
	$(MINGW_CC) $< $(CFLAGS_CROSS) -static-libgcc -o $@

$(BINDIR_WIN):
	$(MKDIR) $(BINDIR_WIN)

# Windows DLL management
win-dlls: | $(BINDIR_WIN)
	@echo "Getting Windows DLLs..."
	@$(MKDIR) $(DLL_CACHE)
	@if [ ! -f "$(DLL_CACHE)/SDL3.dll" ]; then \
		echo "Downloading SDL3.dll..."; \
		cd $(DLL_CACHE) && \
		wget -q https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-3.2.10-win32-x64.zip && \
		unzip -j SDL3-3.2.10-win32-x64.zip SDL3.dll && \
		$(RM) SDL3-3.2.10-win32-x64.zip; \
	fi
	@if [ ! -f "$(DLL_CACHE)/SDL3_ttf.dll" ]; then \
		echo "Downloading SDL3_ttf.dll..."; \
		cd $(DLL_CACHE) && \
		wget -q https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-3.2.2-win32-x64.zip && \
		unzip -j SDL3_ttf-3.2.2-win32-x64.zip SDL3_ttf.dll && \
		$(RM) SDL3_ttf-3.2.2-win32-x64.zip; \
	fi
	@cp $(DLL_CACHE)/*.dll $(BINDIR_WIN)/
	@echo "Windows build ready in $(BINDIR_WIN)/"

# Copy data files for Windows build
win-data: | $(BINDIR_WIN)
	@echo "Copying data files for Windows..."
	@$(MKDIR) $(BINDIR_WIN)/data/json
	@if [ -f "data/json/glasses.json" ]; then \
		cp data/json/glasses.json $(BINDIR_WIN)/data/json/; \
		cp data/json/glasses.json $(BINDIR_WIN)/; \
		echo "Copied glasses.json to Windows build"; \
	else \
		echo "Warning: glasses.json not found, Windows build may use fallback data"; \
	fi
	@echo "Copying font for Windows..."
	@if [ -f "/usr/share/fonts/TTF/DejaVuSans.ttf" ]; then \
		cp /usr/share/fonts/TTF/DejaVuSans.ttf $(BINDIR_WIN)/; \
		echo "Copied DejaVuSans.ttf to Windows build"; \
	elif [ -f "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" ]; then \
		cp /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf $(BINDIR_WIN)/; \
		echo "Copied DejaVuSans.ttf to Windows build"; \
	else \
		echo "Warning: DejaVu font not found, Windows build may fail to start"; \
	fi

# Cross-compilation setup
setup-cross:
	@echo "Setting up cross-compilation..."
	@which wget >/dev/null 2>&1 || (echo "Install wget first: sudo pacman -S wget" && exit 1)
	@which $(MINGW_CC) >/dev/null 2>&1 || (echo "Install mingw-w64-gcc first: sudo pacman -S mingw-w64-gcc" && exit 1)
	sudo $(MKDIR) $(CROSS_PREFIX)/include $(CROSS_PREFIX)/lib
	@echo "Downloading SDL3 development libraries..."
	cd /tmp && \
	wget -q https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz && \
	wget -q https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-devel-3.2.2-mingw.tar.gz && \
	tar -xzf SDL3-devel-3.2.10-mingw.tar.gz && \
	tar -xzf SDL3_ttf-devel-3.2.2-mingw.tar.gz && \
	sudo cp -r SDL3-3.2.10/x86_64-w64-mingw32/include/* $(CROSS_PREFIX)/include/ && \
	sudo cp -r SDL3-3.2.10/x86_64-w64-mingw32/lib/* $(CROSS_PREFIX)/lib/ && \
	sudo cp -r SDL3_ttf-3.2.2/x86_64-w64-mingw32/include/* $(CROSS_PREFIX)/include/ && \
	sudo cp -r SDL3_ttf-3.2.2/x86_64-w64-mingw32/lib/* $(CROSS_PREFIX)/lib/ && \
	$(RM) -rf SDL3-3.2.10* SDL3_ttf-3.2.2*
	@echo "Cross-compilation setup complete!"

else
# Windows host - disable cross-compilation
win win-all win-dlls setup-cross:
	@echo "Cross-compilation not available on Windows. Use 'make all' instead."
endif

# Excel to JSON conversion
EXCEL_FILES := $(wildcard data/Excel/*.xlsx)
JSON_FILES := $(patsubst data/Excel/%.xlsx, data/JSON/%.json, $(EXCEL_FILES))

convert-catalogs: $(JSON_FILES)

data/JSON/%.json: data/Excel/%.xlsx scripts/excel_to_json.py
	@echo "Converting $< to JSON..."
	@$(MKDIR) data/JSON
	@python3 scripts/excel_to_json.py $< -o $@

# Dependency management
deps:
ifeq ($(PLATFORM),linux)
	@echo "Installing dependencies..."
	sudo pacman -S --needed sdl3 git cmake pkgconf freetype2 python python-pandas python-openpyxl
	@echo "Building SDL3_ttf from source..."
	cd /tmp && $(RM) -rf SDL_ttf && \
	git clone https://github.com/libsdl-org/SDL_ttf.git && \
	cd SDL_ttf && git checkout release-3.2.2 && \
	cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr && \
	make -C build -j$$(nproc) && sudo make -C build install && \
	sudo ldconfig
	@echo "Dependencies installed!"
else
	@echo "Install SDL3 development libraries manually on Windows."
	@echo "Install Python with pandas and openpyxl for Excel conversion."
endif

# Cleanup
clean:
	$(RMDIR) $(BINDIR) 2>/dev/null || true

clean-deps:
ifeq ($(PLATFORM),linux)
	@echo "Removing SDL3 dependencies..."
	sudo pacman -R sdl3 --noconfirm 2>/dev/null || true
	sudo $(RM) -f /usr/lib/libSDL3_ttf.so* /usr/lib/libSDL3_ttf.a /usr/lib/pkgconfig/SDL3_ttf.pc
	sudo $(RMDIR) /usr/include/SDL3_ttf/ 2>/dev/null || true
	sudo ldconfig
	@echo "Dependencies removed!"
endif

clean-cache:
	$(RMDIR) $(DLL_CACHE) 2>/dev/null || true

clean-all: clean clean-cache

rebuild: clean all

# Help
help:
	@echo "GlaMaC Build System"
	@echo ""
	@echo "Build targets:"
	@echo "  all       - Build for current platform"
	@echo "  glamac    - Build main application only"
	@echo "  glautils  - Build utilities only"
	@echo "  clean     - Clean build files"
	@echo "  rebuild   - Clean and rebuild"
ifeq ($(PLATFORM),linux)
	@echo ""
	@echo "Cross-compilation (Linux → Windows):"
	@echo "  win       - Build glamac for Windows"
	@echo "  win-all   - Build everything for Windows"
	@echo "  setup-cross - Setup cross-compilation"
endif
	@echo ""
	@echo "Dependencies:"
	@echo "  deps      - Install SDL3 dependencies"
	@echo "  clean-deps - Remove SDL3 dependencies"
	@echo ""
	@echo "Glass catalog conversion:"
	@echo "  convert-catalogs - Convert Excel files to JSON"
	@echo ""
	@echo "Quick start:"
ifeq ($(PLATFORM),linux)
	@echo "  make deps && make all    # Native build"
	@echo "  make setup-cross && make win  # Windows build"
else
	@echo "  Install SDL3, then: make all"
endif

.PHONY: all glamac glautils win win-all win-dlls setup-cross convert-catalogs deps clean clean-deps clean-cache clean-all rebuild help
Back to https://optics-design.com