escript.mk 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2014 Dave Cottlehuber <dch@skunkwerks.at>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. .PHONY: distclean-escript escript
  4. # Configuration.
  5. ESCRIPT_NAME ?= $(PROJECT)
  6. ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
  7. ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
  8. ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
  9. ESCRIPT_EMU_ARGS ?= -pa . \
  10. -noshell -noinput \
  11. -sasl errlog_type error \
  12. -escript main $(ESCRIPT_NAME)
  13. ESCRIPT_SHEBANG ?= /usr/bin/env escript
  14. ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
  15. # Core targets.
  16. distclean:: distclean-escript
  17. help::
  18. @printf "%s\n" "" \
  19. "Escript targets:" \
  20. " escript Build an executable escript archive" \
  21. # Plugin-specific targets.
  22. # Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
  23. # Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
  24. # Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
  25. # Software may only be used for the great good and the true happiness of all
  26. # sentient beings.
  27. define ESCRIPT_RAW
  28. 'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
  29. 'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
  30. ' [F || F <- A, not filelib:is_dir(F) ] end,'\
  31. 'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
  32. 'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
  33. 'Ez = fun(Escript) ->'\
  34. ' Static = Files([$(ESCRIPT_STATIC)]),'\
  35. ' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
  36. ' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
  37. ' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
  38. ' {archive, Archive, [memory]},'\
  39. ' {shebang, "$(ESCRIPT_SHEBANG)"},'\
  40. ' {comment, "$(ESCRIPT_COMMENT)"},'\
  41. ' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
  42. ' ]),'\
  43. ' file:change_mode(Escript, 8#755)'\
  44. 'end,'\
  45. 'Ez("$(ESCRIPT_NAME)").'
  46. endef
  47. ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
  48. escript:: distclean-escript deps app
  49. $(gen_verbose) erl -noshell -eval $(ESCRIPT_COMMAND) -s init stop
  50. distclean-escript:
  51. $(gen_verbose) rm -f $(ESCRIPT_NAME)