5

I'm building a native library for my Android application with bazel.

I'd like to use some OpenSSL functions on it like this:

#include <jni.h>
#include <openssl/aes.h>
...
AES_encrypt(in, out, key);

How to add the openssl library to bazel build ?

Subsidiary question: which archive I should use ?

openssl-1.1.0c.tar.gz
openssl-1.0.2j.tar.gz
openssl-1.0.1u.tar.gz
openssl-fips-2.0.13.tar.gz
openssl-fips-ecp-2.0.13.tar.gz

What I've tried

I've downloaded the openssl-1.0.2j archive. and added a cc_library entry to my BUILD file.

cc_library(
    name = "openssl",
    srcs = glob([
         "openssl-1.0.2j/crypto/**/*.h",
         "openssl-1.0.2j/crypto/**/*.c"
         ]),
    includes = [
        "openssl-1.0.2j",
        "openssl-1.0.2j/crypto/",
        ],
    visibility = ["//visibility:public"],
)

I've this error:

openssl-1.0.2j/crypto/dh/p512.c:60:24: fatal error: openssl/bn.h: No such file or directory
 #include <openssl/bn.h>

But I don't understand why this code is trying to include a file from openssl while it's in openssl-1.0.2j/crypto/

With openssl-1.1.0c

openssl-1.1.0c/include/openssl/e_os2.h:13:34: fatal error: openssl/opensslconf.h: No such file or directory
 # include <openssl/opensslconf.h>

Even if I run the Configure command, no opensslconf.h file is generated.

Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
  • `` and `` are created by OpenSSL's `Configure` script. They are specific to the platform and architecture. Also see [Build Multiarch OpenSSL on OS X](http://stackoverflow.com/q/25530429/608639). – jww Dec 21 '16 at 00:06
  • 1
    I'm afraid the OpenSSL build may be more complex than a simple cc_library can express.I just tried to download the 1.0.2j, and it's not that simple. I think what's happening is that bn.h is only generated by ./config if bn (whatever that is) is enabled at config time, which it's not by default, so you'd have to manually (?) exclude the bn sub-library from the sources in the cc_library. You can build with -k (i.e., bazel -k :openssl) to see more errors - bn.h is just one of the problems - I also saw missing rc5.h and md2.h. – Ulf Adams Feb 17 '17 at 21:13
  • There are build rules for building OpenSSL with Bazel: [lbuchy/bazel-openssl](https://github.com/lbuchy/bazel-openssl), [bazelment/trunk](https://github.com/bazelment/trunk/tree/master/third_party/openssl). I didn't try any of them but at least these build files can be useful to get basic idea how to build it. – kolen Jul 26 '17 at 19:23

1 Answers1

3

Based on @Ulf Adams' answer, I gave up trying to compile OpenSSL with bazel. I used BoringSSL instead.

BoringSSLis a fork of OpenSSL, and can easily be incorporated to a bazel project by doing:

git_repository(
    name = "boringssl",
    commit = "_some commit_",
    remote = "https://boringssl.googlesource.com/boringssl",
)
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99