gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 0bfed4c6 1/2: Makefile extensions: new functio


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0bfed4c6 1/2: Makefile extensions: new function to return previous element in list
Date: Thu, 15 Feb 2024 12:47:25 -0500 (EST)

branch: master
commit 0bfed4c60ff4f96e9ab7f359448d6315370d31aa
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Makefile extensions: new function to return previous element in list
    
    Until now, when using Gnuastro within a large pipeline, it was not easy to
    serialize only one part of a pipeline in Makeafiles! Make's '.NOTPARALLEL'
    target would also serialize all the prerequisites of the given targets.
    
    With this commit, a new Makefile extension function has been added to
    Gnuastro for this purpose: 'ast-text-prev-in-list'. It will return the
    previous word to the given word and when placed as a prerequisite, it will
    serialize the operations.
---
 NEWS              |  4 ++++
 doc/gnuastro.texi | 24 ++++++++++++++++++++++++
 lib/makeplugin.c  | 28 ++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/NEWS b/NEWS
index 861555bb..51c25ab4 100644
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,10 @@ See the end of the file for license conditions.
     finding the scale factor; added by Sepideh Eskandarlou and Raul
     Infante-Sainz.
 
+*** Makefile extensions
+  - $(ast-text-prev-in-list TARGET, LIST): select the word that is previous
+    to 'TARGET' in a list of words. See the documentation for a fully
+    working example and how this can be useful.
 ** Removed features
 ** Changed features
 *** All programs
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index f52eccdc..a2e0e216 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -35914,6 +35914,30 @@ all:
      echo $(ast-text-not-contains Aa, $(list))
 @end example
 
+@item $(ast-text-prev-in-list TARGET, LIST)
+Returns the word in @code{LIST} that is previous to @code{TARGET}.
+If @code{TARGET} is the first word of the list, or is not within it at all, 
this function will return an empty string (nothing).
+
+One scenario when this function can be useful is when you want a list of 
targets to always be executed in sequence (even when Make is run in parallel).
+The @code{sleep} command is used in the recipe to show the serialization more 
clearly.
+To see the effect of this function put the example below in a @file{Makefile} 
and run @code{make -j4} (to simultaneously exectute 4 jobs); then 
comment/remove this function (so there is no prerequisite) and re-run 
@code{make -j4}.
+
+@example
+all: final
+.SECONDEXPANSION:
+load /usr/local/lib/libgnuastro_make.so
+
+list=$(foreach i,a b c d,$(i).txt)
+$(list): %.txt: $$(ast-text-prev-in-list $$@@, $(list))
+         @@sleep 0.3; echo "$@@: $^"
+
+final: $(list)
+@end example
+
+As you see, when this function is in the prerequisite list, the recipes are 
run one by one; but without it, all four are printed after an initial 0.3 
seconds of delay.
+Unfortunately the @code{.NOTPARALLEL} target of GNU Make, will also serialize 
the prerequisites of the targets given to it.
+But through this function, the prerequisites of the serialized targets will 
still be executed in parallel.
+
 @item $(ast-text-to-upper STRING)
 Returns the input string but with all characters in UPPER-CASE.
 For example, the following minimal Makefile will print @code{FOOO BAAR UGGH} 
word of the list.
diff --git a/lib/makeplugin.c b/lib/makeplugin.c
index 2d1fd6cf..157a8013 100644
--- a/lib/makeplugin.c
+++ b/lib/makeplugin.c
@@ -52,6 +52,7 @@ int plugin_is_GPL_compatible=1;
 static char *text_to_upper=MAKEPLUGIN_FUNC_PREFIX"-text-to-upper";
 static char *text_to_lower=MAKEPLUGIN_FUNC_PREFIX"-text-to-lower";
 static char *text_contains_name=MAKEPLUGIN_FUNC_PREFIX"-text-contains";
+static char *text_prev_in_list=MAKEPLUGIN_FUNC_PREFIX"-text-prev-in-list";
 static char *text_not_contains_name=MAKEPLUGIN_FUNC_PREFIX"-text-not-contains";
 
 /* Gnuastro analysis functions */
@@ -203,6 +204,29 @@ makeplugin_text_to_lower(const char *caller, unsigned int 
argc,
 
 
 
+/* Return the previous word in the given list. */
+static char *
+makeplugin_text_prev_in_list(const char *caller, unsigned int argc,
+                             char **argv)
+{
+  char *prev=NULL, *target=argv[0];
+  gal_list_str_t *tmp, *list=gal_list_str_extract(argv[1]);
+
+  /* Parse the input list. */
+  for(tmp=list; tmp!=NULL; tmp=tmp->next)
+    {
+      if( strcmp(tmp->v,target) ) prev=tmp->v; /* Not equal. */
+      else break;                              /* Equal.     */
+    }
+
+  /* Return the output. */
+  return prev;
+}
+
+
+
+
+
 
 
 
@@ -363,6 +387,10 @@ libgnuastro_make_gmk_setup()
   gmk_add_function(text_to_lower, makeplugin_text_to_lower,
                    1, 1, GMK_FUNC_DEFAULT);
 
+  /* Select previous item in list*/
+  gmk_add_function(text_prev_in_list, makeplugin_text_prev_in_list,
+                   2, 2, GMK_FUNC_DEFAULT);
+
 
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]