-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
171 lines (141 loc) · 4.43 KB
/
Makefile
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
# Vulcalien's Executable Makefile
# version 0.3.0
# === Detect OS ===
ifeq ($(OS),Windows_NT)
CURRENT_OS := WINDOWS
else
CURRENT_OS := UNIX
endif
TARGET_OS := $(CURRENT_OS)
# === Basic Info ===
OUT_FILENAME := luag-console
SRC_DIRS := src src/data-structs
OBJ_DIR := obj
BIN_DIR := bin
# === C Flags ===
CPPFLAGS := -Iinclude -MMD -MP
CFLAGS := -Wall -pedantic -Wno-format-truncation
# list libraries and add C flags for dependencies
ifeq ($(TARGET_OS),UNIX)
# UNIX
LIBS := sdl2 SDL2_image SDL2_mixer lua5.4 libarchive
CPPFLAGS += `pkg-config --cflags-only-I $(LIBS)`
CFLAGS += `pkg-config --cflags-only-other $(LIBS)`
else ifeq ($(TARGET_OS),WINDOWS)
ifeq ($(CURRENT_OS),WINDOWS)
# WINDOWS
CPPFLAGS +=
CFLAGS +=
else ifeq ($(CURRENT_OS),UNIX)
# UNIX to WINDOWS cross-compile
WIN_CC_DIR := win-cc/lib
CPPFLAGS += -I$(WIN_CC_DIR)/SDL2/x86_64-w64-mingw32/include/SDL2 \
-I$(WIN_CC_DIR)/SDL2_image/x86_64-w64-mingw32/include/SDL2 \
-I$(WIN_CC_DIR)/SDL2_mixer/x86_64-w64-mingw32/include/SDL2 \
-I$(WIN_CC_DIR)/libarchive/include \
-I$(WIN_CC_DIR)/lua54/include \
-I$(WIN_CC_DIR)/dlfcn-win32
CFLAGS += `$(WIN_CC_DIR)/SDL2/x86_64-w64-mingw32/bin/sdl2-config --cflags`
endif
endif
# === Linker Flags ===
ifeq ($(TARGET_OS),UNIX)
ifdef LINK_STATIC
LDFLAGS := -static -rdynamic\
`pkg-config --static --libs-only-L $(LIBS)`
LDLIBS := `pkg-config --static --libs-only-l $(LIBS)` \
-lpthread -ldl
else
LDFLAGS := -rdynamic\
`pkg-config --libs-only-L $(LIBS)`
LDLIBS := `pkg-config --libs-only-l $(LIBS)` \
-lpthread -ldl
endif
else ifeq ($(TARGET_OS),WINDOWS)
ifeq ($(CURRENT_OS),WINDOWS)
# WINDOWS
LDFLAGS :=
LDLIBS :=
else ifeq ($(CURRENT_OS),UNIX)
# UNIX to WINDOWS cross-compile
LDFLAGS := -Wl,--export-all-symbols \
-Wl,--out-implib,$(BIN_DIR)/$(OUT_FILENAME).lib \
-L$(WIN_CC_DIR)/SDL2/x86_64-w64-mingw32/bin \
-L$(WIN_CC_DIR)/SDL2/x86_64-w64-mingw32/lib \
-L$(WIN_CC_DIR)/SDL2_image/x86_64-w64-mingw32/bin \
-L$(WIN_CC_DIR)/SDL2_image/x86_64-w64-mingw32/lib \
-L$(WIN_CC_DIR)/SDL2_mixer/x86_64-w64-mingw32/bin \
-L$(WIN_CC_DIR)/SDL2_mixer/x86_64-w64-mingw32/lib \
-L$(WIN_CC_DIR)/libarchive/bin \
-L$(WIN_CC_DIR)/libarchive/lib \
-L$(WIN_CC_DIR)/lua54 \
-L$(WIN_CC_DIR)/dlfcn-win32
LDLIBS := -lSDL2_image -lSDL2_mixer -ldlfcn -llua54 -larchive \
`$(WIN_CC_DIR)/SDL2/x86_64-w64-mingw32/bin/sdl2-config --libs` \
-lpthread
endif
endif
# === Compilers ===
ifeq ($(TARGET_OS),UNIX)
# UNIX
CC := gcc
else ifeq ($(TARGET_OS),WINDOWS)
ifeq ($(CURRENT_OS),WINDOWS)
# WINDOWS
CC := gcc
else ifeq ($(CURRENT_OS),UNIX)
# UNIX to WINDOWS cross-compile
CC := x86_64-w64-mingw32-gcc
endif
endif
# === OS Specific ===
ifeq ($(TARGET_OS),UNIX)
OBJ_EXT := o
OUT_SUFFIX :=
else ifeq ($(TARGET_OS),WINDOWS)
OBJ_EXT := obj
OUT_SUFFIX := .exe
endif
ifeq ($(CURRENT_OS),UNIX)
MKDIR := mkdir
MKDIRFLAGS := -p
RM := rm
RMFLAGS := -rfv
else ifeq ($(CURRENT_OS),WINDOWS)
MKDIR := mkdir
MKDIRFLAGS :=
RM := rmdir
RMFLAGS := /Q /S
endif
# === Resources ===
# list of source file extensions
SRC_EXT := c
# list of source files
SRC := $(foreach DIR,$(SRC_DIRS),\
$(foreach EXT,$(SRC_EXT),\
$(wildcard $(DIR)/*.$(EXT))))
# list of object files
OBJ := $(SRC:%=$(OBJ_DIR)/%.$(OBJ_EXT))
# list of object directories
OBJ_DIRS := $(SRC_DIRS:%=$(OBJ_DIR)/%)
# output file
OUT := $(BIN_DIR)/$(OUT_FILENAME)$(OUT_SUFFIX)
# === Targets ===
.PHONY: all run build clean
all: build run
run:
./$(OUT)
build: $(OUT)
clean:
@$(RM) $(RMFLAGS) $(BIN_DIR) $(OBJ_DIR)
# generate output file
$(OUT): $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
# compile .c files
$(OBJ_DIR)/%.c.$(OBJ_EXT): %.c | $(OBJ_DIRS)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# create directories
$(BIN_DIR) $(OBJ_DIRS):
$(MKDIR) $(MKDIRFLAGS) "$@"
-include $(OBJ:.$(OBJ_EXT)=.d)
-include install.mk