Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. ##
  2. # MegaZeux Build System (GNU Make)
  3. #
  4. # NOTE: This build system was recently re-designed to not use recursive
  5. # Makefiles. The rationale for this is documented here:
  6. # http://aegis.sourceforge.net/auug97.pdf
  7. ##
  8. #
  9. # Remove all built-in rules.
  10. #
  11. .SUFFIXES:
  12. ifeq ($(filter -r,$(MAKEFLAGS)),)
  13. MAKEFLAGS += -r
  14. endif
  15. .PHONY: all clean help_check test test_clean mzx mzx.debug build build_clean source
  16. -include platform.inc
  17. include version.inc
  18. all: mzx
  19. debuglink: all mzx.debug
  20. -include arch/${PLATFORM}/Makefile.in
  21. CC ?= gcc
  22. CXX ?= g++
  23. AR ?= ar
  24. STRIP ?= strip --strip-unneeded
  25. OBJCOPY ?= objcopy
  26. PEFIX ?= true
  27. CHMOD ?= chmod
  28. CP ?= cp
  29. HOST_CC ?= gcc
  30. LN ?= ln
  31. MKDIR ?= mkdir
  32. MV ?= mv
  33. RM ?= rm
  34. #
  35. # Set up CFLAGS/LDFLAGS for all MegaZeux external dependencies.
  36. #
  37. ifeq (${BUILD_SDL},1)
  38. #
  39. # SDL 2
  40. #
  41. ifneq (${BUILD_LIBSDL2},)
  42. # Check PREFIX for sdl2-config.
  43. ifneq ($(wildcard ${SDL_PREFIX}/bin/sdl2-config),)
  44. SDL_CONFIG := ${SDL_PREFIX}/bin/sdl2-config
  45. else ifneq ($(wildcard ${PREFIX}/bin/sdl2-config),)
  46. SDL_CONFIG := ${PREFIX}/bin/sdl2-config
  47. else
  48. SDL_CONFIG := sdl2-config
  49. endif
  50. SDL_PREFIX ?= $(shell ${SDL_CONFIG} --prefix)
  51. SDL_CFLAGS ?= $(shell ${SDL_CONFIG} --prefix=${SDL_PREFIX} --cflags | sed 's,-I,-isystem ,g')
  52. SDL_LDFLAGS ?= $(shell ${SDL_CONFIG} --prefix=${SDL_PREFIX} --libs)
  53. endif
  54. #
  55. # SDL 1.2
  56. #
  57. ifeq (${BUILD_LIBSDL2},)
  58. # Check PREFIX for sdl-config.
  59. ifneq ($(wildcard ${SDL_PREFIX}/bin/sdl-config),)
  60. SDL_CONFIG := ${SDL_PREFIX}/bin/sdl-config
  61. else ifneq ($(wildcard ${PREFIX}/bin/sdl-config),)
  62. SDL_CONFIG := ${PREFIX}/bin/sdl-config
  63. else
  64. SDL_CONFIG := sdl-config
  65. endif
  66. SDL_PREFIX ?= $(shell ${SDL_CONFIG} --prefix)
  67. SDL_CFLAGS ?= $(shell ${SDL_CONFIG} --prefix=${SDL_PREFIX} --cflags)
  68. SDL_LDFLAGS ?= $(shell ${SDL_CONFIG} --prefix=${SDL_PREFIX} --libs)
  69. endif
  70. # Make these immediate so the scripts run only once.
  71. SDL_PREFIX := $(SDL_PREFIX)
  72. SDL_CFLAGS := $(SDL_CFLAGS)
  73. SDL_LDFLAGS := $(LINK_DYNAMIC_IF_MIXED) $(SDL_LDFLAGS)
  74. endif
  75. #
  76. # libvorbis/tremor
  77. #
  78. VORBIS_CFLAGS ?= -I${PREFIX}/include -DOV_EXCLUDE_STATIC_CALLBACKS
  79. ifeq (${VORBIS},vorbis)
  80. VORBIS_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lvorbisfile -lvorbis -logg
  81. else ifeq (${VORBIS},tremor)
  82. VORBIS_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lvorbisidec -logg
  83. else ifeq (${VORBIS},tremor-lowmem)
  84. VORBIS_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lvorbisidec
  85. endif
  86. #
  87. # MikMod (optional mod engine)
  88. #
  89. MIKMOD_CFLAGS ?= -I${PREFIX}/include
  90. MIKMOD_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lmikmod
  91. #
  92. # libopenmpt (optional mod engine)
  93. #
  94. OPENMPT_CFLAGS ?= -I${PREFIX}/include
  95. OPENMPT_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lopenmpt
  96. #
  97. # zlib
  98. #
  99. ZLIB_CFLAGS ?= -I${PREFIX}/include \
  100. -D_FILE_OFFSET_BITS=32 -U_LARGEFILE64_SOURCE
  101. ZLIB_LDFLAGS ?= $(LINK_STATIC_IF_MIXED) -L${PREFIX}/lib -lz
  102. #
  103. # libpng
  104. #
  105. ifeq (${LIBPNG},1)
  106. # Check PREFIX for libpng-config.
  107. ifneq ($(wildcard ${PREFIX}/bin/libpng-config),)
  108. LIBPNG_CONFIG := ${PREFIX}/bin/libpng-config
  109. else
  110. LIBPNG_CONFIG := libpng-config
  111. endif
  112. LIBPNG_CFLAGS ?= $(shell ${LIBPNG_CONFIG} --cflags)
  113. LIBPNG_LDFLAGS ?= $(shell ${LIBPNG_CONFIG} --ldflags)
  114. # Make these immediate so the scripts run only once.
  115. LIBPNG_CFLAGS := $(LIBPNG_CFLAGS)
  116. LIBPNG_LDFLAGS := $(LINK_STATIC_IF_MIXED) $(LIBPNG_LDFLAGS)
  117. endif
  118. #
  119. # X11
  120. #
  121. ifneq (${X11DIR},)
  122. X11_CFLAGS ?= -I${X11DIR}/../include
  123. X11_LDFLAGS ?= -L${X11DIR}/../lib -lX11
  124. # Make these immediate
  125. X11_CFLAGS := $(X11_CFLAGS)
  126. X11_LDFLAGS := $(X11_LDFLAGS)
  127. endif
  128. #
  129. # pthread
  130. #
  131. PTHREAD_LDFLAGS ?= -lpthread
  132. #
  133. # Set up general CFLAGS/LDFLAGS
  134. #
  135. OPTIMIZE_CFLAGS ?= -O3
  136. ifeq (${DEBUG},1)
  137. #
  138. # Disable the optimizer for "true" debug builds
  139. #
  140. CFLAGS = -O0 -DDEBUG
  141. CXXFLAGS = -O0 -DDEBUG
  142. else
  143. #
  144. # Optimized builds have assert() compiled out
  145. #
  146. CFLAGS += ${OPTIMIZE_CFLAGS} -DNDEBUG
  147. CXXFLAGS += ${OPTIMIZE_CFLAGS} -DNDEBUG
  148. endif
  149. #
  150. # Android headers are busted and we get too many warnings..
  151. #
  152. ifneq (${PLATFORM},android)
  153. CFLAGS += -Wundef -Wunused-macros
  154. CXXFLAGS += -Wundef -Wunused-macros
  155. endif
  156. #
  157. # Always generate debug information; this may end up being
  158. # stripped (on embedded platforms) or objcopy'ed out.
  159. #
  160. CFLAGS += -g -W -Wall -Wno-unused-parameter -std=gnu99
  161. CFLAGS += -Wdeclaration-after-statement ${ARCH_CFLAGS}
  162. CXXFLAGS += -g -W -Wall -Wno-unused-parameter -std=gnu++98
  163. CXXFLAGS += -fno-exceptions -fno-rtti ${ARCH_CXXFLAGS}
  164. LDFLAGS += ${ARCH_LDFLAGS}
  165. #
  166. # GCC version >= 7.x
  167. #
  168. GCC_VER := ${shell ${CC} -dumpversion}
  169. GCC_VER_MAJOR := ${shell ${CC} -dumpversion | cut -d. -f1}
  170. GCC_VER_MAJOR_GE_7 := ${shell test $(GCC_VER_MAJOR) -ge 7; echo $$?}
  171. ifeq ($(GCC_VER_MAJOR_GE_7),0)
  172. # This gives spurious warnings on Linux. The snprintf implementation on Linux
  173. # will terminate even in the case of truncation, making this largely useless.
  174. # It does not trigger using mingw, where it would actually matter.
  175. CFLAGS += -Wno-format-truncation
  176. endif
  177. #
  178. # GCC version >= 4.x
  179. #
  180. GCC_VER_MAJOR_GE_4 := ${shell test $(GCC_VER_MAJOR) -ge 4; echo $$?}
  181. ifeq ($(GCC_VER_MAJOR_GE_4),0)
  182. ifeq (${DEBUG},1)
  183. ifneq (${GCC_VER},4.2.1)
  184. CFLAGS += -fbounds-check
  185. CXXFLAGS += -fbounds-check
  186. endif
  187. endif
  188. #
  189. # We enable pedantic warnings here, but this ends up turning on some things
  190. # we must disable by hand.
  191. #
  192. # Variadic macros are arguably less portable, but all the compilers we
  193. # support have them.
  194. #
  195. ifneq (${PLATFORM},android)
  196. CFLAGS += -pedantic -Wno-variadic-macros
  197. CXXFLAGS += -pedantic -fpermissive -Wno-variadic-macros
  198. endif
  199. ifneq (${PLATFORM},mingw)
  200. #
  201. # Symbols in COFF binaries are implicitly hidden unless exported; this
  202. # flag just confuses GCC and must be disabled.
  203. #
  204. CFLAGS += -fvisibility=hidden
  205. CXXFLAGS += -fvisibility=hidden
  206. #
  207. # Skip the stack protector on embedded platforms; it just unnecessarily
  208. # slows things down, and there's no easy way to write a convincing
  209. # __stack_chk_fail function. MinGW may or may not have a __stack_chk_fail
  210. # function. Skip android, too.
  211. #
  212. ifeq ($(or ${BUILD_GP2X},${BUILD_NDS},${BUILD_3DS},${BUILD_PSP},${BUILD_WII}),)
  213. ifneq (${PLATFORM},android)
  214. CFLAGS += -fstack-protector-all
  215. CXXFLAGS += -fstack-protector-all
  216. endif
  217. endif
  218. endif
  219. endif
  220. #
  221. # Enable position-independent code across the board for modular builds.
  222. #
  223. ifeq (${BUILD_MODULAR},1)
  224. CFLAGS += -fPIC
  225. CXXFLAGS += -fPIC
  226. endif
  227. #
  228. # We don't want these commands to be echo'ed in non-verbose mode
  229. #
  230. ifneq (${V},1)
  231. override V:=
  232. CC := @${CC}
  233. CXX := @${CXX}
  234. AR := @${AR}
  235. STRIP := @${STRIP}
  236. OBJCOPY := @${OBJCOPY}
  237. PEFIX := @${PEFIX}
  238. CHMOD := @${CHMOD}
  239. CP := @${CP}
  240. HOST_CC := @${HOST_CC}
  241. LN := @${LN}
  242. MKDIR := @${MKDIR}
  243. MV := @${MV}
  244. RM := @${RM}
  245. endif
  246. build_clean:
  247. $(if ${V},,@echo " RM " build)
  248. ${RM} -r build
  249. source: build/${TARGET}src
  250. #
  251. # Build source target
  252. # Targetting unix primarily, so turn off autocrlf if necessary
  253. #
  254. ifneq ($(shell which git),)
  255. USER_AUTOCRLF=$(shell git config core.autocrlf)
  256. endif
  257. build/${TARGET}src:
  258. ${RM} -r build/${TARGET}
  259. ${MKDIR} -p build/dist/source
  260. @git config core.autocrlf false
  261. @git checkout-index -a --prefix build/${TARGET}/
  262. @git config core.autocrlf ${USER_AUTOCRLF}
  263. ${RM} -r build/${TARGET}/scripts
  264. ${RM} build/${TARGET}/.gitignore build/${TARGET}/.gitattributes
  265. @cd build/${TARGET} && make distclean
  266. @tar -C build -Jcf build/dist/source/${TARGET}src.tar.xz ${TARGET}
  267. #
  268. # The SUPPRESS_BUILD hack is required to allow the placebo "dist"
  269. # Makefile to provide an 'all:' target, which allows it to print
  270. # a message. We don't want to pull in other targets, confusing Make.
  271. #
  272. ifneq (${SUPPRESS_BUILD},1)
  273. mzxrun = mzxrun${BINEXT}
  274. mzx = megazeux${BINEXT}
  275. mzx: ${mzxrun} ${mzx}
  276. mzx.debug: ${mzxrun}.debug ${mzx}.debug
  277. ifeq (${BUILD_MODPLUG},1)
  278. BUILD_GDM2S3M=1
  279. endif
  280. %/.build:
  281. $(if ${V},,@echo " MKDIR " $@)
  282. ${MKDIR} $@
  283. %.debug: %
  284. $(if ${V},,@echo " OBJCOPY " --only-keep-debug $< $@)
  285. ${OBJCOPY} --only-keep-debug $< $@
  286. ${PEFIX} $@
  287. ${CHMOD} a-x $@
  288. $(if ${V},,@echo " STRIP " $<)
  289. ${STRIP} $<
  290. $(if ${V},,@echo " OBJCOPY " --add-gnu-debuglink $@ $<)
  291. ${OBJCOPY} --add-gnu-debuglink=$@ $<
  292. ${PEFIX} $<
  293. @touch $@
  294. include src/Makefile.in
  295. clean: mzx_clean test_clean
  296. ifeq (${BUILD_UTILS},1)
  297. include src/utils/Makefile.in
  298. debuglink: utils utils.debug
  299. clean: utils_clean
  300. all: utils
  301. endif
  302. ifeq (${build},)
  303. build := build/${SUBPLATFORM}
  304. endif
  305. build: ${build}
  306. ${build}:
  307. ${MKDIR} -p ${build}/docs
  308. ${MKDIR} -p ${build}/assets
  309. ${CP} config.txt LICENSE ${build}
  310. ${CP} assets/default.chr assets/edit.chr ${build}/assets
  311. ${CP} assets/smzx.pal ${build}/assets
  312. ${CP} docs/macro.txt docs/keycodes.html docs/mzxhelp.html ${build}/docs
  313. ${CP} docs/changelog.txt docs/platform_matrix.html ${build}/docs
  314. ${CP} ${mzxrun} ${build}
  315. @if test -f ${mzxrun}.debug; then \
  316. cp ${mzxrun}.debug ${build}; \
  317. fi
  318. ifeq (${BUILD_EDITOR},1)
  319. ${CP} assets/ascii.chr assets/blank.chr ${build}/assets
  320. ${CP} assets/smzx.chr ${build}/assets
  321. ${CP} ${mzx} ${build}
  322. @if test -f ${mzx}.debug; then \
  323. cp ${mzx}.debug ${build}; \
  324. fi
  325. endif
  326. ifeq (${BUILD_HELPSYS},1)
  327. ${CP} assets/help.fil ${build}/assets
  328. endif
  329. ifeq (${BUILD_MODULAR},1)
  330. ${CP} ${core_target} ${editor_target} ${build}
  331. @if test -f ${core_target}.debug; then \
  332. cp ${core_target}.debug ${build}; \
  333. fi
  334. @if test -f ${editor_target}.debug; then \
  335. cp ${editor_target}.debug ${build}; \
  336. fi
  337. endif
  338. ifeq (${BUILD_UTILS},1)
  339. ${MKDIR} ${build}/utils
  340. ${CP} ${checkres} ${downver} ${build}/utils
  341. ${CP} ${hlp2txt} ${txt2hlp} ${build}/utils
  342. ifeq (${LIBPNG},1)
  343. ${CP} ${png2smzx} ${build}/utils
  344. endif
  345. ${CP} ${ccv} ${build}/utils
  346. @if test -f ${checkres}.debug; then \
  347. cp ${checkres}.debug ${downver}.debug ${build}/utils; \
  348. cp ${hlp2txt}.debug ${txt2hlp}.debug ${build}/utils; \
  349. cp ${png2smzx}.debug ${build}/utils; \
  350. fi
  351. endif
  352. ifeq (${BUILD_RENDER_GL_PROGRAM},1)
  353. ${MKDIR} -p ${build}/assets/glsl/scalers
  354. ${CP} assets/glsl/*.vert ${build}/assets/glsl
  355. ${CP} assets/glsl/*.frag ${build}/assets/glsl
  356. ${CP} assets/glsl/README.md ${build}/assets/glsl
  357. ${CP} assets/glsl/scalers/*.frag assets/glsl/scalers/*.vert \
  358. ${build}/assets/glsl/scalers
  359. endif
  360. ifeq (${BUILD_GAMECONTROLLERDB},1)
  361. ${CP} assets/gamecontrollerdb.txt assets/gamecontrollerdb.LICENSE \
  362. ${build}/assets
  363. endif
  364. distclean: clean
  365. @echo " DISTCLEAN"
  366. @rm -f src/config.h
  367. @echo "PLATFORM=none" > platform.inc
  368. assets/help.fil: ${txt2hlp} docs/WIPHelp.txt
  369. $(if ${V},,@echo " txt2hlp " $@)
  370. @src/utils/txt2hlp docs/WIPHelp.txt $@
  371. docs/mzxhelp.html: ${hlp2html} docs/WIPHelp.txt
  372. $(if ${V},,@echo " hlp2html" $@)
  373. @src/utils/hlp2html docs/WIPHelp.txt docs/mzxhelp.html
  374. help_check: ${hlp2txt} assets/help.fil
  375. @src/utils/hlp2txt assets/help.fil help.txt
  376. @echo @ >> help.txt
  377. @diff --strip-trailing-cr -q docs/WIPHelp.txt help.txt
  378. @rm -f help.txt
  379. test:
  380. @testworlds/run.sh @{PLATFORM} @{LIBDIR}
  381. test_clean:
  382. @rm -rf testworlds/log
  383. endif