-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
172 lines (138 loc) · 4.76 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
172
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vsteffen <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/02/06 20:52:05 by vsteffen #+# #+# #
# Updated: 2018/03/20 18:17:27 by vsteffen ### ########.fr #
# #
# **************************************************************************** #
PROJECT = woody_woodpacker
NAME = woody_woodpacker
NEW_BIN = woody
CC = /usr/bin/clang
RM = /bin/rm
MAKE = /usr/bin/make
MKDIR = /bin/mkdir -p
AR = /usr/bin/ar
RANLIB = /usr/bin/ranlib
GIT = /usr/bin/git
NASM = /usr/bin/nasm
OBJCOPY = /usr/bin/objcopy
HEXDUMP = /usr/bin/hexdump
OBJ = $(patsubst %.c, $(OPATH)/%.o, $(SRC))
CFLAGS = -Wall -Wextra -Werror -g #-fsanitize=address
ifeq ($(with-asm),y)
WITH-ASM:= with-asm
endif
LIB = $(ROOT)/lib
LIBSRCS = $(ROOT)/libsrcs
LIBFT = $(LIBSRCS)/libft
ROOT = $(shell /bin/pwd)
OPATH = $(ROOT)/objs
CPATH = $(ROOT)/srcs
LPATH = $(LIBFT)/libft.a
HPATH = -I $(ROOT)/includes -I $(LIBFT)/includes
SRC = woody_woodpacker.c \
debug.c \
elf.c \
elf_read.c \
elf_write.c \
elf_modify.c \
elf_save.c
ASMPATH = $(ROOT)/asm
ASM_SRC = $(ASMPATH)/unpacker.asm
ASM_OBJ = $(ASMPATH)/unpacker.o
ASM_BYTECODE = $(ASMPATH)/bytecode
PRE_CHECK_SUB_LIBFT := $(LIBFT)/Makefile
PRE_CHECK_SUB := $(PRE_CHECK_SUB_LIBFT)
PRE_CHECK_LIB_LIBFT := $(LIBFT)/libft.a
PRE_CHECK_LIB := $(PRE_CHECK_LIB_LIBFT) $(PRE_CHECK_LIB_LULZ)
COMPILE = no
OS := $(shell uname -s)
ifeq ($(OS),Darwin)
NPROCS:=$(shell sysctl -n hw.ncpu)
else
NPROCS:=$(shell grep -c ^processor /proc/cpuinfo)
endif
define PRINT_RED
printf "\033[31m$(1)\033[0m"
endef
define PRINT_GREEN
printf "\033[32m$(1)\033[0m"
endef
define PRINT_YELLOW
printf "\033[33m$(1)\033[0m"
endef
define PRINT_STATUS
printf '['
$(if $(filter $(2),SUCCESS),$(call PRINT_GREEN,$(1)))
$(if $(filter $(2),FAIL),$(call PRINT_RED,$(1)))
$(if $(filter $(2),WARN),$(call PRINT_YELLOW,$(1)))
$(if $(filter $(2),INFO),printf $(1))
$(if $(filter $(3),-n),printf $(1),echo ']')
endef
.PHONY: all clean fclean re lib-clean lib-update
.SILENT: $(PRE_CHECK_SUB) $(PRE_CHECK_LIB) $(NAME) $(OPATH) $(ASM_OBJ) $(OPATH)/%.o clean fclean re lib-clean lib-update
all: $(NAME)
$(PRE_CHECK_SUB):
@echo $(PROJECT)": Init submodule $(dir $@) ... "
@$(GIT) submodule update --init --recursive > /dev/null # can't directly redirect stdout on /dev/null cause of sync wait on Linux
@printf $(PROJECT)": $(dir $@) "
@$(call PRINT_STATUS,INITIALIZED,SUCCESS)
$(PRE_CHECK_LIB): $(PRE_CHECK_SUB)
echo $(PROJECT)": Compile $@ ... " ; \
if [ $@ = $(PRE_CHECK_LIB_LIBFT) ] ; then \
if [ $(OS) = "Darwin" ] ; then \
$(MAKE) -C $(LIBFT) $(WITH-ASM) -j$(NPROCS) > /dev/null ; \
else \
$(MAKE) -C $(LIBFT) -j$(NPROCS) ; \
fi; \
else \
echo "Other libraries here" ; \
fi; \
printf $(PROJECT)": $@ " ; \
$(call PRINT_STATUS,COMPILED,SUCCESS) ;
$(ASM_OBJ):
$(NASM) -f elf64 $(ASM_SRC) -o $(ASM_OBJ)
$(OBJCOPY) -O binary -j .text $(ASM_OBJ) $(ASM_BYTECODE)
$(NAME): $(PRE_CHECK_LIB) $(ASM_OBJ) $(OPATH) $(OBJ)
$(if $(filter $(COMPILE),yes),echo ']')
printf $(PROJECT)": Building $@ ... "
$(CC) -o $@ $(CFLAGS) $(OBJ) $(LPATH) $(HPATH) $(ASM_OBJ)
$(call PRINT_STATUS,DONE,SUCCESS)
$(OPATH)/%.o: $(CPATH)/%.c | $(PRE_CHECK_LIB)
$(if $(filter $(COMPILE),no),@printf $(PROJECT)': Files compiling [')
$(eval COMPILE := yes)
@$(CC) $(CFLAGS) -c $< -o $@ $(HPATH) -DBYTECODE=\"`$(HEXDUMP) -v -e '"\\\\x" 1/1 "%02X"' $(ASM_BYTECODE)`\"
@$(call PRINT_GREEN,.)
$(OPATH):
echo $(PROJECT)": Creation of objects directory"
$(MKDIR) $@
clean:
$(RM) -Rf $(OPATH)
$(RM) -f $(ASM_OBJ)
echo $(PROJECT)": Objects cleaned "
printf $(PROJECT)": $@ rule "
$(call PRINT_STATUS,DONE,SUCCESS)
fclean: clean
$(RM) -f $(NAME)
$(RM) -f $(NEW_BIN)
$(RM) -f $(ASM_BYTECODE)
echo $(PROJECT)": executable clean"
printf $(PROJECT)": $@ rule "
$(call PRINT_STATUS,DONE,SUCCESS)
re: fclean
$(MAKE) -C $(ROOT) -j$(NPROCS) -s
lib-clean:
echo $(PROJECT)": cleaning libraries ..."
-$(MAKE) -C $(LIBFT) fclean -j$(NPROCS) > /dev/null
printf $(PROJECT)": $@ rule "
$(call PRINT_STATUS,DONE,SUCCESS)
lib-update:
echo $(PROJECT)": Update submodules ... "
$(GIT) submodule update --init --recursive --remote > /dev/null
printf $(PROJECT)": submodules "
$(call PRINT_STATUS,UPDATED,SUCCESS)