summaryrefslogtreecommitdiff
path: root/Makefile
blob: dc5f7507f6bd78b5ae4a8e046b047b5b5b6d7360 (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
CC=gcc
# Cross-compilation (only used on Linux)
MINGW_CC=x86_64-w64-mingw32-gcc

#Base Directories
SRCDIR=src
BINDIR=bin
BINDIR_WIN=bin/win
INCDIR=include

#Common flags
CFLAGS_BASE=-I$(INCDIR) -O2 -flto
CFLAGS_NATIVE=$(CFLAGS_BASE) -march=native #-ffast-math
CFLAGS_PORTABLE=$(CFLAGS_BASE) #-ffast-math (no -march=native for portability)

# OS Detection
ifeq ($(OS),Windows_NT)
    # Windows host
    EXE_EXT=.exe
    MKDIR=mkdir
    RM=del /Q
    LDFLAGS=
    TREE_CMD=powershell -Command "(Get-Item .).Name | Set-Content structure.txt; (tree /a /f | Select-Object -Skip 3) | Add-Content structure.txt"
    
    # On Windows: compile Windows binaries without -march=native for portability
    NATIVE_CC=$(CC)
    NATIVE_CFLAGS=$(CFLAGS_PORTABLE)
    NATIVE_LDFLAGS=$(LDFLAGS)
    NATIVE_EXT=.exe
else
    # Linux host
    EXE_EXT=
    MKDIR=mkdir -p
    RM=rm -f
    LDFLAGS=-lm
    TREE_CMD=basename $(PWD) > structure.txt; tree -a -F >> structure.txt 2>/dev/null || (find . -type f -name "*.c" -o -name "*.h" -o -name "Makefile" | sort >> structure.txt)
    
    # On Linux: compile Linux binaries with -march=native for performance
    NATIVE_CC=$(CC)
    NATIVE_CFLAGS=$(CFLAGS_NATIVE)
    NATIVE_LDFLAGS=$(LDFLAGS)
    NATIVE_EXT=
endif

#tolutils
TOL_SRCS = $(wildcard $(SRCDIR)/*.c)
TOL_EXES_NATIVE = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%$(NATIVE_EXT), $(TOL_SRCS))
TOL_EXES_WIN = $(patsubst $(SRCDIR)/%.c, $(BINDIR_WIN)/%.exe, $(TOL_SRCS))

# Setup target
setup:
	$(MKDIR) $(BINDIR) $(INCDIR)

setup-cross:
	$(MKDIR) $(BINDIR) $(BINDIR_WIN) $(INCDIR)

# Default: build for current platform
all: setup tolsac structure

# Build for current platform (native)
tolsac: $(TOL_EXES_NATIVE)

# Native compilation rule
$(BINDIR)/%$(NATIVE_EXT): $(SRCDIR)/%.c
	$(NATIVE_CC) $< $(NATIVE_CFLAGS) -o $@ $(NATIVE_LDFLAGS)

# Cross-compilation targets (Linux host only)
ifneq ($(OS),Windows_NT)
tolsac-win: setup-cross $(TOL_EXES_WIN)
tolsac-all: tolsac tolsac-win
rebuild-win: clean tolsac-win
rebuild-all: clean tolsac-all

$(BINDIR_WIN)/%.exe: $(SRCDIR)/%.c
	$(MINGW_CC) $< $(CFLAGS_PORTABLE) -o $@
else
# On Windows, provide helpful messages for cross-compilation targets
tolsac-win:
	@echo "Cross-compilation to Windows not needed on Windows host. Use 'make tolsac' instead."

tolsac-all: tolsac
	@echo "On Windows host: built Windows binaries (cross-compilation not applicable)."

rebuild-win: rebuild
	@echo "On Windows host: rebuilt Windows binaries."

rebuild-all: rebuild
	@echo "On Windows host: rebuilt Windows binaries."
endif

clean:
	$(RM) $(BINDIR)/*
ifneq ($(OS),Windows_NT)
	$(RM) $(BINDIR_WIN)/*
endif
	$(RM) structure.txt

rebuild: clean all

# Generate project structure
structure:
	@$(TREE_CMD)

.PHONY: all setup setup-cross tolsac tolsac-win tolsac-all clean rebuild rebuild-win rebuild-all structure
Back to https://optics-design.com