forked from dkataskin/erlang-keccakf1600
-
Notifications
You must be signed in to change notification settings - Fork 7
/
erlang.mk
2404 lines (1955 loc) · 65.7 KB
/
erlang.mk
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.PHONY: all app deps search rel docs install-docs check tests clean distclean help erlang-mk
ERLANG_MK_FILENAME := $(realpath $(lastword $(MAKEFILE_LIST)))
ERLANG_MK_VERSION = 2.0.0-pre.2-63-g250b348-dirty
# Core configuration.
PROJECT ?= $(notdir $(CURDIR))
PROJECT := $(strip $(PROJECT))
PROJECT_VERSION ?= rolling
# Verbosity.
V ?= 0
verbose_0 = @
verbose_2 = set -x;
verbose = $(verbose_$(V))
gen_verbose_0 = @echo " GEN " $@;
gen_verbose_2 = set -x;
gen_verbose = $(gen_verbose_$(V))
# Temporary files directory.
ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk
export ERLANG_MK_TMP
# "erl" command.
ERL = erl +A0 -noinput -boot start_clean
# Platform detection.
ifeq ($(PLATFORM),)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
PLATFORM = linux
else ifeq ($(UNAME_S),Darwin)
PLATFORM = darwin
else ifeq ($(UNAME_S),SunOS)
PLATFORM = solaris
else ifeq ($(UNAME_S),GNU)
PLATFORM = gnu
else ifeq ($(UNAME_S),FreeBSD)
PLATFORM = freebsd
else ifeq ($(UNAME_S),NetBSD)
PLATFORM = netbsd
else ifeq ($(UNAME_S),OpenBSD)
PLATFORM = openbsd
else ifeq ($(UNAME_S),DragonFly)
PLATFORM = dragonfly
else ifeq ($(shell uname -o),Msys)
PLATFORM = msys2
else
$(error Unable to detect platform. Please open a ticket with the output of uname -a.)
endif
export PLATFORM
endif
# Core targets.
all:: deps app rel
# Noop to avoid a Make warning when there's nothing to do.
rel::
$(verbose) :
check:: tests
clean:: clean-crashdump
clean-crashdump:
ifneq ($(wildcard erl_crash.dump),)
$(gen_verbose) rm -f erl_crash.dump
endif
distclean:: clean distclean-tmp
distclean-tmp:
$(gen_verbose) rm -rf $(ERLANG_MK_TMP)
help::
$(verbose) printf "%s\n" \
"erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
"Copyright (c) 2013-2015 Loïc Hoguin <[email protected]>" \
"" \
"Usage: [V=1] $(MAKE) [target]..." \
"" \
"Core targets:" \
" all Run deps, app and rel targets in that order" \
" app Compile the project" \
" deps Fetch dependencies (if needed) and compile them" \
" search q=... Search for a package in the built-in index" \
" rel Build a release for this project, if applicable" \
" docs Build the documentation for this project" \
" install-docs Install the man pages for this project" \
" check Compile and run all tests and analysis for this project" \
" tests Run the tests for this project" \
" clean Delete temporary and output files from most targets" \
" distclean Delete all temporary and output files" \
" help Display this help and exit" \
" erlang-mk Update erlang.mk to the latest version"
# Core functions.
empty :=
space := $(empty) $(empty)
tab := $(empty) $(empty)
comma := ,
define newline
endef
define comma_list
$(subst $(space),$(comma),$(strip $(1)))
endef
# Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
define erlang
$(ERL) $(2) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
endef
ifeq ($(PLATFORM),msys2)
core_native_path = $(subst \,\\\\,$(shell cygpath -w $1))
else
core_native_path = $1
endif
ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
define core_http_get
wget --no-check-certificate -O $(1) $(2)|| rm $(1)
endef
else
define core_http_get.erl
ssl:start(),
inets:start(),
case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of
{ok, {{_, 200, _}, _, Body}} ->
case file:write_file("$(1)", Body) of
ok -> ok;
{error, R1} -> halt(R1)
end;
{error, R2} ->
halt(R2)
end,
halt(0).
endef
define core_http_get
$(call erlang,$(call core_http_get.erl,$(call core_native_path,$1),$2))
endef
endif
core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) -type f -name $(subst *,\*,$2)))
core_lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$(1)))))))))))))))))))))))))))
core_ls = $(filter-out $(1),$(shell echo $(1)))
# @todo Use a solution that does not require using perl.
core_relpath = $(shell perl -e 'use File::Spec; print File::Spec->abs2rel(@ARGV) . "\n"' $1 $2)
# Automated update.
ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk
ERLANG_MK_COMMIT ?=
ERLANG_MK_BUILD_CONFIG ?= build.config
ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
erlang-mk:
git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR)
ifdef ERLANG_MK_COMMIT
cd $(ERLANG_MK_BUILD_DIR) && git checkout $(ERLANG_MK_COMMIT)
endif
if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi
$(MAKE) -C $(ERLANG_MK_BUILD_DIR)
cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
rm -rf $(ERLANG_MK_BUILD_DIR)
# The erlang.mk package index is bundled in the default erlang.mk build.
# Search for the string "copyright" to skip to the rest of the code.
# Copyright (c) 2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: search
define pkg_print
$(verbose) printf "%s\n" \
$(if $(call core_eq,$(1),$(pkg_$(1)_name)),,"Pkg name: $(1)") \
"App name: $(pkg_$(1)_name)" \
"Description: $(pkg_$(1)_description)" \
"Home page: $(pkg_$(1)_homepage)" \
"Fetch with: $(pkg_$(1)_fetch)" \
"Repository: $(pkg_$(1)_repo)" \
"Commit: $(pkg_$(1)_commit)" \
""
endef
search:
ifdef q
$(foreach p,$(PACKAGES), \
$(if $(findstring $(call core_lc,$(q)),$(call core_lc,$(pkg_$(p)_name) $(pkg_$(p)_description))), \
$(call pkg_print,$(p))))
else
$(foreach p,$(PACKAGES),$(call pkg_print,$(p)))
endif
# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: distclean-deps
# Configuration.
ifdef OTP_DEPS
$(warning The variable OTP_DEPS is deprecated in favor of LOCAL_DEPS.)
endif
IGNORE_DEPS ?=
export IGNORE_DEPS
APPS_DIR ?= $(CURDIR)/apps
export APPS_DIR
DEPS_DIR ?= $(CURDIR)/deps
export DEPS_DIR
REBAR_DEPS_DIR = $(DEPS_DIR)
export REBAR_DEPS_DIR
dep_name = $(if $(dep_$(1)),$(1),$(if $(pkg_$(1)_name),$(pkg_$(1)_name),$(1)))
dep_repo = $(patsubst git://github.com/%,https://github.com/%, \
$(if $(dep_$(1)),$(word 2,$(dep_$(1))),$(pkg_$(1)_repo)))
dep_commit = $(if $(dep_$(1)_commit),$(dep_$(1)_commit),$(if $(dep_$(1)),$(word 3,$(dep_$(1))),$(pkg_$(1)_commit)))
ALL_APPS_DIRS = $(if $(wildcard $(APPS_DIR)/),$(filter-out $(APPS_DIR),$(shell find $(APPS_DIR) -maxdepth 1 -type d)))
ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(foreach dep,$(filter-out $(IGNORE_DEPS),$(BUILD_DEPS) $(DEPS)),$(call dep_name,$(dep))))
ifeq ($(filter $(APPS_DIR) $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
ifeq ($(ERL_LIBS),)
ERL_LIBS = $(APPS_DIR):$(DEPS_DIR)
else
ERL_LIBS := $(ERL_LIBS):$(APPS_DIR):$(DEPS_DIR)
endif
endif
export ERL_LIBS
export NO_AUTOPATCH
# Verbosity.
dep_verbose_0 = @echo " DEP " $(1);
dep_verbose_2 = set -x;
dep_verbose = $(dep_verbose_$(V))
# Core targets.
ifneq ($(SKIP_DEPS),)
deps::
else
deps:: $(ALL_DEPS_DIRS)
ifndef IS_APP
$(verbose) for dep in $(ALL_APPS_DIRS) ; do \
$(MAKE) -C $$dep IS_APP=1 || exit $$?; \
done
endif
ifneq ($(IS_DEP),1)
$(verbose) rm -f $(ERLANG_MK_TMP)/deps.log
endif
$(verbose) mkdir -p $(ERLANG_MK_TMP)
$(verbose) for dep in $(ALL_DEPS_DIRS) ; do \
if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/deps.log; then \
:; \
else \
echo $$dep >> $(ERLANG_MK_TMP)/deps.log; \
if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ]; then \
$(MAKE) -C $$dep IS_DEP=1 || exit $$?; \
else \
echo "Error: No Makefile to build dependency $$dep."; \
exit 2; \
fi \
fi \
done
endif
# Deps related targets.
# @todo rename GNUmakefile and makefile into Makefile first, if they exist
# While Makefile file could be GNUmakefile or makefile,
# in practice only Makefile is needed so far.
define dep_autopatch
if [ -f $(DEPS_DIR)/$(1)/erlang.mk ]; then \
$(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
$(call dep_autopatch_erlang_mk,$(1)); \
elif [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
if [ 0 != `grep -c "include ../\w*\.mk" $(DEPS_DIR)/$(1)/Makefile` ]; then \
$(call dep_autopatch2,$(1)); \
elif [ 0 != `grep -ci rebar $(DEPS_DIR)/$(1)/Makefile` ]; then \
$(call dep_autopatch2,$(1)); \
elif [ -n "`find $(DEPS_DIR)/$(1)/ -type f -name \*.mk -not -name erlang.mk -exec grep -i rebar '{}' \;`" ]; then \
$(call dep_autopatch2,$(1)); \
else \
$(call erlang,$(call dep_autopatch_app.erl,$(1))); \
fi \
else \
if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \
$(call dep_autopatch_noop,$(1)); \
else \
$(call dep_autopatch2,$(1)); \
fi \
fi
endef
define dep_autopatch2
if [ -f $(DEPS_DIR)/$1/src/$1.app.src.script ]; then \
$(call erlang,$(call dep_autopatch_appsrc_script.erl,$(1))); \
fi; \
$(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
if [ -f $(DEPS_DIR)/$(1)/rebar -o -f $(DEPS_DIR)/$(1)/rebar.config -o -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
$(call dep_autopatch_fetch_rebar); \
$(call dep_autopatch_rebar,$(1)); \
else \
$(call dep_autopatch_gen,$(1)); \
fi
endef
define dep_autopatch_noop
printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile
endef
# Overwrite erlang.mk with the current file by default.
ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
define dep_autopatch_erlang_mk
echo "include $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(DEPS_DIR)/app)/erlang.mk" \
> $(DEPS_DIR)/$1/erlang.mk
endef
else
define dep_autopatch_erlang_mk
:
endef
endif
define dep_autopatch_gen
printf "%s\n" \
"ERLC_OPTS = +debug_info" \
"include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile
endef
define dep_autopatch_fetch_rebar
mkdir -p $(ERLANG_MK_TMP); \
if [ ! -d $(ERLANG_MK_TMP)/rebar ]; then \
git clone -q -n -- https://github.com/rebar/rebar $(ERLANG_MK_TMP)/rebar; \
cd $(ERLANG_MK_TMP)/rebar; \
git checkout -q 791db716b5a3a7671e0b351f95ddf24b848ee173; \
$(MAKE); \
cd -; \
fi
endef
define dep_autopatch_rebar
if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \
fi; \
$(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
rm -f $(DEPS_DIR)/$(1)/ebin/$(1).app
endef
define dep_autopatch_rebar.erl
application:load(rebar),
application:set_env(rebar, log_level, debug),
Conf1 = case file:consult("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config)") of
{ok, Conf0} -> Conf0;
_ -> []
end,
{Conf, OsEnv} = fun() ->
case filelib:is_file("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)") of
false -> {Conf1, []};
true ->
Bindings0 = erl_eval:new_bindings(),
Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
Bindings = erl_eval:add_binding('SCRIPT', "$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings1),
Before = os:getenv(),
{ok, Conf2} = file:script("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings),
{Conf2, lists:foldl(fun(E, Acc) -> lists:delete(E, Acc) end, os:getenv(), Before)}
end
end(),
Write = fun (Text) ->
file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/Makefile)", Text, [append])
end,
Escape = fun (Text) ->
re:replace(Text, "\\\\$$", "\$$$$", [global, {return, list}])
end,
Write("IGNORE_DEPS += edown eper eunit_formatters meck node_package "
"rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n"),
Write("C_SRC_DIR = /path/do/not/exist\n"),
Write("C_SRC_TYPE = rebar\n"),
Write("DRV_CFLAGS = -fPIC\nexport DRV_CFLAGS\n"),
Write(["ERLANG_ARCH = ", rebar_utils:wordsize(), "\nexport ERLANG_ARCH\n"]),
fun() ->
Write("ERLC_OPTS = +debug_info\nexport ERLC_OPTS\n"),
case lists:keyfind(erl_opts, 1, Conf) of
false -> ok;
{_, ErlOpts} ->
lists:foreach(fun
({d, D}) ->
Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
({i, I}) ->
Write(["ERLC_OPTS += -I ", I, "\n"]);
({platform_define, Regex, D}) ->
case rebar_utils:is_arch(Regex) of
true -> Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
false -> ok
end;
({parse_transform, PT}) ->
Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
(_) -> ok
end, ErlOpts)
end,
Write("\n")
end(),
fun() ->
File = case lists:keyfind(deps, 1, Conf) of
false -> [];
{_, Deps} ->
[begin case case Dep of
{N, S} when is_atom(N), is_list(S) -> {N, {hex, S}};
{N, S} when is_tuple(S) -> {N, S};
{N, _, S} -> {N, S};
{N, _, S, _} -> {N, S};
_ -> false
end of
false -> ok;
{Name, Source} ->
{Method, Repo, Commit} = case Source of
{hex, V} -> {hex, V, undefined};
{git, R} -> {git, R, master};
{M, R, {branch, C}} -> {M, R, C};
{M, R, {ref, C}} -> {M, R, C};
{M, R, {tag, C}} -> {M, R, C};
{M, R, C} -> {M, R, C}
end,
Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
end end || Dep <- Deps]
end
end(),
fun() ->
case lists:keyfind(erl_first_files, 1, Conf) of
false -> ok;
{_, Files} ->
Names = [[" ", case lists:reverse(F) of
"lre." ++ Elif -> lists:reverse(Elif);
Elif -> lists:reverse(Elif)
end] || "src/" ++ F <- Files],
Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
end
end(),
Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
Write("\npreprocess::\n"),
Write("\npre-deps::\n"),
Write("\npre-app::\n"),
PatchHook = fun(Cmd) ->
case Cmd of
"make -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
"gmake -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
"make " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
"gmake " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
_ -> Escape(Cmd)
end
end,
fun() ->
case lists:keyfind(pre_hooks, 1, Conf) of
false -> ok;
{_, Hooks} ->
[case H of
{'get-deps', Cmd} ->
Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
{compile, Cmd} ->
Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
{Regex, compile, Cmd} ->
case rebar_utils:is_arch(Regex) of
true -> Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
false -> ok
end;
_ -> ok
end || H <- Hooks]
end
end(),
ShellToMk = fun(V) ->
re:replace(re:replace(V, "(\\\\$$)(\\\\w*)", "\\\\1(\\\\2)", [global]),
"-Werror\\\\b", "", [{return, list}, global])
end,
PortSpecs = fun() ->
case lists:keyfind(port_specs, 1, Conf) of
false ->
case filelib:is_dir("$(call core_native_path,$(DEPS_DIR)/$1/c_src)") of
false -> [];
true ->
[{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
end;
{_, Specs} ->
lists:flatten([case S of
{Output, Input} -> {ShellToMk(Output), Input, []};
{Regex, Output, Input} ->
case rebar_utils:is_arch(Regex) of
true -> {ShellToMk(Output), Input, []};
false -> []
end;
{Regex, Output, Input, [{env, Env}]} ->
case rebar_utils:is_arch(Regex) of
true -> {ShellToMk(Output), Input, Env};
false -> []
end
end || S <- Specs])
end
end(),
PortSpecWrite = fun (Text) ->
file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/c_src/Makefile.erlang.mk)", Text, [append])
end,
case PortSpecs of
[] -> ok;
_ ->
Write("\npre-app::\n\t$$\(MAKE) -f c_src/Makefile.erlang.mk\n"),
PortSpecWrite(io_lib:format("ERL_CFLAGS = -finline-functions -Wall -fPIC -I ~s/erts-~s/include -I ~s\n",
[code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
PortSpecWrite(io_lib:format("ERL_LDFLAGS = -L ~s -lerl_interface -lei\n",
[code:lib_dir(erl_interface, lib)])),
[PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
FilterEnv = fun(Env) ->
lists:flatten([case E of
{_, _} -> E;
{Regex, K, V} ->
case rebar_utils:is_arch(Regex) of
true -> {K, V};
false -> []
end
end || E <- Env])
end,
MergeEnv = fun(Env) ->
lists:foldl(fun ({K, V}, Acc) ->
case lists:keyfind(K, 1, Acc) of
false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
{_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
end
end, [], Env)
end,
PortEnv = case lists:keyfind(port_env, 1, Conf) of
false -> [];
{_, PortEnv0} -> FilterEnv(PortEnv0)
end,
PortSpec = fun ({Output, Input0, Env}) ->
filelib:ensure_dir("$(call core_native_path,$(DEPS_DIR)/$1/)" ++ Output),
Input = [[" ", I] || I <- Input0],
PortSpecWrite([
[["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
case $(PLATFORM) of
darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress";
_ -> ""
end,
"\n\nall:: ", Output, "\n\n",
"%.o: %.c\n\t$$\(CC) -c -o $$\@ $$\< $$\(CFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
"%.o: %.C\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
"%.o: %.cc\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
"%.o: %.cpp\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
[[Output, ": ", K, " = ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
Output, ": $$\(foreach ext,.c .C .cc .cpp,",
"$$\(patsubst %$$\(ext),%.o,$$\(filter %$$\(ext),$$\(wildcard", Input, "))))\n",
"\t$$\(CC) -o $$\@ $$\? $$\(LDFLAGS) $$\(ERL_LDFLAGS) $$\(DRV_LDFLAGS) $$\(EXE_LDFLAGS)",
case {filename:extension(Output), $(PLATFORM)} of
{[], _} -> "\n";
{_, darwin} -> "\n";
_ -> " -shared\n"
end])
end,
[PortSpec(S) || S <- PortSpecs]
end,
Write("\ninclude $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(DEPS_DIR)/app)/erlang.mk"),
RunPlugin = fun(Plugin, Step) ->
case erlang:function_exported(Plugin, Step, 2) of
false -> ok;
true ->
c:cd("$(call core_native_path,$(DEPS_DIR)/$1/)"),
Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
dict:store(base_dir, "", dict:new())}, undefined),
io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
end
end,
fun() ->
case lists:keyfind(plugins, 1, Conf) of
false -> ok;
{_, Plugins} ->
[begin
case lists:keyfind(deps, 1, Conf) of
false -> ok;
{_, Deps} ->
case lists:keyfind(P, 1, Deps) of
false -> ok;
_ ->
Path = "$(call core_native_path,$(DEPS_DIR)/)" ++ atom_to_list(P),
io:format("~s", [os:cmd("$(MAKE) -C $(call core_native_path,$(DEPS_DIR)/$1) " ++ Path)]),
io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
code:add_patha(Path ++ "/ebin")
end
end
end || P <- Plugins],
[case code:load_file(P) of
{module, P} -> ok;
_ ->
case lists:keyfind(plugin_dir, 1, Conf) of
false -> ok;
{_, PluginsDir} ->
ErlFile = "$(call core_native_path,$(DEPS_DIR)/$1/)" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
{ok, P, Bin} = compile:file(ErlFile, [binary]),
{module, P} = code:load_binary(P, ErlFile, Bin)
end
end || P <- Plugins],
[RunPlugin(P, preprocess) || P <- Plugins],
[RunPlugin(P, pre_compile) || P <- Plugins],
[RunPlugin(P, compile) || P <- Plugins]
end
end(),
halt()
endef
define dep_autopatch_app.erl
UpdateModules = fun(App) ->
case filelib:is_regular(App) of
false -> ok;
true ->
{ok, [{application, '$(1)', L0}]} = file:consult(App),
Mods = filelib:fold_files("$(call core_native_path,$(DEPS_DIR)/$1/src)", "\\\\.erl$$", true,
fun (F, Acc) -> [list_to_atom(filename:rootname(filename:basename(F)))|Acc] end, []),
L = lists:keystore(modules, 1, L0, {modules, Mods}),
ok = file:write_file(App, io_lib:format("~p.~n", [{application, '$(1)', L}]))
end
end,
UpdateModules("$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"),
halt()
endef
define dep_autopatch_appsrc_script.erl
AppSrc = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
AppSrcScript = AppSrc ++ ".script",
Bindings = erl_eval:new_bindings(),
{ok, Conf} = file:script(AppSrcScript, Bindings),
ok = file:write_file(AppSrc, io_lib:format("~p.~n", [Conf])),
halt()
endef
define dep_autopatch_appsrc.erl
AppSrcOut = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"; true -> AppSrcOut end,
case filelib:is_regular(AppSrcIn) of
false -> ok;
true ->
{ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
L1 = lists:keystore(modules, 1, L0, {modules, []}),
L2 = case lists:keyfind(vsn, 1, L1) of {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, "git"}); _ -> L1 end,
L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
end,
halt()
endef
define dep_fetch_git
git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1));
endef
define dep_fetch_git-submodule
git submodule update --init -- $(DEPS_DIR)/$1;
endef
define dep_fetch_hg
hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1));
endef
define dep_fetch_svn
svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
endef
define dep_fetch_cp
cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
endef
define dep_fetch_hex.erl
ssl:start(),
inets:start(),
{ok, {{_, 200, _}, _, Body}} = httpc:request(get,
{"https://s3.amazonaws.com/s3.hex.pm/tarballs/$(1)-$(2).tar", []},
[], [{body_format, binary}]),
{ok, Files} = erl_tar:extract({binary, Body}, [memory]),
{_, Source} = lists:keyfind("contents.tar.gz", 1, Files),
ok = erl_tar:extract({binary, Source}, [{cwd, "$(call core_native_path,$(DEPS_DIR)/$1)"}, compressed]),
halt()
endef
# Hex only has a package version. No need to look in the Erlang.mk packages.
define dep_fetch_hex
$(call erlang,$(call dep_fetch_hex.erl,$(1),$(strip $(word 2,$(dep_$(1))))));
endef
define dep_fetch_fail
echo "Error: Unknown or invalid dependency: $(1)." >&2; \
exit 78;
endef
# Kept for compatibility purposes with older Erlang.mk configuration.
define dep_fetch_legacy
$(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \
git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \
cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master);
endef
define dep_fetch
$(if $(dep_$(1)), \
$(if $(dep_fetch_$(word 1,$(dep_$(1)))), \
$(word 1,$(dep_$(1))), \
$(if $(IS_DEP),legacy,fail)), \
$(if $(filter $(1),$(PACKAGES)), \
$(pkg_$(1)_fetch), \
fail))
endef
define dep_target
$(DEPS_DIR)/$(call dep_name,$1):
$(eval DEP_NAME := $(call dep_name,$1))
$(eval DEP_STR := $(if $(filter-out $1,$(DEP_NAME)),$1,"$1 ($(DEP_NAME))"))
$(verbose) if test -d $(APPS_DIR)/$(DEP_NAME); then \
echo "Error: Dependency" $(DEP_STR) "conflicts with application found in $(APPS_DIR)/$(DEP_NAME)."; \
exit 17; \
fi
$(verbose) mkdir -p $(DEPS_DIR)
$(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1))
$(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ] \
&& [ ! -f $(DEPS_DIR)/$(1)/configure ]; then \
echo " AUTO " $(1); \
cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
fi
- $(verbose) if [ -f $(DEPS_DIR)/$(DEP_NAME)/configure ]; then \
echo " CONF " $(DEP_STR); \
cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \
fi
ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
$(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
echo " PATCH Downloading rabbitmq-codegen"; \
git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
fi; \
if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \
echo " PATCH Downloading rabbitmq-server"; \
git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \
fi; \
ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \
elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \
if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
echo " PATCH Downloading rabbitmq-codegen"; \
git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
fi \
else \
$$(call dep_autopatch,$(DEP_NAME)) \
fi
endif
endef
$(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep))))
ifndef IS_APP
clean:: clean-apps
clean-apps:
$(verbose) for dep in $(ALL_APPS_DIRS) ; do \
$(MAKE) -C $$dep clean IS_APP=1 || exit $$?; \
done
distclean:: distclean-apps
distclean-apps:
$(verbose) for dep in $(ALL_APPS_DIRS) ; do \
$(MAKE) -C $$dep distclean IS_APP=1 || exit $$?; \
done
endif
ifndef SKIP_DEPS
distclean:: distclean-deps
distclean-deps:
$(gen_verbose) rm -rf $(DEPS_DIR)
endif
# External plugins.
DEP_PLUGINS ?=
define core_dep_plugin
-include $(DEPS_DIR)/$(1)
$(DEPS_DIR)/$(1): $(DEPS_DIR)/$(2) ;
endef
$(foreach p,$(DEP_PLUGINS),\
$(eval $(if $(findstring /,$p),\
$(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\
$(call core_dep_plugin,$p/plugins.mk,$p))))
# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
# This file is part of erlang.mk and subject to the terms of the ISC License.
.PHONY: clean-app
# Configuration.
ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \
+warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec
COMPILE_FIRST ?=
COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
ERLC_EXCLUDE ?=
ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
ERLC_MIB_OPTS ?=
COMPILE_MIB_FIRST ?=
COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
# Verbosity.
app_verbose_0 = @echo " APP " $(PROJECT);
app_verbose_2 = set -x;
app_verbose = $(app_verbose_$(V))
appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
appsrc_verbose_2 = set -x;
appsrc_verbose = $(appsrc_verbose_$(V))
makedep_verbose_0 = @echo " DEPEND" $(PROJECT).d;
makedep_verbose_2 = set -x;
makedep_verbose = $(makedep_verbose_$(V))
erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
$(filter %.erl %.core,$(?F)));
erlc_verbose_2 = set -x;
erlc_verbose = $(erlc_verbose_$(V))
xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
xyrl_verbose_2 = set -x;
xyrl_verbose = $(xyrl_verbose_$(V))
asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
asn1_verbose_2 = set -x;
asn1_verbose = $(asn1_verbose_$(V))
mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
mib_verbose_2 = set -x;
mib_verbose = $(mib_verbose_$(V))
ifneq ($(wildcard src/),)
# Targets.
ifeq ($(wildcard ebin/test),)
app:: deps $(PROJECT).d
$(verbose) $(MAKE) --no-print-directory app-build
else
app:: clean deps $(PROJECT).d
$(verbose) $(MAKE) --no-print-directory app-build
endif
ifeq ($(wildcard src/$(PROJECT)_app.erl),)
define app_file
{application, $(PROJECT), [
{description, "$(PROJECT_DESCRIPTION)"},
{vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
{id$(comma)$(space)"$(1)"}$(comma))
{modules, [$(call comma_list,$(2))]},
{registered, []},
{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(DEPS))]}
]}.
endef
else
define app_file
{application, $(PROJECT), [
{description, "$(PROJECT_DESCRIPTION)"},
{vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
{id$(comma)$(space)"$(1)"}$(comma))
{modules, [$(call comma_list,$(2))]},
{registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(DEPS))]},
{mod, {$(PROJECT)_app, []}}
]}.
endef
endif
app-build: ebin/$(PROJECT).app
$(verbose) :
# Source files.
ERL_FILES = $(sort $(call core_find,src/,*.erl))
CORE_FILES = $(sort $(call core_find,src/,*.core))
# ASN.1 files.
ifneq ($(wildcard asn1/),)
ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1))
ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
define compile_asn1
$(verbose) mkdir -p include/
$(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(1)
$(verbose) mv asn1/*.erl src/
$(verbose) mv asn1/*.hrl include/
$(verbose) mv asn1/*.asn1db include/
endef
$(PROJECT).d:: $(ASN1_FILES)
$(if $(strip $?),$(call compile_asn1,$?))
endif
# SNMP MIB files.
ifneq ($(wildcard mibs/),)
MIB_FILES = $(sort $(call core_find,mibs/,*.mib))
$(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES)
$(verbose) mkdir -p include/ priv/mibs/
$(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $?
$(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?)))
endif
# Leex and Yecc files.
XRL_FILES = $(sort $(call core_find,src/,*.xrl))
XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES))))
ERL_FILES += $(XRL_ERL_FILES)
YRL_FILES = $(sort $(call core_find,src/,*.yrl))
YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES))))
ERL_FILES += $(YRL_ERL_FILES)
$(PROJECT).d:: $(XRL_FILES) $(YRL_FILES)
$(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $?)
# Erlang and Core Erlang files.
define makedep.erl
E = ets:new(makedep, [bag]),
G = digraph:new([acyclic]),
ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")),
Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles],
Add = fun (Mod, Dep) ->
case lists:keyfind(Dep, 1, Modules) of
false -> ok;
{_, DepFile} ->
{_, ModFile} = lists:keyfind(Mod, 1, Modules),
ets:insert(E, {ModFile, DepFile}),
digraph:add_vertex(G, Mod),
digraph:add_vertex(G, Dep),
digraph:add_edge(G, Mod, Dep)
end
end,
AddHd = fun (F, Mod, DepFile) ->
case file:open(DepFile, [read]) of
{error, enoent} -> ok;
{ok, Fd} ->
F(F, Fd, Mod),
{_, ModFile} = lists:keyfind(Mod, 1, Modules),
ets:insert(E, {ModFile, DepFile})
end
end,
Attr = fun
(F, Mod, behavior, Dep) -> Add(Mod, Dep);
(F, Mod, behaviour, Dep) -> Add(Mod, Dep);
(F, Mod, compile, {parse_transform, Dep}) -> Add(Mod, Dep);
(F, Mod, compile, Opts) when is_list(Opts) ->