escript.mk 2.1 KB

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