Skip to content

Commit

Permalink
use Makefile to "cross"-build xen and freestanding
Browse files Browse the repository at this point in the history
As discussed in mirage/mirage-xen#23, using dune's C compilation support leads
to unexpected CFLAGS, especially -fPIC and -fno-strict-aliasing in our setup.

To regain control of CFLAGS, instead use a Makefile. There's no need for the
OCaml mirage-crypto.freestanding / mirage-crypto.xen sublibraries anymore.
Failure of compilation are allowed if the respective opam packages
(mirage-xen-posix, ocaml-freestanding) are not installed.

For a successful cold `dune build @install` run, (alias install) is added to
the (copy-files# ..) stanze in xen/dune and freestanding/dune.

The libmirage_crypto_{xen,freestanding}_stubs.a are installed directly in the
lib/mirage-crypto/ directory.
  • Loading branch information
hannesm committed Oct 17, 2020
1 parent ddd5ca8 commit 3f39140
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
include:
- name: OCaml 4.08 with mirage-xen
arch: amd64
env: OCAML_VERSION=4.08 DEPOPTS="zarith-xen mirage-xen-posix"
env: OCAML_VERSION=4.08 DEPOPTS="zarith-xen mirage-xen-posix mirage-xen-ocaml"
- name: OCaml 4.10 on arm64
arch: arm64
env: OCAML_VERSION=4.10
Expand Down
4 changes: 2 additions & 2 deletions META.mirage-crypto.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DUNE_GEN

xen_linkopts = "-l:xen/libmirage_crypto_xen_stubs.a"
freestanding_linkopts = "-l:freestanding/libmirage_crypto_freestanding_stubs.a"
xen_linkopts = "-l:libmirage_crypto_xen_stubs.a"
freestanding_linkopts = "-l:libmirage_crypto_freestanding_stubs.a"
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 2.6)
(lang dune 2.7)
(name mirage-crypto)
27 changes: 27 additions & 0 deletions freestanding/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
PKG_CONFIG_PATH := $(shell opam config var prefix)/lib/pkgconfig

EXISTS := $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --exists ocaml-freestanding; echo $$?)

.PHONY: all clean
all: libmirage_crypto_freestanding_stubs.a

ifeq ($(EXISTS), 1)
libmirage_crypto_freestanding_stubs.a:
touch $@
else
CC ?= cc
FREESTANDING_CFLAGS := $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --cflags ocaml-freestanding)
MIRAGE_CRYPTO_CFLAGS := $(shell sed 's/^(\(.*\))$$/\1/' ../src/cflags.sexp | tr -d '"')
CFLAGS := $(MIRAGE_CRYPTO_CFLAGS) $(FREESTANDING_CFLAGS)

OBJS=detect_cpu_features.o misc.o misc_sse.o hash_stubs.o md5.o sha1.o \
sha256.o sha512.o aes_generic.o aes_aesni.o des_generic.o chacha.o \
poly1305-donna.o ghash_pclmul.o ghash_generic.o ghash_ctmul.o \
entropy_cpu_stubs.o

libmirage_crypto_freestanding_stubs.a: $(OBJS)
$(AR) r $@ $^
endif

clean:
$(RM) $(OBJS) libmirage_crypto_freestanding_stubs.a
4 changes: 0 additions & 4 deletions freestanding/cflags.sh

This file was deleted.

33 changes: 14 additions & 19 deletions freestanding/dune
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
(copy_files# ../src/native/*)

(library
(name mirage_crypto_freestanding)
(public_name mirage-crypto.freestanding)
(optional)
(libraries ocaml-freestanding)
(foreign_stubs
(language c)
(flags
(:standard)
(:include ../src/cflags.sexp)
(:include cflags-freestanding.sexp))
(names detect_cpu_features misc misc_sse hash_stubs md5 sha1 sha256 sha512
aes_generic aes_aesni des_generic chacha poly1305-donna ghash_pclmul
ghash_generic ghash_ctmul entropy_cpu_stubs)))
(copy_files#
(alias install)
(files ../src/native/*))

(rule
(with-stdout-to
cflags-freestanding.sexp
(run ./cflags.sh)))
(deps ../src/cflags.sexp Makefile)
(targets libmirage_crypto_freestanding_stubs.a)
(action
(no-infer
(progn
(run %{make})))))

(install
(package mirage-crypto)
(section lib)
(files libmirage_crypto_freestanding_stubs.a))
2 changes: 1 addition & 1 deletion mirage-crypto-pk.opam
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build: [ ["dune" "subst"] {pinned}
depends: [
"conf-gmp-powm-sec" {build}
"ocaml" {>= "4.08.0"}
"dune" {>= "2.6"}
"dune" {>= "2.7"}
"ounit" {with-test}
"randomconv" {with-test & >= "0.1.3"}
"cstruct" {>="3.2.0"}
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng-mirage.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build: [ ["dune" "subst"] {pinned}

depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "2.6"}
"dune" {>= "2.7"}
"mirage-crypto-rng" {=version}
"duration"
"cstruct" {>= "4.0.0"}
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build: [ ["dune" "subst"] {pinned}

depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "2.6"}
"dune" {>= "2.7"}
"dune-configurator" {>= "2.0.0"}
"duration"
"cstruct" {>= "4.0.0"}
Expand Down
4 changes: 2 additions & 2 deletions mirage-crypto.opam
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ build: [ ["dune" "subst"] {pinned}
depends: [
"conf-pkg-config" {build}
"ocaml" {>= "4.08.0"}
"dune" {>= "2.6"}
"dune" {>= "2.7"}
"dune-configurator" {>= "2.0.0"}
"ounit" {with-test}
"cstruct" {>="3.2.0"}
"eqaf" {>= "0.7"}
]
depopts: [
"mirage-xen-posix"
"mirage-xen-posix" "mirage-xen-ocaml"
"ocaml-freestanding"
]
conflicts: [
Expand Down
27 changes: 27 additions & 0 deletions xen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
PKG_CONFIG_PATH := $(shell opam config var prefix)/lib/pkgconfig

EXISTS := $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --exists mirage-xen-posix,mirage-xen-ocaml; echo $$?)

.PHONY: all clean
all: libmirage_crypto_xen_stubs.a

ifeq ($(EXISTS), 1)
libmirage_crypto_xen_stubs.a:
touch $@
else
CC ?= cc
XEN_CFLAGS := $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --cflags mirage-xen-posix,mirage-xen-ocaml)
MIRAGE_CRYPTO_CFLAGS := $(shell sed 's/^(\(.*\))$$/\1/' ../src/cflags.sexp | tr -d '"')
CFLAGS := $(MIRAGE_CRYPTO_CFLAGS) $(XEN_CFLAGS)

OBJS=detect_cpu_features.o misc.o misc_sse.o hash_stubs.o md5.o sha1.o \
sha256.o sha512.o aes_generic.o aes_aesni.o des_generic.o chacha.o \
poly1305-donna.o ghash_pclmul.o ghash_generic.o ghash_ctmul.o \
entropy_cpu_stubs.o

libmirage_crypto_xen_stubs.a: $(OBJS)
$(AR) r $@ $^
endif

clean:
$(RM) $(OBJS) libmirage_crypto_xen_stubs.a
4 changes: 0 additions & 4 deletions xen/cflags.sh

This file was deleted.

33 changes: 14 additions & 19 deletions xen/dune
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
(copy_files# ../src/native/*)

(library
(name mirage_crypto_xen)
(public_name mirage-crypto.xen)
(optional)
(libraries mirage-xen-posix)
(foreign_stubs
(language c)
(names detect_cpu_features misc misc_sse hash_stubs md5 sha1 sha256 sha512
aes_generic aes_aesni des_generic chacha poly1305-donna ghash_pclmul
ghash_generic ghash_ctmul entropy_cpu_stubs)
(flags
(:standard)
(:include ../src/cflags.sexp)
(:include cflags-xen.sexp))))
(copy_files#
(alias install)
(files ../src/native/*))

(rule
(with-stdout-to
cflags-xen.sexp
(run ./cflags.sh)))
(deps ../src/cflags.sexp Makefile)
(targets libmirage_crypto_xen_stubs.a)
(action
(no-infer
(progn
(run %{make})))))

(install
(package mirage-crypto)
(section lib)
(files libmirage_crypto_xen_stubs.a))

0 comments on commit 3f39140

Please sign in to comment.