summaryrefslogtreecommitdiff
path: root/makefiles/Makefile.tools
blob: 072d2991070cfe6ee32bdc33332c0e98105606f9 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Tools Makefile fragment
#
# Expected inputs:
#
# COMPONENT_TYPE	Type of component:
# 				binary		-	Executable binary
# 				lib-static	-	Static library
# 				lib-shared	-	Shared library
# 				riscos-module	-	RISC OS module
#
# Optional inputs:
#
# BUILD			Type of build to perform:
# 				release		-	Release build (default)
# 				debug		-	Debug build
# OPTCFLAGS		Optional C compiler flags for $(BUILD)
# OPTCXXFLAGS		Optional C++ compiler flags for $(BUILD)
# OPTLDFLAGS		Optional linker flags for $(BUILD)
# TARGET		Target platform (defaults to host)
#

###############################################################################
# Sanity checks
###############################################################################

ifeq ($(COMPONENT_TYPE),)
  $(error COMPONENT_TYPE not set)
endif

# Default build to release
ifeq ($(BUILD),)
  BUILD := release
endif

###############################################################################
# Host/target platform detection
###############################################################################

# Autodetect host if necessary
ifeq ($(HOST),)
  HOST := $(shell uname -s)
endif

# Simple host sanitisation
ifeq ($(HOST),)
  # Don't ask
  HOST := riscos
else
  ifeq ($(HOST),RISC OS)
    HOST := riscos
  endif

  ifeq ($(HOST),BeOS)
    HOST := beos
  endif
  ifeq ($(HOST),Haiku)
    HOST := haiku
  endif
endif

ifeq ($(TARGET),)
  # Default target to host. Please add exceptions as required.
  TARGET := $(HOST)

  ifeq ($(HOST),haiku)
    # This isn't necessarily correct -- they have differences. However, in the 
    # general case, this will work. If there are differences that are actually 
    # important wrt the target platform (as opposed to the build host) then 
    # we'll just have to introduce a haiku target, too.
    TARGET := beos
  endif
endif

# Sanitise HOST and TARGET
# TODO: Ideally, we want the equivalent of s/[^A-Za-z0-9]/_/g here
HOST := $(subst .,_,$(subst -,_,$(subst /,_,$(HOST))))
TARGET := $(subst .,_,$(subst -,_,$(subst /,_,$(TARGET))))

# Now setup our tooling
ifeq ($(TARGET),riscos)
  ifeq ($(HOST),riscos)
    # Building on native RISC OS
    GCCSDK_INSTALL_ENV ?= <NSLibs$$Dir>

    CC__ := gcc
    CXX__ := g++
    CMHG ?= cmunge
    GENHTML ?= echo
    LCOV ?= echo
    PKGCONFIG ?=

    # This is nasty, but needed because $(CURDIR) will 
    # contain colons, and thus confuse make mightily
    $(shell SetMacro Alias$$$(COMPONENT)pwd Set %0 <FileSwitch$$CurrentFilingSystem>:|<FileSwitch$$<FileSwitch$$CurrentFilingSystem>$$CSD>|mUnset Alias$$$(COMPONENT)pwd)
    $(shell $(COMPONENT)pwd $(COMPONENT)$$Dir)
    CURDIR := <$(COMPONENT)$$Dir>
  else
    # Cross compiling for RISC OS
    GCCSDK_INSTALL_ENV ?= /home/riscos/env
    GCCSDK_INSTALL_CROSSBIN ?= /home/riscos/cross/bin

    AR__ := $(wildcard $(GCCSDK_INSTALL_CROSSBIN)/*ar)
    CC__ := $(wildcard $(GCCSDK_INSTALL_CROSSBIN)/*gcc)
    CXX__ := $(wildcard $(GCCSDK_INSTALL_CROSSBIN)/*g++)
    CMHG ?= PATH="$(GCCSDK_INSTALL_CROSSBIN):$(PATH)" $(GCCSDK_INSTALL_CROSSBIN)/cmunge
    GENHTML ?= echo
    LCOV ?= echo
    PKGCONFIG ?= $(GCCSDK_INSTALL_ENV)/ro-pkg-config

    ifneq ($(COMPONENT_TYPE),riscos-module)
      ifeq ($(origin CC),default)
        ifneq ($(findstring arm-unknown-riscos-gcc,$(CC__)),)
          EXEEXT := ,e1f
        else
          EXEEXT := ,ff8
        endif
      else
        ifneq ($(findstring arm-unknown-riscos-gcc,$(CC)),)
          EXEEXT := ,e1f
        else
          EXEEXT := ,ff8
        endif
      endif
    else
      EXEEXT := ,ffa
    endif
  endif

  CFLAGS := $(CFLAGS) -mpoke-function-name -I$(GCCSDK_INSTALL_ENV)/include
  CXXFLAGS := $(CXXFLAGS) -mpoke-function-name -I$(GCCSDK_INSTALL_ENV)/include
  LDFLAGS := $(LDFLAGS) -L$(GCCSDK_INSTALL_ENV)/lib

  CMHGFLAGS := -p -tgcc -32bit -apcs 3/32/nonreent/fpe2/noswst/nofpr/nofp

  # Default prefix
  PREFIX ?= $(GCCSDK_INSTALL_ENV)
endif

# BeOS-like targets
ifeq ($(TARGET),beos)
  ifeq ($(HOST),beos)
    # Building on BeOS
    CC__ := gcc

    # No pkg-config
    PKGCONFIG ?=

    # Default prefix
    PREFIX ?= /boot/home/config
  else
    ifeq ($(HOST),haiku)
      # Building on Haiku

      # Default prefix
      PREFIX ?= /boot/common
    else
      $(error Cross-compiling for BeOS is not supported)
    endif
  endif
endif

# Windows
ifeq ($(TARGET),windows)
  ifeq ($(HOST),windows)
    $(error Compiling on Windows is not supported)
  else
    # Cross compiling for Windows -- assumes mingw toolchain
    MINGW_INSTALL_ENV ?= /usr/local/mingw

    CC__ := i586-mingw32msvc-gcc
    CXX__ := i586-mingw32msvc-g++
    AR__ := i586-mingw32msvc-ar

    PKGCONFIG ?= $(MINGW_INSTALL_ENV)/bin/win-pkg-config
  endif

  CFLAGS := $(CFLAGS) -U__STRICT_ANSI__ -I$(MINGW_INSTALL_ENV)/include
  CXXFLAGS := $(CXXFLAGS) -U__STRICT_ANSI__ -I$(MINGW_INSTALL_ENV)/include
  LDFLAGS := $(LDFLAGS) -L$(MINGW_INSTALL_ENV)/lib

  # Default prefix
  PREFIX ?= $(MINGW_INSTALL_ENV)
endif

###############################################################################
# Tool defaults
###############################################################################

CP ?= cp

DOXYGEN ?= doxygen

ECHO ?= echo

GENHTML ?= genhtml

ifeq ($(HOST),$(TARGET))
  HOST_CC ?= $(CC)

  HOST_CXX ?= $(CXX)
else
  HOST_CC ?= cc

  HOST_CXX ?= c++
endif

INSTALL ?= install

LCOV ?= lcov

LN ?= ln

MAKE ?= make

MKDIR ?= mkdir
MKDIRFLAGS ?= -p

MV ?= mv

PERL ?= perl

PKGCONFIG ?= pkg-config

SED ?= sed

TOUCH ?= touch

XSLTPROC ?= xsltproc

###############################################################################
# Default assembler/compiler/linker/archiver flags
###############################################################################

ifeq ($(BUILD),release)
  OPTCFLAGS ?= -DNDEBUG -O2
  OPTCXXFLAGS ?= -DNDEBUG -O2
else
  OPTCFLAGS ?= -g -O0
  OPTCXXFLAGS ?= -g -O0
  OPTLDFLAGS ?= -g
endif

ifeq ($(origin ARFLAGS),default)
  ARFLAGS := cru
endif

ASFLAGS ?= -xassembler-with-cpp

CFLAGS := $(CFLAGS) $(OPTCFLAGS) -DBUILD_TARGET_$(TARGET) -DBUILD_HOST_$(HOST)
CXXFLAGS := $(CXXFLAGS) $(OPTCXXFLAGS) \
		-DBUILD_TARGET_$(TARGET) -DBUILD_HOST_$(HOST)

ASFLAGS := $(ASFLAGS) $(CFLAGS)
LDFLAGS := $(LDFLAGS) $(OPTLDFLAGS)

# Extensions for coverage target
ifeq ($(MAKECMDGOALS),coverage)
  COVCFLAGS ?= -fprofile-arcs -ftest-coverage
  COVCXXFLAGS ?= -fprofile-arcs -ftest-coverage
  COVLDFLAGS ?= -lgcov

  CFLAGS := $(CFLAGS) $(COVCFLAGS)
  CXXFLAGS := $(CXXFLAGS) $(COVCXXFLAGS)
  LDFLAGS := $(LDFLAGS) $(COVLDFLAGS)
endif

# Extensions for profile target
ifeq ($(MAKECMDGOALS),profile)
  PROFCFLAGS ?= -pg
  PROFCXXFLAGS ?= -pg
  PROFLDFLAGS ?= -pg

  CFLAGS := $(CFLAGS) $(PROFCFLAGS)
  CXXFLAGS := $(CXXFLAGS) $(PROFCXXFLAGS)
  LDFLAGS := $(LDFLAGS) $(PROFLDFLAGS)
endif

###############################################################################
# lib-shared defaults
###############################################################################

# Default library extension
ifeq ($(COMPONENT_TYPE),lib-static)
  LIBEXT ?= .a
else
  LIBEXT ?= .so
endif

# If we're building a shared library, modify the flags appropriately
ifeq ($(COMPONENT_TYPE),lib-shared)
  # Default CFLAGS/LDFLAGS for shared libraries
  SHAREDCFLAGS ?= -fPIC -DPIC
  SHAREDCXXFLAGS ?= -fPIC -DPIC
  SHAREDLDFLAGS ?= -shared -Wl,-soname,$(SONAME)
  SHAREDLDPATH ?= LD_LIBRARY_PATH="$(BUILDDIR):$(LD_LIBRARY_PATH)"
endif

###############################################################################
# RISC OS module extensions
###############################################################################

ifeq ($(COMPONENT_TYPE),riscos-module)
  ifneq ($(TARGET),riscos)
    $(error Attempting to build a RISC OS module for a non-RISC OS target)
  endif

  CFLAGS := $(CFLAGS) -mmodule
  CXXFLAGS := $(CXXFLAGS) -mmodule
  LDFLAGS := $(LDFLAGS) -mmodule
endif

###############################################################################
# Override defaulted tools
###############################################################################

# CC
ifeq ($(findstring ccc-analyzer,$(CC)),ccc-analyzer)
    # We're being invoked by scan-build, so export 
    # the compiler we would have used such that
    # scan-build works with cross-compilation.
    # There's no need to do this if we would have
    # used the default compiler.
    ifdef CC__
      export CCC_CC := $(CC__)
    endif
else
  # Only set CC if it's not already set in the 
  # environment and we have a value for it. 
  # Otherwise, leave it to be defaulted.
  ifeq ($(origin CC),default)
    ifdef CC__
      CC := $(CC__)
    endif
  endif
endif

# CXX
ifeq ($(origin CXX),default)
  ifdef CXX__
    CXX := $(CXX__)
  endif
endif

# AR
ifeq ($(origin AR),default)
  ifdef AR__
    AR := $(AR__)
  endif
endif

###############################################################################
# Other settings
###############################################################################

# Determine if the compiler is GCC and if it supports simultaneous build & dep.
ccvsn := $(shell $(CC) --version)
ifeq ($(ccvsn),)
  # Version string is blank
  ifeq ($(HOST),riscos)
    # For some reason we never see the output of SCL apps, so assume Norcroft.
    ccvsn := Norcroft
  else
    # Give up
    $(error Failed to determine compiler and version)
  endif
endif

# "<binary name> (GCC) x.y.z (foo bar baz)"
ifeq ($(word 2,$(ccvsn)),(GCC))
  # Looks like GCC, look for version
  # If the major version (x, above) is not 2, then assume build & dep.
  # This will break if using a version of GCC < 2, but that's unlikely.
  GCCVER := $(word 1,$(subst ., ,$(word 3, $(ccvsn))))
  ifneq ($(GCCVER),2)
    CC_CAN_BUILD_AND_DEP ?= yes
  endif
else
  # Older versions of GCC just output the version number, so examine the 
  # binary name in the hope of detecting more GCC.
  ifeq ($(findstring gcc,$(CC)),gcc)
    GCCVER := $(word 1,$(subst ., ,$(ccvsn)))
    ifneq ($(GCCVER),2)
      CC_CAN_BUILD_AND_DEP ?= yes
    endif
  else
    # Not GCC -- assume inability to dep
    CC_CANNOT_DEP ?= yes
  endif
endif