Repository: conan-io/conan-center-index

Issue:
[package] openssl/1.1.1d: building with mingw on Linux creates packages with both shared and static libs
<!-- 
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.
-->

### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **openssl/1.1.1d**
  * Operating System+version: **Fedora Linux 30**
  * Compiler+version: **GCC 8**
  * Conan version: **conan 1.22.1**
  * Python version: **Python 3.7.6**


### Conan profile (output of `conan profile show default` or `conan profile show 
```
[settings]
os=Windows
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=8
compiler.libcxx=libstdc++11
compiler.threads=win32
compiler.exception=seh
build_type=Release
[options]
[build_requires]
[env]
AR=x86_64-w64-mingw32-ar
AS=x86_64-w64-mingw32-as
CC=x86_64-w64-mingw32-gcc
CXX=x86_64-w64-mingw32-g++
CPP=x86_64-w64-mingw32-cpp
LD=x86_64-w64-mingw32-ld
NM=x86_64-w64-mingw32-nm
OBJCOPY=x86_64-w64-mingw32-objcopy
OBJDUMP=x86_64-w64-mingw32-objdump
RC=x86_64-w64-mingw32-windres
STRIP=x86_64-w64-mingw32-strip
```



### Steps to reproduce (Include if Applicable)

`conan install openssl/1.1.1d@ -pr mingw`

The contents of the lib directory is:
```
├── lib
│   ├── engines-1_1
│   │   ├── capi.dll
│   │   └── padlock.dll
│   ├── libcrypto.a
│   ├── libcrypto.dll.a
│   ├── libssl.a
│   └── libssl.dll.a
```



Discussion:
This is also the case when not cross building.
```
├── lib
│   ├── engines-1.1
│   │   ├── afalg.so
│   │   ├── capi.so
│   │   └── padlock.so
│   ├── libcrypto.a
│   ├── libcrypto.so -> libcrypto.so.1.1
│   ├── libcrypto.so.1.1
│   ├── libssl.a
│   ├── libssl.so -> libssl.so.1.1
│   └── libssl.so.1.1
```


~The `no_zlib` options is not working:~
```
$ ldd lib/libssl.so
	linux-vdso.so.1 (0x00007ffd50dfa000)
	libcrypto.so.1.1 => /lib64/libcrypto.so.1.1 (0x00007f58ce278000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f58ce272000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f58ce251000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f58ce08b000)
	libz.so.1 => /lib64/libz.so.1 (0x00007f58ce071000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f58ce61a000)
```
~It links to the system `libz`.~

Nevermind this comment. This was weird `LD_LIBRARY_PATH` behavior.
I didn't know that `ldd` showed all the libraries.
(these commands were ran in the package folder of openssl)
```
$ ldd libssl.so
	linux-vdso.so.1 (0x00007ffd249c9000)
	libcrypto.so.1.1 => /lib64/libcrypto.so.1.1 (0x00007fd65edeb000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007fd65ede5000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd65edc4000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fd65ebfe000)
	libz.so.1 => /lib64/libz.so.1 (0x00007fd65ebe4000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fd65f18d000)
$ LD_LIBRARY_PATH=$PWD ldd libssl.so
	linux-vdso.so.1 (0x00007ffc333fa000)
	libcrypto.so.1.1 => /home/maarten/.conan/data/openssl/1.1.1d/_/_/package/8a71d609bd4fa5ad7a21848e2ff8712a0662b158/lib/libcrypto.so.1.1 (0x00007f05205f0000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f05205bd000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f052059c000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f05203d6000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f052095f000)

```

@madebr do you have a complete log? was an option (`shared` or `no-shared`) properly passed to the OpenSSL configure command?
@SSE4 Yes:
```
openssl/1.1.1d: activated option: shared
openssl/1.1.1d: activated option: shared
openssl/1.1.1d: ['"conan-Release-Linux-x86_64-gcc-9"', 'shared', '--prefix=/home/maarten/.conan/data/openssl/1.1.1d/_/_/package/160c0f76fcf1ad0d80837054bc0d59a5f4068208', '--openssldir=/home/maarten/.conan/data/openssl/1.1.1d/_/_/package/160c0f76fcf1ad0d80837054bc0d59a5f4068208/res', 'no-unit-test', 'PERL=perl', 'no-tests', '--release', '-fPIC', '--with-zlib-include="/home/maarten/.conan/data/zlib/1.2.11/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/include"', '--with-zlib-lib="/home/maarten/.conan/data/zlib/1.2.11/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib"', 'shared']

----Running------
> perl ./Configure "conan-Release-Linux-x86_64-gcc-9" shared --prefix=/home/maarten/.conan/data/openssl/1.1.1d/_/_/package/160c0f76fcf1ad0d80837054bc0d59a5f4068208 --openssldir=/home/maarten/.conan/data/openssl/1.1.1d/_/_/package/160c0f76fcf1ad0d80837054bc0d59a5f4068208/res no-unit-test PERL=perl no-tests --release -fPIC --with-zlib-include="/home/maarten/.conan/data/zlib/1.2.11/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/include" --with-zlib-lib="/home/maarten/.conan/data/zlib/1.2.11/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib" shared
-----------------
```
The complete command I've used for the log is:
```
conan create . openssl/1.1.1d@  -o openssl:shared=True 2>&1 >openssl_default.log
```
Here is the log: [openssl_default.log](https://github.com/conan-io/conan-center-index/files/4287825/openssl_default.log)

The library is not built twice, the build script just creates a static library from the objects.
okay, then we just need to remove libraries as a post-process step

Resolving patch:
diff --git a/recipes/openssl/ALL/conanfile.py b/recipes/openssl/all/conanfile.py
similarity index 96%
rename from recipes/openssl/ALL/conanfile.py
rename to recipes/openssl/all/conanfile.py
--- a/recipes/openssl/ALL/conanfile.py
+++ b/recipes/openssl/all/conanfile.py
@@ -4,7 +4,6 @@
 from functools import total_ordering
 from conans.errors import ConanInvalidConfiguration, ConanException
 from conans import ConanFile, AutoToolsBuildEnvironment, tools
-from conans.tools import os_info
 
 
 @total_ordering
@@ -96,17 +95,16 @@ class OpenSSLConan(ConanFile):
     default_options["fPIC"] = True
     default_options["openssldir"] = None
     _env_build = None
-    _source_subfolder = "sources"
+    _source_subfolder = "source_subfolder"
 
     def build_requirements(self):
-        # useful for example for conditional build_requires
         if tools.os_info.is_windows:
             if not self._win_bash:
                 self.build_requires("strawberryperl/5.30.0.1")
             if not self.options.no_asm and not tools.which("nasm"):
                 self.build_requires("nasm/2.14")
         if self._win_bash:
-            if "CONAN_BASH_PATH" not in os.environ and os_info.detect_windows_subsystem() != 'msys2':
+            if "CONAN_BASH_PATH" not in os.environ and tools.os_info.detect_windows_subsystem() != 'msys2':
                 self.build_requires("msys2/20190524")
 
     @property
@@ -417,11 +415,11 @@ def _configure_args(self):
             if self.options.capieng_dialog:
                 args.append("-DOPENSSL_CAPIENG_DIALOG=1")
         else:
-            args.append("-fPIC" if self.options.fPIC else "")
+            args.append("-fPIC" if self.options.fPIC else "no-pic")
         if self.settings.os == "Neutrino":
             args.append("-lsocket no-asm")
 
-        if "zlib" in self.deps_cpp_info.deps:
+        if not self.options.no_zlib:
             zlib_info = self.deps_cpp_info["zlib"]
             include_path = zlib_info.include_paths[0]
             if self.settings.os == "Windows":
@@ -530,11 +528,12 @@ def _perl(self):
             return os.path.join(self.deps_cpp_info["strawberryperl"].rootpath, "bin", "perl.exe")
         return "perl"
 
+    @property
+    def _nmake_makefile(self):
+        return r"ms\ntdll.mak" if self.options.shared else r"ms\nt.mak"
+
     def _make(self):
         with tools.chdir(self._source_subfolder):
-            # workaround for MinGW (https://github.com/openssl/openssl/issues/7653)
-            if not os.path.isdir(os.path.join(self.package_folder, "bin")):
-                os.makedirs(os.path.join(self.package_folder, "bin"))
             # workaround for clang-cl not producing .pdb files
             if self._is_clangcl:
                 tools.save("ossl_static.pdb", "")
@@ -550,7 +549,6 @@ def _make(self):
                     self.run(r"ms\do_nasm")
                 else:
                     self.run(r"ms\do_ms" if self.settings.arch == "x86" else r"ms\do_win64a")
-                makefile = r"ms\ntdll.mak" if self.options.shared else r"ms\nt.mak"
 
                 self._replace_runtime_in_file(os.path.join("ms", "nt.mak"))
                 self._replace_runtime_in_file(os.path.join("ms", "ntdll.mak"))
@@ -558,10 +556,19 @@ def _make(self):
                     tools.replace_in_file(os.path.join("ms", "nt.mak"), "-WX", "")
                     tools.replace_in_file(os.path.join("ms", "ntdll.mak"), "-WX", "")
 
-                self._run_make(makefile=makefile)
-                self._run_make(makefile=makefile, targets=["install"], parallel=False)
+                self._run_make(makefile=self._nmake_makefile)
             else:
                 self._run_make()
+
+    def _make_install(self):
+        with tools.chdir(self._source_subfolder):
+            # workaround for MinGW (https://github.com/openssl/openssl/issues/7653)
+            if not os.path.isdir(os.path.join(self.package_folder, "bin")):
+                os.makedirs(os.path.join(self.package_folder, "bin"))
+
+            if self._use_nmake and self._full_version < "1.1.0":
+                self._run_make(makefile=self._nmake_makefile, targets=["install"], parallel=False)
+            else:
                 self._run_make(targets=["install_sw"], parallel=False)
 
     @property
@@ -595,18 +602,10 @@ def build(self):
                     self._patch_makefile_org()
                 self._make()
 
-    @staticmethod
-    def detected_os():
-        if tools.OSInfo().is_macos:
-            return "Macos"
-        if tools.OSInfo().is_windows:
-            return "Windows"
-        return platform.system()
-
     @property
     def _cross_building(self):
         if tools.cross_building(self.settings):
-            if self.settings.os == self.detected_os():
+            if self.settings.os == tools.detected_os():
                 if self.settings.arch == "x86" and tools.detected_architecture() == "x86_64":
                     return False
             return True
@@ -642,6 +641,8 @@ def _replace_runtime_in_file(self, filename):
 
     def package(self):
         self.copy(src=self._source_subfolder, pattern="*LICENSE", dst="licenses")
+        with tools.vcvars(self.settings) if self._use_nmake else tools.no_op():
+            self._make_install()
         for root, _, files in os.walk(self.package_folder):
             for filename in files:
                 if fnmatch.fnmatch(filename, "*.pdb"):
@@ -657,6 +658,15 @@ def package(self):
             with tools.chdir(os.path.join(self.package_folder, "lib")):
                 os.chmod("libssl.so.1.0.0", 0o755)
                 os.chmod("libcrypto.so.1.0.0", 0o755)
+
+        if self.options.shared:
+            libdir = os.path.join(self.package_folder, "lib")
+            for file in os.listdir(libdir):
+                if self._is_mingw and file.endswith(".dll.a"):
+                    continue
+                if file.endswith(".a"):
+                    os.unlink(os.path.join(libdir, file))
+
         tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
 
     def package_info(self):


Test patch:
diff --git a/recipes/openssl/ALL/test_package/CMakeLists.txt b/recipes/openssl/ALL/test_package/CMakeLists.txt
deleted file mode 100644
--- a/recipes/openssl/ALL/test_package/CMakeLists.txt
+++ /dev/null
@@ -1,30 +0,0 @@
-cmake_minimum_required(VERSION 2.8)
-PROJECT(MyHello)
-
-include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
-conan_basic_setup()
-
-ADD_EXECUTABLE(digest digest.cpp)
-
-if(USE_FIND_PACKAGE)
-    set(OpenSSL_DEBUG 1)
-    find_package(OpenSSL REQUIRED)
-    MESSAGE("LINK WITH ${OPENSSL_LIBRARIES}")
-
-    target_include_directories(digest PRIVATE ${OPENSSL_INCLUDE_DIRS})
-    TARGET_LINK_LIBRARIES(digest ${OPENSSL_LIBRARIES})
-
-    find_package(Threads)
-    target_link_libraries(digest PRIVATE ${CMAKE_THREAD_LIBS_INIT})
-
-    if(WIN32)
-        TARGET_LINK_LIBRARIES(digest PRIVATE ws2_32 crypt32)
-    endif()
-    if(UNIX AND NOT APPLE)
-        TARGET_LINK_LIBRARIES(digest PRIVATE ${CMAKE_DL_LIBS})
-    endif()
-else()
-    MESSAGE("LINK WITH ${CONAN_LIBS}")
-    target_include_directories(digest PRIVATE ${CONAN_INCLUDE_DIRS})
-    TARGET_LINK_LIBRARIES(digest PRIVATE ${CONAN_LIBS})
-endif()
diff --git a/recipes/openssl/all/test_package/CMakeLists.txt b/recipes/openssl/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/openssl/all/test_package/CMakeLists.txt
@@ -0,0 +1,36 @@
+cmake_minimum_required(VERSION 2.8)
+project(OpenSSLTests)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+option(OPENSSL_WITH_ZLIB "OpenSSL with zlib support" ON)
+
+add_executable(digest digest.cpp)
+if(OPENSSL_WITH_ZLIB)
+    target_compile_definitions(digest PRIVATE WITH_ZLIB)
+endif()
+
+
+if(USE_FIND_PACKAGE)
+    set(OpenSSL_DEBUG 1)
+    find_package(OpenSSL REQUIRED)
+    message("LINK WITH ${OPENSSL_LIBRARIES}")
+
+    target_include_directories(digest PRIVATE ${OPENSSL_INCLUDE_DIRS})
+    target_link_libraries(digest ${OPENSSL_LIBRARIES})
+
+    find_package(Threads)
+    target_link_libraries(digest PRIVATE ${CMAKE_THREAD_LIBS_INIT})
+
+    if(WIN32)
+        target_link_libraries(digest PRIVATE ws2_32 crypt32)
+    endif()
+    if(UNIX AND NOT APPLE)
+        target_link_libraries(digest PRIVATE ${CMAKE_DL_LIBS})
+    endif()
+else()
+    message("LINK WITH ${CONAN_LIBS}")
+    target_include_directories(digest PRIVATE ${CONAN_INCLUDE_DIRS})
+    target_link_libraries(digest PRIVATE ${CONAN_LIBS})
+endif()
diff --git a/recipes/openssl/ALL/test_package/conanfile.py b/recipes/openssl/all/test_package/conanfile.py
similarity index 93%
rename from recipes/openssl/ALL/test_package/conanfile.py
rename to recipes/openssl/all/test_package/conanfile.py
--- a/recipes/openssl/ALL/test_package/conanfile.py
+++ b/recipes/openssl/all/test_package/conanfile.py
@@ -11,6 +11,7 @@ def _build_cmake(self, use_find_package):
 
         if self.settings.os == "Android":
             cmake.definitions["CONAN_LIBCXX"] = ""
+        cmake.definitions["OPENSSL_WITH_ZLIB"] = not self.options["openssl"].no_zlib
         cmake.definitions["USE_FIND_PACKAGE"] = use_find_package
         cmake.definitions["OPENSSL_ROOT_DIR"] = self.deps_cpp_info["openssl"].rootpath
         cmake.definitions["OPENSSL_USE_STATIC_LIBS"] = not self.options["openssl"].shared
diff --git a/recipes/openssl/ALL/test_package/digest.cpp b/recipes/openssl/all/test_package/digest.cpp
similarity index 92%
rename from recipes/openssl/ALL/test_package/digest.cpp
rename to recipes/openssl/all/test_package/digest.cpp
--- a/recipes/openssl/ALL/test_package/digest.cpp
+++ b/recipes/openssl/all/test_package/digest.cpp
@@ -6,17 +6,15 @@
 #include <openssl/sha.h>
 #include <openssl/crypto.h>
 #include <openssl/ssl.h>
+#if defined(WITH_ZLIB)
 #include <zlib.h>
+#endif
 
 #if defined(_MSC_VER) && _MSC_VER < 1900
 #define snprintf _snprintf
 #endif
 
-#if OPENSSL_VERSION_MAJOR > 1
-#define OPENSSL_1_1_1_OR_LATER
-#elif (OPENSSL_VERSION_MAJOR == 1) && (OPENSSL_VERSION_MINOR > 1)
-#define OPENSSL_1_1_1_OR_LATER
-#elif (OPENSSL_VERSION_MAJOR == 1) && (OPENSSL_VERSION_MINOR == 1) && (OPENSSL_VERSION_PATCH >= 1)
+#if OPENSSL_VERSION_NUMBER > 0x10101000L
 #define OPENSSL_1_1_1_OR_LATER
 #endif
 
@@ -90,7 +88,9 @@ int main()
 	printf("sha3 512 digest: %s\n", sha3_512_string);
 #endif
 	printf("SSL library version: %s\n", SSLeay_version(SSLEAY_VERSION));
+#if defined(WITH_ZLIB)
 	printf("ZLIB version: %s\n", ZLIB_VERSION);
+#endif
 
 	return 0;
 }