summaryrefslogtreecommitdiffstats
path: root/Makefile.global
blob: 60a2c485d8828d41accaf05897454d1ab0637431 (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
#-*- mode: makefile; indent-tabs-mode: t; coding: utf-8; show-trailing-whitespace: t -*-
# Makefile.global
# Copyright 2022 Angelo Rossi <angelo.rossi.homelab@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#
# Get the operating system name.  If this is Cygwin, the .d files will be
# munged to convert c: into /cygdrive/c so that "make" will be happy with the
# auto-generated dependencies.
#

# The command for calling the compiler.
ifeq (${shell uname},linux)
	AS=${shell which as}
	CC=${shell which gcc}
	CXX=${shell which g++}
	LD=${shell which ld}
	OBJCOPY=${shell which objcopy}
	OBJDUMP=${shell which objdump}
	AR=${shell which ar}
	NM=${shell which nm}
else
	ifeq (${shell uname},OpenBSD)
		AS=${shell which gas}
		CC=${shell which egcc}
		CXX=${shell which eg++}
		LD=${shell which ld}
		OBJCOPY=${shell which objcopy}
		OBJDUMP=${shell which objdump}
		AR=${shell which egcc-ar}
		NM=${shell which egcc-nm}
	endif
endif
LINKER=${CXX}

# The flags passed to the assembler.

AS_FLAGS=

ifdef ARCH
	AS_FLAGS+=-march=${ARCH}
endif

# The flags passed to the c compiler.

CC_FLAGS=-O0				\
	 -Wall				\
	 -std=c99			\
         -pthread                       \
	 -c

# The flags passed to the c++ compiler.

CXX_FLAGS=-O0				        \
	  -Wall				        \
	  -std=gnu++11			        \
          -pthread                              \
	  -c

ifdef WITH_OVER1
CXX_FLAGS+=-DWITH_OVER1
endif
ifdef WITH_OVER20
CXX_FLAGS+=-DWITH_OVER20
endif
ifdef WITH_OVER51
CXX_FLAGS+=-DWITH_OVER51
endif


# The flags passed to the linker.
LD_SCRIPT_FLAGS=
ifneq (${LINKER_SCRIPT},)
	LD_SCRIPT_FLAGS+=-T ${SCRIPTS_PATH}/${LINKER_SCRIPT}.lnk
endif

# Specs files.
SPECS_FILES=

ifneq (${SPECS_FILES},)
	SPECS_FLAGS=${patsubst %,--specs=%,${subst :, ,${SPECS_FILES}}}
else
	SPECS_FLAGS=
endif

LD_FLAGS=

LD_FLAGS+=-Wl,--start-group			\
          ${LD_SCRIPT_FLAGS}			\
	  ${SPECS_FLAGS}			\
	  -Wl,-Map=${TOOLCHAIN}/${TARGET}.map	\
	  -Wl,--end-group

# ar most common flags.
AR_FLAGS=-cr

# objdump most common flags.
OBJDUMP_FLAGS=	-h	\
		-EL	\
		-s	\
		-S	\
		-d

# objcopy most common flags.
OBJCOPY_FLAGS=	-S		\
		-R .data

# Get the location of libgcc.a, libc.a, libstdc++ and
# libm.a from the GCC front-end.
LIBGCC=${shell ${CC} ${CC_FLAGS} -print-libgcc-file-name}
LIBC=${shell ${CC} ${CC_FLAGS} -print-file-name=libc.a}
LIBM=${shell ${CC} ${CC_FLAGS} -print-file-name=libm.a}
LIBPTHREAD=${shell ${CC} ${CC_FLAGS} --print-file-name=libpthread.a}
LIBSTDCXX=${shell ${CXX} ${CXX_FLAGS} --print-file-name=libstdc++.a}

# Libraries to link.
LIBRARIES=pthread

# Tell the compiler to include debugging information if the DEBUG environment
# variable is set.
ifneq (${DEBUG},)
	AS_FLAGS+=-ggdb3
	CC_FLAGS+=-ggdb3 -DDEBUG
	CXX_FLAGS+=-ggdb3 -DDEBUG
	LD_FLAGS+=-ggdb3
endif

# Add the include file paths to AS_FLAGS, CC_FLAGS and CXX_FLAGS.
AS_FLAGS+=${patsubst %,-I%,${subst :, ,${INCLUDES_PATH}}}
CC_FLAGS+=${patsubst %,-I%,${subst :, ,${INCLUDES_PATH}}}
CXX_FLAGS+=${patsubst %,-I%,${subst :, ,${INCLUDES_PATH}}}

LD_FLAGS+=-pthread

ifneq (${LIBRARIES_PATH},)
LD_FLAGS+=${patsubst %,-L%,${subst :, ,${LIBRARIES_PATH}}}
endif

ifneq (${LIBRARIES},)
LD_FLAGS+=${patsubst %,-l%,${subst :, ,${LIBRARIES}}}
endif

# Rule for building the object file from each assembly source files.
${TOOLCHAIN}/%.o: %.S
	@if [ 'x${VERBOSE}' = x ];			\
	then						\
		echo " [ AS ] ${<}";			\
	else						\
		echo "${AS} ${AS_FLAGS}  ${<} -o ${@}";	\
	fi
	@${AS} ${AS_FLAGS} ${<} -o ${@}

# Rule for building the object file from each C source file.

${TOOLCHAIN}/%.o: %.c
	@if [ 'x${VERBOSE}' = x ];                      \
	 then                                           \
	     echo " [ CC ] ${<}";                       \
	 else                               		\
	     echo ${CC} ${CC_FLAGS} -o ${@} ${<}; 	\
	 fi
	@${CC} ${CC_FLAGS} -o ${@} ${<}

${TOOLCHAIN}/%.o: %.cpp
	@if [ 'x${VERBOSE}' = x ];                      \
	 then                                           \
	     echo " [ CPP ] ${<}";                      \
	 else                                           \
	     echo ${CXX} ${CXX_FLAGS} -o ${@} ${<};	\
	 fi
	@${CXX} ${CXX_FLAGS} -o ${@} ${<}

# Rule for creating an object library.
${TOOLCHAIN}/%.a:
	@if [ 'x${VERBOSE}' = x ];     		\
	 then                          		\
	     echo " [ AR ] ${@}";      		\
	 else                          		\
	     echo ${AR} ${AR_FLAGS} ${@} ${^}; 	\
	 fi
	@${AR} ${AR_FLAGS} ${@} ${^}

# Rule for linking the application.
${TOOLCHAIN}/%:
	@if [ 'x${VERBOSE}' = x ];			\
	then						\
		echo " [ LD ] ${@}";			\
	else						\
		echo ${LINKER} $(filter %.o %.a, ${^})	\
                               ${LD_FLAGS}              \
                               -o ${@};                 \
	fi
	@${LINKER} $(filter %.o %.a, ${^})      \
                   ${LD_FLAGS}                  \
                   -o ${@}

# Rule to create a .lst file.
${TOOLCHAIN}/%.lst:
	@if [ 'x${VERBOSE}' = x ];							\
	then										\
		echo " [ OBJDUMP ] ${@}";						\
	else										\
		echo "${OBJDUMP} ${OBJDUMP_FLAGS} ${TOOLCHAIN}/${TARGET} > ${@}";	\
	fi
	@${OBJDUMP} ${OBJDUMP_FLAGS} ${TOOLCHAIN}/${TARGET} > ${@}