Skip to contentSkip to conversation

toolchain/wrapper: some cleanups

PATCH New Yann E. MORIN 2 participants 4 messages Dec 4, 2016 – Dec 11, 2016

lore
Share & tools
lore
#0 lore raw

cover toolchain/wrapper: some cleanups

Hello All!

This small series brings a little bit of cleanups in the toolchain wrapper, with no functional change.

Regards,
Yann E. MORIN.

The following changes since commit 0904b80b47245cf85d414f78c381b9cf61c6ef4b

cmake: pass HOST_CONFIGURE_OPTS at configure time (2016-12-03 16:24:23 +0100)

are available in the git repository at:

git://git.buildroot.org/~ymorin/git/buildroot.git

for you to fetch changes up to fdbfb69d6d7d7ee1b1473ac2562fbf69df49dbb8

toolchain/wrapper: use the {str,len} tuples for the unsafe paths (2016-12-04 10:08:04 +0100)

----------------------------------------------------------------
Yann E. MORIN (2):
toolchain/wrapper: make the {str,len} tuple more generic, add comments toolchain/wrapper: use the {str,len} tuples for the unsafe paths

 toolchain/toolchain-wrapper.c | 58 ++++++++++++++++++++++++++++---------------
 1 file changed, 38 insertions(+), 20 deletions(-)
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#1 lore raw

PATCH 1/2 toolchain/wrapper: make the {str, len} tuple more generic, add comments

In 61cb120 (toolchain/wrapper: extend paranoid check to -isystem), we introduced a {str,len} tuple to check the various arguments passed to gcc, to avoid hard-coding an ever-growing, long list of those args directly in the condition check.

It was made specific to the arguments (the structure member is named 'arg'), but can also be used to store the unsafe paths as well.

Also, that piece is almost un-documented.

Rename the structure member so that it is more generic, and add a bit of comments to explain the whole of it.

Cc Thomas Petazzoni <[email protected]>
---
 toolchain/toolchain-wrapper.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)
toolchain/toolchain-wrapper.c +21 -14
index d59629b..8bc0007 100644
@@ -83,23 +83,30 @@ static char *predef_args[] = {
 #endif
 };
 
-struct unsafe_opt_s {
-	const char *arg;
+/* A {string,length} tuple, to avoid computing strlen() on constants.
+ *  - str must be a \0-terminated string
+ *  - len does not account for the terminating '\0'
+ */
+struct str_len_s {
+	const char *str;
 	size_t     len;
 };
 
+/* Define a {string,length} tuple. Takes an unquoted constant string as
+ * parameter. sizeof() on a string literal includes the terminating \0,
+ * but we don't want to count it.
+ */
+#define STR_LEN(s) { #s, sizeof(#s)-1 }
+
 /* Unsafe options are options that specify a potentialy unsafe path,
  * that will be checked by check_unsafe_path(), below.
- *
- * sizeof() on a string literal includes the terminating \0.
  */
-#define UNSAFE_OPT(o) { #o, sizeof(#o)-1 }
-static const struct unsafe_opt_s unsafe_opts[] = {
-	UNSAFE_OPT(-I),
-	UNSAFE_OPT(-idirafter),
-	UNSAFE_OPT(-iquote),
-	UNSAFE_OPT(-isystem),
-	UNSAFE_OPT(-L),
+static const struct str_len_s unsafe_opts[] = {
+	STR_LEN(-I),
+	STR_LEN(-idirafter),
+	STR_LEN(-iquote),
+	STR_LEN(-isystem),
+	STR_LEN(-L),
 	{ NULL, 0 },
 };
 
@@ -262,10 +269,10 @@ int main(int argc, char **argv)
 
 	/* Check for unsafe library and header paths */
 	for (i = 1; i < argc; i++) {
-		const struct unsafe_opt_s *opt;
-		for (opt=unsafe_opts; opt->arg; opt++ ) {
+		const struct str_len_s *opt;
+		for (opt=unsafe_opts; opt->str; opt++ ) {
 			/* Skip any non-unsafe option. */
-			if (strncmp(argv[i], opt->arg, opt->len))
+			if (strncmp(argv[i], opt->str, opt->len))
 				continue;
 
 			/* Handle both cases:
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 toolchain/wrapper: use the {str, len} tuples for the unsafe paths

In reply to Yann E. MORIN

In 61cb120 (toolchain/wrapper: extend paranoid check to -isystem), we introduced a {str,len} tuple to check the various arguments pased to gcc, to avoid hard-coding an ever-growing, long list of those args directly in the condition check.

Now, we're left with a long list of unsafe paths, somehow hidden within the code, which can use the same mechanism we use for arguments.

Cc Thomas Petazzoni <[email protected]>
---
 toolchain/toolchain-wrapper.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
toolchain/toolchain-wrapper.c +17 -6
index 8bc0007..100aa18 100644
@@ -98,6 +98,20 @@ struct str_len_s {
  */
 #define STR_LEN(s) { #s, sizeof(#s)-1 }
 
+/* List of paths considered unsafe for cross-compilation.
+ *
+ * An unsafe path is one that points to a directory with libraries or
+ * headers for the build machine, which are not suitable for the target.
+ */
+static const struct str_len_s unsafe_paths[] = {
+	STR_LEN(/lib),
+	STR_LEN(/usr/include),
+	STR_LEN(/usr/lib),
+	STR_LEN(/usr/local/include),
+	STR_LEN(/usr/local/lib),
+	{ NULL, 0 },
+};
+
 /* Unsafe options are options that specify a potentialy unsafe path,
  * that will be checked by check_unsafe_path(), below.
  */
@@ -126,13 +140,10 @@ static void check_unsafe_path(const char *arg,
 			      int paranoid,
 			      int arg_has_path)
 {
-	char **c;
-	static char *unsafe_paths[] = {
-		"/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
-	};
+	const struct str_len_s *p;
 
-	for (c = unsafe_paths; *c != NULL; c++) {
-		if (strncmp(path, *c, strlen(*c)))
+	for (p=unsafe_paths; p->str; p++) {
+		if (strncmp(path, p->str, p->len))
 			continue;
 		fprintf(stderr,
 			"%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
Signature · 1 line
2.7.4
#3 lore raw

cover toolchain/wrapper: some cleanups

In reply to Yann E. MORIN

Hello,

Quoted 3 lines · Yann E. MORIN

Yann E. MORIN (2):
toolchain/wrapper: make the {str,len} tuple more generic, add comments toolchain/wrapper: use the {str,len} tuples for the unsafe paths

Both applied. Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com