herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
450 stars 152 forks source link

How to build the `*.a` static libs when using cmake? #98

Closed mxaddict closed 3 years ago

mxaddict commented 3 years ago

I'm trying to build mcl lib with static libs when using the cmake to build, I don't get all the bn*.a files

But when building with just the Makefile setup, I see these:

-rw-rw-r--  1 mxaddict mxaddict       0 Feb 10 03:27 .emptydir
-rw-rw-r--  1 mxaddict mxaddict 9949812 Feb 14 06:44 libmcl.a
-rw-rw-r--  1 mxaddict mxaddict 4875740 Feb 14 06:44 libmclbn256.a
-rwxrwxr-x  1 mxaddict mxaddict 2159056 Feb 14 06:44 libmclbn256.so*
-rw-rw-r--  1 mxaddict mxaddict 4870558 Feb 14 06:44 libmclbn384_256.a
-rwxrwxr-x  1 mxaddict mxaddict 2162472 Feb 14 06:44 libmclbn384_256.so*
-rw-rw-r--  1 mxaddict mxaddict 4870014 Feb 14 06:44 libmclbn384.a
-rwxrwxr-x  1 mxaddict mxaddict 2162024 Feb 14 06:44 libmclbn384.so*
-rw-rw-r--  1 mxaddict mxaddict 4868854 Feb 14 06:44 libmclbn512.a
-rwxrwxr-x  1 mxaddict mxaddict 2161944 Feb 14 06:44 libmclbn512.so*
-rw-rw-r--  1 mxaddict mxaddict 2145174 Feb 14 06:45 libmclecdsa.a
-rw-rw-r--  1 mxaddict mxaddict 7202870 Feb 14 06:44 libmclshe256.a
-rwxrwxr-x  1 mxaddict mxaddict 7485328 Feb 14 06:44 libmclshe256.so*
-rw-rw-r--  1 mxaddict mxaddict 7187366 Feb 14 06:45 libmclshe384_256.a
-rwxrwxr-x  1 mxaddict mxaddict 7484744 Feb 14 06:45 libmclshe384_256.so*
-rw-rw-r--  1 mxaddict mxaddict 7189886 Feb 14 06:45 libmclshe384.a
-rwxrwxr-x  1 mxaddict mxaddict 7485056 Feb 14 06:45 libmclshe384.so*
-rwxrwxr-x  1 mxaddict mxaddict 4560176 Feb 14 06:44 libmcl.so*

On cmake build, I only get:

-rw-rw-r-- 1 mxaddict mxaddict 1087706 Feb 14 06:39 libmcl.a
lrwxrwxrwx 1 mxaddict mxaddict      16 Feb 14 06:39 libmclbn256.so -> libmclbn256.so.1*
lrwxrwxrwx 1 mxaddict mxaddict      19 Feb 14 06:39 libmclbn256.so.1 -> libmclbn256.so.1.22*
-rwxrwxr-x 1 mxaddict mxaddict  363560 Feb 14 06:39 libmclbn256.so.1.22*
lrwxrwxrwx 1 mxaddict mxaddict      20 Feb 14 06:39 libmclbn384_256.so -> libmclbn384_256.so.1*
lrwxrwxrwx 1 mxaddict mxaddict      23 Feb 14 06:39 libmclbn384_256.so.1 -> libmclbn384_256.so.1.22*
-rwxrwxr-x 1 mxaddict mxaddict  363992 Feb 14 06:39 libmclbn384_256.so.1.22*
lrwxrwxrwx 1 mxaddict mxaddict      16 Feb 14 06:39 libmclbn384.so -> libmclbn384.so.1*
lrwxrwxrwx 1 mxaddict mxaddict      19 Feb 14 06:39 libmclbn384.so.1 -> libmclbn384.so.1.22*
-rwxrwxr-x 1 mxaddict mxaddict  363992 Feb 14 06:39 libmclbn384.so.1.22*
lrwxrwxrwx 1 mxaddict mxaddict      11 Feb 14 06:39 libmcl.so -> libmcl.so.1*
lrwxrwxrwx 1 mxaddict mxaddict      14 Feb 14 06:39 libmcl.so.1 -> libmcl.so.1.22*
-rwxrwxr-x 1 mxaddict mxaddict 1124120 Feb 14 06:39 libmcl.so.1.22*
mxaddict commented 3 years ago

Issue I'm having is that when I think against the generated libmcl.a from the cmake build, It does not have the mclBnG1_clear function

But I see that mcl/build/lib/libmclbn256.so contains it

mxaddict commented 3 years ago

I was able to build static libs by editing the CMakeList.txt and chaging the following:

diff --git a/src/mcl/CMakeLists.txt b/src/mcl/CMakeLists.txt
index c4eb3e89..9526f236 100644
--- a/src/mcl/CMakeLists.txt
+++ b/src/mcl/CMakeLists.txt
@@ -320,7 +320,7 @@ endif()

 # mclbnXXX
 foreach(bit IN ITEMS 256 384 384_256)
-       add_library(mclbn${bit} SHARED src/bn_c${bit}.cpp)
+    add_library(mclbn${bit} STATIC src/bn_c${bit}.cpp)
        add_library(mcl::mclbn${bit} ALIAS mclbn${bit})
        set_target_properties(mclbn${bit} PROPERTIES
                CXX_STANDARD 11

Maybe adding those as static to the repo for cmake builds would be a nice addition, I can create a PR that allows for it if you like.

herumi commented 3 years ago

Thank you for the advice. How about https://github.com/herumi/mcl/commit/10183458d3d4ff81d52c4f96bf3ef0ae6da1de87 ?

mxaddict commented 3 years ago

That looks good, but I think you will also have to add -fPIC when building static bn_ libs

herumi commented 3 years ago

I don't think that -fPIC is necessary for a static library. src/fp.cpp is compiled with -fPIC for a shard-library. What do you have some problem when -fPIC is not be added?

mxaddict commented 3 years ago

I don't think that -fPIC is necessary for a static library. src/fp.cpp is compiled with -fPIC for a shard-library. What do you have some problem when -fPIC is not be added?

Yeah, I think I ran into an issue with linking when -fPIC was not added.

I'll test it out and get some more details to post here

herumi commented 3 years ago

Issue I'm having is that when I think against the generated libmcl.a from the cmake build, It does >not have the mclBnG1_clear function

But I see that mcl/build/lib/libmclbn256.so contains it

Could you please link both -lmcl and -lmclbn256? Or https://github.com/herumi/bls-eth-go-binary/tree/master/bls/lib/linux/amd64 contains all functions in both mcl and mclbn384_256.

mxaddict commented 3 years ago

Actually, you can check how we are compiling and linking the lib here: https://github.com/navcoin/navcoin-core/pull/775

Files of note are in the configure.ac and the changes to src/mcl/CMakeList.txt

herumi commented 3 years ago

I've appended -fPIC to CMakeLists.txt.