blob: 3141cbd068ec7b849cb1baf14c074ad50c8e16b3 [file] [log] [blame]
Gordon Henriksen0908d492007-09-18 12:26:17 +00001##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
2#
3# The LLVM Compiler Infrastructure
4#
5# This file was developed by Gordon Henriksen and is distributed under the
6# University of Illinois Open Source License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# An ocaml library is a unique project type in the context of LLVM, so rules are
11# here rather than in Makefile.rules.
12#
13##===----------------------------------------------------------------------===##
14
15include $(LEVEL)/Makefile.config
16
17# Find the ocaml stdlib root. /usr/local/lib/ocaml is the default when built
18# from source; distros use something like /usr/lib/ocaml/3.10.0.
19ifndef OCAML_LIBDIR
20OCAML_LIBDIR := $(shell $(OCAMLC) -where)
21endif
22
23# CFLAGS needs to be set before Makefile.rules is included. Yes, ocaml puts its
24# includes under its libdir.
25CFLAGS += -I$(OCAML_LIBDIR)
26
27include $(LEVEL)/Makefile.common
28
29# Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
30# user can override this with OCAML_LIBDIR.
31PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
32OcamlDir := $(LibDir)/ocaml
33
34# Info from llvm-config and similar
35ifdef UsedComponents
36UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
37UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
38endif
39
40# Tools
41OCAMLCFLAGS += -I $(OcamlDir)
42OCAMLAFLAGS += $(patsubst %,-cclib %, \
43 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
44 $(UsedLibs) -l$(LIBRARYNAME))
45
46Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
48Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
49
50Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
51Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
52
53# Source files
54OcamlSources := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
55OcamlHeaders := $(OcamlSources:.ml=.mli)
56
57# Output and intermediate files
58# The .cmo files are the only intermediates; all others get installed.
59BareLibraryA := lib$(LIBRARYNAME).a
60LibraryA := $(OcamlDir)/$(BareLibraryA)
61LibraryCMA := $(OcamlDir)/$(LIBRARYNAME).cma
62LibraryCMXA := $(OcamlDir)/$(LIBRARYNAME).cmxa
63ObjectsCMI := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(OcamlDir)/%.cmi)
64ObjectsCMO := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(ObjDir)/%.cmo)
65ObjectsCMX := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(OcamlDir)/%.cmx)
66
67# Dependencies
68# Punting on ocamldep, since its output is only suitable for builds where
69# objects are placed directly adjacent to sources, which is not us.
70# Unfortunately, this is subtly incorrect and leads to occasional problems.
71# ocamlc/ocamlopt really need an option akin to gcc -M or gcc -MD.
72$(ObjectsCMO): $(ObjectsCMI) $(UsedOcamLibs:%=$(OcamlDir)/%.cmi)
73$(ObjectsCMX): $(ObjectsCMI) $(UsedOcamLibs:%=$(OcamlDir)/%.cmi)
74
75# Installation targets
76DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
77DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
78DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
79
80
81##===- Build static library from C sources --------------------------------===##
82
83all-local:: $(LibraryA)
84clean-local:: clean-a
85install-local:: install-a
86uninstall-local:: uninstall-a
87
88$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
89 $(Echo) "Building $(BuildMode) $(notdir $@)"
90 -$(Verb) $(RM) -f $@
91 $(Verb) $(Archive) $@ $(ObjectsO)
92 $(Verb) $(Ranlib) $@
93
94clean-a::
95 -$(Verb) $(RM) -f $(LibraryA)
96
97install-a:: $(LibraryA)
98 $(Echo) "Installing $(BuildMode) $(DestA)"
99 $(Verb) $(MKDIR) $(PROJ_libocamldir)
100 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
101 $(Verb)
102
103uninstall-a::
104 $(Echo) "Uninstalling $(DestA)"
105 -$(Verb) $(RM) -f $(DestA)
106
107
108##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
109
110all-local:: build-cmis
111clean-local:: clean-cmis
112install-local:: install-cmis
113uninstall-local:: uninstall-cmis
114
115build-cmis: $(ObjectsCMI)
116
117$(OcamlDir)/%.cmi: $(PROJ_SRC_DIR)/%.mli $(OcamlDir)/.dir
118 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
119 $(Verb) $(Compile.CMI) $@ $<
120
121clean-cmis::
122 -$(Verb) $(RM) -f $(ObjectsCMI)
123
124# Also install the .mli's (headers) as documentation.
125install-cmis: $(ObjectsCMI)
126 $(Verb) $(MKDIR) $(PROJ_libocamldir)
127 $(Verb) for i in $(patsubst $(OcamlDir)/%,%,$(ObjectsCMI)); do \
128 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
129 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
130 done
131 $(Verb) for i in $(patsubst $(PROJ_SRC_DIR)/%,%,$(OcamlHeaders)); do \
132 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
133 $(DataInstall) $(PROJ_SRC_DIR)/$$i "$(PROJ_libocamldir)/$$i"; \
134 done
135
136uninstall-cmis::
137 $(Verb) for i in $(patsubst $(OcamlDir)/%,%,$(ObjectsCMI)); do \
138 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
139 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
140 done
141 $(Verb) for i in $(patsubst $(PROJ_SRC_DIR)/%,%,$(OcamlHeaders)); do \
142 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
143 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
144 done
145
146
147##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
148
149all-local:: $(LibraryCMA)
150clean-local:: clean-cma
151install-local:: install-cma
152uninstall-local:: uninstall-cma
153
154$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
155 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
156 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
157 $(Verb) for i in $(UsedLibNames); do \
158 ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
159 done
160
161$(ObjDir)/%.cmo: $(PROJ_SRC_DIR)/%.ml $(OcamlDir)/.dir
162 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
163 $(Verb) $(Compile.CMO) $@ $<
164
165clean-cma::
166 $(Verb) $(RM) -f $(LibraryCMA)
167
168install-cma:: $(LibraryCMA)
169 $(Echo) "Installing $(BuildMode) $(DestCMA)"
170 $(Verb) $(MKDIR) $(PROJ_libocamldir)
171 $(Verb) $(DataInstall) $(LibraryCMA) "$(DestCMA)"
172 $(Verb) for i in $(UsedLibNames); do \
173 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
174 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
175 done
176
177uninstall-cma::
178 $(Echo) "Uninstalling $(DestCMA)"
179 -$(Verb) $(RM) -f $(DestCMA)
180 $(Verb) for i in $(UsedLibNames); do \
181 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
182 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
183 done
184
185
186##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
187
188# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
189# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
190ifdef OCAMLOPT
191
192all-local:: $(LibraryCMXA)
193clean-local:: clean-cmxa
194install-local:: install-cmxa
195uninstall-local:: uninstall-cmxa
196
197$(LibraryCMXA): $(ObjectsCMX)
198 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
199 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
200 $(Verb) $(RM) -f $(@:.cmxa=.o)
201
202$(OcamlDir)/%.cmx: $(PROJ_SRC_DIR)/%.ml
203 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
204 $(Verb) $(Compile.CMX) $@ $<
205
206clean-cmxa::
207 $(Verb) $(RM) -f $(LibraryCMXA) $(LibraryCMXA:.cmxa=.o) \
208 $(LibraryCMXA:.cmxa=.a) $(ObjectsCMX)
209
210install-cmxa:: $(LibraryCMXA)
211 $(Verb) $(MKDIR) $(PROJ_libocamldir)
212 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
213 $(Verb) $(DataInstall) $(LibraryCMXA) $(DestCMXA)
214 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
215 $(Verb) $(DataInstall) $(LibraryCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
216 $(Verb) for i in $(ObjectsCMX:$(OcamlDir)/%=%); do \
217 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
218 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
219 done
220
221uninstall-cmxa:: $(LibraryCMXA)
222 $(Echo) "Uninstalling $(DestCMXA)"
223 $(Verb) $(RM) -f $(DestCMXA)
224 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
225 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
226 $(Verb) for i in $(ObjectsCMX:$(OcamlDir)/%=%); do \
227 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
228 $(RM) -f $(PROJ_libocamldir)/$$i; \
229 done
230
231endif
232
233
234##===- Debugging gunk -----------------------------------------------------===##
235printvars:: printcamlvars
236
237printcamlvars::
238 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
239 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
240 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
241 $(Echo) "OCAMLC : " '$(OCAMLC)'
242 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
243 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
244 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
245 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
246 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
247 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
248 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
249 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
250 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
251 $(Echo) "OcamlSources : " '$(OcamlSources)'
252 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
253 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
254 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
255 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
256 $(Echo) "DestA : " '$(DestA)'
257 $(Echo) "DestCMA : " '$(DestCMA)'
258 $(Echo) "DestCMXA : " '$(DestCMXA)'
259 $(Echo) "UsedLibs : " '$(UsedLibs)'
260 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
261
262.PHONY: printcamlvars build-cmis \
263 clean-a clean-cmis clean-cma clean-cmxa \
264 install-a install-cmis install-cma install-cmxa \
265 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa