Skip to content

Commit

Permalink
Simplify tools/Makefile (#10265)
Browse files Browse the repository at this point in the history
  • Loading branch information
stedolan committed May 26, 2021
1 parent 600aad5 commit fd59303
Showing 1 changed file with 70 additions and 83 deletions.
153 changes: 70 additions & 83 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,24 @@
#* *
#**************************************************************************

MAKEFLAGS := -r -R
ROOTDIR = ..
# NOTE: it is important that OCAMLLEX is defined *before* Makefile.common
# gets included, so that its definition here takes precedence
# over the one there.
OCAMLLEX ?= $(BOOT_OCAMLLEX)
include $(ROOTDIR)/Makefile.common

DESTDIR ?=
# Setup GNU make variables storing per-target source and target,
# a list of installed tools, and a function to quote a filename for
# the shell.
override installed_tools := ocamldep ocamlprof ocamlcp ocamloptp \
installed_tools := ocamldep ocamlprof ocamlcp ocamloptp \
ocamlmktop ocamlmklib ocamlobjinfo

install_files :=
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 = $(OCAMLRUN) $(ROOTDIR)/ocamlopt$(EXE) \
Expand All @@ -84,44 +44,67 @@ COMPFLAGS = -absname -w +a-4-9-41-42-44-45-48-70 -strict-sequence \
LINKFLAGS = $(INCLUDES)
VPATH := $(filter-out -I,$(INCLUDES))

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)

.PHONY: all allopt opt.opt # allopt and opt.opt are synonyms
all: $(programs_byte)
opt.opt: $(programs_opt)
allopt: opt.opt

$(foreach program, $(programs_byte) $(programs_opt),\
$(eval $(call PROGRAM_SYNONYM,$(program))))

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

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

clean::
rm -f $(programs_byte) $(programs_byte:%=%.exe)
rm -f $(programs_opt) $(programs_opt:%=%.exe)

# The dependency generator

CAMLDEP_OBJ=ocamldep.cmo
CAMLDEP_IMPORTS= \
OCAMLDEP = \
$(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
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
ocamldep.cmo depend.cmi

clean::
rm -f ocamldep ocamldep.exe ocamldep.opt ocamldep.opt.exe
ocamldep$(EXE): LINKFLAGS += -compat-32
ocamldep$(EXE): $(OCAMLDEP)
ocamldep.opt$(EXE): $(call byte2native, $(OCAMLDEP))

# The profiler

CSLPROF=ocamlprof.cmo
CSLPROF_IMPORTS=config.cmo build_path_prefix_map.cmo misc.cmo identifiable.cmo \
OCAMLPROF=config.cmo build_path_prefix_map.cmo misc.cmo identifiable.cmo \
numbers.cmo arg_helper.cmo clflags.cmo terminfo.cmo \
warnings.cmo location.cmo longident.cmo docstrings.cmo \
syntaxerr.cmo ast_helper.cmo \
camlinternalMenhirLib.cmo parser.cmo \
pprintast.cmo \
lexer.cmo parse.cmo
lexer.cmo parse.cmo ocamlprof.cmo

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

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
OCAMLCP = 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) ocamlcp.cmo
ocamlcp.opt$(EXE): $(call byte2native, $(OCAMLCP) ocamlcp.cmo)
ocamloptp$(EXE): $(OCAMLCP) ocamloptp.cmo
ocamloptp.opt$(EXE): $(call byte2native, $(OCAMLCP) ocamloptp.cmo)

opt:: profiling.cmx

Expand All @@ -141,18 +124,19 @@ installopt::
"$(INSTALL_LIBDIR)"

# To help building mixed-mode libraries (OCaml + C)
OCAMLMKLIB = config.cmo build_path_prefix_map.cmo misc.cmo ocamlmklib.cmo

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

# To make custom toplevels

OCAMLMKTOP=ocamlmktop.cmo
OCAMLMKTOP_IMPORTS=config.cmo build_path_prefix_map.cmo misc.cmo \
OCAMLMKTOP=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
local_store.cmo load_path.cmo profile.cmo ccomp.cmo ocamlmktop.cmo

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

# Converter olabl/ocaml 2.99 to ocaml 3

Expand Down Expand Up @@ -202,14 +186,13 @@ beforedepend:: cvt_emit.ml

# Reading cmt files

ocamlcmt_objects= \
OCAMLCMT = \
$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
\
ocamlcmt.cmo

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

install::
if test -f ocamlcmt.opt$(EXE); then \
Expand All @@ -224,10 +207,10 @@ install::
DUMPOBJ= \
$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
\
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 @@ -256,19 +239,21 @@ ifeq "$(SYSTEM)" "cygwin"
DEF_SYMBOL_PREFIX = '-Dsymbol_prefix="_"'
endif

OBJINFO=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
$(ROOTDIR)/compilerlibs/ocamlmiddleend.cma \
objinfo.cmo
OCAMLOBJINFO=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
$(ROOTDIR)/compilerlibs/ocamlbytecomp.cma \
$(ROOTDIR)/compilerlibs/ocamlmiddleend.cma \
objinfo.cmo

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

primreq=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
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 All @@ -292,19 +277,21 @@ install::

# Copy a bytecode executable, stripping debug info

stripdebug=$(ROOTDIR)/compilerlibs/ocamlcommon.cma \
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 fd59303

Please sign in to comment.