Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions doc/make.texi
Original file line number Diff line number Diff line change
Expand Up @@ -7626,6 +7626,30 @@ separators (@code{/}). Note that, in contrast to @code{realpath}
function, @code{abspath} does not resolve symlinks and does not require
the file names to refer to an existing file or directory. Use the
@code{wildcard} function to test for existence.

@item $(this_line)
@findex this_line
@cindex this_line, function
The invocation @code{$(this_line)} expands to the current line
number. It is inspired by the @code{__LINE__} magical macro of the C
programming language.


@item $(this_file)
@findex this_file
@cindex this_file, function
The invocation @code{$(this_file)} expands to the current filename,
e.g. to @code{Makefile}. It is inspired by the @code{__FILE__}
magical macro of the C programming language.


@item $(this_counter)
@findex this_counter
@cindex this_counter, function
The invocation @code{$(this_counter)} expands to a unique, incremented,
counter (in decimal). It is inspired by the @code{__COUNTER__} magical
macro of the C programming language understood by GCC.

@end table

@node Conditional Functions, Foreach Function, File Name Functions, Functions
Expand Down
68 changes: 67 additions & 1 deletion src/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,65 @@ func_value (char *o, char **argv, const char *funcname UNUSED)
return o;
}

///// added by <[email protected]>


/**
$(this_file)

Always expands to the current Makefile path. Inspired by the __FILE__ macro of C.
**/

static char *
func_this_file (char *o UNUSED, char **argv UNUSED, const char *funcname UNUSED)
{
if (reading_file) {
return xstrdup(reading_file->filenm);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using o = variable_buffer_output and return o instead of xstrdup and the internal function(s) do work if adding a pseudo argument, like $(this_file fluffy).

Then it's down to the handling of no-arg functions.

}
else
return xstrdup("?");
}


/**
$(this_line)

Always expands to the current line number. Inspired by the __LINE__ macro of C.
**/

static char *
func_this_line (char *o UNUSED, char **argv UNUSED, const char *funcname UNUSED)
{
if (reading_file) {
char linumbuf[32];
memset (linumbuf, 0, sizeof(linumbuf));
snprintf(linumbuf, sizeof(linumbuf), "%lu", reading_file->lineno);
return xstrdup(linumbuf);
}
else
return xstrdup("0");
}


/**
$(this_counter)

Always expands to a unique, incremented, counter. Inspired by the __COUNTER__ macro of GCC.
**/

static char *
func_this_counter (char *o UNUSED, char **argv UNUSED, const char *funcname UNUSED)
{
static long counter;
char cntbuf[32];
memset (cntbuf, 0, sizeof(cntbuf));
counter++;
snprintf (cntbuf, sizeof(cntbuf), "%ld", counter);
return xstrdup(cntbuf);
}
///// end of functions added by <[email protected]>


/*
\r is replaced on UNIX as well. Is this desirable?
*/
Expand Down Expand Up @@ -2210,6 +2269,10 @@ static struct function_table_entry function_table_init[] =
FT_ENTRY ("eval", 0, 1, 1, func_eval),
FT_ENTRY ("file", 1, 2, 1, func_file),
FT_ENTRY ("debugger", 0, 1, 1, func_debugger),
/// three functions added by <[email protected]>
FT_ENTRY ("this_file", 0, 0, 0, func_this_file),
FT_ENTRY ("this_line", 0, 0, 0, func_this_line),
FT_ENTRY ("this_counter", 0, 0, 0, func_this_counter),
#ifdef EXPERIMENTAL
FT_ENTRY ("eq", 2, 2, 1, func_eq),
FT_ENTRY ("not", 0, 1, 1, func_not),
Expand All @@ -2236,7 +2299,10 @@ expand_builtin_function (char *o, int argc, char **argv,
but so far no internal ones do, so just test it for all functions here
rather than in each one. We can change it later if necessary. */

if (!argc && !entry_p->alloc_fn)
if (!argc
/// the functions named this_* by <[email protected]> take no arguments...
&& strncmp(entry_p->name, "this", sizeof("this")-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my untrained eye sizeof() for a string doesn't seem like a good idea, why not use strlen?

Copy link

@bstarynk bstarynk Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a constant string, sizeof is computed at compile time.

But strlen might be computed at runtime. I know that some versions of GCC + GNU libc are able to compute strlen at compile time (on Linux/x86-64 at least).

&& !entry_p->alloc_fn)
return o;

if (!entry_p->fptr.func_ptr)
Expand Down
2 changes: 1 addition & 1 deletion src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ read_all_makefiles (const char **makefiles)
{
PATH_VAR (current_directory);
static const char *default_makefiles[] =
{ "GNUmakefile", "makefile", "Makefile", 0 };
{ "GNUremakefile", "GNUmakefile", "makefile", "Makefile", 0 };
const char **p;

while (1) {
Expand Down
11 changes: 11 additions & 0 deletions tests/scripts/features/default_names
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
$description = "This script tests to make sure that Make looks for
default makefiles in the correct order (GNUmakefile,makefile,Makefile)";

# Create a makefile called "GNUremakefile"
$remakefile = "GNUremakefile";

open(MAKEFILE,"> $remakefile");
print MAKEFILE "FIRST: ; \@echo It chose GNUremakefile\n";
close(MAKEFILE);

# Create a makefile called "GNUmakefile"
$makefile = "GNUmakefile";

Expand All @@ -27,6 +34,10 @@ if (! -f 'Makefile') {
close(MAKEFILE);
}

run_make_with_options("","",&get_logfile);
compare_output("It chose GNUremakefile\n",&get_logfile(1));
unlink($remakefile);

run_make_with_options("","",&get_logfile);
compare_output("It chose GNUmakefile\n",&get_logfile(1));
unlink($makefile);
Expand Down