Skip to content

Commit

Permalink
Merge pull request #11828 from OlivierNicole/distinct_n_b_objects
Browse files Browse the repository at this point in the history
Compile otherlibs/ C stubs in two version for native and bytecode
  • Loading branch information
shindere committed Dec 23, 2022
2 parents 0250779 + 9474f93 commit 4935f7b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -253,6 +253,9 @@ Working version
the root Makefile.
(Sébastien Hinderer, review by David Allsopp and Florian Angeletti)

- #11828: Compile otherlibs/ C stubs in two version for native and bytecode
(Olivier Nicole, review by Sébastien Hinderer)

### Bug fixes:

- #10664, #11600: Unsoundness in the typing of polymorphic methods
Expand Down
65 changes: 47 additions & 18 deletions otherlibs/Makefile.otherlibs.common
Expand Up @@ -36,13 +36,17 @@ OPTCOMPFLAGS += -O3
endif
MKLIB=$(OCAMLRUN) $(ROOTDIR)/tools/ocamlmklib

# The C stubs for the native backend may be built with specific flags by
# redefining this variable.
OC_NATIVE_C_FLAGS =

# Variables that must be defined by individual libraries:
# LIBNAME
# CAMLOBJS

# Variables that can be defined by individual libraries,
# but have sensible default values:
COBJS ?=
C_SOURCES ?=
EXTRACAMLFLAGS ?=
LINKOPTS ?=
LDOPTS ?=
Expand All @@ -51,49 +55,66 @@ CMIFILES ?= $(CAMLOBJS:.cmo=.cmi)
CAMLOBJS_NAT ?= $(CAMLOBJS:.cmo=.cmx)
CLIBNAME ?= $(LIBNAME)

ifeq "$(COBJS)" ""
ifeq "$(C_SOURCES)" ""
STUBSLIB=
else
STUBSLIB=lib$(CLIBNAME).$(A)
COBJS_BYTECODE = $(C_SOURCES:.c=.b.$(O))
COBJS_NATIVE = $(C_SOURCES:.c=.n.$(O))
COBJS = $(COBJS_BYTECODE) $(COBJS_NATIVE)

CLIBNAME_BYTECODE=$(CLIBNAME)byt
CLIBNAME_NATIVE=$(CLIBNAME)nat
STUBSLIB_BYTECODE=lib$(CLIBNAME_BYTECODE).$(A)
STUBSLIB_NATIVE=lib$(CLIBNAME_NATIVE).$(A)
endif

.PHONY: all allopt opt.opt # allopt and opt.opt are synonyms
all: $(STUBSLIB) $(LIBNAME).cma $(CMIFILES)
all: $(STUBSLIB_BYTECODE) $(LIBNAME).cma $(CMIFILES)

allopt: $(STUBSLIB) $(LIBNAME).cmxa $(LIBNAME).$(CMXS) $(CMIFILES)
allopt: $(STUBSLIB_NATIVE) $(LIBNAME).cmxa $(LIBNAME).$(CMXS) $(CMIFILES)
opt.opt: allopt

$(LIBNAME).cma: $(CAMLOBJS)
ifeq "$(COBJS)" ""
$(CAMLC) -o $@ -a -linkall $(CAMLOBJS) $(LINKOPTS)
else
$(MKLIB) -o $(LIBNAME) -oc $(CLIBNAME) -ocamlc '$(CAMLC)' -linkall \
$(CAMLOBJS) $(LINKOPTS)
$(MKLIB) -o $(LIBNAME) -oc $(CLIBNAME_BYTECODE) -ocamlc '$(CAMLC)' \
-linkall $(CAMLOBJS) $(LINKOPTS)
endif

$(LIBNAME).cmxa: $(CAMLOBJS_NAT)
ifeq "$(COBJS)" ""
$(CAMLOPT) -o $@ -a -linkall $(CAMLOBJS_NAT) $(LINKOPTS)
else
$(MKLIB) -o $(LIBNAME) -oc $(CLIBNAME) -ocamlopt '$(CAMLOPT)' -linkall \
$(CAMLOBJS_NAT) $(LINKOPTS)
$(MKLIB) -o $(LIBNAME) -oc $(CLIBNAME_NATIVE) -ocamlopt '$(CAMLOPT)' \
-linkall $(CAMLOBJS_NAT) $(LINKOPTS)
endif

$(LIBNAME).cmxs: $(LIBNAME).cmxa $(STUBSLIB)
$(LIBNAME).cmxs: $(LIBNAME).cmxa $(STUBSLIB_NATIVE)
$(CAMLOPT_CMD) -shared -o $(LIBNAME).cmxs -I . $(LIBNAME).cmxa

lib$(CLIBNAME).$(A): $(COBJS)
$(MKLIB_CMD) -oc $(CLIBNAME) $(COBJS) $(LDOPTS)
lib$(CLIBNAME_BYTECODE).$(A): $(COBJS)
$(MKLIB_CMD) -oc $(CLIBNAME_BYTECODE) $(COBJS_BYTECODE) $(LDOPTS)

lib$(CLIBNAME_NATIVE).$(A): $(COBJS)
$(MKLIB_CMD) -oc $(CLIBNAME_NATIVE) $(COBJS_NATIVE) $(LDOPTS)

INSTALL_LIBDIR_LIBNAME = $(INSTALL_LIBDIR)/$(LIBNAME)

install::
if test -f dll$(CLIBNAME)$(EXT_DLL); then \
if test -f dll$(CLIBNAME_BYTECODE)$(EXT_DLL); then \
$(INSTALL_PROG) \
dll$(CLIBNAME_BYTECODE)$(EXT_DLL) "$(INSTALL_STUBLIBDIR)"; \
fi
if test -f dll$(CLIBNAME_NATIVE)$(EXT_DLL); then \
$(INSTALL_PROG) \
dll$(CLIBNAME)$(EXT_DLL) "$(INSTALL_STUBLIBDIR)"; \
dll$(CLIBNAME_NATIVE)$(EXT_DLL) "$(INSTALL_STUBLIBDIR)"; \
fi
ifneq "$(STUBSLIB)" ""
$(INSTALL_DATA) $(STUBSLIB) "$(INSTALL_LIBDIR)/"
ifneq "$(STUBSLIB_BYTECODE)" ""
$(INSTALL_DATA) $(STUBSLIB_BYTECODE) "$(INSTALL_LIBDIR)/"
endif
ifneq "$(STUBSLIB_NATIVE)" ""
$(INSTALL_DATA) $(STUBSLIB_NATIVE) "$(INSTALL_LIBDIR)/"
endif
# If installing over a previous OCaml version, ensure the library is removed
# from the previous installation.
Expand Down Expand Up @@ -143,9 +164,17 @@ distclean:: clean
%.cmx: %.ml
$(CAMLOPT) -c $(COMPFLAGS) $(OPTCOMPFLAGS) $<

%.b.$(O): %.c $(REQUIRED_HEADERS)
$(CC) -c $(OC_CFLAGS) $(CFLAGS) $(OC_CPPFLAGS) $(CPPFLAGS) \
$(OUTPUTOBJ)$@ $<

%.n.$(O): %.c $(REQUIRED_HEADERS)
$(CC) -c $(OC_CFLAGS) $(OC_NATIVE_C_FLAGS) $(CFLAGS) \
$(OC_CPPFLAGS) $(CPPFLAGS) $(OUTPUTOBJ)$@ $<

ifeq "$(COMPUTE_DEPS)" "true"
ifneq "$(COBJS)" ""
include $(addprefix $(DEPDIR)/, $(COBJS:.$(O)=.$(D)))
ifneq "$(COBJS_BYTECODE)" ""
include $(addprefix $(DEPDIR)/, $(COBJS_BYTECODE:.b.$(O)=.$(D)))
endif
endif

Expand Down
2 changes: 1 addition & 1 deletion otherlibs/runtime_events/Makefile
Expand Up @@ -18,7 +18,7 @@
LIBNAME=runtime_events
CLIBNAME=camlruntime_events
CAMLOBJS=runtime_events.cmo
COBJS=runtime_events_consumer.$(O)
C_SOURCES=runtime_events_consumer.c
HEADERS=runtime_events_consumer.h

include ../Makefile.otherlibs.common
Expand Down
2 changes: 1 addition & 1 deletion otherlibs/str/Makefile
Expand Up @@ -16,7 +16,7 @@
# Makefile for the str library

LIBNAME=str
COBJS=strstubs.$(O)
C_SOURCES=strstubs.c
CLIBNAME=camlstr
CAMLOBJS=str.cmo

Expand Down
2 changes: 1 addition & 1 deletion otherlibs/systhreads/Makefile
Expand Up @@ -66,7 +66,7 @@ lib$(LIBNAME)nat.$(A): $(NATIVECODE_C_OBJS)

$(LIBNAME).cma: $(THREADS_BCOBJS)
ifeq "$(UNIX_OR_WIN32)" "unix"
$(MKLIB) -o $(LIBNAME) -ocamlc '$(CAMLC)' -cclib -lunix -linkall $^
$(MKLIB) -o $(LIBNAME) -ocamlc '$(CAMLC)' -cclib -lunixbyt -linkall $^
# TODO: Figure out why -cclib -lunix is used here.
# It may be because of the threadsUnix module which is deprecated.
# It may hence be good to figure out whether this module shouldn't be
Expand Down
4 changes: 1 addition & 3 deletions otherlibs/unix/Makefile
Expand Up @@ -69,9 +69,7 @@ OS_C_SOURCES += $(addsuffix .c, \
setgroups setsid setuid signals spawn termios umask wait)
endif

ALL_C_SOURCES = $(COMMON_C_SOURCES) $(OS_C_SOURCES)

COBJS = $(ALL_C_SOURCES:.c=.$(O))
C_SOURCES = $(COMMON_C_SOURCES) $(OS_C_SOURCES)

CAMLOBJS=unix.cmo unixLabels.cmo

Expand Down

0 comments on commit 4935f7b

Please sign in to comment.