git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 11/13] Makefile: auto-build C strings from make variables
Date: Wed, 5 Feb 2014 13:02:06 -0500	[thread overview]
Message-ID: <20140205180206.GK15218@sigill.intra.peff.net> (raw)
In-Reply-To: <20140205174823.GA15070@sigill.intra.peff.net>

We already insert the values of some make variables into
files in MAKE/*. We can therefore build a simple pattern
rule for converting such a value into a C string in a header
file, which can then be included and used as normal.

The new system is demonstrated on version.c, where it can
replace the use of "-D" on the command-line. Note that we
still have to manually specify a dependency in the
Makefile. In an ideal world, we would auto-detect the header
dependency; however, there are two holdups:

  1. We cannot rely on having automatic header dependency
     generation on all platforms.

  2. Even when we do generate the dependencies, we rely on
     being able to compile the file once, which means our
     system cannot handle generated headers without a manual
     dependency to bootstrap it.

Signed-off-by: Jeff King <peff@peff.net>
---
This is a technique that I have used in other projects with great
success, as the make variable dependencies are represented as file
dependencies (which is the only type of dependency make knows about).

_But_ it ends up a lot less nice here because of the way we do
auto-dependencies. I'm totally open to revamping the way we handle our
header dependencies, but I didn't do that in this series.

 Makefile         | 11 +++++++----
 script/mkcstring | 18 ++++++++++++++++++
 version.c        |  6 ++++--
 3 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 script/mkcstring

diff --git a/Makefile b/Makefile
index cd07a70..203171d 100644
--- a/Makefile
+++ b/Makefile
@@ -1593,6 +1593,11 @@ MAKE/$1: FORCE
 	fi
 endef
 
+MAKE/%-string.h: MAKE/% script/mkcstring
+	$(QUIET_GEN)$(SHELL_PATH) script/mkcstring \
+		$(subst -,_,$*) <$< >$@+ && \
+		mv $@+ $@
+
 LIBS = $(GITLIBS) $(EXTLIBS)
 
 BASIC_CFLAGS += -DSHA1_HEADER=$(call sq,$(SHA1_HEADER)) \
@@ -1614,6 +1619,7 @@ BASIC_CFLAGS += -DSHELL_PATH=$(call scq,$(SHELL_PATH))
 endif
 
 $(eval $(call make-var,USER-AGENT,user agent string,$(GIT_USER_AGENT)))
+$(eval $(call make-var,VERSION,version,$(GIT_VERSION)))
 
 ifdef DEFAULT_HELP_FORMAT
 BASIC_CFLAGS += -DDEFAULT_HELP_FORMAT='"$(DEFAULT_HELP_FORMAT)"'
@@ -1713,10 +1719,7 @@ builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
 	-DGIT_MAN_PATH=$(call scq,$(mandir_relative)) \
 	-DGIT_INFO_PATH=$(call scq,$(infodir_relative))
 
-version.sp version.s version.o: GIT-VERSION-FILE MAKE/USER-AGENT
-version.sp version.s version.o: EXTRA_CPPFLAGS = \
-	-DGIT_VERSION=$(call scq,$(GIT_VERSION)) \
-	-DGIT_USER_AGENT=$(call scq,$(GIT_USER_AGENT))
+version.sp version.s version.o: MAKE/VERSION-string.h MAKE/USER-AGENT-string.h
 
 $(BUILT_INS): git$X
 	$(QUIET_BUILT_IN)$(RM) $@ && \
diff --git a/script/mkcstring b/script/mkcstring
new file mode 100644
index 0000000..c01f430
--- /dev/null
+++ b/script/mkcstring
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+name=$1; shift
+
+c_quote() {
+	sed 's/\\/\\\\/g; s/"/\\"/'
+}
+
+cat <<-EOF
+#ifndef MAKE_${name}_H
+#define MAKE_${name}_H
+
+/* Auto-generated by mkcstring */
+
+#define MAKE_${name} "$(c_quote)"
+
+#endif /* MAKE_${name}_H */
+EOF
diff --git a/version.c b/version.c
index 6106a80..f68a93b 100644
--- a/version.c
+++ b/version.c
@@ -1,8 +1,10 @@
 #include "git-compat-util.h"
 #include "version.h"
 #include "strbuf.h"
+#include "MAKE/USER-AGENT-string.h"
+#include "MAKE/VERSION-string.h"
 
-const char git_version_string[] = GIT_VERSION;
+const char git_version_string[] = MAKE_VERSION;
 
 const char *git_user_agent(void)
 {
@@ -11,7 +13,7 @@ const char *git_user_agent(void)
 	if (!agent) {
 		agent = getenv("GIT_USER_AGENT");
 		if (!agent)
-			agent = GIT_USER_AGENT;
+			agent = MAKE_USER_AGENT;
 	}
 
 	return agent;
-- 
1.8.5.2.500.g8060133

  parent reply	other threads:[~2014-02-05 18:02 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-05 17:48 [PATCH/RFC 0/13] makefile refactoring Jeff King
2014-02-05 17:49 ` [PATCH 01/13] Makefile: drop USE_GETTEXT_SCHEME from GIT-CFLAGS Jeff King
2014-02-05 17:49 ` [PATCH 02/13] Makefile: fix git-instaweb dependency on gitweb Jeff King
2014-02-05 17:50 ` [PATCH 03/13] Makefile: introduce make-var helper function Jeff King
2014-02-06  8:55   ` Eric Sunshine
2014-02-05 17:50 ` [PATCH 04/13] Makefile: use tempfile/mv strategy for GIT-* Jeff King
2014-02-05 17:50 ` [PATCH 05/13] Makefile: prefer printf to echo " Jeff King
2014-02-05 17:52 ` [PATCH 06/13] Makefile: store GIT-* sentinel files in MAKE/ Jeff King
2014-02-05 19:05   ` Junio C Hamano
2014-02-05 17:53 ` [PATCH 07/13] Makefile: always create files via make-var Jeff King
2014-02-05 17:57 ` [PATCH 08/13] Makefile: introduce sq function for shell-quoting Jeff King
2014-02-05 19:12   ` Junio C Hamano
2014-02-05 17:58 ` [PATCH 09/13] Makefile: add c-quote helper function Jeff King
2014-02-05 19:13   ` Junio C Hamano
2014-02-05 19:17     ` Jeff King
2014-02-05 17:58 ` [PATCH 10/13] Makefile: drop *_SQ variables Jeff King
2014-02-05 19:14   ` Junio C Hamano
2014-02-05 18:02 ` Jeff King [this message]
2014-02-05 19:17   ` [PATCH 11/13] Makefile: auto-build C strings from make variables Junio C Hamano
2014-02-05 19:20     ` Jeff King
2014-02-05 18:05 ` [PATCH 12/13] Makefile: teach scripts to include " Jeff King
2014-02-05 19:26   ` Junio C Hamano
2014-02-05 19:50     ` Jeff King
2014-02-08 21:47   ` Thomas Rast
2014-02-10  1:15     ` Jeff King
2014-02-05 18:08 ` [PATCH 13/13] move LESS/LV pager environment to Makefile Jeff King
2014-02-05 19:23   ` Jeff King
2014-02-05 19:52     ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140205180206.GK15218@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).