forked from edubart/nelua-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
382 lines (325 loc) · 10.1 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
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
# Detect variables based on the host system.
DEVNULL=/dev/null
ifeq ($(OS), Windows_NT)
SYS=Windows
ifeq ($(wildcard /dev/null),)
DEVNULL=NUL
endif
# Unix compatible environment?
ifeq (, $(shell where which 2>$(DEVNULL)))
WINMODE=1
endif
else
SYS=$(shell uname -s)
endif
NELUALUA=nelua-lua
NELUALUAC=nelua-luac
NELUA=nelua
ifeq ($(SYS), Windows)
NELUALUA=nelua-lua.exe
NELUALUAC=nelua-luac.exe
NELUA=nelua.bat
endif
NELUA_RUN=./$(NELUA)
###############################################################################
# Nelua's Lua interpreter
# Compiler Flags
INCS=-Isrc/lua
DEFS=-DNDEBUG
LUA_DEFS=-DMAXRECLEVEL=400
SRCS=$(wildcard src/*.c) \
$(wildcard src/lpeglabel/*.c)
HDRS=$(wildcard src/*.h) \
$(wildcard src/lua/*.h) \
$(wildcard src/lua/*.c) \
$(wildcard src/lpeglabel/*.h)
OPTCFLAGS=-O2
FAST_CFLAGS=-O3 -flto=auto -fno-plt -fno-stack-protector
ARCH_CFLAGS=-march=native
ifeq ($(SYS), Linux)
CC=gcc
DEFS+=-std=gnu99 -DLUA_USE_LINUX
LIBS+=-lm -ldl -Wl,-E
ifeq ($(CC), musl-gcc)
LIBS+=-static
endif
else ifeq ($(SYS), Windows)
ifneq (,$(findstring cygdrive,$(ORIGINAL_PATH)))
# Cygwin
CC=$(shell uname -m)-w64-mingw32-gcc
else ifneq (, $(shell where clang 2>$(DEVNULL)))
# prefer clang (Visual Studio + Clang)
CC=clang
else # gcc (MSYS)
CC=gcc
endif
DEFS+=-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS
LIBS+=-static
else ifeq ($(SYS), Darwin)
CC=clang
DEFS+=-DLUA_USE_MACOSX
LIBS+=-lm -rdynamic
else # probably POSIX
CC=gcc
DEFS+=-DLUA_USE_POSIX
LIBS+=-lm
NO_RPMALLOC=1
endif
ifndef NO_RPMALLOC
SRCS+=src/srpmalloc/srpmalloc.c
HDRS+=src/srpmalloc/srpmalloc.h
LUA_DEFS+=-DLUA_USE_RPMALLOC
endif
export CC
# The default target.
default: $(NELUALUA)
# Compile Nelua's bundled Lua interpreter.
$(NELUALUA): $(SRCS) $(HDRS)
$(CC) \
$(LUA_DEFS) $(DEFS) $(MYDEFS) \
$(INCS) $(MYINCS) \
$(OPTCFLAGS) $(CFLAGS) $(MYCFLAGS) \
$(SRCS) $(MYSRCS) \
-o $(NELUALUA) \
$(LIBS) $(MYLIBS) $(LDFLAGS) $(MYLDFLAGS)
# Compile Nelua's bundled Lua interpreter using PGO (Profile Guided optimization) and native,
# this can usually speed up compilation speed by ~6%.
optimized-nelua-lua:
$(RM_DIR) pgo
$(MAKE) --no-print-directory MYCFLAGS="$(ARCH_CFLAGS) $(FAST_CFLAGS) $(MYCFLAGS) -fprofile-generate=pgo" clean-nelualua default
$(NELUA_RUN) -qb tests/all_test.nelua
$(MAKE) --no-print-directory MYCFLAGS="$(ARCH_CFLAGS) $(FAST_CFLAGS) $(MYCFLAGS) -fprofile-use=pgo" clean-nelualua default
$(RM_DIR) pgo
###############################################################################
## Luac
LUAC=./$(NELUALUAC)
LUAC_DEFS=-DMAKE_LUAC
LUAC_SRCS=src/onelua.c
LUAC_HDRS=$(wildcard src/lua/*.h) $(wildcard src/lua/*.c)
# Compile Nelua's Lua compiler.
$(NELUALUAC): $(LUAC_SRCS) $(LUAC_HDRS)
$(CC) \
$(LUAC_DEFS) $(DEFS) $(MYDEFS) \
$(INCS) $(MYINCS) \
$(CFLAGS) $(MYCFLAGS) \
$(LUAC_SRCS) \
-o $(NELUALUAC) \
$(LIBS) $(MYLIBS) $(LDFLAGS) $(MYLDFLAGS)
###############################################################################
## Lua init script
XXD=xxd
# Generates src/luainit.c (requires xxd tool from Vim)
gen-luainit: src/luainit.lua
$(XXD) -i src/luainit.lua > src/luainit.h
###############################################################################
# Testing
LUA=./$(NELUALUA)
LUACHECK=luacheck
LUACOV=luacov
LUAMON=luamon -w nelua,spec,examples,lib,lualib,tests -e lua,nelua -q -x
# Run test suite.
test: $(NELUALUA)
$(LUA) spec/init.lua
# Run test suite, stop on the first error.
test-quick: $(NELUALUA)
@LESTER_QUIET=true LESTER_STOP_ON_FAIL=true $(LUA) spec/init.lua
# Run lua static analysis using lua check.
check:
@$(LUACHECK) -q .
# Generate coverage report.
coverage-genreport:
@$(LUACOV)
@$(LUA) spec/tools/covreporter.lua
# Run the test suite analyzing code coverage.
coverage-test: $(NELUALUA)
@$(MAKE) --no-print-directory clean-coverage
$(LUA) -lluacov spec/init.lua
@$(MAKE) --no-print-directory coverage-genreport
# Compile each example.
examples/*.nelua: %.nelua:
$(NELUA_RUN) -qb $@
.PHONY: examples/*.nelua
# Compile all examples.
compile-examples: $(NELUALUA) examples/*.nelua
# Run the test suite, code coverage, lua checker and compile all examples.
test-full: $(NELUALUA) coverage-test check compile-examples
# Run the test suite on any file change (requires luamon).
live-dev:
$(LUAMON) "make -Ss _live-dev"
_live-dev:
@clear
@$(MAKE) $(NELUALUA)
@$(MAKE) test-quick
@$(MAKE) check
###############################################################################
# Docker
# Variables for Docker
DOCKER_UID=$(shell id -u $(USER))
DOCKER_GID=$(shell id -g $(USER))
DOCKER_IMAGE=nelua
DOCKER_SHELL=/bin/bash
DOCKER=docker
DOCKER_RUN=$(DOCKER) run -u $(DOCKER_UID):$(DOCKER_GID) --rm -it -v "$(shell pwd):/mnt" $(DOCKER_IMAGE)
# Make the docker image used to test inside docker containers.
docker-image:
$(DOCKER) build -t "nelua" .
# Run tests inside a docker container.
docker-test:
$(DOCKER_RUN) make -s test
# Run the test suite, code coverage, lua checker and compile all examples and install.
docker-test-full:
$(MAKE) clean-nelualua
$(DOCKER_RUN) make coverage-test check compile-examples
# Get a shell inside a new docker container.
docker-shell:
$(DOCKER_RUN) $(DOCKER_SHELL)
###############################################################################
# Documentation
JEKYLL=bundle exec jekyll
# Compile documentation.
docs:
cd docs && $(JEKYLL) build
.PHONY: docs
# Serve documentation in a local web server, recompile on any change.
docs-serve:
cd docs && $(JEKYLL) serve
# Generate documentation (requires `nldoc` cloned and nelua installed).
docs-gen:
cd ../nldoc && nelua --script nelua-docs.lua ../nelua-lang
###############################################################################
# Install
# Install paths
PREFIX=/usr/local
DPREFIX=$(DESTDIR)$(PREFIX)
PREFIX_BIN=$(DPREFIX)/bin
PREFIX_LIB=$(DPREFIX)/lib
PREFIX_LIB_NELUA=$(PREFIX_LIB)/nelua
# Utilities
SED=sed
GIT=git
RM_FILE=rm -f
RM_DIR=rm -rf
MKDIR=mkdir -p
INSTALL_EXE=install -m755
INSTALL_FILE=install -m644
INSTALL_FILES=cp -Rf
ifdef WINMODE
RM_FILE=del /Q
RM_DIR=del /S /Q
else ifeq ($(OS), Windows_NT)
# maybe the shell has unix like tools (e.g. BusyBox)
RM_FILE=$(shell which rm) -f
RM_DIR=$(shell which rm) -rf
MKDIR=$(shell which mkdir) -p
INSTALL_EXE=$(shell which install) -m755
INSTALL_FILE=$(shell which install) -m644
INSTALL_FILES=$(shell which cp) -Rf
endif
# In git directory?
IS_GIT=$(shell $(GIT) rev-parse --is-inside-work-tree 2>$(DEVNULL))
# Patch 'version.lua' file using the current Git information.
install-version-patch:
ifeq ($(IS_GIT),true)
$(eval NELUA_GIT_HASH := $(shell $(GIT) rev-parse HEAD))
$(eval NELUA_GIT_DATE := $(shell $(GIT) log -1 --format=%ci))
$(eval NELUA_GIT_BUILD := $(shell $(GIT) rev-list HEAD --count))
$(SED) -i.bak 's/NELUA_GIT_HASH = nil/NELUA_GIT_HASH = "$(NELUA_GIT_HASH)"/' "$(PREFIX_LIB_NELUA)/lualib/nelua/version.lua"
$(SED) -i.bak 's/NELUA_GIT_DATE = nil/NELUA_GIT_DATE = "$(NELUA_GIT_DATE)"/' "$(PREFIX_LIB_NELUA)/lualib/nelua/version.lua"
$(SED) -i.bak 's/NELUA_GIT_BUILD = nil/NELUA_GIT_BUILD = $(NELUA_GIT_BUILD)/' "$(PREFIX_LIB_NELUA)/lualib/nelua/version.lua"
$(RM_FILE) "$(PREFIX_LIB_NELUA)/lualib/nelua/version.lua.bak"
endif
# Install Nelua using PREFIX into DESTDIR.
install: $(NELUALUA)
$(MKDIR) "$(PREFIX_BIN)"
$(INSTALL_EXE) $(NELUALUA) "$(PREFIX_BIN)/$(NELUALUA)"
$(INSTALL_EXE) nelua "$(PREFIX_BIN)/nelua"
$(RM_DIR) "$(PREFIX_LIB_NELUA)"
$(MKDIR) "$(PREFIX_LIB_NELUA)"
$(INSTALL_FILES) lualib "$(PREFIX_LIB_NELUA)/lualib"
$(INSTALL_FILES) lib "$(PREFIX_LIB_NELUA)/lib"
$(MAKE) --no-print-directory install-version-patch
# Install Nelua using this folder in the system.
install-as-symlink: $(NELUALUA)
$(MAKE) --no-print-directory uninstall
$(RM_FILE) "$(PREFIX_BIN)/$(NELUALUA)"
ln -fs "$(realpath $(NELUALUA))" "$(PREFIX_BIN)/$(NELUALUA)"
$(RM_FILE) "$(PREFIX_BIN)/nelua"
ln -fs "$(realpath nelua)" "$(PREFIX_BIN)/nelua"
# Uninstall Nelua
uninstall:
$(RM_FILE) "$(PREFIX_BIN)/$(NELUALUA)"
$(RM_FILE) "$(PREFIX_BIN)/nelua"
$(RM_DIR) "$(PREFIX_LIB_NELUA)"
###############################################################################
# Packaging
# Utilities
TAR_XZ=tar cfJ
ZIP_DIR=7z a -mm=Deflate -mx=9
STRIP=strip
# Package name
ifeq ($(SYS), Windows)
ARCHNAME=x86_64
else
ARCHNAME=$(shell uname -m)
endif
OSNAME=posix
ifeq ($(SYS), Linux)
OSNAME=linux
else ifeq ($(SYS), Windows)
OSNAME=windows
else ifeq ($(SYS), Darwin)
OSNAME=macos
endif
PKGDIR=pkg
PKGNAME=nelua-$(OSNAME)-$(ARCHNAME)-latest
package: $(NELUALUA)
$(RM_DIR) "$(PKGDIR)/$(PKGNAME)"
$(MKDIR) "$(PKGDIR)/$(PKGNAME)"
$(INSTALL_FILES) lib "$(PKGDIR)/$(PKGNAME)/lib"
$(INSTALL_FILES) lualib "$(PKGDIR)/$(PKGNAME)/lualib"
$(MAKE) --no-print-directory install-version-patch PREFIX_LIB_NELUA="$(PKGDIR)/$(PKGNAME)"
$(INSTALL_FILE) LICENSE "$(PKGDIR)/$(PKGNAME)/LICENSE"
ifeq ($(SYS), Windows)
$(INSTALL_EXE) nelua.bat "$(PKGDIR)/$(PKGNAME)/nelua.bat"
$(INSTALL_EXE) nelua-lua.exe "$(PKGDIR)/$(PKGNAME)/nelua-lua.exe"
$(STRIP) "$(PKGDIR)/$(PKGNAME)/nelua-lua.exe"
$(RM_FILE) "$(PKGDIR)/$(PKGNAME).zip"
cd $(PKGDIR); $(ZIP_DIR) "$(PKGNAME).zip" "$(PKGNAME)"
else
$(INSTALL_EXE) nelua "$(PKGDIR)/$(PKGNAME)/nelua"
$(INSTALL_EXE) nelua-lua "$(PKGDIR)/$(PKGNAME)/nelua-lua"
$(STRIP) "$(PKGDIR)/$(PKGNAME)/nelua-lua"
$(RM_FILE) "$(PKGDIR)/$(PKGNAME).tar.xz"
cd $(PKGDIR); $(TAR_XZ) "$(PKGNAME).tar.xz" "$(PKGNAME)"
endif
package-versioned:
$(MAKE) --no-print-directory package \
PKGNAME=nelua-$(OSNAME)-$(ARCHNAME)-$(shell $(NELUA_RUN) --semver)
###############################################################################
# Clean
CACHE_DIR=.cache
ifdef HOME
CACHE_DIR=$(HOME)/.cache/nelua
else ifdef USERPROFILE
CACHE_DIR=$(USERPROFILE)\\.cache\\nelua
endif
# Clean the nelua cache directory.
clean-cache:
$(RM_DIR) "$(CACHE_DIR)"
# Clean coverage files.
clean-coverage:
$(RM_FILE) luacov.report.out
$(RM_FILE) luacov.stats.out
# Clean the Lua interpreter.
clean-nelualua:
$(RM_FILE) $(NELUALUA)
# Clean the Lua interpreter.
clean-nelualuac:
$(RM_FILE) $(NELUALUAC)
clean-packages:
$(RM_DIR) pkg
# Clean documentation.
clean-docs:
cd docs && $(JEKYLL) clean
# Clean everything.
clean: clean-nelualua clean-cache clean-coverage clean-nelualuac clean-packages