Ok. Then this is what I was able to make of it. To be honest, I prefer the
Makefile syntax over the script syntax, because of its direct substitutions.

Other improvements that anyone likes to suggest? Is this the best bash can
do?


On Thu, Mar 14, 2024 at 3:47 AM Martin D Kealey <mar...@kurahaupo.gen.nz>
wrote:

> On Wed, 13 Mar 2024, Mischa Baars wrote:
>
> > Date: Wed, 13 Mar 2024 22:52:16
> > From: Mischa Baars <mjbaars1977.bac...@gmail.com>
> > To: alex xmb sw ratchev <fxmb...@gmail.com>
> > Cc: psm...@gnu.org, bug-bash <bug-b...@gnu.org>, help-make@gnu.org
> > Subject: Re: multi-threaded compiling
> >
> > I found another mistake of mine:
> >
> > #ifndef __STRINGIZED__
> >
> > should be
> >
> > #if __STRINGIZED__ == 0
> >
> > or it will always go for the second statement in the conditional.
> >
> > Can someone please correct my script??? Things are starting to itch.
>
> The fundamental limitation is that Bash only provides lists of scalars
> masquerading as arrays, and lists of scalar-scalar pairs masquerading as
> maps (aka hashes, dicts, or "associative arrays").
>
> It does not support any more complex data structures, and in particular,
> does not have arrays-of-arrays or lists-of-lists.
>
> So I would change
>
>   CFLAGS[0]="-D__STRINGIZED__=0 -D__STRING__=\"${STR[0]}\""
>   CFLAGS[1]="-D__STRINGIZED__=0 -D__STRING__=\"${STR[1]}\""
>   CFLAGS[2]="-D__STRINGIZED__=1 -D__STRING__=\"${STR[0]}\""
>   CFLAGS[3]="-D__STRINGIZED__=1 -D__STRING__=\"${STR[1]}\""
>
> to
>
>   CFLAGS0=( -D__STRINGIZED__=0 -D__STRING__="${STR[0]}" )
>   CFLAGS1=( -D__STRINGIZED__=0 -D__STRING__="${STR[1]}" )
>   CFLAGS2=( -D__STRINGIZED__=1 -D__STRING__="${STR[0]}" )
>   CFLAGS3=( -D__STRINGIZED__=1 -D__STRING__="${STR[1]}" )
>
> and then change
>
>   gcc -o main main.c ${CFLAGS[0]} ; ./main
>   gcc -o main main.c ${CFLAGS[1]} ; ./main
>   gcc -o main main.c ${CFLAGS[2]} ; ./main
>   gcc -o main main.c ${CFLAGS[3]} ; ./main
>
> to
>
>   gcc -o main main.c "${CFLAGS0[@]}" && ./main
>   gcc -o main main.c "${CFLAGS1[@]}" && ./main
>   gcc -o main main.c "${CFLAGS2[@]}" && ./main
>   gcc -o main main.c "${CFLAGS3[@]}" && ./main
>
> If you absolutely must have some kind one indexed array for all the
> permutations of cflags, then you could use a synthetic multidimensional
> array, like this:
>
>   CFLAGS=( -D__STRINGIZED__=0 -D__STRING__="${STR[0]}"
>            -D__STRINGIZED__=0 -D__STRING__="${STR[1]}"
>            -D__STRINGIZED__=1 -D__STRING__="${STR[0]}"
>            -D__STRINGIZED__=1 -D__STRING__="${STR[1]}" )
>
>   gcc -o main main.c "${CFLAGS[@]:0*2:2}" && ./main
>   gcc -o main main.c "${CFLAGS[@]:1*2:2}" && ./main
>   gcc -o main main.c "${CFLAGS[@]:2*2:2}" && ./main
>   gcc -o main main.c "${CFLAGS[@]:3*2:2}" && ./main
>
> However, given the pattern, I'd have gone with:
>
>   for i in 0 1 2 3 ; do
>     gcc -o main main.c -D__STRINGIZED__=$((i/2))
> -D__STRING__="${STR[i%2]}" &&
>     ./main
>   done
>
> -Martin
>

Attachment: make.sh
Description: application/shellscript

/* * * *
 *	:set tabstop=9
 */

#include	<stdio.h>

#define	str(x)	lit(x)
#define	lit(x)	#x

#if	__STRINGIZED__ == 0
const	char*	s      =	    __STRING__ ;
#else
const	char*	s      =	str(__STRING__);
#endif

int	main	()
{
	printf	("STRINGIZED: %d, STRING: %s\n", __STRINGIZED__, s);
}

Attachment: Makefile
Description: Binary data

Reply via email to