git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] Move SHA-1 implementation selection into a header file
@ 2017-03-11 22:28 brian m. carlson
  2017-03-12 13:01 ` Jeff King
  2017-03-14 18:41 ` Jonathan Nieder
  0 siblings, 2 replies; 16+ messages in thread
From: brian m. carlson @ 2017-03-11 22:28 UTC (permalink / raw)
  To: git

Many developers use functionality in their editors that allows for quick
syntax checks, including warning about questionable constructs.  This
functionality allows rapid development with fewer errors.  However, such
functionality generally does not allow the specification of
project-specific defines or command-line options.

Since the SHA1_HEADER include is not defined in such a case, developers
see spurious errors when using these tools.  Furthermore, while using a
macro as the argument to #include is permitted by C11, it isn't
permitted by C89 and C99, and there are known implementations which
reject it.

Instead of using SHA1_HEADER, create a hash.h header and use #if
and #elif to select the desired header.  Have the Makefile pass an
appropriate option to help the header select the right implementation to
use.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---

Fixing this makes my development using the vim-ale plugin much nicer.  I
don't care which implementation is selected by default, as long as
*some* implementation is selected by default.

I called this "hash.h" instead of "sha1.h" to allow for future hash
function extensions.  I was worried that some OS might define a hash.h
header as well, but the use of quotation marks instead of angle brackets
should cause it to look in the current directory first.

I also picked "SHA1_*" instead of "*_SHA1" as it makes it easier to find
all the constants.

 Makefile |  8 ++++----
 cache.h  |  2 +-
 hash.h   | 14 ++++++++++++++
 3 files changed, 19 insertions(+), 5 deletions(-)
 create mode 100644 hash.h

diff --git a/Makefile b/Makefile
index ed68700acb..244eb6a0f2 100644
--- a/Makefile
+++ b/Makefile
@@ -1384,19 +1384,19 @@ ifdef APPLE_COMMON_CRYPTO
 endif
 
 ifdef BLK_SHA1
-	SHA1_HEADER = "block-sha1/sha1.h"
 	LIB_OBJS += block-sha1/sha1.o
+	BASIC_CFLAGS += -DSHA1_BLK
 else
 ifdef PPC_SHA1
-	SHA1_HEADER = "ppc/sha1.h"
 	LIB_OBJS += ppc/sha1.o ppc/sha1ppc.o
+	BASIC_CFLAGS += -DSHA1_PPC
 else
 ifdef APPLE_COMMON_CRYPTO
 	COMPAT_CFLAGS += -DCOMMON_DIGEST_FOR_OPENSSL
-	SHA1_HEADER = <CommonCrypto/CommonDigest.h>
+	BASIC_CFLAGS += -DSHA1_APPLE
 else
-	SHA1_HEADER = <openssl/sha.h>
 	EXTLIBS += $(LIB_4_CRYPTO)
+	BASIC_CFLAGS += -DSHA1_OPENSSL
 endif
 endif
 endif
diff --git a/cache.h b/cache.h
index 283ab8fb40..6a9afb8561 100644
--- a/cache.h
+++ b/cache.h
@@ -10,8 +10,8 @@
 #include "trace.h"
 #include "string-list.h"
 #include "pack-revindex.h"
+#include "hash.h"
 
-#include SHA1_HEADER
 #ifndef platform_SHA_CTX
 /*
  * platform's underlying implementation of SHA-1; could be OpenSSL,
diff --git a/hash.h b/hash.h
new file mode 100644
index 0000000000..7c6b52835c
--- /dev/null
+++ b/hash.h
@@ -0,0 +1,14 @@
+#ifndef HASH_H
+#define HASH_H
+
+#if defined(SHA1_BLK)
+#include "block-sha1/sha1.h"
+#elif defined(SHA1_PPC)
+#include "ppc/sha1.h"
+#elif defined(SHA1_APPLE)
+#include <CommonCrypto/CommonDigest.h>
+#else /* SHA1_OPENSSL */
+#include <openssl/sha.h>
+#endif
+
+#endif

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-03-15 19:50 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-11 22:28 [RFC PATCH] Move SHA-1 implementation selection into a header file brian m. carlson
2017-03-12 13:01 ` Jeff King
2017-03-12 16:51   ` brian m. carlson
2017-03-12 20:12     ` Jeff King
2017-03-12 17:54   ` Junio C Hamano
2017-03-14 18:41 ` Jonathan Nieder
2017-03-14 20:14   ` Jeff King
2017-03-14 20:44     ` Junio C Hamano
2017-03-14 21:26       ` Jeff King
2017-03-14 21:50       ` Jonathan Nieder
2017-03-14 23:42       ` Ramsay Jones
2017-03-14 23:46         ` brian m. carlson
2017-03-15  0:15           ` Ramsay Jones
2017-03-15 15:57             ` Junio C Hamano
2017-03-15 19:48               ` Ramsay Jones
2017-03-14 21:56     ` Jonathan Nieder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).