From fcf80c469aa722f7c6eca68d23d86d22f7f7efb9 Mon Sep 17 00:00:00 2001
From: Richard Levitte <levitte@openssl.org>
Date: Sat, 30 Jan 2016 05:45:29 +0100
Subject: [PATCH] unified build scheme: add the tweaks to build on Cygwin &
 Mingw

Cygwin and Mingw name their libraries a bit differently from the rest
of the POSIXly universe, we need to adapt to that.

In Makefile.tmpl, it means that some hunks will only be output
conditionally.

This also means that shared_extension for the Cygwin and Mingw
configurations in Configurations/10-main.conf are changing from .dll.a
to .dll.  Makefile.shared does a fine job without having them
specified, and it's much easier to work with tucking an extra .a at
the end of files in the installation recipes than any amount of name
rewrites, especially with the support of the SHARED_NAME in the top
build.info.

Reviewed-by: Rich Salz <rsalz@openssl.org>
---
 Configurations/10-main.conf       |  8 ++++----
 Configurations/unix-Makefile.tmpl | 34 +++++++++++++++++++++++++++++++
 Makefile.in                       | 17 ++++++++++++++++
 Makefile.shared                   |  6 +-----
 build.info                        |  8 ++++++++
 5 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/Configurations/10-main.conf b/Configurations/10-main.conf
index c5c1424bdc..db64b9ef07 100644
--- a/Configurations/10-main.conf
+++ b/Configurations/10-main.conf
@@ -1174,7 +1174,7 @@
         shared_target    => "cygwin-shared",
         shared_cflag     => "-D_WINDLL -DOPENSSL_USE_APPLINK",
         shared_ldflag    => "-mno-cygwin",
-        shared_extension => ".dll.a",
+        shared_extension => ".dll",
     },
     "mingw64" => {
         # As for OPENSSL_USE_APPLINK. Applink makes it possible to use
@@ -1199,7 +1199,7 @@
         shared_target    => "cygwin-shared",
         shared_cflag     => "-D_WINDLL",
         shared_ldflag    => "-mno-cygwin",
-        shared_extension => ".dll.a",
+        shared_extension => ".dll",
     },
 
 #### UEFI
@@ -1232,7 +1232,7 @@
         shared_target    => "cygwin-shared",
         shared_cflag     => "-D_WINDLL",
         shared_ldflag    => "-shared",
-        shared_extension => ".dll.a",
+        shared_extension => ".dll",
     },
     "Cygwin-x86_64" => {
         inherit_from     => [ asm("x86_64_asm") ],
@@ -1247,7 +1247,7 @@
         shared_target    => "cygwin-shared",
         shared_cflag     => "-D_WINDLL",
         shared_ldflag    => "-shared",
-        shared_extension => ".dll.a",
+        shared_extension => ".dll",
     },
     # Backward compatibility for those using this target
     "Cygwin" => {
diff --git a/Configurations/unix-Makefile.tmpl b/Configurations/unix-Makefile.tmpl
index 5e9e10bcad..4888dd0240 100644
--- a/Configurations/unix-Makefile.tmpl
+++ b/Configurations/unix-Makefile.tmpl
@@ -228,6 +228,13 @@ install_dev:
 			fn2=`basename $$fn $(SHLIB_EXT)`$(SHLIB_EXT_SIMPLE); \
 			ln -sf $$fn $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn2; \
 		fi; \
+		: {- output_off() unless windowsdll(); "" -}; \
+		echo "install $$s.a -> $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a"; \
+		cp $$s.a $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a.new; \
+		chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a.new; \
+		mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a.new \
+		      $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a; \
+		: {- output_on() -}; \
 	done
 	@ : {- output_on() -}
 	@$(PERL) $(SRCDIR)/util/mkdir-p.pl $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig
@@ -263,6 +270,10 @@ uninstall_dev:
 		fi; \
 		echo "$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn"; \
 		$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn; \
+		: {- output_off() unless windowsdll(); "" -}; \
+		echo "$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a"; \
+		$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$fn.a; \
+		: {- output_on() -}; \
 	done
 	@echo "$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig/libcrypto.pc"
 	@$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig/libcrypto.pc
@@ -297,6 +308,16 @@ install_runtime:
 	@$(PERL) $(SRCDIR)/util/mkdir-p.pl $(INSTALL_PREFIX)$(INSTALLTOP)/bin
 	@$(PERL) $(SRCDIR)/util/mkdir-p.pl $(INSTALL_PREFIX)$(OPENSSLDIR)/misc
 	@echo "*** Installing runtime files"
+	: {- output_off() unless windowsdll(); "" -};
+	@set -e; for s in $(SHLIBS); do \
+		fn=`basename $$i`; \
+		echo "install $$s -> $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn"; \
+		cp $$s $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn.new; \
+		chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn.new; \
+		mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn.new \
+		      $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn; \
+	done
+	: {- output_on() -};
 	@set -e; for x in $(PROGRAMS); do \
 		fn=`basename $$x`; \
 		echo "install $$x -> $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn"; \
@@ -346,6 +367,13 @@ uninstall_runtime:
 		echo "$(RM) $(INSTALL_PREFIX)$(OPENSSLDIR)/misc/$$fn"; \
 		$(RM) $(INSTALL_PREFIX)$(OPENSSLDIR)/misc/$$fn; \
 	done
+	: {- output_off() unless windowsdll(); "" -};
+	@set -e; for s in $(SHLIBS); do \
+		fn=`basename $$i`; \
+		echo "$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn"; \
+		$(RM) $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$fn; \
+	done
+	: {- output_on() -};
 	$(RM) $(INSTALL_PREFIX)$(OPENSSLDIR)/openssl.cnf
 
 # A method to extract all names from a .pod file
@@ -667,6 +695,12 @@ $targets : $lib\$(LIB_EXT) $deps $ordinalsfile
                 CC="\$(CC)" CFLAGS="\$(CFLAGS)" LDFLAGS="\$(LDFLAGS)" \\
                 SHARED_LDFLAGS="\$(SHARED_LDFLAGS)" SHLIB_EXT=\$(SHLIB_EXT) \\
                 link_a.$shlib_target
+EOF
+	  . (windowsdll() ? <<"EOF" : "");
+	rm -f apps/$shlib\$(SHLIB_EXT)
+	rm -f test/$shlib\$(SHLIB_EXT)
+	cp -p $shlib\$(SHLIB_EXT) apps/
+	cp -p $shlib\$(SHLIB_EXT) test/
 EOF
   }
   sub obj2dynlib {
diff --git a/Makefile.in b/Makefile.in
index 44362d0a96..8083a54f7d 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -372,6 +372,23 @@ do_$(SHLIB_TARGET):
 			LIBDEPS="$$libs $(EX_LIBS)" \
 			link_a.$(SHLIB_TARGET); \
 		libs="-l$$i $$libs"; \
+		case "$(PLATFORM)" in \
+		Cygwin*) \
+			rm -f apps/cyg$$i-$(SHLIB_MAJOR).$(SHLIB_MINOR).dll; \
+			rm -f test/cyg$$i-$(SHLIB_MAJOR).$(SHLIB_MINOR).dll; \
+			cp cyg$$i-$(SHLIB_MAJOR).$(SHLIB_MINOR).dll apps/; \
+			cp cyg$$i-$(SHLIB_MAJOR).$(SHLIB_MINOR).dll test/; \
+			;; \
+		mingw*) \
+			case $$i in \
+				crypto) i=libeay32;; \
+				ssl) i=ssleay32;; \
+			esac; \
+			rm -f apps/$$i.dll; \
+			rm -f test/$$i.dll; \
+			cp $$i.dll apps/; \
+			cp $$i.dll test/; \
+		esac; \
 	done
 
 libcrypto.pc: Makefile
diff --git a/Makefile.shared b/Makefile.shared
index af2dc8ce85..a474f16916 100644
--- a/Makefile.shared
+++ b/Makefile.shared
@@ -310,12 +310,8 @@ link_a.cygwin:
 	ALLSYMSFLAGS='-Wl,--whole-archive'; \
 	NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \
 	SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a $$extras"; \
-	[ -f apps/$$dll_name ] && rm apps/$$dll_name; \
-	[ -f test/$$dll_name ] && rm test/$$dll_name; \
 	$(LINK_SO_A) || exit 1; \
-	rm $$extras; \
-	cp -p $$dll_name apps/; \
-	cp -p $$dll_name test/
+	rm $$extras
 link_app.cygwin:
 	@if expr "$(CFLAGS)" : '.*OPENSSL_USE_APPLINK' > /dev/null; then \
 		LIBDEPS="$(SRCDIR)/crypto/applink.o $${LIBDEPS:-$(LIBDEPS)}"; \
diff --git a/build.info b/build.info
index a518efeae6..019fb86ad7 100644
--- a/build.info
+++ b/build.info
@@ -5,3 +5,11 @@ ORDINALS[libssl]=ssl
 INCLUDE[libcrypto]={- rel2abs(catdir($builddir,"include")) -} . crypto/include include
 INCLUDE[libssl]={- rel2abs(catdir($builddir,"include")) -} . include
 DEPEND[libssl]=libcrypto
+
+IF[{- $config{target} =~ /^Cygwin/ -}]
+ SHARED_NAME[libcrypto]=cygcrypto-{- $config{shlib_major}.".".$config{shlib_minor} -}
+ SHARED_NAME[libssl]=cygssl-{- $config{shlib_major}.".".$config{shlib_minor} -}
+ELSIF[{- $config{target} =~ /^mingw/ -}]
+ SHARED_NAME[libcrypto]=libeay32
+ SHARED_NAME[libssl]=ssleay32
+ENDIF