1#
2# This GNU make makefile has been tested on:
3#   Linux (x86 & Sparc)
4#   OS X
5#   Solaris (x86 & Sparc)
6#   OpenBSD
7#   NetBSD
8#   FreeBSD
9#   Windows (MinGW & cygwin)
10#   Linux x86 targeting Android (using agcc script)
11#
12# Android targeted builds should invoke GNU make with GCC=agcc on
13# the command line.
14#
15# In general, the logic below will detect and build with the available
16# features which the host build environment provides.
17#
18# Dynamic loading of libpcap is the default behavior if pcap.h is
19# available at build time.  Direct calls to libpcap can be enabled
20# if GNU make is invoked with USE_NETWORK=1 on the command line.
21#
22# The default build will build compiler optimized binaries.
23# If debugging is desired, then GNU make can be invoked with
24# DEBUG=1 on the command line.
25#
26# For linting (or other code analyzers) make may be invoked similar to:
27#
28#   make GCC=cppcheck CC_OUTSPEC= LDFLAGS= CFLAGS_G="--enable=all --template=gcc" CC_STD=--std=c99
29#
30# CC Command (and platform available options).  (Poor man's autoconf)
31#
32# building the pdp11, or any vax simulator could use networking support
33# No Asynch I/O support for now.
34NOASYNCH = 1
35BUILD_SINGLE := $(MAKECMDGOALS) $(BLANK_PREFIX)
36ifneq (,$(findstring all,$(MAKECMDGOALS)))
37  BUILD_MULTIPLE = s
38else
39  ifeq ($(MAKECMDGOALS),)
40    # default target is all
41    BUILD_MULTIPLE = s
42    BUILD_SINGLE := all $(BLANK_PREFIX)
43  endif
44endif
45ifeq ($(findstring Windows,$(OS)),Windows)
46  WIN32 = 1
47endif
48ifeq ($(WIN32),)  #*nix Environments (&& cygwin)
49  ifeq ($(GCC),)
50    GCC = gcc
51  endif
52  OSTYPE = $(shell uname)
53  # OSNAME is used in messages to indicate the source of libpcap components
54  OSNAME = $(OSTYPE)
55  ifeq (SunOS,$(OSTYPE))
56    TEST = /bin/test
57  else
58    TEST = test
59  endif
60  ifeq (CYGWIN,$(findstring CYGWIN,$(OSTYPE))) # uname returns CYGWIN_NT-n.n-ver
61    OSTYPE = cygwin
62    OSNAME = windows-build
63  endif
64  ifeq (,$(shell $(GCC) -v /dev/null 2>&1 | grep 'clang'))
65    GCC_VERSION = $(shell $(GCC) -v /dev/null 2>&1 | grep 'gcc version' | awk '{ print $$3 }')
66    COMPILER_NAME = GCC Version: $(GCC_VERSION)
67    ifeq (,$(GCC_VERSION))
68      ifeq (SunOS,$(OSTYPE))
69        ifneq (,$(shell $(GCC) -V 2>&1 | grep 'Sun C'))
70          SUNC_VERSION = $(shell $(GCC) -V 2>&1 | grep 'Sun C')
71          COMPILER_NAME = $(wordlist 2,10,$(SUNC_VERSION))
72          CC_STD = -std=c99
73        endif
74      endif
75      ifeq (HP-UX,$(OSTYPE))
76        ifneq (,$(shell what `which $(firstword $(GCC)) 2>&1`| grep -i compiler))
77          COMPILER_NAME = $(strip $(shell what `which $(firstword $(GCC)) 2>&1` | grep -i compiler))
78          CC_STD = -std=gnu99
79        endif
80      endif
81    else
82      ifeq (,$(findstring ++,$(GCC)))
83        CC_STD = -std=gnu99
84      else
85        CPP_BUILD = 1
86      endif
87    endif
88  else
89    NO_LTO = 1
90    OS_CCDEFS += -Wno-parentheses
91    ifeq (Apple,$(shell $(GCC) -v /dev/null 2>&1 | grep 'Apple' | awk '{ print $$1 }'))
92      COMPILER_NAME = $(shell $(GCC) -v /dev/null 2>&1 | grep 'Apple' | awk '{ print $$1 " " $$2 " " $$3 " " $$4 }')
93      CLANG_VERSION = $(word 4,$(COMPILER_NAME))
94    else
95      COMPILER_NAME = $(shell $(GCC) -v /dev/null 2>&1 | grep 'clang version' | awk '{ print $$1 " " $$2 " " $$3 }')
96      CLANG_VERSION = $(word 3,$(COMPILER_NAME))
97      ifeq (,$(findstring .,$(CLANG_VERSION)))
98        COMPILER_NAME = $(shell $(GCC) -v /dev/null 2>&1 | grep 'clang version' | awk '{ print $$1 " " $$2 " " $$3 " " $$4 }')
99        CLANG_VERSION = $(word 4,$(COMPILER_NAME))
100      endif
101    endif
102    ifeq (,$(findstring ++,$(GCC)))
103      CC_STD = -std=c99
104    else
105      CPP_BUILD = 1
106    endif
107  endif
108  LTO_EXCLUDE_VERSIONS =
109  PCAPLIB = pcap
110  ifeq (agcc,$(findstring agcc,$(GCC))) # Android target build?
111    OS_CCDEFS = -D_GNU_SOURCE
112    ifeq (,$(NOASYNCH))
113      OS_CCDEFS += -DSIM_ASYNCH_IO
114    endif
115    OS_LDFLAGS = -lm
116  else # Non-Android (or Native Android) Builds
117    ifeq (,$(INCLUDES)$(LIBRARIES))
118      INCPATH:=$(shell LANG=C; $(GCC) -x c -v -E /dev/null 2>&1 | grep -A 10 '> search starts here' | grep '^ ' | tr -d '\n')
119      ifeq (,$(INCPATH))
120        INCPATH:=/usr/include
121      endif
122      LIBPATH:=/usr/lib
123    else
124      $(info *** Warning ***)
125      ifeq (,$(INCLUDES))
126        INCPATH:=$(shell LANG=C; $(GCC) -x c -v -E /dev/null 2>&1 | grep -A 10 '> search starts here' | grep '^ ' | tr -d '\n')
127      else
128        $(info *** Warning *** Unsupported build with INCLUDES defined as: $(INCLUDES))
129        INCPATH:=$(strip $(subst :, ,$(INCLUDES)))
130        UNSUPPORTED_BUILD := include
131      endif
132      ifeq (,$(LIBRARIES))
133        LIBPATH:=/usr/lib
134      else
135        $(info *** Warning *** Unsupported build with LIBRARIES defined as: $(LIBRARIES))
136        LIBPATH:=$(strip $(subst :, ,$(LIBRARIES)))
137        ifeq (include,$(UNSUPPORTED_BUILD))
138          UNSUPPORTED_BUILD := include+lib
139        else
140          UNSUPPORTED_BUILD := lib
141        endif
142      endif
143      $(info *** Warning ***)
144    endif
145    OS_CCDEFS += -D_GNU_SOURCE
146    GCC_OPTIMIZERS_CMD = $(GCC) -v --help 2>&1
147    GCC_WARNINGS_CMD = $(GCC) -v --help 2>&1
148    LD_ELF = $(shell echo | $(GCC) -E -dM - | grep __ELF__)
149    ifeq (Darwin,$(OSTYPE))
150      OSNAME = OSX
151      LIBEXT = dylib
152      ifneq (include,$(findstring include,$(UNSUPPORTED_BUILD)))
153        INCPATH:=$(shell LANG=C; $(GCC) -x c -v -E /dev/null 2>&1 | grep -A 10 '> search starts here' | grep '^ ' | grep -v 'framework directory' | tr -d '\n')
154      endif
155      ifeq (incopt,$(shell if $(TEST) -d /opt/local/include; then echo incopt; fi))
156        INCPATH += /opt/local/include
157        OS_CCDEFS += -I/opt/local/include
158      endif
159      ifeq (libopt,$(shell if $(TEST) -d /opt/local/lib; then echo libopt; fi))
160        LIBPATH += /opt/local/lib
161        OS_LDFLAGS += -L/opt/local/lib
162      endif
163      ifeq (HomeBrew,$(shell if $(TEST) -d /usr/local/Cellar; then echo HomeBrew; fi))
164        INCPATH += $(foreach dir,$(wildcard /usr/local/Cellar/*/*),$(dir)/include)
165        LIBPATH += $(foreach dir,$(wildcard /usr/local/Cellar/*/*),$(dir)/lib)
166      endif
167      ifeq (libXt,$(shell if $(TEST) -d /usr/X11/lib; then echo libXt; fi))
168        LIBPATH += /usr/X11/lib
169        OS_LDFLAGS += -L/usr/X11/lib
170      endif
171    else
172      ifeq (Linux,$(OSTYPE))
173        ifeq (Android,$(shell uname -o))
174          OS_CCDEFS += -D__ANDROID_API__=$(shell getprop ro.build.version.sdk) -DSIM_BUILD_OS=" On Android Version $(shell getprop ro.build.version.release)"
175        endif
176        ifneq (lib,$(findstring lib,$(UNSUPPORTED_BUILD)))
177          ifeq (Android,$(shell uname -o))
178            ifneq (,$(shell if $(TEST) -d /system/lib; then echo systemlib; fi))
179              LIBPATH += /system/lib
180            endif
181            LIBPATH += $(LD_LIBRARY_PATH)
182          endif
183          ifeq (ldconfig,$(shell if $(TEST) -e /sbin/ldconfig; then echo ldconfig; fi))
184            LIBPATH := $(sort $(foreach lib,$(shell /sbin/ldconfig -p | grep ' => /' | sed 's/^.* => //'),$(dir $(lib))))
185          endif
186        endif
187        LIBEXT = so
188      else
189        ifeq (SunOS,$(OSTYPE))
190          OSNAME = Solaris
191          ifneq (lib,$(findstring lib,$(UNSUPPORTED_BUILD)))
192            LIBPATH := $(shell LANG=C; crle | grep 'Default Library Path' | awk '{ print $$5 }' | sed 's/:/ /g')
193          endif
194          LIBEXT = so
195          OS_LDFLAGS += -lsocket -lnsl
196          ifeq (incsfw,$(shell if $(TEST) -d /opt/sfw/include; then echo incsfw; fi))
197            INCPATH += /opt/sfw/include
198            OS_CCDEFS += -I/opt/sfw/include
199          endif
200          ifeq (libsfw,$(shell if $(TEST) -d /opt/sfw/lib; then echo libsfw; fi))
201            LIBPATH += /opt/sfw/lib
202            OS_LDFLAGS += -L/opt/sfw/lib -R/opt/sfw/lib
203          endif
204          OS_CCDEFS += -D_LARGEFILE_SOURCE
205        else
206          ifeq (cygwin,$(OSTYPE))
207            # use 0readme_ethernet.txt documented Windows pcap build components
208#            INCPATH += ../windows-build/winpcap/WpdPack/include
209#            LIBPATH += ../windows-build/winpcap/WpdPack/lib
210#            PCAPLIB = wpcap
211            LIBEXT = a
212          else
213            ifneq (,$(findstring AIX,$(OSTYPE)))
214              OS_LDFLAGS += -lm -lrt
215              ifeq (incopt,$(shell if $(TEST) -d /opt/freeware/include; then echo incopt; fi))
216                INCPATH += /opt/freeware/include
217                OS_CCDEFS += -I/opt/freeware/include
218              endif
219              ifeq (libopt,$(shell if $(TEST) -d /opt/freeware/lib; then echo libopt; fi))
220                LIBPATH += /opt/freeware/lib
221                OS_LDFLAGS += -L/opt/freeware/lib
222              endif
223            else
224              ifneq (,$(findstring Haiku,$(OSTYPE)))
225                HAIKU_ARCH=$(shell getarch)
226                ifeq ($(HAIKU_ARCH),)
227                  $(error Missing getarch command, your Haiku release is probably too old)
228                endif
229                ifeq ($(HAIKU_ARCH),x86_gcc2)
230                  $(error Unsupported arch x86_gcc2. Run setarch x86 and retry)
231                endif
232                INCPATH := $(shell findpaths -e -a $(HAIKU_ARCH) B_FIND_PATH_HEADERS_DIRECTORY)
233                INCPATH += $(shell findpaths -e B_FIND_PATH_HEADERS_DIRECTORY posix)
234                LIBPATH := $(shell findpaths -e -a $(HAIKU_ARCH) B_FIND_PATH_DEVELOP_LIB_DIRECTORY)
235                OS_LDFLAGS += -lnetwork
236              else
237                ifeq (,$(findstring NetBSD,$(OSTYPE)))
238                  ifneq (no ldconfig,$(findstring no ldconfig,$(shell which ldconfig 2>&1)))
239                    LDSEARCH :=$(shell LANG=C; ldconfig -r | grep 'search directories' | awk '{print $$3}' | sed 's/:/ /g')
240                  endif
241                  ifneq (,$(LDSEARCH))
242                    LIBPATH := $(LDSEARCH)
243                  else
244                    ifeq (,$(strip $(LPATH)))
245                      $(info *** Warning ***)
246                      $(info *** Warning *** The library search path on your $(OSTYPE) platform can't be)
247                      $(info *** Warning *** determined.  This should be resolved before you can expect)
248                      $(info *** Warning *** to have fully working simulators.)
249                      $(info *** Warning ***)
250                      $(info *** Warning *** You can specify your library paths via the LPATH environment)
251                      $(info *** Warning *** variable.)
252                      $(info *** Warning ***)
253                    else
254                      LIBPATH = $(subst :, ,$(LPATH))
255                    endif
256                  endif
257                  OS_LDFLAGS += $(patsubst %,-L%,$(LIBPATH))
258                endif
259              endif
260            endif
261            ifeq (usrpkglib,$(shell if $(TEST) -d /usr/pkg/lib; then echo usrpkglib; fi))
262              LIBPATH += /usr/pkg/lib
263              INCPATH += /usr/pkg/include
264              OS_LDFLAGS += -L/usr/pkg/lib -R/usr/pkg/lib
265              OS_CCDEFS += -I/usr/pkg/include
266            endif
267            ifeq (X11R7,$(shell if $(TEST) -d /usr/X11R7/lib; then echo X11R7; fi))
268              LIBPATH += /usr/X11R7/lib
269              INCPATH += /usr/X11R7/include
270              OS_LDFLAGS += -L/usr/X11R7/lib -R/usr/X11R7/lib
271              OS_CCDEFS += -I/usr/X11R7/include
272            endif
273            ifeq (/usr/local/lib,$(findstring /usr/local/lib,$(LIBPATH)))
274              INCPATH += /usr/local/include
275              OS_CCDEFS += -I/usr/local/include
276            endif
277            ifneq (,$(findstring NetBSD,$(OSTYPE))$(findstring FreeBSD,$(OSTYPE))$(findstring AIX,$(OSTYPE)))
278              LIBEXT = so
279            else
280              ifeq (HP-UX,$(OSTYPE))
281                ifeq (ia64,$(shell uname -m))
282                  LIBEXT = so
283                else
284                  LIBEXT = sl
285                endif
286                OS_CCDEFS += -D_HPUX_SOURCE -D_LARGEFILE64_SOURCE
287                OS_LDFLAGS += -Wl,+b:
288                NO_LTO = 1
289              else
290                LIBEXT = a
291              endif
292            endif
293          endif
294        endif
295      endif
296    endif
297    # Some gcc versions don't support LTO, so only use LTO when the compiler is known to support it
298    ifeq (,$(NO_LTO))
299      ifneq (,$(GCC_VERSION))
300        ifeq (,$(shell $(GCC) -v /dev/null 2>&1 | grep '\-\-enable-lto'))
301          LTO_EXCLUDE_VERSIONS += $(GCC_VERSION)
302        endif
303      endif
304    endif
305  endif
306  $(info lib paths are: $(LIBPATH))
307  $(info include paths are: $(INCPATH))
308  find_lib = $(strip $(firstword $(foreach dir,$(strip $(LIBPATH)),$(wildcard $(dir)/lib$(1).$(LIBEXT)))))
309  find_include = $(strip $(firstword $(foreach dir,$(strip $(INCPATH)),$(wildcard $(dir)/$(1).h))))
310  ifneq (,$(call find_lib,m))
311    OS_LDFLAGS += -lm
312    $(info using libm: $(call find_lib,m))
313  endif
314  ifneq (,$(call find_lib,rt))
315    OS_LDFLAGS += -lrt
316    $(info using librt: $(call find_lib,rt))
317  endif
318  ifneq (,$(call find_lib,pthread))
319    ifneq (,$(call find_include,pthread))
320#      OS_CCDEFS += -DUSE_READER_THREAD
321      ifeq (,$(NOASYNCH))
322        OS_CCDEFS += -DSIM_ASYNCH_IO
323      endif
324      OS_LDFLAGS += -lpthread
325      $(info using libpthread: $(call find_lib,pthread) $(call find_include,pthread))
326    endif
327  endif
328  ifneq (,$(call find_include,semaphore))
329    ifneq (, $(shell grep sem_timedwait $(call find_include,semaphore)))
330      OS_CCDEFS += -DHAVE_SEMAPHORE
331      $(info using semaphore: $(call find_include,semaphore))
332    endif
333  endif
334  ifneq (,$(call find_include,sys/mman))
335    ifneq (,$(shell grep shm_open $(call find_include,sys/mman)))
336      OS_CCDEFS += -DHAVE_SHM_OPEN
337      $(info using mman: $(call find_include,sys/mman))
338    endif
339  endif
340  ifneq (,$(call find_include,dlfcn))
341    ifneq (,$(call find_lib,dl))
342      OS_CCDEFS += -DHAVE_DLOPEN=$(LIBEXT)
343      OS_LDFLAGS += -ldl
344      $(info using libdl: $(call find_lib,dl) $(call find_include,dlfcn))
345    else
346      ifeq (BSD,$(findstring BSD,$(OSTYPE)))
347        OS_CCDEFS += -DHAVE_DLOPEN=so
348        $(info using libdl: $(call find_include,dlfcn))
349      endif
350    endif
351  endif
352  ifneq (,$(NETWORK_USEFUL))
353    ifneq (,$(call find_include,pcap))
354      ifneq (,$(shell grep 'pcap/pcap.h' $(call find_include,pcap) | grep include))
355        PCAP_H_PATH = $(dir $(call find_include,pcap))pcap/pcap.h
356      else
357        PCAP_H_PATH = $(call find_include,pcap)
358      endif
359      ifneq (,$(shell grep pcap_compile $(PCAP_H_PATH) | grep const))
360        BPF_CONST_STRING = -DBPF_CONST_STRING
361      endif
362      ifneq (,$(call find_lib,$(PCAPLIB)))
363        ifneq ($(USE_NETWORK),) # Network support specified on the GNU make command line
364          NETWORK_CCDEFS = -DUSE_NETWORK -I$(dir $(call find_include,pcap)) $(BPF_CONST_STRING)
365          ifeq (cygwin,$(OSTYPE))
366            # cygwin has no ldconfig so explicitly specify pcap object library
367            NETWORK_LDFLAGS = -L$(dir $(call find_lib,$(PCAPLIB))) -Wl,-R,$(dir $(call find_lib,$(PCAPLIB))) -l$(PCAPLIB)
368          else
369            NETWORK_LDFLAGS = -l$(PCAPLIB)
370          endif
371          $(info using libpcap: $(call find_lib,$(PCAPLIB)) $(call find_include,pcap))
372          NETWORK_FEATURES = - static networking support using $(OSNAME) provided libpcap components
373        else # default build uses dynamic libpcap
374          NETWORK_CCDEFS = -DUSE_SHARED -I$(dir $(call find_include,pcap)) $(BPF_CONST_STRING)
375          $(info using libpcap: $(call find_include,pcap))
376          NETWORK_FEATURES = - dynamic networking support using $(OSNAME) provided libpcap components
377        endif
378      else
379        NETWORK_CCDEFS = -DUSE_SHARED -I$(dir $(call find_include,pcap)) $(BPF_CONST_STRING)
380        NETWORK_FEATURES = - dynamic networking support using $(OSNAME) provided libpcap components
381        $(info using libpcap: $(call find_include,pcap))
382      endif
383    else
384      # Look for package built from tcpdump.org sources with default install target (or cygwin winpcap)
385      LIBPATH += /usr/local/lib
386      INCPATH += /usr/local/include
387      LIBEXTSAVE := $(LIBEXT)
388      LIBEXT = a
389      ifneq (,$(call find_lib,$(PCAPLIB)))
390        ifneq (,$(call find_include,pcap))
391          $(info using libpcap: $(call find_lib,$(PCAPLIB)) $(call find_include,pcap))
392          ifeq (cygwin,$(OSTYPE))
393            NETWORK_CCDEFS = -DUSE_NETWORK -I$(dir $(call find_include,pcap))
394            NETWORK_LDFLAGS = -L$(dir $(call find_lib,$(PCAPLIB))) -Wl,-R,$(dir $(call find_lib,$(PCAPLIB))) -l$(PCAPLIB)
395            NETWORK_FEATURES = - static networking support using libpcap components located in the cygwin directories
396          else
397            NETWORK_CCDEFS := -DUSE_NETWORK -isystem $(dir $(call find_include,pcap)) $(call find_lib,$(PCAPLIB))
398            NETWORK_FEATURES = - networking support using libpcap components from www.tcpdump.org
399            $(info *** Warning ***)
400            $(info *** Warning *** $(BUILD_SINGLE)Simulator$(BUILD_MULTIPLE) being built with networking support using)
401            $(info *** Warning *** libpcap components from www.tcpdump.org.)
402            $(info *** Warning *** Some users have had problems using the www.tcpdump.org libpcap)
403            $(info *** Warning *** components for simh networking.  For best results, with)
404            $(info *** Warning *** simh networking, it is recommended that you install the)
405            $(info *** Warning *** libpcap-dev package from your $(OSTYPE) distribution)
406            $(info *** Warning ***)
407          endif
408        else
409          $(error using libpcap: $(call find_lib,$(PCAPLIB)) missing pcap.h)
410        endif
411      endif
412      LIBEXT = $(LIBEXTSAVE)
413    endif
414    ifneq (,$(findstring USE_NETWORK,$(NETWORK_CCDEFS))$(findstring USE_SHARED,$(NETWORK_CCDEFS)))
415      # Given we have libpcap components, consider other network connections as well
416      ifneq (,$(call find_lib,vdeplug))
417        # libvdeplug requires the use of the OS provided libpcap
418        ifeq (,$(findstring usr/local,$(NETWORK_CCDEFS)))
419          ifneq (,$(call find_include,libvdeplug))
420            # Provide support for vde networking
421            NETWORK_CCDEFS += -DUSE_VDE_NETWORK
422            NETWORK_LDFLAGS += -lvdeplug
423            $(info using libvdeplug: $(call find_lib,vdeplug) $(call find_include,libvdeplug))
424          endif
425        endif
426      endif
427      ifneq (,$(call find_include,linux/if_tun))
428        # Provide support for Tap networking on Linux
429        NETWORK_CCDEFS += -DUSE_TAP_NETWORK
430      endif
431      ifeq (bsdtuntap,$(shell if $(TEST) -e /usr/include/net/if_tun.h -o -e /Library/Extensions/tap.kext; then echo bsdtuntap; fi))
432        # Provide support for Tap networking on BSD platforms (including OS X)
433        NETWORK_CCDEFS += -DUSE_TAP_NETWORK -DUSE_BSDTUNTAP
434      endif
435    else
436      NETWORK_FEATURES = - WITHOUT networking support
437      $(info *** Warning ***)
438      $(info *** Warning *** $(BUILD_SINGLE)Simulator$(BUILD_MULTIPLE) are being built WITHOUT networking support)
439      $(info *** Warning ***)
440      $(info *** Warning *** To build simulator(s) with networking support you should read)
441      $(info *** Warning *** 0readme_ethernet.txt and follow the instructions regarding the)
442      $(info *** Warning *** needed libpcap components for your $(OSTYPE) platform)
443      $(info *** Warning ***)
444    endif
445    NETWORK_OPT = $(NETWORK_CCDEFS)
446  endif
447#  ifneq (binexists,$(shell if $(TEST) -e BIN; then echo binexists; fi))
448#    MKDIRBIN = if $(TEST) ! -e BIN; then mkdir BIN; fi
449#  endif
450else
451  #Win32 Environments (via MinGW32)
452  GCC = gcc
453#  GCC_Path := $(dir $(shell where gcc.exe))
454  GCC_VERSION = $(word 3,$(shell $(GCC) --version))
455  LTO_EXCLUDE_VERSIONS = 4.5.2
456  OS_CCDEFS =  -fms-extensions $(PTHREADS_CCDEFS) $(PCAP_CCDEFS)
457  OS_LDFLAGS = -lm -lwsock32 -lwinmm $(PTHREADS_LDFLAGS)
458  EXE = .exe
459#  ifneq (binexists,$(shell if exist BIN echo binexists))
460#    MKDIRBIN = if not exist BIN mkdir BIN
461#  endif
462  ifneq ($(USE_NETWORK),)
463    NETWORK_OPT = -DUSE_SHARED
464  endif
465endif
466ifneq ($(DEBUG),)
467  CFLAGS_G = -g -ggdb -g3
468  CFLAGS_O = -O0
469  BUILD_FEATURES = - debugging support
470else
471  CFLAGS_O = -O2
472  LDFLAGS_O =
473  ifeq (Darwin,$(OSTYPE))
474    NO_LTO = 1
475  endif
476  GCC_MAJOR_VERSION = $(firstword $(subst  ., ,$(GCC_VERSION)))
477  ifneq (3,$(GCC_MAJOR_VERSION))
478    ifeq (,$(GCC_OPTIMIZERS_CMD))
479      GCC_OPTIMIZERS_CMD = $(GCC) --help=optimizers
480    endif
481    GCC_OPTIMIZERS = $(shell $(GCC_OPTIMIZERS_CMD))
482  endif
483  ifneq (,$(findstring $(GCC_VERSION),$(LTO_EXCLUDE_VERSIONS)))
484    NO_LTO = 1
485  endif
486  ifneq (,$(findstring -finline-functions,$(GCC_OPTIMIZERS)))
487    CFLAGS_O += -finline-functions
488  endif
489  ifneq (,$(findstring -fgcse-after-reload,$(GCC_OPTIMIZERS)))
490    CFLAGS_O += -fgcse-after-reload
491  endif
492  ifneq (,$(findstring -fpredictive-commoning,$(GCC_OPTIMIZERS)))
493    CFLAGS_O += -fpredictive-commoning
494  endif
495  ifneq (,$(findstring -fipa-cp-clone,$(GCC_OPTIMIZERS)))
496    CFLAGS_O += -fipa-cp-clone
497  endif
498  ifneq (,$(findstring -funsafe-loop-optimizations,$(GCC_OPTIMIZERS)))
499    CFLAGS_O += -fno-unsafe-loop-optimizations
500  endif
501  ifneq (,$(findstring -fstrict-overflow,$(GCC_OPTIMIZERS)))
502    CFLAGS_O += -fno-strict-overflow
503  endif
504  ifeq (,$(NO_LTO))
505    ifneq (,$(findstring -flto,$(GCC_OPTIMIZERS)))
506      CFLAGS_O += -flto -fwhole-program
507      LDFLAGS_O += -flto -fwhole-program
508    endif
509  endif
510  BUILD_FEATURES = - compiler optimizations and no debugging support
511endif
512ifneq (3,$(GCC_MAJOR_VERSION))
513  ifeq (,$(GCC_WARNINGS_CMD))
514    GCC_WARNINGS_CMD = $(GCC) --help=warnings
515  endif
516  ifneq (,$(findstring -Wunused-result,$(shell $(GCC_WARNINGS_CMD))))
517    CFLAGS_O += -Wno-unused-result
518  endif
519endif
520ifneq (clean,$(MAKECMDGOALS))
521  BUILD_FEATURES := $(BUILD_FEATURES). $(COMPILER_NAME)
522  $(info ***)
523  $(info *** $(BUILD_SINGLE)Simulator$(BUILD_MULTIPLE) being built with:)
524  $(info *** $(BUILD_FEATURES).)
525  ifneq (,$(NETWORK_FEATURES))
526    $(info *** $(NETWORK_FEATURES).)
527  endif
528  $(info ***)
529endif
530ifneq ($(DONT_USE_READER_THREAD),)
531  NETWORK_OPT += -DDONT_USE_READER_THREAD
532endif
533
534
535# Shut up annoying clang default warnings.
536
537ifeq ($(findstring clang,$(COMPILER_NAME)),clang)
538  OS_CCDEFS += -Wno-parentheses -Wno-bitwise-op-parentheses -Wno-dangling-else
539endif
540
541
542CC_STD = -std=c99
543CC_OUTSPEC = -o $@
544CC = $(GCC) $(CC_STD) -U__STRICT_ANSI__ $(CFLAGS_G) $(CFLAGS_O) -I . $(OS_CCDEFS) $(ROMS_OPT)
545LDFLAGS = $(OS_LDFLAGS) $(NETWORK_LDFLAGS) $(LDFLAGS_O)
546
547#
548# Common Libraries
549#
550BIN = ..
551SIM = scp.c sim_console.c sim_fio.c sim_timer.c sim_sock.c \
552	sim_tmxr.c sim_tape.c sim_shmem.c sim_extension.c sim_serial.c
553
554
555#
556# Emulator source files and compile time options
557#
558HP2100D = HP2100
559HP2100 = ${HP2100D}/hp2100_baci.c ${HP2100D}/hp2100_cpu.c ${HP2100D}/hp2100_cpu_fp.c \
560	${HP2100D}/hp2100_cpu_fpp.c ${HP2100D}/hp2100_cpu0.c ${HP2100D}/hp2100_cpu1.c \
561	${HP2100D}/hp2100_cpu2.c ${HP2100D}/hp2100_cpu3.c ${HP2100D}/hp2100_cpu4.c \
562	${HP2100D}/hp2100_cpu5.c ${HP2100D}/hp2100_cpu6.c ${HP2100D}/hp2100_cpu7.c \
563	${HP2100D}/hp2100_di.c ${HP2100D}/hp2100_di_da.c ${HP2100D}/hp2100_disclib.c \
564	${HP2100D}/hp2100_dma.c ${HP2100D}/hp2100_dp.c ${HP2100D}/hp2100_dq.c \
565	${HP2100D}/hp2100_dr.c ${HP2100D}/hp2100_ds.c ${HP2100D}/hp2100_ipl.c \
566	${HP2100D}/hp2100_lps.c ${HP2100D}/hp2100_lpt.c ${HP2100D}/hp2100_mc.c \
567	${HP2100D}/hp2100_mem.c ${HP2100D}/hp2100_mpx.c ${HP2100D}/hp2100_ms.c \
568	${HP2100D}/hp2100_mt.c ${HP2100D}/hp2100_mux.c ${HP2100D}/hp2100_pif.c \
569	${HP2100D}/hp2100_pt.c ${HP2100D}/hp2100_sys.c ${HP2100D}/hp2100_tbg.c \
570	${HP2100D}/hp2100_tty.c
571HP2100_OPT = -D USE_VM_INIT -D HAVE_INT64 -I ${HP2100D}
572
573HP3000D = HP3000
574HP3000 = ${HP3000D}/hp_disclib.c ${HP3000D}/hp_tapelib.c ${HP3000D}/hp3000_atc.c \
575	${HP3000D}/hp3000_clk.c ${HP3000D}/hp3000_cpu.c ${HP3000D}/hp3000_cpu_base.c \
576	${HP3000D}/hp3000_cpu_fp.c ${HP3000D}/hp3000_cpu_eis.c \
577	${HP3000D}/hp3000_cpu_cis.c ${HP3000D}/hp3000_ds.c \
578	${HP3000D}/hp3000_iop.c ${HP3000D}/hp3000_lp.c ${HP3000D}/hp3000_mem.c \
579	${HP3000D}/hp3000_mpx.c ${HP3000D}/hp3000_ms.c ${HP3000D}/hp3000_scmb.c \
580	${HP3000D}/hp3000_sel.c ${HP3000D}/hp3000_sys.c
581HP3000_OPT = -D USE_VM_INIT -I ${HP3000D}
582
583
584#
585# Build everything
586#
587ALL = hp2100 hp3000
588
589all : ${ALL}
590
591clean :
592ifeq ($(WIN32),)
593	${RM} $(addprefix ${BIN}/,$(addsuffix ${EXE},${ALL}))
594else
595	if exist ${BIN}\hp*.exe del /q ${BIN}\hp*.exe
596#	if exist BIN rmdir BIN
597endif
598
599#
600# Individual builds
601#
602
603hp2100 : ${BIN}/hp2100${EXE}
604
605${BIN}/hp2100${EXE} : ${HP2100} ${SIM}
606ifneq (1,$(CPP_BUILD)$(CPP_FORCE))
607#	${MKDIRBIN}
608	${CC} ${HP2100} ${SIM} ${HP2100_OPT} $(CC_OUTSPEC) ${LDFLAGS}
609else
610	$(info hp2100 can't be built using C++)
611endif
612
613
614hp3000 : ${BIN}/hp3000${EXE}
615
616${BIN}/hp3000${EXE} : ${HP3000} ${SIM}
617ifneq (1,$(CPP_BUILD)$(CPP_FORCE))
618#	${MKDIRBIN}
619	${CC} ${HP3000} ${SIM} ${HP3000_OPT} $(CC_OUTSPEC) ${LDFLAGS}
620else
621	$(info hp3000 can't be built using C++)
622endif
623