Skip to content

Commit

Permalink
Simplify tools/Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
stedolan committed Mar 2, 2021
1 parent b2a0c6d commit c680d90
Showing 1 changed file with 50 additions and 57 deletions.
107 changes: 50 additions & 57 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,6 @@ define byte2native
$(patsubst %.cmo,%.cmx,$(patsubst %.cma,%.cmxa,$1))
endef

# $1 = target, $2 = OCaml object dependencies, $3 = other dependencies
# There is a lot of subtle code here. The multiple layers of expansion
# are due to `make`'s eval() function, which evaluates the string
# passed to it as a makefile fragment. So it is crucial that variables
# not get expanded too many times.
define byte_and_opt_
# This check is defensive programming
$(and $(filter-out 1,$(words $1)),$(error \
cannot build file with whitespace in name))
$(call PROGRAM_SYNONYM, $1)

$1$(EXE): $3 $2
$$(CAMLC) $$(LINKFLAGS) -I $$(ROOTDIR) -o $$@ $2

$(call PROGRAM_SYNONYM, $1.opt)

$1.opt$(EXE): $3 $$(call byte2native,$2)
$$(CAMLOPT_CMD) $$(LINKFLAGS) -I $$(ROOTDIR) -o $$@ \
$$(call byte2native,$2)

all: $1

opt.opt: $1.opt

ifeq '$(filter $(installed_tools),$1)' '$1'
install_files += $1
endif
clean::
rm -f -- $1 $1.opt $1.exe $1.opt.exe

endef

# Escape any $ characters in the arguments and eval the result.
define byte_and_opt
$(eval $(call \
byte_and_opt_,$(subst $$,$$$$,$1),$(subst $$,$$$$,$2),$(subst $$,$$$$,$3)))
endef

CAMLC = $(BOOT_OCAMLC) -g -nostdlib -I $(ROOTDIR)/boot \
-use-prims $(ROOTDIR)/runtime/primitives -I $(ROOTDIR)
CAMLOPT = $(CAMLRUN) $(ROOTDIR)/ocamlopt$(EXE) -g -nostdlib -I $(ROOTDIR)/stdlib
Expand All @@ -84,19 +46,39 @@ VPATH := $(filter-out -I,$(INCLUDES))
.PHONY: all allopt opt.opt # allopt and opt.opt are synonyms
allopt: opt.opt

programs_byte := \
ocamldep ocamlprof ocamlcp ocamloptp ocamlmklib \
ocamlmktop ocamlcmt dumpobj ocamlobjinfo \
primreq stripdebug cmpbyt
install_files += $(filter $(installed_tools), $(programs_byte))
programs_opt := $(programs_byte:%=%.opt)

programs_byte_exe := $(programs_byte:%=%$(EXE))
programs_opt_exe := $(programs_opt:%=%$(EXE))
$(programs_byte) $(programs_opt): %: $(if $(EXE), %$(EXE), )

$(programs_byte_exe):
$(CAMLC) $(LINKFLAGS) -I $(ROOTDIR) -o $@ $(filter-out %.cmi,$^)

$(programs_opt_exe):
$(CAMLOPT_CMD) $(LINKFLAGS) -I $(ROOTDIR) -o $@ $(filter-out %.cmi,$^)

all: $(programs_byte_exe)
opt.opt: $(programs_opt_exe)
clean::
rm -f $(programs_byte_exe) $(programs_opt_exe)

# The dependency generator

CAMLDEP_OBJ=ocamldep.cmo
CAMLDEP_IMPORTS= \
$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma
ocamldep$(EXE): LINKFLAGS += -compat-32
$(call byte_and_opt,ocamldep,$(CAMLDEP_IMPORTS) $(CAMLDEP_OBJ),)
ocamldep$(EXE): depend.cmi
ocamldep.opt$(EXE): depend.cmi

clean::
rm -f ocamldep ocamldep.exe ocamldep.opt ocamldep.opt.exe
ocamldep$(EXE): LINKFLAGS += -compat-32
ocamldep$(EXE): depend.cmi $(CAMLDEP_IMPORTS) $(CAMLDEP_OBJ)
ocamldep.opt$(EXE): depend.cmi \
$(call byte2native,$(CAMLDEP_IMPORTS) $(CAMLDEP_OBJ))

# The profiler

Expand All @@ -109,16 +91,20 @@ CSLPROF_IMPORTS=config.cmo build_path_prefix_map.cmo misc.cmo identifiable.cmo \
pprintast.cmo \
lexer.cmo parse.cmo

$(call byte_and_opt,ocamlprof,$(CSLPROF_IMPORTS) profiling.cmo $(CSLPROF),)
ocamlprof$(EXE): $(CSLPROF_IMPORTS) profiling.cmo $(CSLPROF)
ocamlprof.opt$(EXE): \
$(call byte2native, $(CSLPROF_IMPORTS) profiling.cmo $(CSLPROF))

ocamlcp_cmos = config.cmo build_path_prefix_map.cmo misc.cmo profile.cmo \
warnings.cmo identifiable.cmo numbers.cmo arg_helper.cmo \
clflags.cmo local_store.cmo \
terminfo.cmo location.cmo load_path.cmo ccomp.cmo compenv.cmo \
main_args.cmo

$(call byte_and_opt,ocamlcp,$(ocamlcp_cmos) ocamlcp.cmo,)
$(call byte_and_opt,ocamloptp,$(ocamlcp_cmos) ocamloptp.cmo,)
ocamlcp$(EXE): $(ocamlcp_cmos) ocamlcp.cmo
ocamlcp.opt$(EXE): $(call byte2native, $(ocamlcp_cmos) ocamlcp.cmo)
ocamloptp$(EXE): $(ocamlcp_cmos) ocamloptp.cmo
ocamloptp.opt$(EXE): $(call byte2native, $(ocamlcp_cmos) ocamloptp.cmo)

opt:: profiling.cmx

Expand All @@ -139,8 +125,9 @@ installopt::

# To help building mixed-mode libraries (OCaml + C)

$(call byte_and_opt,ocamlmklib,config.cmo \
build_path_prefix_map.cmo misc.cmo ocamlmklib.cmo,)
ocamlmklib$(EXE): config.cmo build_path_prefix_map.cmo misc.cmo ocamlmklib.cmo
ocamlmklib.opt$(EXE): \
$(call byte2native, config.cmo build_path_prefix_map.cmo misc.cmo ocamlmklib.cmo)

# To make custom toplevels

Expand All @@ -149,7 +136,8 @@ OCAMLMKTOP_IMPORTS=config.cmo build_path_prefix_map.cmo misc.cmo \
identifiable.cmo numbers.cmo arg_helper.cmo clflags.cmo \
local_store.cmo load_path.cmo profile.cmo ccomp.cmo

$(call byte_and_opt,ocamlmktop,$(OCAMLMKTOP_IMPORTS) $(OCAMLMKTOP),)
ocamlmktop$(EXE): $(OCAMLMKTOP_IMPORTS) $(OCAMLMKTOP)
ocamlmktop.opt$(EXE): $(call byte2native, $(OCAMLMKTOP_IMPORTS) $(OCAMLMKTOP))

# Converter olabl/ocaml 2.99 to ocaml 3

Expand Down Expand Up @@ -205,8 +193,8 @@ ocamlcmt_objects= \
\
ocamlcmt.cmo

# Reading cmt files
$(call byte_and_opt,ocamlcmt,$(ocamlcmt_objects),)
ocamlcmt$(EXE): $(ocamlcmt_objects)
ocamlcmt.opt$(EXE): $(call byte2native, $(ocamlcmt_objects))

install::
if test -f ocamlcmt.opt$(EXE); then \
Expand All @@ -224,7 +212,8 @@ DUMPOBJ= \
\
opnames.cmo dumpobj.cmo

$(call byte_and_opt,dumpobj,$(DUMPOBJ),)
dumpobj$(EXE): $(DUMPOBJ)
dumpobj.opt$(EXE): $(call byte2native, $(DUMPOBJ))

make_opcodes := make_opcodes$(EXE)

Expand Down Expand Up @@ -258,14 +247,16 @@ OBJINFO=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlmiddleend.cma \
objinfo.cmo

$(call byte_and_opt,ocamlobjinfo,$(OBJINFO),)
ocamlobjinfo$(EXE): $(OBJINFO)
ocamlobjinfo.opt$(EXE): $(call byte2native, $(OBJINFO))

primreq=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
primreq.cmo

# Scan object files for required primitives
$(call byte_and_opt,primreq,$(primreq),)
primreq$(EXE): $(primreq)
primreq.opt$(EXE): $(call byte2native, $(primreq))

LINTAPIDIFF=$(ROOTDIR)/compilerlibs/ocamlcommon.cmxa \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cmxa \
Expand Down Expand Up @@ -293,15 +284,17 @@ stripdebug=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
stripdebug.cmo

$(call byte_and_opt,stripdebug,$(stripdebug),)
stripdebug$(EXE): $(stripdebug)
stripdebug.opt$(EXE): $(call byte2native, $(stripdebug))

# Compare two bytecode executables

CMPBYT=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
cmpbyt.cmo

$(call byte_and_opt,cmpbyt,$(CMPBYT),)
cmpbyt$(EXE): $(CMPBYT)
cmpbyt.opt$(EXE): $(call byte2native, $(CMPBYT))

caml_tex_files := \
$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
Expand Down

0 comments on commit c680d90

Please sign in to comment.