summaryrefslogtreecommitdiffstats
path: root/src/OCamlMakefile
blob: cc71121954362bdd788498204d81ad4db4fd10c1 (plain)
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
###########################################################################
#                              OCamlMakefile
#                  Copyright (C) 1999-2002  Markus Mottl
#
#                             For updates see:
#                http://www.oefai.at/~markus/ocaml_sources
#
#        $Id: OCamlMakefile 215492 2002-11-14 20:03:21Z prigaux $
#
###########################################################################

# Set these variables to the names of the sources to be processed and
# the result variable. Order matters during linkage!

ifndef SOURCES
  SOURCES := foo.ml
endif
export SOURCES

ifndef RES_CLIB_SUF
  RES_CLIB_SUF := _stubs
endif
export RES_CLIB_SUF

ifndef RESULT
  RESULT := foo
endif
export RESULT

ifndef DOC_FILES
  DOC_FILES := $(filter %.mli, $(SOURCES))
endif
export DOC_FILES

export BCSUFFIX
export NCSUFFIX

ifndef TOPSUFFIX
  TOPSUFFIX := .top
endif

export TOPSUFFIX

# Eventually set include- and library-paths, libraries to link,
# additional compilation-, link- and ocamlyacc-flags
# Path- and library information needs not be written with "-I" and such...
# Define THREADS if you need it, otherwise leave it unset (same for
# USE_CAMLP4)!

export THREADS
export USE_CAMLP4

export INCDIRS
export LIBDIRS
export EXTLIBDIRS
export OCAML_DEFAULT_DIRS
export OCAML_LIB_INSTALL

export LIBS
export CLIBS

export OCAMLFLAGS
export OCAMLNCFLAGS
export OCAMLBCFLAGS

export OCAMLLDFLAGS
export OCAMLNLDFLAGS
export OCAMLBLDFLAGS

ifndef OCAMLCPFLAGS
  OCAMLCPFLAGS := a
endif

export OCAMLCPFLAGS

export YFLAGS
export IDLFLAGS

export OCAMLDOCFLAGS

export DVIPSFLAGS

export STATIC

# Add a list of optional trash files that should be deleted by "make clean"
export TRASH

####################  variables depending on your OCaml-installation

ifdef MINGW
  export MINGW
  WIN32   := 1
endif
ifdef MSVC
  export MSVC
  WIN32   := 1
  EXT_OBJ := obj
  EXT_LIB := lib
  ifeq ($(CC),gcc)
    # work around GNU Make default value
    ifdef THREADS
      CC := cl /MT
    else
      CC := cl
    endif
  endif
  ifeq ($(CXX),g++)
    # work around GNU Make default value
    CXX := $(CC)
  endif
  CFLAG_O := -Fo
endif
ifdef WIN32
  EXT_CXX := cpp
  EXE     := .exe
endif

ifndef EXT_OBJ
  EXT_OBJ := o
endif
ifndef EXT_LIB
  EXT_LIB := a
endif
ifndef EXT_CXX
  EXT_CXX := cc
endif
ifndef EXE
  EXE := # empty
endif
ifndef CFLAG_O
  CFLAG_O := -o # do not delete this comment (preserves trailing whitespace)!
endif

export CC
export CXX
export CFLAGS
export CXXFLAGS
export LDFLAGS

BCRESULT  := $(addsuffix $(BCSUFFIX), $(RESULT))
NCRESULT  := $(addsuffix $(NCSUFFIX), $(RESULT))
TOPRESULT := $(addsuffix $(TOPSUFFIX), $(RESULT))

ifndef OCAMLC
  OCAMLC := ocamlc -g
endif

export OCAMLC

ifndef OCAMLOPT
  OCAMLOPT := ocamlopt -g
endif

export OCAMLOPT

ifndef OCAMLMKTOP
  OCAMLMKTOP := ocamlmktop
endif

export OCAMLMKTOP

ifndef OCAMLCP
  OCAMLCP := ocamlcp
endif

export OCAMLCP

ifndef OCAMLDEP
  OCAMLDEP := ocamldep
endif

export OCAMLDEP

ifndef OCAMLLEX
  OCAMLLEX := ocamllex
endif

export OCAMLLEX

ifndef OCAMLYACC
  OCAMLYACC := ocamlyacc
endif

export OCAMLYACC

ifndef CAMELEON_REPORT
  CAMELEON_REPORT := report
endif

ifndef CAMELEON_REPORT_FLAGS
  CAMELEON_REPORT_FLAGS :=
endif

ifndef CAMELEON_ZOGGY
  CAMELEON_ZOGGY := camlp4o pa_zog.cma pr_o.cmo
endif

ifndef CAMELEON_ZOGGY_FLAGS
  CAMELEON_ZOGGY_FLAGS := 
endif

ifndef CAMLIDL
  CAMLIDL := camlidl
endif

export CAMLIDL

ifndef CAMLIDLDLL
  CAMLIDLDLL := camlidldll
endif

export CAMLIDLDLL

ifndef NOIDLHEADER
  MAYBE_IDL_HEADER := -header
endif

export NOIDLHEADER

ifndef CAMLP4
  CAMLP4 := camlp4
endif

export CAMLP4

ifndef OCAMLDOC
  OCAMLDOC := ocamldoc
endif

export OCAMLDOC

ifndef LATEX
  LATEX := latex
endif

export LATEX

ifndef DVIPS
  DVIPS := dvips
endif

export DVIPS

ifndef PS2PDF
  PS2PDF := ps2pdf
endif

export PS2PDF

ifndef OCAMLMAKEFILE
  OCAMLMAKEFILE := OCamlMakefile
endif

export OCAMLMAKEFILE

ifndef OCAMLLIBPATH
  OCAMLLIBPATH := \
    $(shell $(OCAMLC) 2>/dev/null -where || echo /usr/local/lib/ocaml)
endif

export OCAMLLIBPATH

ifndef OCAML_LIB_INSTALL
  OCAML_LIB_INSTALL := $(OCAMLLIBPATH)/contrib
endif

export OCAML_LIB_INSTALL

###########################################################################

####################  change following sections only if
####################    you know what you are doing!

# delete target files when a build command fails
.PHONY: .DELETE_ON_ERROR
.DELETE_ON_ERROR:

# for pedants using "--warn-undefined-variables"
export MAYBE_IDL
export REAL_RESULT
export CAMLIDLFLAGS
export THREAD_FLAG
export RES_CLIB
export MAKEDLL

SHELL := /bin/sh

MLDEPDIR := ._d
BCDIDIR  := ._bcdi
NCDIDIR  := ._ncdi

FILTER_EXTNS := %.mli %.ml %.mll %.mly %.idl %.c %.$(EXT_CXX) %.rep %.zog

FILTERED     := $(filter $(FILTER_EXTNS), $(SOURCES))
SOURCE_DIRS  := $(filter-out ./, $(sort $(dir $(FILTERED))))

FILTERED_REP := $(filter %.rep, $(FILTERED))
DEP_REP      := $(FILTERED_REP:%.rep=$(MLDEPDIR)/%.d)
AUTO_REP     := $(FILTERED_REP:.rep=.ml)

FILTERED_ZOG := $(filter %.zog, $(FILTERED))
DEP_ZOG      := $(FILTERED_ZOG:%.zog=$(MLDEPDIR)/%.d)
AUTO_ZOG     := $(FILTERED_ZOG:.zog=.ml)

FILTERED_ML  := $(filter %.ml, $(FILTERED))
DEP_ML       := $(FILTERED_ML:%.ml=$(MLDEPDIR)/%.d)

FILTERED_MLI := $(filter %.mli, $(FILTERED))
DEP_MLI      := $(FILTERED_MLI:.mli=.di)

FILTERED_MLL := $(filter %.mll, $(FILTERED))
DEP_MLL      := $(FILTERED_MLL:%.mll=$(MLDEPDIR)/%.d)
AUTO_MLL     := $(FILTERED_MLL:.mll=.ml)

FILTERED_MLY := $(filter %.mly, $(FILTERED))
DEP_MLY      := $(FILTERED_MLY:%.mly=$(MLDEPDIR)/%.d) $(FILTERED_MLY:.mly=.di)
AUTO_MLY     := $(FILTERED_MLY:.mly=.mli) $(FILTERED_MLY:.mly=.ml)

FILTERED_IDL := $(filter %.idl, $(FILTERED))
DEP_IDL      := $(FILTERED_IDL:%.idl=$(MLDEPDIR)/%.d) $(FILTERED_IDL:.idl=.di)
C_IDL        := $(FILTERED_IDL:%.idl=%_stubs.c) $(FILTERED_IDL:.idl=.h)
OBJ_C_IDL    := $(FILTERED_IDL:%.idl=%_stubs.$(EXT_OBJ))
AUTO_IDL     := $(FILTERED_IDL:.idl=.mli) $(FILTERED_IDL:.idl=.ml) $(C_IDL)

FILTERED_C_CXX := $(filter %.c %.$(EXT_CXX), $(FILTERED))
OBJ_C_CXX      := $(FILTERED_C_CXX:.c=.$(EXT_OBJ))
OBJ_C_CXX      := $(OBJ_C_CXX:.$(EXT_CXX)=.$(EXT_OBJ))

PRE_TARGETS  += $(AUTO_MLL) $(AUTO_MLY) $(AUTO_IDL) $(AUTO_ZOG) $(AUTO_REP)

ALL_DEPS     := $(DEP_ML) $(DEP_MLI) $(DEP_MLL) $(DEP_MLY) $(DEP_IDL) $(DEP_ZOG) $(DEP_REP)

MLDEPS       := $(filter %.d, $(ALL_DEPS))
MLIDEPS      := $(filter %.di, $(ALL_DEPS))
BCDEPIS      := $(MLIDEPS:%.di=$(BCDIDIR)/%.di)
NCDEPIS      := $(MLIDEPS:%.di=$(NCDIDIR)/%.di)

ALLML        := $(filter %.mli %.ml %.mll %.mly %.idl %.rep %.zog, $(FILTERED)) 

IMPLO_INTF   := $(ALLML:%.mli=%.mli.__)
IMPLO_INTF   := $(foreach file, $(IMPLO_INTF), \
                  $(basename $(file)).cmi $(basename $(file)).cmo)
IMPLO_INTF   := $(filter-out %.mli.cmo, $(IMPLO_INTF))
IMPLO_INTF   := $(IMPLO_INTF:%.mli.cmi=%.cmi)

IMPLX_INTF   := $(IMPLO_INTF:.cmo=.cmx)

INTF         := $(filter %.cmi, $(IMPLO_INTF))
IMPL_CMO     := $(filter %.cmo, $(IMPLO_INTF))
IMPL_CMX     := $(IMPL_CMO:.cmo=.cmx)

OBJ_LINK     := $(OBJ_C_IDL) $(OBJ_C_CXX)
OBJ_FILES    := $(IMPL_CMO:.cmo=.$(EXT_OBJ)) $(OBJ_LINK)

EXECS        := $(addsuffix $(EXE), \
                            $(sort $(TOPRESULT) $(BCRESULT) $(NCRESULT)))
ifdef WIN32
  EXECS      += $(BCRESULT).dll $(NCRESULT).dll
endif

CLIB_BASE    := $(RESULT)$(RES_CLIB_SUF)
ifneq ($(strip $(OBJ_LINK)),)
  RES_CLIB     := lib$(CLIB_BASE).$(EXT_LIB)
endif

ifndef MSVC
DLLSONAME    := dll$(CLIB_BASE).so
endif

NONEXECS     := $(INTF) $(IMPL_CMO) $(IMPL_CMX) $(OBJ_FILES) $(PRE_TARGETS) \
                $(BCRESULT).cma $(NCRESULT).cmxa $(NCRESULT).$(EXT_LIB) \
		$(BCRESULT).cmi $(BCRESULT).cmo \
		$(NCRESULT).cmi $(NCRESULT).cmx $(NCRESULT).o \
		$(RES_CLIB)

ifndef MSVC
  NONEXECS   +=  $(DLLSONAME)
endif

ifndef LIBINSTALL_FILES
  LIBINSTALL_FILES := $(RESULT).mli $(RESULT).cmi $(RESULT).cma \
		      $(RESULT).cmxa $(RESULT).a $(RES_CLIB)
endif

ifndef MSVC
  LIBINSTALL_FILES += $(DLLSONAME)
endif

export LIBINSTALL_FILES

ifdef WIN32
  # some extra stuff is created while linking DLLs
  NONEXECS   += $(BCRESULT).$(EXT_LIB) $(BCRESULT).exp $(NCRESULT).exp
endif

TARGETS      := $(EXECS) $(NONEXECS)

# handle ocamlfind
ifdef USING_OCAMLFIND
  PACKOPT := -pack
else
  PACKOPT := -passopt "-pack"
endif

# If there are IDL-files
ifneq ($(strip $(FILTERED_IDL)),)
  MAYBE_IDL := -cclib -lcamlidl
endif

ifdef USE_CAMLP4
  CAMLP4PATH := \
    $(shell $(CAMLP4) -where 2>/dev/null || echo /usr/local/lib/camlp4)
  INCFLAGS := -I $(CAMLP4PATH)
  CINCFLAGS := -I$(CAMLP4PATH)
endif

INCFLAGS += $(SOURCE_DIRS:%=-I %) $(INCDIRS:%=-I %) $(OCAML_DEFAULT_DIRS:%=-I %)
CINCFLAGS += $(SOURCE_DIRS:%=-I%) $(INCDIRS:%=-I%) $(OCAML_DEFAULT_DIRS:%=-I%)
CLIBFLAGS += $(SOURCE_DIRS:%=-L%) $(LIBDIRS:%=-L%) \
             $(EXTLIBDIRS:%=-L%) $(EXTLIBDIRS:%=-R%) \
             $(OCAML_DEFAULT_DIRS:%=-L%)

ifndef PROFILING
  INTF_OCAMLC := $(OCAMLC)
else
  ifndef THREADS
    INTF_OCAMLC := $(OCAMLCP) -p $(OCAMLCPFLAGS)
  else
    # OCaml does not support profiling byte code
    # with threads (yet), therefore we force an error.
    ifndef REAL_OCAMLC
      $(error Profiling of multithreaded byte code not yet supported by OCaml)
    endif
  endif
endif

ifndef MSVC
  COMMON_LDFLAGS := $(LDFLAGS:%=-ccopt %) $(SOURCE_DIRS:%=-ccopt -L%) \
                    $(LIBDIRS:%=-ccopt -L%) \
                    $(EXTLIBDIRS:%=-ccopt -L%) $(EXTLIBDIRS:%=-ccopt -R%) \
                    $(OCAML_DEFAULT_DIRS:%=-ccopt -L%)
else
  # currenly MSVC-build ocamlc/ocamlopt cannot pass any option to C linker :-(
  COMMON_LDFLAGS :=
endif

ifndef MSVC
  CLIBS_OPTS := $(CLIBS:%=-cclib -l%)
else
  # MSVC libraries do not have 'lib' prefix
  CLIBS_OPTS := $(CLIBS:%=-ccopt %)
endif
ifneq ($(strip $(OBJ_LINK)),)
  ifdef CREATE_LIB
    OBJS_LIBS := -cclib -l$(CLIB_BASE) $(CLIBS_OPTS) $(MAYBE_IDL)
  else
    OBJS_LIBS := $(OBJ_LINK) $(CLIBS_OPTS) $(MAYBE_IDL)
  endif
else
  OBJS_LIBS := $(CLIBS_OPTS) $(MAYBE_IDL)
endif

# If we have to make byte-code
ifndef REAL_OCAMLC
  # EXTRADEPS is added dependencies we have to insert for all
  # executable files we generate.  Ideally it should be all of the
  # libraries we use, but it's hard to find the ones that get searched on
  # the path since I don't know the paths built into the compiler, so
  # just include the ones with slashes in their names. 
  EXTRADEPS := $(addsuffix .cma,$(foreach i,$(LIBS),$(if $(findstring /,$(i)),$(i))))
  SPECIAL_OCAMLFLAGS := $(OCAMLBCFLAGS)

  REAL_OCAMLC := $(INTF_OCAMLC)

  REAL_IMPL := $(IMPL_CMO)
  REAL_IMPL_INTF := $(IMPLO_INTF)
  IMPL_SUF := .cmo

  DEPFLAGS  :=
  MAKE_DEPS := $(MLDEPS) $(BCDEPIS)

  ifdef CREATE_LIB
    ifndef STATIC
      ifneq ($(strip $(OBJ_LINK)),)
	MAKEDLL := $(DLLSONAME)
	ALL_LDFLAGS := -dllib $(DLLSONAME)
      endif
    endif
  endif

  ifndef NO_CUSTOM
    ifneq "$(strip $(OBJ_LINK) $(THREADS) $(MAYBE_IDL) $(CLIBS))" ""
      ALL_LDFLAGS += -custom
    endif
  endif

  ALL_LDFLAGS += $(INCFLAGS) $(OCAMLLDFLAGS) $(OCAMLBLDFLAGS) \
                 $(COMMON_LDFLAGS) $(LIBS:%=%.cma)
  CAMLIDLDLLFLAGS :=

  ifdef THREADS
    ALL_LDFLAGS := -thread $(ALL_LDFLAGS)
    ifndef CREATE_LIB
      ALL_LDFLAGS := unix.cma threads.cma $(ALL_LDFLAGS)
    endif
    THREAD_FLAG := -thread
  endif

# we have to make native-code
else
  EXTRADEPS := $(addsuffix .cmxa,$(foreach i,$(LIBS),$(if $(findstring /,$(i)),$(i))))
  ifndef PROFILING
    SPECIAL_OCAMLFLAGS := $(OCAMLNCFLAGS)
    PLDFLAGS :=
  else
    SPECIAL_OCAMLFLAGS := -p $(OCAMLNCFLAGS)
    PLDFLAGS := -p
  endif

  REAL_IMPL := $(IMPL_CMX)
  REAL_IMPL_INTF := $(IMPLX_INTF)
  IMPL_SUF := .cmx

  CFLAGS := -DNATIVE_CODE $(CFLAGS)

  DEPFLAGS  := -native
  MAKE_DEPS := $(MLDEPS) $(NCDEPIS)

  ALL_LDFLAGS := $(PLDFLAGS) $(INCFLAGS) $(OCAMLLDFLAGS) \
                 $(OCAMLNLDFLAGS) $(COMMON_LDFLAGS)
  CAMLIDLDLLFLAGS := -opt

  ifndef CREATE_LIB
    ALL_LDFLAGS += $(LIBS:%=%.cmxa)
  endif

  ifdef THREADS
    ALL_LDFLAGS := -thread $(ALL_LDFLAGS)
    ifndef CREATE_LIB
      ALL_LDFLAGS := unix.cmxa threads.cmxa $(ALL_LDFLAGS)
    endif
    THREAD_FLAG := -thread
  endif
endif

export MAKE_DEPS

ALL_OCAMLCFLAGS := $(THREAD_FLAG) $(OCAMLFLAGS) \
                   $(INCFLAGS) $(SPECIAL_OCAMLFLAGS)

ifdef make_deps
  -include $(MAKE_DEPS)
  PRE_TARGETS :=
endif

###########################################################################
# USER RULES

# Call "OCamlMakefile QUIET=" to get rid of all of the @'s.
QUIET=@

# generates byte-code (default)
byte-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \
				REAL_RESULT="$(BCRESULT)" make_deps=yes
bc:	byte-code

byte-code-nolink:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \
				REAL_RESULT="$(BCRESULT)" make_deps=yes
bcnl:	byte-code-nolink

top:			$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(TOPRESULT) \
				REAL_RESULT="$(BCRESULT)" make_deps=yes

# generates native-code

native-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(NCRESULT) \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				make_deps=yes
nc:	native-code

native-code-nolink:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				make_deps=yes
ncnl:	native-code-nolink

# generates byte-code libraries
byte-code-library:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(BCRESULT).cma \
				REAL_RESULT="$(BCRESULT)" \
				CREATE_LIB=yes \
				make_deps=yes
bcl:	byte-code-library

# generates native-code libraries
native-code-library:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(NCRESULT).cmxa \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				CREATE_LIB=yes \
				make_deps=yes
ncl:	native-code-library

ifdef WIN32
# generates byte-code dll
byte-code-dll:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(BCRESULT).dll \
				REAL_RESULT="$(BCRESULT)" \
				make_deps=yes
bcd:	byte-code-dll

# generates native-code dll
native-code-dll:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(NCRESULT).dll \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				make_deps=yes
ncd:	native-code-dll
endif

# generates byte-code with debugging information
debug-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \
				REAL_RESULT="$(BCRESULT)" make_deps=yes \
				OCAMLFLAGS="-g $(OCAMLFLAGS)" \
				OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)"
dc:	debug-code

# generates byte-code libraries with debugging information
debug-code-library:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(BCRESULT).cma \
				REAL_RESULT="$(BCRESULT)" make_deps=yes \
				CREATE_LIB=yes \
				OCAMLFLAGS="-g $(OCAMLFLAGS)" \
				OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)"
dcl:	debug-code-library

# generates byte-code for profiling
profiling-byte-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \
				REAL_RESULT="$(BCRESULT)" PROFILING="y" \
				make_deps=yes
pbc:	profiling-byte-code

# generates native-code

profiling-native-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(NCRESULT) \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				PROFILING="y" \
				make_deps=yes
pnc:	profiling-native-code

# generates byte-code libraries
profiling-byte-code-library:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(BCRESULT).cma \
				REAL_RESULT="$(BCRESULT)" PROFILING="y" \
				CREATE_LIB=yes \
				make_deps=yes
pbcl:	profiling-byte-code-library

# generates native-code libraries
profiling-native-code-library:	$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(RES_CLIB) $(NCRESULT).cmxa \
				REAL_RESULT="$(NCRESULT)" PROFILING="y" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				CREATE_LIB=yes \
				make_deps=yes
pncl:	profiling-native-code-library

# packs byte-code objects
pack-byte-code:			$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT).cmo \
				REAL_RESULT="$(BCRESULT)" \
				PACK_LIB=yes make_deps=yes
pabc:	pack-byte-code

# packs native-code objects
pack-native-code:		$(PRE_TARGETS)
			$(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \
				$(NCRESULT).cmx $(NCRESULT).o \
				REAL_RESULT="$(NCRESULT)" \
				REAL_OCAMLC="$(OCAMLOPT)" \
				PACK_LIB=yes make_deps=yes
panc:	pack-native-code

# generates HTML-documentation
htdoc:	doc/html

# generates Latex-documentation
ladoc:	doc/latex

# generates PostScript-documentation
psdoc:	doc/latex/doc.ps

# generates PDF-documentation
pdfdoc:	doc/latex/doc.pdf

# generates all supported forms of documentation
doc: htdoc ladoc psdoc pdfdoc

###########################################################################
# LOW LEVEL RULES

$(REAL_RESULT):		$(REAL_IMPL_INTF) $(OBJ_LINK) $(EXTRADEPS)
			$(REAL_OCAMLC) $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@$(EXE) \
				$(REAL_IMPL)
ifdef MSVC
# work around the bug in ocamlc -- it should delete this file itself
			rm -f camlprim?.$(EXT_OBJ)
endif

nolink:			$(REAL_IMPL_INTF) $(OBJ_LINK)

ifdef WIN32
$(REAL_RESULT).dll:	$(REAL_IMPL_INTF) $(OBJ_LINK)
			$(CAMLIDLDLL) $(CAMLIDLDLLFLAGS) $(OBJ_LINK) $(CLIBS) \
				-o $@ $(REAL_IMPL)
endif

%$(TOPSUFFIX):		$(REAL_IMPL_INTF) $(OBJ_LINK) $(EXTRADEPS)
			$(OCAMLMKTOP) $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@$(EXE) \
				$(REAL_IMPL)
ifdef MSVC
# work around the bug in ocamltop -- it should delete this file itself
			rm -f camlprim?.$(EXT_OBJ)
endif

.SUFFIXES:		.mli .ml .cmi .cmo .cmx .cma .cmxa .$(EXT_OBJ) \
                        .mly .di .d .$(EXT_LIB) .idl .c .$(EXT_CXX) .h .so \
                        .rep .zog
ifndef MSVC
$(DLLSONAME):		$(OBJ_LINK)
			$(CC) -shared $(CINCFLAGS) $(CLIBFLAGS) \
				-o $@ $(OBJ_LINK) $(CLIBS:%=-l%)
endif

$(RESULT).cma:		$(REAL_IMPL_INTF) $(MAKEDLL) $(EXTRADEPS)
			$(REAL_OCAMLC) -a $(ALL_LDFLAGS) \
				$(OBJS_LIBS) -o $@ $(OCAMLBLDFLAGS) $(REAL_IMPL)

$(RESULT).cmxa $(RESULT).$(EXT_LIB):	$(REAL_IMPL_INTF) $(EXTRADEPS)
			$(OCAMLOPT) -a $(ALL_LDFLAGS) $(OBJS_LIBS) \
				$(OCAMLNLDFLAGS) -o $@ $(REAL_IMPL)

$(RES_CLIB): 		$(OBJ_LINK)
ifndef MSVC
  ifneq ($(strip $(OBJ_LINK)),)
		      ar rc $@ $(OBJ_LINK)
		      ranlib $@
  endif
else
  ifneq ($(strip $(OBJ_LINK)),)
			lib /nologo /debugtype:cv /out:$(RES_CLIB) $(OBJ_LINK)
  endif
endif

.mli.cmi: $(EXTRADEPS)
			$(QUIET)pp=`sed -n -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \
			if [ -z "$$pp" ]; then \
			  echo $(INTF_OCAMLC) -c $(THREAD_FLAG) \
			    $(OCAMLFLAGS) $(INCFLAGS) $<; \
			  $(INTF_OCAMLC) -c $(THREAD_FLAG) $(OCAMLFLAGS) \
			    $(INCFLAGS) $<; \
			else \
			    echo $(INTF_OCAMLC) -c -pp \"$$pp\" $(THREAD_FLAG) \
			      $(OCAMLFLAGS) $(INCFLAGS) $<; \
			    $(INTF_OCAMLC) -c -pp "$$pp" $(THREAD_FLAG) \
				 $(OCAMLFLAGS) $(INCFLAGS) $<; \
			fi

.ml.cmi .ml.$(EXT_OBJ) .ml.cmx .ml.cmo: $(EXTRADEPS)
			$(QUIET)pp=`sed -n -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \
			if [ -z "$$pp" ]; then \
			  echo $(REAL_OCAMLC) -c $(ALL_OCAMLCFLAGS) $<; \
			  $(REAL_OCAMLC) -c $(ALL_OCAMLCFLAGS) $<; \
			else \
			  echo $(REAL_OCAMLC) -c -pp \"$$pp\" \
			    $(ALL_OCAMLCFLAGS) $<; \
			  $(REAL_OCAMLC) -c -pp "$$pp" $(ALL_OCAMLCFLAGS) $<; \
			fi

ifdef PACK_LIB
$(REAL_RESULT).cmo $(REAL_RESULT).cmx $(REAL_RESULT).o: $(REAL_IMPL_INTF) $(OBJ_LINK) $(EXTRADEPS)
			$(REAL_OCAMLC) $(PACKOPT) $(ALL_LDFLAGS) \
				$(OBJS_LIBS) -o $@ $(REAL_IMPL)
endif

.PRECIOUS:		%.ml
%.ml:			%.mll
			$(OCAMLLEX) $<

.PRECIOUS:		%.ml %.mli
%.ml %.mli:		%.mly
			$(OCAMLYACC) $(YFLAGS) $<

.PRECIOUS:		%.ml
%.ml :			%.rep
			$(CAMELEON_REPORT) $(CAMELEON_REPORT_FLAGS) -gen $<

.PRECIOUS:		%.ml
%.ml :			%.zog
			$(CAMELEON_ZOGGY)  $(CAMELEON_ZOGGY_FLAGS) -impl $< > $@

.PRECIOUS:		%.ml %.mli %_stubs.c %.h
%.ml %.mli %_stubs.c %.h:		%.idl
			$(CAMLIDL) $(MAYBE_IDL_HEADER) $(IDLFLAGS) \
				$(CAMLIDLFLAGS) $<
			$(QUIET)if [ $(NOIDLHEADER) ]; then touch $*.h; fi

.c.$(EXT_OBJ):
			$(CC) -c $(CFLAGS) $(CINCFLAGS) -I$(OCAMLLIBPATH) \
				$< $(CFLAG_O)$@

.$(EXT_CXX).$(EXT_OBJ):
			$(CXX) -c $(CXXFLAGS) $(CINCFLAGS) -I$(OCAMLLIBPATH) \
				$< $(CFLAG_O)$@

$(MLDEPDIR)/%.d:	%.ml
			$(QUIET)echo making $@ from $<
			$(QUIET)if [ ! -d $(@D) ]; then mkdir -p $(@D); fi
			$(QUIET)pp=`sed -n -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \
			if [ -z "$$pp" ]; then \
			  $(OCAMLDEP) $(INCFLAGS) $< > $@; \
			else \
			  $(OCAMLDEP) -pp "$$pp" $(INCFLAGS) $< > $@; \
			fi

$(BCDIDIR)/%.di $(NCDIDIR)/%.di:	%.mli
			$(QUIET)echo making $@ from $<
			$(QUIET)if [ ! -d $(@D) ]; then mkdir -p $(@D); fi
			$(QUIET)pp=`sed -n -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \
			if [ -z "$$pp" ]; then \
			  $(OCAMLDEP) $(DEPFLAGS) $(INCFLAGS) $< > $@; \
			else \
			  $(OCAMLDEP) $(DEPFLAGS) \
			    -pp "$$pp" $(INCFLAGS) $< > $@; \
			fi

doc/html: $(DOC_FILES)
	rm -rf $@
	mkdir -p $@
	$(OCAMLDOC) -html -d $@ $(OCAMLDOCFLAGS) $(DOC_FILES)

doc/latex: $(DOC_FILES)
	rm -rf $@
	mkdir -p $@
	$(OCAMLDOC) -latex -d $@ $(OCAMLDOCFLAGS) $(DOC_FILES) -o doc.tex

doc/latex/doc.ps: doc/latex
	cd doc/latex && \
	  $(LATEX) doc.tex && \
	  $(LATEX) doc.tex && \
	  $(DVIPS) $(DVIPSFLAGS) doc.dvi -o $(@F)

doc/latex/doc.pdf: doc/latex/doc.ps
	cd doc/latex && $(PS2PDF) $(<F)

###########################################################################
# (UN)INSTALL RULES FOR LIBRARIES

.PHONY: libinstall
libinstall:	all
	$(QUIET)printf "\nInstalling library to: $(OCAML_LIB_INSTALL)\n"
	-install -d $(OCAML_LIB_INSTALL)
	for i in $(LIBINSTALL_FILES); do \
	  if [ -f $$i ]; then \
	    install -c -m 0644 $$i $(OCAML_LIB_INSTALL); \
	  fi; \
	done
	$(QUIET)printf "\nInstallation successful.\n"

.PHONY: libuninstall
libuninstall:
	$(QUIET)printf "\nUninstalling library from: $(OCAML_LIB_INSTALL)\n"
	cd $(OCAML_LIB_INSTALL); rm $(notdir $(LIBINSTALL_FILES))
	$(QUIET)printf "\nUninstallation successful.\n"

###########################################################################
# MAINTAINANCE RULES

.PHONY:	clean
clean:
	rm -f $(TARGETS) $(TRASH)
	rm -rf $(BCDIDIR) $(NCDIDIR) $(MLDEPDIR)

.PHONY:	cleanup
cleanup:
	rm -f $(NONEXECS) $(TRASH)
	rm -rf $(BCDIDIR) $(NCDIDIR) $(MLDEPDIR)

.PHONY: clean-doc
clean-doc:
	rm -rf doc

.PHONY: nobackup
nobackup:
	rm -f *.bak *~ *.dup