-
Notifications
You must be signed in to change notification settings - Fork 0
/
OMyMakefile
902 lines (751 loc) · 30.1 KB
/
OMyMakefile
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
# ==========================
# OMyMakefile
# ==========================
# Useful functions to build OCaml projects
#| A flag to tell that we can use OMyMakefile functions
WithOMy=true
#| The prefix. Equal to the PREFIX environment variable
try
export PREFIX
# PREFIX=<prefix> omake or omake PREFIX=<prefix>
if $(not $(defined PREFIX)):
PREFIX=$(getenv PREFIX)
default
export PREFIX
try
# If opam is available we use its prefix
PREFIX=$(shell opam config var prefix)
default
eprintln(Specify PREFIX environment variable.)
exit 1
# OCamlFind basic config
# ======================================================================
#| OMy requires OCamlFind!
USE_OCAMLFIND = true
OCAMLFIND_DESTDIR= $(shell ocamlfind printconf destdir)
OCAMLFIND_QUERY(name) =
return $(shell ocamlfind query $(name))
#| We fed up with the warning
setenv(OCAMLFIND_IGNORE_DUPS_IN, $(OCAMLFIND_DESTDIR)/ocaml/compiler-libs)
# Misc tools
# ======================================================================
#|ditto.
mkdir_if_not_exists(dir) =
if $(not $(test -e $(dir))):
mkdir -p $(dir)
return
#| filter list
# example::
# is_url(file) =
# match $(file)
# case $'://'
# return true
# default
# return false
#
# DOWNLOAD_URLS[]=$(list_filter $(fun x, $(is_url $(x))), $(URLS))
list_filter(pred, list) =
result=
foreach(x, $(list))
export result
if $(apply $(pred), $(x))
result+=$(x)
return $(result)
# Common phonies
# ==================================================================
.PHONY: all install uninstall clean veryclean test
# Directories
# =====================================================================
#| The build root directory
BIG_ROOT=$(dir .)
# Installation mark files
# =======================================================================
#| To enable the installation mark files, you must define INSTALLED path variable
# for the mark file directory like INSTALLED=$(BIG_ROOT)/installed and make sure
# the directory $(INSTALLED) exists. This preparation must be done outside of
# this OMyMakefile.
#|Returns the installation mark files of $(packs)
Installed(packs) =
if $(defined INSTALLED)
return $(addsuffix .installed, $(addprefix $(INSTALLED)/, $(packs)))
else
return $(array) # empty
#|Create md5 sum file of files $(targets)
CreateCheckSum(pack, targets)=
chan=$(fopen $(pack), w)
fprintln($(chan), $(string $(targets)))
fprintln($(chan), $(string $(digest $(targets))))
close($(chan))
#|Create $(Installed $(pack)) file from the digests of $(targets)
CreateInstalled(pack, targets)=
println(dump md5 $(INSTALLED)/$(pack))
CreateCheckSum($(INSTALLED)/$(pack).installed, $(targets))
# OCaml
# ========================================================================
#| OCAMLANY is either byte or native
OCAMLANY =
if $(OCAMLC_EXISTS)
value $(OCAMLC)
else
value $(OCAMLOPT)
#|Path to the OCaml library directory
OCAML_WHERE = $(shell $(OCAMLANY) -where)
#|Version without extensions
OCAML_VERSION = $(shell $(OCAMLANY) -version | sed -e 's/^\([0-9.]*\).*/\1/')
#|Set NATIVE_ENABLED false locally if a project does not requre opt compilation.
NATIVE_SHARED_ENABLED = $(OCAMLOPT_EXISTS)
#|BYTE_ENABLED = true by default
BYTE_ENABLED = true
#|The default warning and errors. If you want to use different warning and error switches, redefine the variable.
OCAMLFLAGS = -g
#|OMyMakefile assumes installation with .cmt files
OCAMLANNOTFLAGS= -annot -bin-annot
#|Redefinition of compiler commands
OCamlC() =
value $(OCAMLFIND) $(OCAMLC) \
$(OCAMLPACKAGEFLAGS) \
$(LAZY_OCAMLFINDFLAGS) \
$(PREFIXED_OCAMLPACKS) \
$(OCAMLFLAGS) \
$(OCAMLWARNING_MAKE) \
$(OCAMLCFLAGS) \
$(OCAMLANNOTFLAGS) \
$(OCAMLPPFLAGS) \
$(PREFIXED_OCAMLINCLUDES)
OCamlOpt() =
value $(OCAMLFIND) $(OCAMLOPT) \
$(OCAMLPACKAGEFLAGS) \
$(LAZY_OCAMLFINDFLAGS) \
$(PREFIXED_OCAMLPACKS) \
$(OCAMLFLAGS)\
$(OCAMLWARNING_MAKE) \
$(OCAMLOPTFLAGS) \
$(OCAMLANNOTFLAGS) \
$(OCAMLPPFLAGS) \
$(PREFIXED_OCAMLINCLUDES)
#|Enable the backtrace
setenv(OCAMLRUNPARAM, b)
#|Global variable to be defined as empty. When building a package, it will be `-for-pack <name>`
OCAMLPACKAGEFLAGS=
# OCaml warnings
# ========================================================================
#|Warnings not to be printed, separted with space
OCAMLWARNING_IGNORE[] = 3 4 9 40 41 42 44 45 48
#|Warnings treated as non errors
OCAMLWARNING_NON_ERROR[] = 4 9 40 41 42 44 45 48
#|If it is false, warnings do not stop the compilation even if they are listed in OCAMLWARNING_NON_ERROR
#If you are in developing something, override it to true!
OCAMLWARNING_STOP_AT_ERROR_WARNING=false
#|The maximum warning value.
# This is the maximum warning ID NOT of your current OCaml compiler,
# BUT of the OCaml compiler used by the author of OMyMakefile.
OCAMLWARNING_MAX= 50
#|Function to build warning switches
OCAMLWARNING_MAKE()=
if $(OCAMLWARNING_STOP_AT_ERROR_WARNING)
return -w $(concat -,A $(OCAMLWARNING_IGNORE)) -warn-error +1..$(concat -, $(OCAMLWARNING_MAX) $(OCAMLWARNING_NON_ERROR))
else
return -w $(concat -,A $(OCAMLWARNING_IGNORE)) -warn-error a
# OCaml packages to use
# ========================================================================
#|Preinstalled libraries which are always available for normal ocaml.
#
# You may want to add the required packages which are built and installed out of OMy framework:
#
#::
#
# include OMyMakefile
#
# OCAML_PREINSTALLED_PACKS += llvm # llvm has been installed already, independently
#
# Subdirs()
#
# It includes "findlib" by default. If you want to build findlib in OMy framework, you have to remove it from the list.
OCAML_PREINSTALLED_PACKS[]= bigarray dynlink graphics num num-top stdlib str threads unix findlib compiler-libs compiler-libs.common compiler-libs.bytecomp compiler-libs.optcomp compiler-libs.toplevel
if $(and $(not $(NATIVE_ENABLED)) $(not $(BYTE_ENABLED)))
err. =
extends $(UnbuildableException)
message = $(string $"You must turn on either NATIVE_ENABLED or BYTE_ENABLED")
raise $(err)
#|We recommend to have OCAMLPARAM=_,g=1,annot=1,bin-annot=1
# Very clean
# ======================================================================
#| OCaml object files which are not real targets of OMake are not be
# auto-cleaned away by ``omake clean``. This is not good in some situations
# at linking.
WithOCamlVeryClean() =
veryclean:
rm -f *.cm* *.annot *.o *.a *.so *.exe *.byt *.opt *.run
# Packages
# =========================================================
#| OCaml packages required for compilation. MyCaml* functions automatically add necessary dependencies over packages in $(OCAMLPACKS).
#
# .. note:: They are also required for dependency analysis.
OCAMLPACKS[]=
# Dependencies
# =========================================================================
#|Returns packages managed by OMy framework
OMyManagedPackages(packages) =
# need to rename camlimages.jpeg => camlimages
packages_prefix=
foreach(x, $(packages))
export packages_prefix
match $(x)
case $"^\([^.]+\).*"
packages_prefix += $1
default
packages_prefix += $(x)
return $(set-diff $(packages_prefix), $(OCAML_PREINSTALLED_PACKS))
#|Add dependencies of OCaml compiled files (cmx, cmo, etc.) over $(files).
#
# .. note:: These functions introduce implicit rules: *you may need to export it, if you use this function in a local context.*
OCamlRequire(files) =
.SCANNER: scan-ocaml-%: $(files)
%.cmx %.cmo %.cmi %.cma %.cmxa %.cmxs %.annot %.cmt %.cmti: $(files)
export .RULE .PHONY
#|Add dependencies of OCaml compiled files (cmx, cmo, etc.) over $(packages).
# $(packages) listed in OCAML_PREINSTALLED_PACKS are ignored.
#
# .. note:: These functions introduce implicit rules: *you may need to export it, if you use this function in a local context.*
#
# .. note:: Usually you do not need to call this function. Use OCAMLPACKS variable instead.
OCamlRequirePackages(packages) =
export .RULE .PHONY
if $(defined INSTALLED)
required_packs = $(OMyManagedPackages $(packages))
# replacing the following two lines makes something wrong...
.SCANNER: scan-ocaml-%: $(Installed $(required_packs))
%.cmx %.cmo %.cmi %.cma %.cmxa %.cmxs %.annot %.cmt %.cmti: $(Installed $(required_packs))
#|``omake xxx.auto.mli`` generates .mli file from xxx.ml
%.auto.mli: %.ml
$(OCamlC) -i -c $< > $@
# Build rules
# ==========================================================
OCamlLibrary(name, files) =
# XXX: JYH: these variables should be marked private in 0.9.9
protected.name = $(file $(name))
protected.OFILES = $(addsuffix $(EXT_OBJ), $(files))
protected.CMOFILES = $(addsuffix .cmo, $(files))
protected.CMXFILES = $(addsuffix .cmx, $(files))
protected.CLIB = $(file $(name)$(EXT_LIB))
protected.BYTELIB = $(file $(name).cma)
protected.NATIVELIB = $(file $(name).cmxa)
protected.NATIVESHAREDLIB = $(file $(name).cmxs)
#
# Link commands
#
$(BYTELIB): $(CMOFILES)
$(OCAMLFIND) $(OCAMLLINK) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) $(OCAMLCFLAGS) \
$(OCAML_LIB_FLAGS) -a -o $@ $(OCamlLinkSort $(CMOFILES))
$(NATIVELIB) $(CLIB): $(CMXFILES) $(OFILES)
$(OCAMLFIND) $(OCAMLOPTLINK) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) $(OCAMLOPTFLAGS) \
$(OCAML_LIB_FLAGS) -a -o $(NATIVELIB) $(OCamlLinkSort $(CMXFILES))
$(NATIVESHAREDLIB): $(CMXFILES) $(OFILES)
$(OCAMLFIND) $(OCAMLOPTLINK) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) $(OCAMLOPTFLAGS) \
$(OCAML_LIB_FLAGS) -shared -o $(NATIVESHAREDLIB) $(OCamlLinkSort $(CMXFILES))
return $(array $(if $(NATIVE_ENABLED), $(NATIVELIB)), \
$(if $(NATIVE_ENABLED), $(CLIB)), \
$(if $(NATIVE_SHARED_ENABLED), $(NATIVESHAREDLIB)), \
$(if $(BYTE_ENABLED), $(BYTELIB)))
OCAML_BYTE_LINK_FLAGS =
# The original OCamlPackage does not work properly when omake -j n
# where n > 1. It sometimes creates .cmo/.cmx files with strange module
# signature checksum of itself, because it runs the byte and native
# packaging command in parallel at the same time. See details at:
#
# https://github.com/ocaml-omake/omake/issues/73
# https://github.com/ocaml/opam-repository/pull/8306
#
# Here, we try the packaging in sequence.
FixedOCamlPackage(name, files) =
# XXX: JYH: these variables should be marked private in 0.9.9
protected.OFILES = $(addsuffix $(EXT_OBJ), $(files))
protected.CMOFILES = $(addsuffix .cmo, $(files))
protected.CMXFILES = $(addsuffix .cmx, $(files))
protected.OBJ = $(file $(name)$(EXT_OBJ))
protected.CMO = $(file $(name).cmo)
protected.CMX = $(file $(name).cmx)
protected.CMI = $(file $(name).cmi)
protected.MLI = $(file $(name).mli)
protected.BYTE_TARGETS = $(CMO)
protected.NATIVE_TARGETS = $(CMX) $(OBJ)
protected.TARGETS = $(file $(name).cmt) $(CMI)
if $(NATIVE_ENABLED)
TARGETS += $(NATIVE_TARGETS)
export
if $(BYTE_ENABLED)
TARGETS += $(BYTE_TARGETS)
export
protected.BYTE_DEPS = $(CMOFILES)
protected.NATIVE_DEPS = $(CMXFILES) $(OFILES)
protected.DEPS =
if $(NATIVE_ENABLED)
DEPS += $(NATIVE_DEPS)
export
if $(BYTE_ENABLED)
DEPS += $(BYTE_DEPS)
export
# For now, do not consider about packages with .mli
# if $(target-exists $(MLI))
# $(CMI): $(MLI) :scanner: scan-ocaml-$(name).mli
# $(OCamlC) -c $<
$(TARGETS): $(DEPS)
if $(BYTE_ENABLED)
$(OCAMLFIND) $(OCAMLC) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) \
$(OCAMLCFLAGS) $(OCAML_LIB_FLAGS) -pack $(OCAMLANNOTFLAGS) -o $(CMO) $(OCamlLinkSort $(CMOFILES))
if $(NATIVE_ENABLED)
$(OCAMLFIND) $(OCAMLOPTLINK) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) \
$(OCAMLOPTFLAGS) $(OCAML_LIB_FLAGS) -pack $(OCAMLANNOTFLAGS) -o $(CMX) $(OCamlLinkSort $(CMXFILES))
return $(TARGETS)
# Add implicit dependencies over the packages declared in OCAMLPACKS
# If this function is used in a local scope, you may want to export.
AddLocalOCamlPackageDependencies() =
# We make sure the required libraries are installed
export .RULE .PHONY
OCamlRequirePackages($(OCAMLPACKS))
#| Special variable to gather the targets for ocamlfind installation
OCAMLFIND_INSTALL_TARGETS[]=
#| Install files in $(OCAMLFIND_INSTALL_TARGETS) as a package named $(pack).
MyOCamlfindInstall(pack, dirs) =
OCAMLFIND_INSTALL_TARGETS[] += $(file META)
MyOCamlfindSubdirs($(dirs))
if $(defined INSTALLED)
$(Installed $(pack)): $(OCAMLFIND_INSTALL_TARGETS)
$(OCAMLFIND) remove $(pack)
$(OCAMLFIND) install $(pack) $(OCAMLFIND_INSTALL_TARGETS)
CreateInstalled($(pack), $(OCAMLFIND_INSTALL_TARGETS))
install: $(Installed $(pack))
uninstall:
rm -f $(Installed $(pack))
$(OCAMLFIND) remove $(pack)
else
install: $(OCAMLFIND_INSTALL_TARGETS)
$(OCAMLFIND) remove $(pack)
$(OCAMLFIND) install $(pack) $(OCAMLFIND_INSTALL_TARGETS)
uninstall:
$(OCAMLFIND) remove $(pack)
OCAMLFIND_INSTALL_TARGETS= # clean up
export OCAMLFIND_INSTALL_TARGETS
CMODULES=
LINKOPTS=
# CMODULES, LINKOPTS
MyOCamlPackage(library_name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
export OCAMLPACKAGEFLAGS # This must be exported to outside to change the behaviour of the ocaml compilation
WithOCamlVeryClean()
AddLocalOCamlPackageDependencies()
CSTUBS=$(addsuffix .o,$(CMODULES))
CMO_S=$(addsuffix .cmo,$(library_name))
CMX_S=$(addsuffix .cmx,$(library_name))
CMO=$(library_name).cmo
CMI=$(library_name).cmi
CMA=$(library_name).cma
CMX=$(library_name).cmx
CMXA=$(library_name).cmxa
CMXS=$(library_name).cmxs
CMT=$(library_name).cmt
O=$(library_name).o
A=$(library_name).a
CSTUBLIBRARIES=
if $(not $(equal $(CMODULES), $(EMPTY)))
export CSTUBLIBRARIES
CSTUBLIBRARIES= dll$(library_name).so lib$(library_name).a
$(CMA) $(CMXA) $(CMXS) $(A) $(CSTUBLIBRARIES) : $(CSTUBS) $(CMO_S) $(CMX_S)
if $(not $(equal $(CMODULES), $(EMPTY)))
ocamlmklib -verbose -o $(library_name) $(CSTUBS) $(LINKOPTS) $(CMO_S) $(CMX_S)
$(OCamlOpt) -shared -o $(CMXS) $(CMX_S) # CR jfuruse: right?
else
$(OCamlC) -a -o $(CMA) $(CMO_S)
$(OCamlOpt) -a -o $(CMXA) $(CMX_S)
$(OCamlOpt) -shared -o $(CMXS) $(CMX_S)
## the followings are necessary for packing
OCAMLPACKAGEFLAGS += -for-pack $(capitalize $(library_name))
## build rule
.DEFAULT: $(FixedOCamlPackage $(library_name), $(files))
## clean
AutoClean()
clean:
rm -f $(CMT)
## installation targets
# CR jfuruse: x.cmi is required if x.mli does not exist!
OCAMLFIND_INSTALL_TARGETS[] += $(file $(glob i, *.mli) $(CMI) $(CMO) $(CMX) $(CMA) $(CMXA) $(CMXS) $(CMT) $(O) $(A) $(CSTUBLIBRARIES))
#| Add rules to build OCaml library $(name)
# name
# Name of the library
# files
# OCaml module name (without .ml)
#
# .. note :: Probably you should use MyOCamlPackage
MyOCamlLibrary(library_name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
WithOCamlVeryClean()
AddLocalOCamlPackageDependencies()
protected.CMIFILES = $(addsuffix .cmi, $(files))
protected.CMXFILES = $(addsuffix .cmx, $(files))
targets = $(OCamlLibrary $(library_name), $(files)) $(CMIFILES) $(CMXFILES) $(glob i, *.mli)
cmts = $(addsuffix .cmt, $(removesuffix $(glob i, *.ml)))
cmts += $(addsuffix .cmti, $(removesuffix $(glob i, *.mli)))
targets[] += $(cmts)
.DEFAULT: $(targets)
# The following clean the files twice if MyOCamlPacakge coexists,
# but who cases ?
AutoClean()
clean:
rm -f $(cmts)
## installation targets
# CR jfuruse: x.cmi is required if x.mli does not exist!
OCAMLFIND_INSTALL_TARGETS[] += $(file $(targets))
############################################################## build ocaml exec
MyOCamlGenericProgram(name, files) =
if $(equal $(name), test)
err. =
extends $(UnbuildableException)
message = $"Cannot create executable named $(absname $(name)), since it is confusing with the .PHONY target 'test'"
target=$(file $(name))
raise $(err)
export .RULE .PHONY
WithOCamlVeryClean()
AddLocalOCamlPackageDependencies()
# We must write these two separately, or something messy happens!
$(name).run: $(Installed $(OMyManagedPackages $(OCAMLPACKS)))
$(name).opt: $(Installed $(OMyManagedPackages $(OCAMLPACKS)))
# CR jfuruse: forgot to add the deps over the packages!
.DEFAULT: $(OCamlProgram $(name), $(files))
# The following clean the files twice if MyOCamlPackge coexists,
# but who cases ?
AutoClean()
#| Add a rule to build a test program $(name)
# name
# Name of the program
# files
# OCaml module names (without .ml)
MyOCamlTestProgram(name, files, ?autorun=true, ?mustfail=false) =
export .RULE .PHONY
# We need -linkall not to miss any module for test
OCAML_LINK_FLAGS += -linkall
MyOCamlGenericProgram($(name), $(files))
if $(autorun)
test: $(name)$(EXE)
$(eprintln Testing $(fullname $(name)$(EXE))...)
if $(mustfail)
section:
ret=$(shell-code ./$(name)$(EXE))
if $(ret) = 0
eprintln($(fullname $(name)$(EXE)): must fail but succeeded)
exit 2
else
./$(name)$(EXE)
#| Add a rule to build a program $(name)
# name
# Name of the program
# files
# OCaml module names (without .ml)
# In addition to MyOCamlTestProgram, the binary is installed by omake install
MyOCamlProgram(name, files) =
export .RULE .PHONY
WithOCamlVeryClean()
MyOCamlGenericProgram($(name), $(files))
if $(not $(defined NO_INSTALL))
install: $(name)$(EXE) $(name).run $(name).opt
install $(name)$(EXE) $(name).run $(name).opt $(PREFIX)/bin
uninstall:
rm -f $(PREFIX)/bin/$(name)$(EXE) $(PREFIX)/bin/$(name).run $(PREFIX)/bin/$(name).opt
MyOCamlApplication(name, files) =
export .RULE .PHONY
MyOCamlProgram($(name), $(files))
# This does not install exe
MyOCamlPPX(name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
WithOCamlVeryClean()
# We need -linkall not to miss any module
OCAML_LINK_FLAGS += -linkall
MyOCamlGenericProgram($(name), $(files))
OCAMLFIND_INSTALL_TARGETS[]+=$(file $(name).opt $(name).run $(name)$(EXE))
# OMake glitch!!!!
# If we use 'export' w/o argument, it export back the function arguments
# to its caller countext!!!
MyOCamlPPXMigratePackage(name1, files1) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
export OCAMLPACKAGEFLAGS # This must be exported to outside to change the behaviour of the ocaml compilation
PPXMigrateFiles()
MyOCamlPackage($(name1)_plugin, $(files1) migrate_register)
MyOCamlPPX($(name1), $(files1) legacy)
MyOCamlPPX($(name1)_migrate, $(files1) migrate_register migrate)
MyOCamlPPXMigrateLibrary(name1, files1, legacy) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
PPXMigrateFiles()
# MyOCamlLibrary($(name1)_plugin, $(files1) migrate_register)
MyOCamlPPX($(name1), $(files1) $(legacy))
# MyOCamlPPX($(name1)_migrate, $(files1) migrate_register migrate)
string_to_file(path,s) =
section:
stdout=$(fopen $(path), w)
println($(s))
close($(stdout))
PPXMigrateFiles() =
legacy.ml:
string_to_file($@, let () = Ppx.legacy_main ())
migrate.ml:
string_to_file($@, let () = Migrate_parsetree.Driver.run_main ())
migrate_register.ml:
string_to_file($@, let () = Ppx.register ())
MyOCamlFindPackage(findlib_name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
AddLocalOCamlPackageDependencies()
targets[]= META $(files)
if $(defined INSTALLED)
$(Installed $(findlib_name)): $(targets)
$(OCAMLFIND) remove $(findlib_name)
$(OCAMLFIND) install $(findlib_name) $(targets)
CreateInstalled($(findlib_name), $(targets))
install: $(Installed $(findlib_name))
uninstall:
rm -f $(Installed $(findlib_name))
$(OCAMLFIND) remove $(findlib_name)
else
install: $(targets)
$(OCAMLFIND) remove $(findlib_name)
$(OCAMLFIND) install $(findlib_name) $(targets)
uninstall:
$(OCAMLFIND) remove $(findlib_name)
#| Add a rule to build a toplevel $(name)
# name
# Name of the program
# files
# OCaml module names (without .ml)
OCamlTop(name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
# XXX: JYH: these variables should be marked private in 0.9.9
protected.CMOFILES = $(addsuffix .cmo, $(files))
# protected.CMXFILES = $(addsuffix .cmx, $(files))
# protected.OFILES = $(addsuffix $(EXT_OBJ), $(files))
protected.CMAFILES = $(addsuffix .cma, $(OCAML_LIBS))
# protected.CMXAFILES = $(addsuffix .cmxa, $(OCAML_LIBS))
protected.ARFILES = $(addsuffix $(EXT_LIB), $(OCAML_LIBS))
protected.CMA_OTHER_FILES = $(addsuffix .cma, $(OCAML_OTHER_LIBS))
# protected.CMXA_OTHER_FILES = $(addsuffix .cmxa, $(OCAML_OTHER_LIBS))
protected.CLIBS = $(addsuffix $(EXT_LIB), $(OCAML_CLIBS))
protected.name = $(file $(name))
protected.PROG = $(file $(name)$(EXE))
protected.BYTEPROG = $(file $(name).run)
# protected.OPTPROG = $(file $(name).opt)
#
# Rules to build byte-code and native targets
#
$(BYTEPROG): $(CMAFILES) $(CMOFILES) $(CLIBS)
$(OCAMLFIND) $(OCAMLMKTOP) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLFLAGS) $(OCAMLCFLAGS)\
$(OCAMLWARNING_MAKE) \
$(PREFIXED_OCAMLINCLUDES) $(OCAML_BYTE_LINK_FLAGS)\
-o $@ $(CMA_OTHER_FILES) $(CMAFILES) $(OCamlLinkSort $(CMOFILES))\
$(CLIBS) $(OCAML_LINK_FLAGS)
$(PROG): $(BYTEPROG)
ln-or-cp $< $@
return $(array $(PROG), $(BYTEPROG))
MyOCamlTop(name, files) =
export .RULE .PHONY
export OCAMLFIND_INSTALL_TARGETS
WithOCamlVeryClean()
AddLocalOCamlPackageDependencies()
$(name): $(Installed $(OMyManagedPackages $(OCAMLPACKS)))
# CR jfuruse: forgot to add the deps over the packages!
.DEFAULT: $(OCamlTop $(name), $(files))
# The following clean the files twice if MyOCamlPackge coexists,
# but who cases ?
AutoClean()
if $(not $(defined NO_INSTALL))
install: $(name) $(name).run
install $(name) $(name).run $(PREFIX)/bin
uninstall:
rm -f $(PREFIX)/bin/$(name) $(PREFIX)/bin/$(name).run
# Auto clean
# ====================================================================
#| Install clean command which cleans all the target files exists under the directory. Use with care.
AutoClean()=
export .RULE .PHONY
clean:
rm -f $(filter-proper-targets $(ls R, .))
# Subdir traversal
# =====================================================================
#| Recursively traverse the subdirs except $(dirs)
Subdirs_except(dirs) =
# need to export since .SUBDIRS is evaluated in the global scope
export VISIT_SUBDIRS
# println(PWD: $(shell pwd))
sub_omakefiles = $(glob i, */OMakefile)
subdirs = $(sub_omakefiles.map $(dirname))
VISIT_SUBDIRS=$(set-diff $(subdirs), $(dirs))
# The rule
.SUBDIRS: $(VISIT_SUBDIRS)
#| Recursively traverse all the subdirs
Subdirs() =
Subdirs_except($(array))
#| Recursively traverse the given subdirs $(dirs)
Subdirs_only(dirs) =
.SUBDIRS: $(dirs)
#| Recursively traverse the subdirs. The install targets are preserved.
MyOCamlfindSubdirs(subdirs) =
export OCAMLFIND_INSTALL_TARGETS # track the targets
.SUBDIRS: $(subdirs)
# Dependency dot files for Graphviz
# ======================================================================
#| Add a rule for ``depend.dot`` for a dependency graph of OCaml files in the current directory
Dot() =
depend.dot: $(ls *.ml *.mli)
$(OCAMLFIND) ocamldoc -I +threads $(OCAMLPACKAGEFLAGS) $(LAZY_OCAMLFINDFLAGS) $(PREFIXED_OCAMLPACKS) $(OCAMLPPFLAGS) $(PREFIXED_OCAMLINCLUDES) -dot -dot-include-all -dot-reduce $+ -o $@
# Build .merlin file
# ======================================================================
WithMerlin() =
export .RULE .PHONY
.merlin:
section:
chan=$(fopen $@, wb)
fprintln($(chan), PKG $(string $(OCAMLPACKS)))
foreach(x, $(OCAMLINCLUDES))
fprintln($(chan), S $(x))
fprintln($(chan), FLG $(OCAMLWARNING_MAKE))
close($(chan))
.DEFAULT: .merlin
########################################################################
# Generic build rules.
#
# The order of the %.cmi rules is important.
# The most recent definition is used first, if it applies.
# 1. The .cmi is generated from the .mli, if it exists
# 2. Otherwise it is generated from the .ml
#
# In case 2, make sure to use the same command text that is used for
# generating the .cmo or .cmx file. This will prevent the compiler
# from being called twice: once to generate the .cmi file, and again
# for the .cmo or .cmx file.
#
%.cmt %.annot: %.ml
section rule
if $(and $(NATIVE_ENABLED) $(BYTE_ENABLED))
if $(target-exists %.mli)
%.cmo %.cmx %$(EXT_OBJ) %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
%.cmo %.cmx %$(EXT_OBJ) %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
elseif $(NATIVE_ENABLED) # No BYTE_ENABLED
if $(target-exists %.mli)
%.cmx $(EXT_OBJ) %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
else
%.cmx $(EXT_OBJ) %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
else # BYTE_ENABLED or nothing specified
if $(target-exists %.mli)
%.cmo %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
else
%.cmo %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
%.cmx: %.ml
section rule
if $(not $(NATIVE_ENABLED))
err. =
extends $(UnbuildableException)
message = $(string $"You are trying to build OCaml native code file: "%.cmx$"
However, the NATIVE_ENABLED flag is not set.
Include the following definition in your OMakefile
if you really want to build this file.
NATIVE_ENABLED = true")
target = $(file %.cmx)
raise $(err)
elseif $(BYTE_ENABLED)
if $(target-exists %.mli)
%.cmo %.cmx %$(EXT_OBJ) %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
%.cmx %$(EXT_OBJ) %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
if $(target-exists %.mli)
%.cmx %$(EXT_OBJ) %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
else
%.cmx %$(EXT_OBJ) %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
%$(EXT_OBJ): %.ml
section rule
if $(not $(NATIVE_ENABLED))
err. =
extends $(UnbuildableException)
message = $(string $"You are trying to build OCaml native code file: "%$(EXT_OBJ)$"
However, the NATIVE_ENABLED flag is not set.
Include the following definition in your OMakefile
if you really want to build this file.
NATIVE_ENABLED = true")
target = $(file %.cmx)
raise $(err)
elseif $(BYTE_ENABLED)
if $(target-exists %.mli)
%$(EXT_OBJ) %.cmx %.cmo %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
%$(EXT_OBJ) %.cmi %.cmx %.cmo %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
if $(target-exists %.mli)
%$(EXT_OBJ) %.cmx %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
else
%$(EXT_OBJ) %.cmi %.cmx %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
%.cmo: %.ml
section rule
if $(not $(BYTE_ENABLED))
err. =
extends $(UnbuildableException)
message = $(string $"You are trying to build OCaml native code file: "%.cmo$"
However, the BYTE_ENABLED flag is not set.
Include the following definition in your OMakefile
if you really want to build this file.
BYTE_ENABLED = true")
target = $(file %.cmx)
raise $(err)
elseif $(NATIVE_ENABLED)
if $(target-exists %.mli)
%.cmx %$(EXT_OBJ) %.cmo %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
%.cmx %$(EXT_OBJ) %.cmo %.cmt %.annot %.cmi: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
if $(target-exists %.mli)
%.cmo %.cmt %.annot: %.ml %.cmi :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
else
%.cmo %.cmi %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
%.cmi: %.ml
section rule
if $(BYTE_ENABLED)
if $(NATIVE_ENABLED)
%.cmi %.cmo %.cmx %$(EXT_OBJ) %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
$(OCamlOpt) -c $<
else
%.cmi %.cmo %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlC) -c $<
else
%.cmi %.cmx %$(EXT_OBJ) %.cmt %.annot: %.ml :scanner: scan-ocaml-%.ml
$(OCamlOpt) -c $<
%.cmi %.cmti: %.mli :scanner: scan-ocaml-%.mli
$(OCamlC) -c $<