gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master c8b1563: BuildProgram: to easily compile and l


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c8b1563: BuildProgram: to easily compile and link with Gnuastro's library
Date: Mon, 1 May 2017 17:44:15 -0400 (EDT)

branch: master
commit c8b15639d9f9d22255a319e211934784e8335116
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    BuildProgram: to easily compile and link with Gnuastro's library
    
    Gnuastro's library contains many wrappers to other libraries (for reading
    FITS files for example) and managing all the linkings can be a big hurdle
    to making small programs to check an idea or a hunch. So I created
    BuildProgram within Gnuastro to greatly simply this job. It will use the
    linking information that was gathered during the creation of Gnuastro to
    build link with the user's program. In this way, the user doesn't have to
    worry about linking.
    
    BuildProgram also accepts the standard `-I', `-L', and `-l' compiler
    options so if the user wants to use other libraries, it is easily
    possible. This program is actually just a new implementation of the
    `astcompilerun' shell function that was introduced in the old "Automatic
    linking script" section of the manual (that section has now been
    removed).
    
    The main reason I chose to write a program for this job over a script is
    Argp and all the option management features (for example with configuration
    files) in Gnuastro's programs. It was not easy to implement all those
    features with a shell script for me. So I just made a copy of the
    `bin/TEMPLATE' directory and made this program instead. This also makes for
    a similar user experience for all programs that start with `ast'.
---
 Makefile.am                                        |  14 +-
 bin/TEMPLATE/Makefile.am                           |  10 +-
 bin/TEMPLATE/astTEMPLATE.conf                      |   4 +-
 bin/{TEMPLATE => buildprog}/Makefile.am            |  17 +-
 bin/buildprog/args.h                               | 118 +++++++
 .../astbuildprog.conf}                             |   4 +-
 bin/buildprog/authors-cite.h                       |  38 ++
 bin/buildprog/buildprog.c                          | 130 +++++++
 bin/buildprog/buildprog.h                          |  29 ++
 bin/buildprog/main.c                               |  57 +++
 bin/buildprog/main.h                               |  57 +++
 bin/buildprog/ui.c                                 | 383 +++++++++++++++++++++
 bin/buildprog/ui.h                                 |  57 +++
 configure.ac                                       |  20 +-
 doc/gnuastro.texi                                  | 295 +++++++++++-----
 lib/checkset.c                                     |   2 +-
 lib/gnuastro/dimension.h                           |   4 +-
 tests/Makefile.am                                  |  17 +-
 tests/buildprog/simpleio.c                         |  56 +++
 tests/buildprog/simpleio.sh                        |  54 +++
 20 files changed, 1249 insertions(+), 117 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 0533489..2c60fbd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,6 +50,9 @@ BUILT_SOURCES = $(top_srcdir)/AUTHORS
 if COND_ARITHMETIC
   MAYBE_ARITHMETIC = bin/arithmetic
 endif
+if COND_BUILDPROG
+  MAYBE_BUILDPROG = bin/buildprog
+endif
 if COND_CONVERTT
   MAYBE_CONVERTT = bin/convertt
 endif
@@ -104,11 +107,12 @@ endif
 ## conditions above). When `MAYBE_TEMPLATE' is not defined, then Make will
 ## see it as a blank string and igonore it, so there is no problem with
 ## having an uncommented `MAYBE_TEMPLATE' as a value in `SUBDIRS'.
-SUBDIRS = bootstrapped/lib $(MAYBE_GNULIBCHECK) lib $(MAYBE_ARITHMETIC) \
-  $(MAYBE_CONVERTT) $(MAYBE_CONVOLVE) $(MAYBE_COSMICCAL) $(MAYBE_CROP)  \
-  $(MAYBE_FITS) $(MAYBE_MKCATALOG) $(MAYBE_MKNOISE) $(MAYBE_MKPROF)     \
-  $(MAYBE_NOISECHISEL) $(MAYBE_STATISTICS) $(MAYBE_TABLE)               \
-  $(MAYBE_TEMPLATE) $(MAYBE_WARP) doc tests
+SUBDIRS = bootstrapped/lib $(MAYBE_GNULIBCHECK) lib $(MAYBE_ARITHMETIC)  \
+  $(MAYBE_BUILDPROG) $(MAYBE_CONVERTT) $(MAYBE_CONVOLVE)                 \
+  $(MAYBE_COSMICCAL) $(MAYBE_CROP) $(MAYBE_FITS) $(MAYBE_MKCATALOG)      \
+  $(MAYBE_MKNOISE) $(MAYBE_MKPROF) $(MAYBE_NOISECHISEL)                  \
+  $(MAYBE_STATISTICS) $(MAYBE_TABLE) $(MAYBE_TEMPLATE) $(MAYBE_WARP) doc \
+  tests
 
 
 
diff --git a/bin/TEMPLATE/Makefile.am b/bin/TEMPLATE/Makefile.am
index d0c16ad..f784618 100644
--- a/bin/TEMPLATE/Makefile.am
+++ b/bin/TEMPLATE/Makefile.am
@@ -26,16 +26,16 @@ AM_CPPFLAGS = -I\$(top_srcdir)/bootstrapped/lib
 
 
 ## Program definition (name, linking, sources and headers)
-bin_PROGRAMS = astnoisechisel
+bin_PROGRAMS = astTEMPLATE
 
-astnoisechisel_LDADD = -lgnuastro
+astTEMPLATE_LDADD = -lgnuastro
 
-astnoisechisel_SOURCES = main.c ui.c noisechisel.c
+astTEMPLATE_SOURCES = main.c ui.c TEMPLATE.c
 
-EXTRA_DIST = main.h authors-cite.h args.h ui.h noisechisel.h
+EXTRA_DIST = main.h authors-cite.h args.h ui.h TEMPLATE.h
 
 
 
 ## The configuration file (distribute and install).
 ## NOTE: the man page is created in doc/Makefile.am
-dist_sysconf_DATA = astnoisechisel.conf
+dist_sysconf_DATA = astTEMPLATE.conf
diff --git a/bin/TEMPLATE/astTEMPLATE.conf b/bin/TEMPLATE/astTEMPLATE.conf
index 65e02f5..82954e8 100644
--- a/bin/TEMPLATE/astTEMPLATE.conf
+++ b/bin/TEMPLATE/astTEMPLATE.conf
@@ -1,5 +1,5 @@
-# Default parameters (System) for NoiseChisel.
-# Table is part of GNU Astronomy Utitlies.
+# Default parameters (System) for TEMPLATE.
+# TEMPLATE is part of GNU Astronomy Utitlies.
 #
 # Use the long option name of each paramter followed by
 # a value. The name and value should be separated by
diff --git a/bin/TEMPLATE/Makefile.am b/bin/buildprog/Makefile.am
similarity index 68%
copy from bin/TEMPLATE/Makefile.am
copy to bin/buildprog/Makefile.am
index d0c16ad..e20c19f 100644
--- a/bin/TEMPLATE/Makefile.am
+++ b/bin/buildprog/Makefile.am
@@ -21,21 +21,26 @@
 
 ## Pre-processer flags (for Gnulib's headers). Recall that the compiled
 ## Gnulib library was statically linked to (copied in) Gnuastro's library.
-AM_CPPFLAGS = -I\$(top_srcdir)/bootstrapped/lib
+##
+## Buildprog will also need some system-specific information that is
+## gathered at compile time (for example the library installation directory
+## (LIBDIR) and the executive file suffix (EXEEXT).
+AM_CPPFLAGS = -I\$(top_srcdir)/bootstrapped/lib                   \
+              -DLIBDIR=\"$(libdir)\" -DEXEEXT=\"$(EXEEXT)\"
 
 
 
 ## Program definition (name, linking, sources and headers)
-bin_PROGRAMS = astnoisechisel
+bin_PROGRAMS = astbuildprog
 
-astnoisechisel_LDADD = -lgnuastro
+astbuildprog_LDADD = -lgnuastro
 
-astnoisechisel_SOURCES = main.c ui.c noisechisel.c
+astbuildprog_SOURCES = main.c ui.c buildprog.c
 
-EXTRA_DIST = main.h authors-cite.h args.h ui.h noisechisel.h
+EXTRA_DIST = main.h authors-cite.h args.h ui.h buildprog.h
 
 
 
 ## The configuration file (distribute and install).
 ## NOTE: the man page is created in doc/Makefile.am
-dist_sysconf_DATA = astnoisechisel.conf
+dist_sysconf_DATA = astbuildprog.conf
diff --git a/bin/buildprog/args.h b/bin/buildprog/args.h
new file mode 100644
index 0000000..be00c71
--- /dev/null
+++ b/bin/buildprog/args.h
@@ -0,0 +1,118 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef ARGS_H
+#define ARGS_H
+
+
+
+
+
+
+/* Array of acceptable options. */
+struct argp_option program_options[] =
+  {
+    {
+      "includedir",
+      UI_KEY_INCLUDE,
+      "STR",
+      0,
+      "Directories to search for `#include's.",
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->include,
+      GAL_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
+
+    {
+      "linkdir",
+      UI_KEY_LINKDIR,
+      "STR",
+      0,
+      "Directory to search for libraries to link.",
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->linkdir,
+      GAL_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
+
+    {
+      "linklib",
+      UI_KEY_LINKLIB,
+      "STR",
+      0,
+      "Link libraries, e.g., for libgsl: `-lgsl'.",
+      GAL_OPTIONS_GROUP_INPUT,
+      &p->linklib,
+      GAL_TYPE_STRLL,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
+
+
+    {
+      "onlybuild",
+      UI_KEY_ONLYBUILD,
+      0,
+      0,
+      "Don't run the built program.",
+      GAL_OPTIONS_GROUP_OUTPUT,
+      &p->onlybuild,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
+
+    {0}
+  };
+
+
+
+
+
+/* Define the child argp structure
+   -------------------------------
+
+   NOTE: these parts can be left untouched.*/
+struct argp
+gal_options_common_child = {gal_commonopts_options,
+                            gal_options_common_argp_parse,
+                            NULL, NULL, NULL, NULL, NULL};
+
+/* Use the child argp structure in list of children (only one for now). */
+struct argp_child
+children[]=
+{
+  {&gal_options_common_child, 0, NULL, 0},
+  {0, 0, 0, 0}
+};
+
+/* Set all the necessary argp parameters. */
+struct argp
+thisargp = {program_options, parse_opt, args_doc, doc, children, NULL, NULL};
+#endif
diff --git a/bin/TEMPLATE/astTEMPLATE.conf b/bin/buildprog/astbuildprog.conf
similarity index 86%
copy from bin/TEMPLATE/astTEMPLATE.conf
copy to bin/buildprog/astbuildprog.conf
index 65e02f5..2b0bddc 100644
--- a/bin/TEMPLATE/astTEMPLATE.conf
+++ b/bin/buildprog/astbuildprog.conf
@@ -1,5 +1,5 @@
-# Default parameters (System) for NoiseChisel.
-# Table is part of GNU Astronomy Utitlies.
+# Default parameters (System) for BuildProgram.
+# BuildProgram is part of GNU Astronomy Utitlies.
 #
 # Use the long option name of each paramter followed by
 # a value. The name and value should be separated by
diff --git a/bin/buildprog/authors-cite.h b/bin/buildprog/authors-cite.h
new file mode 100644
index 0000000..dc63077
--- /dev/null
+++ b/bin/buildprog/authors-cite.h
@@ -0,0 +1,38 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef AUTHORS_CITE_H
+#define AUTHORS_CITE_H
+
+/* When any specific citation is necessary, please add its BibTeX (from ADS
+   hopefully) to this variable along with a title decribing what this
+   paper/book does for the progarm in a short line. In the following line
+   put a row of `-' with the same length and then put the BibTeX.
+
+   See the `gnuastro_bibtex' variable in `lib/options' (from the top
+   Gnuastro source code directory) as an example.*/
+
+#define PROGRAM_BIBTEX "";
+
+#define PROGRAM_AUTHORS "Mohammad Akhlaghi";
+
+#endif
diff --git a/bin/buildprog/buildprog.c b/bin/buildprog/buildprog.c
new file mode 100644
index 0000000..407f726
--- /dev/null
+++ b/bin/buildprog/buildprog.c
@@ -0,0 +1,130 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gnuastro/list.h>
+
+#include <main.h>
+
+
+/* Write the given list into  */
+char *
+buildprog_as_one_string(char *opt, gal_list_str_t *list)
+{
+  char *out;
+  size_t len=0;
+  gal_list_str_t *tmp;
+
+  /* Only if we have a list. */
+  if(list)
+    {
+      /* For every node in the list, we want the `opt' and a space along with
+         the actual string. */
+      for(tmp=list; tmp!=NULL; tmp=tmp->next)
+        len += 1 + (opt ? strlen(opt) : 0) + strlen(tmp->v);
+
+      /* Allocate space for the string. */
+      out=gal_data_malloc_array(GAL_TYPE_UINT8, len+1);
+
+      /* Write all the strings into the allocated space. */
+      len=0;
+      for(tmp=list; tmp!=NULL; tmp=tmp->next)
+        len += sprintf(&out[len], "%s%s ", opt ? opt : "", tmp->v);
+    }
+  else out=NULL;
+
+  /* Return the final string. */
+  return out;
+}
+
+
+
+
+
+int
+buildprog(struct buildprogparams *p)
+{
+  /* Note that the first node of `sourceargs' is the acutal source and the
+     rest are arguments to be run later. */
+  int retval;
+  char *command;
+  char *include   = buildprog_as_one_string("-I", p->include);
+  char *linkdir   = buildprog_as_one_string("-L", p->linkdir);
+  char *linklib   = buildprog_as_one_string("-l", p->linklib);
+  char *arguments = buildprog_as_one_string(NULL, p->sourceargs->next);
+
+  /* If not in quiet mode, let the user know. */
+  if(!p->cp.quiet)
+    {
+      printf("\nCompiling and linking the program\n");
+      printf("---------------------------------\n");
+    }
+
+  /* Put the command to run into a string. */
+  asprintf(&command, "libtool %s --mode=link gcc %s %s %s %s "
+           "%s/libgnuastro.la -o %s", p->cp.quiet?"--quiet":"",
+           include?include:"", linkdir?linkdir:"", p->sourceargs->v,
+           linklib?linklib:"", LIBDIR, p->cp.output);
+
+  /* Compile (and link): */
+  retval=system(command);
+  if( retval==EXIT_SUCCESS && p->onlybuild==0)
+    {
+      /* Free the initial command. */
+      free(command);
+
+      /* Right the command to run the program. Note that if the output
+         value doesn't start with a directory, we'll have to put one for
+         it. */
+      switch(p->cp.output[0])
+        {
+        case '.':
+        case '/':
+          asprintf(&command, "%s %s", p->cp.output, arguments?arguments:"");
+          break;
+
+        default:
+          asprintf(&command, "./%s %s", p->cp.output, arguments?arguments:"");
+        }
+
+      /* Print the executed command if necessary, then run it. */
+      if(!p->cp.quiet)
+        {
+          printf("\nRun the compiled program\n");
+          printf("------------------------\n");
+          printf("%s\n", command);
+        }
+      retval=system(command);
+    }
+
+  /* Clean up and return. */
+  free(include);
+  free(linkdir);
+  free(linklib);
+  free(command);
+  return retval;
+}
diff --git a/bin/buildprog/buildprog.h b/bin/buildprog/buildprog.h
new file mode 100644
index 0000000..46b62e4
--- /dev/null
+++ b/bin/buildprog/buildprog.h
@@ -0,0 +1,29 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef BUILDPROG_H
+#define BUILDPROG_H
+
+int
+buildprog(struct buildprogparams *p);
+
+#endif
diff --git a/bin/buildprog/main.c b/bin/buildprog/main.c
new file mode 100644
index 0000000..0dcdedf
--- /dev/null
+++ b/bin/buildprog/main.c
@@ -0,0 +1,57 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <gnuastro-internal/timing.h>
+
+#include "main.h"
+
+#include "ui.h"
+#include "buildprog.h"
+
+
+/* Main function */
+int
+main (int argc, char *argv[])
+{
+  int retval;
+  struct buildprogparams p={{{0},0},0};
+
+  /* Set they starting time. */
+  time(&p.rawtime);
+
+  /* Read the input parameters. */
+  ui_read_check_inputs_setup(argc, argv, &p);
+
+  /* Run MakeProfiles */
+  retval=buildprog(&p);
+
+  /* Free all non-freed allocations. */
+  ui_free_report(&p);
+
+  /* Return successfully.*/
+  return retval;
+}
diff --git a/bin/buildprog/main.h b/bin/buildprog/main.h
new file mode 100644
index 0000000..73cf6f1
--- /dev/null
+++ b/bin/buildprog/main.h
@@ -0,0 +1,57 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef MAIN_H
+#define MAIN_H
+
+/* Include necessary headers */
+#include <gnuastro/data.h>
+
+#include <gnuastro-internal/options.h>
+
+/* Progarm names.  */
+#define PROGRAM_NAME   "Build"    /* Program full name.       */
+#define PROGRAM_EXEC   "astbuild" /* Program executable name. */
+#define PROGRAM_STRING PROGRAM_NAME" (" PACKAGE_NAME ") " PACKAGE_VERSION
+
+
+
+
+
+
+
+/* Main program parameters structure */
+struct buildprogparams
+{
+  /* From command-line */
+  struct gal_options_common_params     cp; /* Common parameters.        */
+  gal_list_str_t  *sourceargs;    /* Source files and arguments.        */
+  gal_list_str_t     *include;    /* Libraries to link against.         */
+  gal_list_str_t     *linkdir;    /* Libraries to link against.         */
+  gal_list_str_t     *linklib;    /* Libraries to link against.         */
+  uint8_t           onlybuild;    /* Don't run the compiled program.    */
+
+  /* Output: */
+  time_t              rawtime;  /* Starting time of the program.        */
+};
+
+#endif
diff --git a/bin/buildprog/ui.c b/bin/buildprog/ui.c
new file mode 100644
index 0000000..b949b27
--- /dev/null
+++ b/bin/buildprog/ui.c
@@ -0,0 +1,383 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#include <config.h>
+
+#include <argp.h>
+#include <errno.h>
+#include <error.h>
+#include <stdio.h>
+
+#include <gnuastro/fits.h>
+#include <gnuastro/linkedlist.h>
+
+#include <gnuastro-internal/timing.h>
+#include <gnuastro-internal/options.h>
+#include <gnuastro-internal/checkset.h>
+#include <gnuastro-internal/fixedstringmacros.h>
+
+#include "main.h"
+
+#include "ui.h"
+#include "authors-cite.h"
+
+
+
+
+
+/**************************************************************/
+/*********      Argp necessary global entities     ************/
+/**************************************************************/
+/* Definition parameters for the Argp: */
+const char *
+argp_program_version = PROGRAM_STRING "\n"
+                       GAL_STRINGS_COPYRIGHT
+                       "\n\nWritten/developed by "PROGRAM_AUTHORS;
+
+const char *
+argp_program_bug_address = PACKAGE_BUGREPORT;
+
+static char
+args_doc[] = "C-source [ARGUMENTS TO RUN]";
+
+const char
+doc[] = GAL_STRINGS_TOP_HELP_INFO PROGRAM_NAME" will compile and run a "
+  "C program, while automatically linking with libraries that Gnuastro "
+  "depends on. Hence you do not have to worry about explicitly linking "
+  "with CFITSIO for example if you want to work on a FITS file, or with "
+  "GSL if you want to use GNU Scientific Library's functions. You can "
+  "optionally use the compiler `-I' and `-L' options to use other "
+  "include headers or link with other libraries respectively.\n"
+  GAL_STRINGS_MORE_HELP_INFO
+  /* After the list of options: */
+  "\v"
+  PACKAGE_NAME" home page: "PACKAGE_URL;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/**************************************************************/
+/*********    Initialize & Parse command-line    **************/
+/**************************************************************/
+static void
+ui_initialize_options(struct buildprogparams *p,
+                      struct argp_option *program_options,
+                      struct argp_option *gal_commonopts_options)
+{
+  size_t i;
+  struct gal_options_common_params *cp=&p->cp;
+
+
+  /* Set the necessary common parameters structure. */
+  cp->poptions           = program_options;
+  cp->program_name       = PROGRAM_NAME;
+  cp->program_exec       = PROGRAM_EXEC;
+  cp->program_bibtex     = PROGRAM_BIBTEX;
+  cp->program_authors    = PROGRAM_AUTHORS;
+  cp->coptions           = gal_commonopts_options;
+
+
+  /* Modify common options. */
+  for(i=0; !gal_options_is_last(&cp->coptions[i]); ++i)
+    {
+      /* Select individually. */
+      switch(cp->coptions[i].key)
+        {
+        case GAL_OPTIONS_KEY_LOG:
+        case GAL_OPTIONS_KEY_HDU:
+        case GAL_OPTIONS_KEY_TYPE:
+        case GAL_OPTIONS_KEY_SEARCHIN:
+        case GAL_OPTIONS_KEY_MINMAPSIZE:
+        case GAL_OPTIONS_KEY_TABLEFORMAT:
+          cp->coptions[i].flags=OPTION_HIDDEN;
+          cp->coptions[i].mandatory=GAL_OPTIONS_NOT_MANDATORY;
+          break;
+
+        /* `--ignorecase's default short format is `I', but here we want to
+           follow the compiler format, hence we need `I' for
+           `include'. Therefore, here, we'll change the key for `include'
+           to some large number just to avoid confusion.*/
+        case GAL_OPTIONS_KEY_IGNORECASE:
+          cp->coptions[i].key=20000;
+          cp->coptions[i].flags=OPTION_HIDDEN;
+          cp->coptions[i].mandatory=GAL_OPTIONS_NOT_MANDATORY;
+        }
+
+
+
+      /* Select by group. */
+      switch(cp->coptions[i].group)
+        {
+        case GAL_OPTIONS_GROUP_TESSELLATION:
+          cp->coptions[i].doc=NULL; /* Necessary to remove title. */
+          cp->coptions[i].flags=OPTION_HIDDEN;
+          break;
+        }
+    }
+}
+
+
+
+
+
+/* Parse a single option: */
+error_t
+parse_opt(int key, char *arg, struct argp_state *state)
+{
+  struct buildprogparams *p = state->input;
+
+  /* Pass `gal_options_common_params' into the child parser.  */
+  state->child_inputs[0] = &p->cp;
+
+  /* In case the user incorrectly uses the equal sign (for example
+     with a short format or with space in the long format, then `arg`
+     start with (if the short version was called) or be (if the long
+     version was called with a space) the equal sign. So, here we
+     check if the first character of arg is the equal sign, then the
+     user is warned and the program is stopped: */
+  if(arg && arg[0]=='=')
+    argp_error(state, "incorrect use of the equal sign (`=`). For short "
+               "options, `=` should not be used and for long options, "
+               "there should be no space between the option, equal sign "
+               "and value");
+
+  /* Set the key to this option. */
+  switch(key)
+    {
+
+    /* Read the non-option tokens (arguments): */
+    case ARGP_KEY_ARG:
+      gal_list_str_add(&p->sourceargs, arg, 0);
+      break;
+
+
+    /* This is an option, set its value. */
+    default:
+      return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
+    }
+
+  return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/**************************************************************/
+/***************       Sanity Check         *******************/
+/**************************************************************/
+/* Read and check ONLY the options. When arguments are involved, do the
+   check in `ui_check_options_and_arguments'.
+static void
+ui_read_check_only_options(struct buildprogparams *p)
+{
+
+}
+*/
+
+
+
+
+
+/* Check the options and arguments. */
+static void
+ui_check_options_and_arguments(struct buildprogparams *p)
+{
+  if(p->sourceargs==NULL)
+    error(EXIT_FAILURE, 0, "no input (C source file) given");
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/**************************************************************/
+/***************       Preparations         *******************/
+/**************************************************************/
+
+void
+ui_preparations(struct buildprogparams *p)
+{
+  /* Reverse the sourceargs list (note that the options are reversed in
+     options.c). */
+  gal_list_str_reverse(&p->sourceargs);
+
+  /* Set the final output name. `EXEEXT' comes from the configuration
+     script (given by BuildProgram's `Makefile.am'). */
+  if(p->cp.output==NULL)
+    p->cp.output=gal_checkset_automatic_output(&p->cp, p->sourceargs->v,
+                                               EXEEXT);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/**************************************************************/
+/************         Set the parameters          *************/
+/**************************************************************/
+
+void
+ui_read_check_inputs_setup(int argc, char *argv[], struct buildprogparams *p)
+{
+  struct gal_options_common_params *cp=&p->cp;
+
+
+  /* Include the parameters necessary for argp from this program (`args.h')
+     and for the common options to all Gnuastro (`commonopts.h'). We want
+     to directly put the pointers to the fields in `p' and `cp', so we are
+     simply including the header here to not have to use long macros in
+     those headers which make them hard to read and modify. This also helps
+     in having a clean environment: everything in those headers is only
+     available within the scope of this function. */
+#include <gnuastro-internal/commonopts.h>
+#include "args.h"
+
+
+  /* Initialize the options and necessary information.  */
+  ui_initialize_options(p, program_options, gal_commonopts_options);
+
+
+  /* Read the command-line options and arguments. */
+  errno=0;
+  if(argp_parse(&thisargp, argc, argv, 0, 0, p))
+    error(EXIT_FAILURE, errno, "parsing arguments");
+
+
+  /* Read the configuration files and set the common values. */
+  gal_options_read_config_set(&p->cp);
+
+
+  /* Read the options into the program's structure, and check them and
+     their relations prior to printing.
+  ui_read_check_only_options(p);
+  */
+
+  /* Print the option values if asked. Note that this needs to be done
+     after the option checks so un-sane values are not printed in the
+     output state. */
+  gal_options_print_state(&p->cp);
+
+
+  /* Check that the options and arguments fit well with each other. Note
+     that arguments don't go in a configuration file. So this test should
+     be done after (possibly) printing the option values. */
+  ui_check_options_and_arguments(p);
+
+
+  /* Read/allocate all the necessary starting arrays. */
+  ui_preparations(p);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/**************************************************************/
+/************      Free allocated, report         *************/
+/**************************************************************/
+void
+ui_free_report(struct buildprogparams *p)
+{
+  /* Free the allocated arrays: */
+  free(p->cp.hdu);
+  free(p->cp.output);
+  gal_list_str_free(p->include,    1);
+  gal_list_str_free(p->linkdir,    1);
+  gal_list_str_free(p->linklib,    1);
+  gal_list_str_free(p->sourceargs, 0);
+}
diff --git a/bin/buildprog/ui.h b/bin/buildprog/ui.h
new file mode 100644
index 0000000..326fda6
--- /dev/null
+++ b/bin/buildprog/ui.h
@@ -0,0 +1,57 @@
+/*********************************************************************
+BuildProgram: Compile and run programs using Gnuastro's library
+BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#ifndef UI_H
+#define UI_H
+
+
+
+
+
+/* Available letters for short options:
+
+   a c d e f g i j k l n p r s t u v w x y z
+   A B C E G H J Q R W X Y
+*/
+enum option_keys_enum
+{
+  /* With short-option version. */
+  UI_KEY_INCLUDE     = 'I',
+  UI_KEY_LINKDIR     = 'L',
+  UI_KEY_LINKLIB     = 'l',
+  UI_KEY_ONLYBUILD   = 'b',
+
+  /* Only with long version (start with a value 1000, the rest will be set
+     automatically). */
+};
+
+
+
+
+
+void
+ui_read_check_inputs_setup(int argc, char *argv[], struct buildprogparams *p);
+
+void
+ui_free_report(struct buildprogparams *p);
+
+#endif
diff --git a/configure.ac b/configure.ac
index a6211ed..f42d7f2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -477,6 +477,12 @@ AC_ARG_ENABLE([arithmetic],
              [AS_IF([test "x$enable_arithmetic" != xno],
                      [enable_arithmetic=yes; ayes=true])],
               [enable_arithmetic=notset])
+AC_ARG_ENABLE([buildprog],
+              [AS_HELP_STRING([--enable-buildprog],
+                    [Install BuildProgram and other enabled programs.])],
+             [AS_IF([test "x$enable_buildprog" != xno],
+                     [enable_buildprog=yes; ayes=true])],
+              [enable_buildprog=notset])
 AC_ARG_ENABLE([convertt],
               [AS_HELP_STRING([--enable-convertt],
                     [Install ConvertType and other enabled programs.])],
@@ -570,12 +576,14 @@ AC_ARG_ENABLE([warp],
 #    every "notset"  becomes yes.       fi
 # fi
 AS_IF([test $ayes = true ],
-      [AS_IF([test $enable_convertt = notset], [enable_convertt=no])
+      [
+       AS_IF([test $enable_arithmetic = notset], [enable_arithmetic=no])
+       AS_IF([test $enable_buildprog = notset], [enable_buildprog=no])
+       AS_IF([test $enable_convertt = notset], [enable_convertt=no])
        AS_IF([test $enable_convolve = notset], [enable_convolve=no])
        AS_IF([test $enable_cosmiccal = notset], [enable_cosmiccal=no])
        AS_IF([test $enable_crop = notset], [enable_crop=no])
        AS_IF([test $enable_fits = notset], [enable_fits=no])
-       AS_IF([test $enable_arithmetic = notset], [enable_arithmetic=no])
        AS_IF([test $enable_mkcatalog = notset], [enable_mkcatalog=no])
        AS_IF([test $enable_mknoise = notset], [enable_mknoise=no])
        AS_IF([test $enable_mkprof = notset], [enable_mkprof=no])
@@ -586,12 +594,14 @@ AS_IF([test $ayes = true ],
        AS_IF([test $enable_warp = notset], [enable_warp=no])
        ],
 
-      [AS_IF([test $enable_convertt = notset], [enable_convertt=yes])
+      [
+       AS_IF([test $enable_arithmetic = notset], [enable_arithmetic=yes])
+       AS_IF([test $enable_buildprog = notset], [enable_buildprog=yes])
+       AS_IF([test $enable_convertt = notset], [enable_convertt=yes])
        AS_IF([test $enable_convolve = notset], [enable_convolve=yes])
        AS_IF([test $enable_cosmiccal = notset], [enable_cosmiccal=yes])
        AS_IF([test $enable_crop = notset], [enable_crop=yes])
        AS_IF([test $enable_fits = notset], [enable_fits=yes])
-       AS_IF([test $enable_arithmetic = notset], [enable_arithmetic=yes])
        AS_IF([test $enable_mkcatalog = notset], [enable_mkcatalog=yes])
        AS_IF([test $enable_mknoise = notset], [enable_mknoise=yes])
        AS_IF([test $enable_mkprof = notset], [enable_mkprof=yes])
@@ -609,6 +619,7 @@ AS_IF([test $ayes = true ],
 
 # Make the enable_package values available for the Makefile:
 AM_CONDITIONAL([COND_ARITHMETIC], [test $enable_arithmetic = yes])
+AM_CONDITIONAL([COND_BUILDPROG], [test $enable_buildprog = yes])
 AM_CONDITIONAL([COND_CONVERTT], [test $enable_convertt = yes])
 AM_CONDITIONAL([COND_CONVOLVE], [test $enable_convolve = yes])
 AM_CONDITIONAL([COND_COSMICCAL], [test $enable_cosmiccal = yes])
@@ -642,6 +653,7 @@ AC_CONFIG_FILES([Makefile
                  bin/mknoise/Makefile
                  bin/convertt/Makefile
                  bin/convolve/Makefile
+                 bin/buildprog/Makefile
                  bin/cosmiccal/Makefile
                 bin/mkcatalog/Makefile
                  bin/arithmetic/Makefile
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 595b605..c3ff0f4 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -50,6 +50,9 @@ License''.
 
 * bug-gnuastro: (gnuastro)Report a bug. How to report bugs
 
+* BuildProgram: (gnuastro)BuildProgram. Compile and run programs using 
Gnuastro's library.
+* astcompilerun: (gnuastro)Invoking astcompilerun. Options to BuildProgram.
+
 * ConvertType: (gnuastro)ConvertType. Convert different file types.
 * astconvertt: (gnuastro)Invoking astconvertt. Options to ConvertType.
 
@@ -515,15 +518,19 @@ CosmicCalculator
 Library
 
 * Review of library fundamentals::  Guide on libraries and linking.
+* BuildProgram::                Link and run source files using Gnuastro's 
library.
 * Gnuastro library::            Description of all library functions.
-* Library demo programs::
+* Library demo programs::       Demonstration for using libraries.
 
 Review of library fundamentals
 
 * Headers::                     Header files included in source.
 * Linking::                     Linking the compiled source files into one.
 * Summary and example on libraries::  A summary and example on using libraries.
-* Automatic linking script::    Shell function for automatic linking.
+
+BuildProgram
+
+* Invoking astcompilerun::      Options and examples for using this program.
 
 Gnuastro library
 
@@ -531,6 +538,7 @@ Gnuastro library
 * Library data types::          Definitions and functions for types in 
Gnuastro.
 * Library blank values::        Blank values and functions to deal with them.
 * Library data container::      General data container in Gnuastro.
+* Dimensions::                  Dealing with coordinates and dimensions.
 * Linked lists::                Various types of linked lists.
 * FITS files::                  Working with FITS data.
 * Text files::                  Functions to work on Text files.
@@ -4477,6 +4485,10 @@ the column names as case insensitive, which is strongly 
recommended here
 also but is not enforced. This option is only relevant for programs that
 take table columns as input.
 
+This option is not relevant to @ref{BuildProgram}, hence in that program the
+short option @option{-I} is used for include directories, not to ignore
+case.
+
 @item -o STR
 @itemx --output=STR
 The name of the output file or directory. With this option the automatic
@@ -15872,11 +15884,12 @@ program} to easily define your own programs(s).
 
 @menu
 * Review of library fundamentals::  Guide on libraries and linking.
+* BuildProgram::                Link and run source files using Gnuastro's 
library.
 * Gnuastro library::            Description of all library functions.
-* Library demo programs::
+* Library demo programs::       Demonstration for using libraries.
 @end menu
 
address@hidden Review of library fundamentals, Gnuastro library, Library, 
Library
address@hidden Review of library fundamentals, BuildProgram, Library, Library
 @section Review of library fundamentals
 
 Gnuastro's libraries are written in the C programming language. In @ref{Why
@@ -15911,7 +15924,6 @@ problems.
 * Headers::                     Header files included in source.
 * Linking::                     Linking the compiled source files into one.
 * Summary and example on libraries::  A summary and example on using libraries.
-* Automatic linking script::    Shell function for automatic linking.
 @end menu
 
 @node Headers, Linking, Review of library fundamentals, Review of library 
fundamentals
@@ -16337,7 +16349,7 @@ the linker will complain and abort.
 
 
 
address@hidden Summary and example on libraries, Automatic linking script, 
Linking, Review of library fundamentals
address@hidden Summary and example on libraries,  , Linking, Review of library 
fundamentals
 @subsection Summary and example on libraries
 
 After the mostly abstract discussions of @ref{Headers} and @ref{Linking},
@@ -16420,89 +16432,130 @@ multiple CPU threads, you also need to add these 
libraries in the call to
 GCC: @option{-lgnuastro -lwcs -lcfitsio -lgsl -lgslcblas -pthread -lm}. In
 @ref{Gnuastro library}, where each function is documented, it is mentioned
 which libraries (if any) must also be linked when you call a function. If
-you feel all these linkings can be confusing, please have a look at
address@hidden linking script}.
+you feel all these linkings can be confusing, please consider Gnuastro's
address@hidden program.
 
 
address@hidden Automatic linking script,  , Summary and example on libraries, 
Review of library fundamentals
address@hidden Automatic linking script
address@hidden BuildProgram, Gnuastro library, Review of library fundamentals, 
Library
address@hidden BuildProgram
 The number and order of libraries that are necessary for linking a program
 with Gnuastro library might be too confusing when you need to compile a
-small program for one particular job (with one source file). To solve this
-problem, please add the following lines to your @file{~/.bashrc} with a
-text editor and replace @code{PREFIX} with Gnuastro's installation
-directory (see @ref{Installation directory}, if you haven't changed it the
-installation directory explicity at installation, put @file{/usr/local} in
-place of @code{PREFIX}). Also copy the comments to help readability later.
-
address@hidden
-# Program-like function to compile and run (once) programs that use
-# the Gnuastro library. The general usage pattern is:
-#
-#     $ astcompilerun SOURCE  [ARGUMENTS]  [OTHER-LIBRARIES]
-#
-#
-#   SOURCE: the name of the `.c' source file without the `.c', for
-#      example, for `myprog.c', SOURCE would be `myprog'.
-#
-#   ARGUMENTS: [optional] any arguments to run the program with after
-#      compilation. If there are multiple arguments, put them in
-#      double quotes, for example "img1.fits img2.fits".
-#
-#   OTHER-LIBRARIES: [optional] if your program depends on any
-#      libraries other than Gnuastro's dependencies. For example
-#      if it needs libfirst and libsecond, use "-lfirst -lsecond".
-#
-# When there are no arguments and no libraries other than Gnuastro's
-# dependencies, you can simply run this command (when your source is
-# in `myprog.c'
-#
-#     $ astcompilerun myprog
-#
-function astcompilerun @{
-  echo "---- compilation command ----"
-  if libtool --mode=link gcc $1.c $3 PREFIX/lib/libgnuastro.la -o $1
-    then
-      echo "-----------------------------"
-      echo " \"$1\" created in this directory and will be run below."
-      echo " For later runs in this directory, run: \"\$ ./$1 $2\""
-      echo "-----------------------------"
-      ./$1 $2
-  fi
address@hidden
address@hidden example
+small program for one particular job (with one source file). BuildProgram
+will use the information gathered during configuring Gnuastro and link with
+all the appropriate libraries on your system. This will allow you to easily
+compile, link and run programs that use Gnuastro's library with one simple
+command and not worry about which libraries to link to, or the linking
+order.
 
 @cindex GNU Libtool
-This tiny shell function uses GNU Libtool to find the necessary libraries
-to link against. Libtool uses the same libraries that were checked when you
-configured Gnuastro to link your program. So in the future, if Gnuastro's
-prerequisite libraries change, you don't have to worry. Afterwards, when
-you want to compile a C program that uses Gnuastro library (for example
address@hidden), just run the following command from the directory which
-contains the source file:
+BuildProgram uses GNU Libtool to find the necessary libraries to link
+against (GNU Libtool is the same program that builds all of Gnuastro's
+libraries and programs when you run @code{make}). So in the future, if
+Gnuastro's prerequisite libraries change or other libraries are added, you
+don't have to worry, you can just run BuildProgram and internal linking
+will be done correctly.
 
address@hidden
-$ astcompilerun myprog
address@hidden example
address@hidden
+* Invoking astcompilerun::      Options and examples for using this program.
address@hidden menu
 
address@hidden
-If your program needs to link with libraries other than those needed by
-Gnuastro, you can specify them as a second argument (within double quotes):
address@hidden Invoking astcompilerun,  , BuildProgram, BuildProgram
address@hidden Invoking BuildProgram
+
+BuildProgram will compile and link a C source program with Gnuastro's
+library and all its dependencies, greatly facilitating the compilation and
+running of small programs that use Gnuastro's library. The executable name
+is @file{astbuildprog} with the following general template:
 
 @example
-$ gnuastro-gcc myprog "-lfirst -lsecond"
+$ astbuildprog [OPTION...] C_SOURCE_FILE
 @end example
 
+
 @noindent
-If your program can take arguments, then you can also include them
-afterwards.
+One line examples:
 
 @example
-$ gnuastro-gcc myprog && ./myprog
+$ astbuildprog myprogram.c image.fits
+$ astbuildprog -Lother -Iother/dir myprogram.c
+$ astbuildprog myprogram.c --onlybuild image.fits catalog.txt
 @end example
 
+If BuildProgram is to run, it needs a C programming language source file as
+input. By default it will compile and link the program to build the a final
+executable file and run it. The built executable name can be set with the
+optional @option{--output} option. When no output name is set, BuildProgram
+will use Gnuastro's @ref{Automatic output}, and remove the suffix of the
+input and use that as the output name. For the full list of options that
+BuildProgram shares with other Gnuastro programs, see @ref{Common
+options}. You may also use Gnuastro's @ref{Configuration files} to specify
+other libraries/headers to use for special directories and not have to type
+them in every time.
+
+The first argument is considered to be the C source file that must be
+compiled and linked. Any other arguments (non-option tokens on the
+command-line) will be passed onto the program when BuildProgram wants to
+run it. Recall that by default BuildProgram will run the program after
+building it. This behavior can be disabled with the @code{--onlybuild}
+option.
+
address@hidden GNU Make
+When the @option{--quiet} option (see @ref{Operating mode options}) is not
+called, BuildPrograms will print the compilation and running commands. Once
+your program grows and you break it up into multiple files (which are much
+more easily managed with Make), you can use the linking flags of the
+non-quiet output in your @code{Makefile}.
+
address@hidden @option
address@hidden -I STR
address@hidden --includedir=STR
address@hidden GNU CPP
address@hidden C Pre-Processor
+Directory to search for files that you @code{#include} in your C program.
+Note that headers relating to Gnuastro and its dependencies don't need this
+option. This is only necessary if you want to use other headers. It may be
+called multiple times and order matters. This directory will be searched
+before those of Gnuastro's build and also the system search
+directories. See @ref{Headers} for a thorough introduction.
+
+From the GNU C Pre-Processor manual: ``Add the directory @code{STR} to the
+list of directories to be searched for header files. Directories named by
address@hidden are searched before the standard system include directories.
+If the directory @code{STR} is a standard system include directory, the
+option is ignored to ensure that the default search order for system
+directories and the special treatment of system headers are not defeated''.
+
address@hidden -L STR
address@hidden --linkdir=STR
address@hidden GNU Libtool
+Directory to search for compiled libraries to link the program with. Note
+that all the directories that Gnuastro was built with will already be used
+by BuildProgram (GNU Libtool). This option is only necessary if your
+libraries are in other directories. Multiple calls to this option are
+possible and order matters. This directory will be searched before those of
+Gnuastro's build and also the system search directories. See @ref{Linking}
+for a thorough introduction.
+
address@hidden -l STR
address@hidden --linklib=STR
+Library to link with your program. Note that all the libraries that
+Gnuastro was built with will already be linked by BuildProgram (GNU
+Libtool). This option is only necessary if you want to link with other
+directories. Multiple calls to this option are possible and order
+matters. This library will be linked before Gnuastro's library or its
+dependencies. See @ref{Linking} for a thorough introduction.
+
address@hidden -b
address@hidden --onlybuild
+Only build the program, don't run it. By default, the built program is
+immediately run afterwards.
address@hidden table
+
 
address@hidden Gnuastro library, Library demo programs, Review of library 
fundamentals, Library
+
+
+
address@hidden Gnuastro library, Library demo programs, BuildProgram, Library
 @section Gnuastro library
 
 Gnuastro library's programming constructs (function declarations, macros,
@@ -16533,9 +16586,9 @@ library which depends on lower level libraries for some 
operations (see
 your program use functions from the dependencies, you will also need to
 link those dependencies after linking with Gnuastro. The outside libraries
 that need to be linked for such functions are mentioned following the
-function name. See @ref{Automatic linking script} for a small shell
-function to worry about the libraries to link against and let you focus on
-your exciting science.
+function name. See @ref{BuildProgram} for a small Gnuastro program that will
+take care of the libraries to link against and lets you focus on your
+exciting science.
 
 
 
@@ -16558,6 +16611,7 @@ documentation will correspond to your installed version.
 * Library data types::          Definitions and functions for types in 
Gnuastro.
 * Library blank values::        Blank values and functions to deal with them.
 * Library data container::      General data container in Gnuastro.
+* Dimensions::                  Dealing with coordinates and dimensions.
 * Linked lists::                Various types of linked lists.
 * FITS files::                  Working with FITS data.
 * Text files::                  Functions to work on Text files.
@@ -17095,7 +17149,7 @@ return it. The space for the string is dynamically 
allocated so it must be
 freed after you are done with it.
 @end deftypefun
 
address@hidden Library data container, Linked lists, Library blank values, 
Gnuastro library
address@hidden Library data container, Dimensions, Library blank values, 
Gnuastro library
 @subsection Data container (@file{data.h})
 
 Astronomical datasets have various dimensions, for example 1D spectra or
@@ -17642,7 +17696,75 @@ single-element dataset.
 @end deftypefun
 
 
address@hidden Linked lists, FITS files, Library data container, Gnuastro 
library
address@hidden Dimensions, Linked lists, Library data container, Gnuastro 
library
address@hidden Dimensions (@file{dimension.h})
+
+An array is a contiguous region of memory.  Hence, at the lowest level,
+every element of an array just has one single-valued position: the number
+of elements that lie between it and the first element in the array. This is
+also known as the @emph{index} of the element within the
+array. Dimensionality is high-level abstraction (meta-data) that we project
+onto that contiguous patch of memory. When the array is interpreted as a
+one-dimensional dataset, this index is also the @emph{coordinate} of the
+element. But once we associate the patch of memory with a higher
+dimensionality, there must also be one coordinate for each dimension.
+
+The functions and macros in this section provide you with the tools to
+convert an index into a coordinate and vice-versa along with several other
+issues for example issues with the neighbors of an element in a
+multi-dimensional context.
+
address@hidden {Function-like macro} GAL_DIMENSION_NEIGHBOR_OP (@code{index}, 
@code{ndim}, @code{dsize}, @code{connectivity}, @code{dinc}, @code{operation})
+Parse the neighbors of the element located at @code{index} and do the
+requested operation on them. This is defined as a macro to allow easy
+definition of any operation on the neighbors of a given element without
+having to use loops within your source code (the loops are implemented by
+this macro). The input arguments to this function-like macro are described
+below:
+
address@hidden @code
address@hidden index
+Distance of this element from the first element in the array on a
+contiguous patch of memory (starting from 0), see the discussion above.
address@hidden ndim
+The number of dimensions associated with the contiugous patch of memory.
address@hidden dsize
+The full array size along each dimension. This must be an array and is
+assumed to have the same number elements as @code{ndim}. See the discussion
+under the same element in @ref{Generic data container}.
address@hidden connectivity
+Most distant neighbors to consider. Depending on the number of dimensions,
+different neighbros may be defined for each element. This function-like
+macro distinguish between these different neighbors with this argument. It
+has a value between @code{1} (one) and @code{ndim}. For example in a 2D
+dataset, 4-connected neighbors have a connectivity of @code{1} and
+8-connected neighbors have a connectivity of @code{2}. Note that this is
+inclusive, so in this example, a connectivity of @code{2} will also include
+connectivity @code{1} neighbors.
address@hidden dinc
+An array keeping the length necessary to increment along each
+dimension. You can make this array with the following function. Just don't
+forget to free the array after you are done with it:
+
address@hidden
+size_t *dinc=gal_dimension_increment(ndim, dsize);
address@hidden example
+
address@hidden depends on @code{ndim} and @code{dsize}, but it must be defined
+outside this function-like macro since it involves allocation to help in
+preformance.
+
address@hidden operation
+Any C operation that you would like to do on the neighbor. This macro will
+provide you a @code{nind} variable that can be used as the index of the
+neighbor that is currently being studied. It is defined as address@hidden
+ndim;}'. Note that @code{operation} will be repeated the number of times
+there is a neighbor for this element.
+
address@hidden table
address@hidden deffn
+
address@hidden Linked lists, FITS files, Dimensions, Gnuastro library
 @subsection Linked lists (@file{list.h})
 
 @cindex Array
@@ -19887,10 +20009,10 @@ arcsec-squared.
 In this final section of @ref{Library}, we give some example Gnuastro
 programs to demonstrate various features in the library. All these programs
 have been tested and once Gnuastro is installed you can compile and run
-them with with the program-like shell function that is described in
address@hidden linking script}. If you don't have any FITS file to
-experiment on, you can use those that are generated by Gnuastro after
address@hidden check} in the @file{tests/} directory, see @ref{Quick start}.
+them with with Gnuastro's @ref{BuildProgram} program that will take care of
+linking issues. If you don't have any FITS file to experiment on, you can
+use those that are generated by Gnuastro after @command{make check} in the
address@hidden/} directory, see @ref{Quick start}.
 
 
 @menu
@@ -19903,9 +20025,9 @@ experiment on, you can use those that are generated by 
Gnuastro after
 The following simple program demonstrates how to read a FITS image into
 memory and use the @code{void *array} pointer in of @ref{Generic data
 container}. For easy linking/compilation of this program along with a first
-run see @ref{Automatic linking script}. Before running, also change the
address@hidden and @code{hdu} variable values to specify an existing FITS
-file and/or extension/HDU.
+run see @ref{BuildProgram}. Before running, also change the @code{filename}
+and @code{hdu} variable values to specify an existing FITS file and/or
+extension/HDU.
 
 This is just intended to demonstrate how to use the @code{array} pointer of
 @code{gal_data_t}. Hence it doesn't do important sanity checks, for example
@@ -21499,6 +21621,13 @@ operators (for example @command{+}, @command{-}, 
@command{*}, @command{/},
 @command{sqrt}, @command{log}, @command{min}, @command{average},
 @command{median}).
 
address@hidden BuildProgram
+(@file{astbuildprog}, see @ref{BuildProgram}) Compile, link and run
+programs that depend on the Gnuastro library (see @ref{Gnuastro
+library}). This program will automatically link with the libraries that
+Gnuastro depends on, so there is no need to explicily mention them every
+time you are compiling a Gnuastro library dependent program.
+
 @item ConvertType
 (@file{astconvertt}, see @ref{ConvertType}) Convert astronomical data files
 (FITS or IMH) to and from several other standard image and data formats,
diff --git a/lib/checkset.c b/lib/checkset.c
index ffb1981..7a8ac5d 100644
--- a/lib/checkset.c
+++ b/lib/checkset.c
@@ -324,7 +324,7 @@ gal_checkset_automatic_output(struct 
gal_options_common_params *cp,
 
   /* If there is actually a suffix, replace it with the (possibly) existing
      suffix. */
-  if(suffix[0]!='\0')
+  if(suffix)
     {
       /* Start from the end of the input array*/
       l=strlen(inname);
diff --git a/lib/gnuastro/dimension.h b/lib/gnuastro/dimension.h
index 61d6cd9..9d71d68 100644
--- a/lib/gnuastro/dimension.h
+++ b/lib/gnuastro/dimension.h
@@ -163,9 +163,7 @@ gal_dimension_dist_manhattan(size_t *a, size_t *b, size_t 
ndim);
                 that is defined by this macro and will have the index of
                 each neighbor. You can use this `nind' for any processing
                 that you like on the neighbor. Note that `op' will be
-                repeated the number of times there is a neighbor. If you
-                want to stop parsing the neighbors, set the `stop' variable
-                to 1.
+                repeated the number of times there is a neighbor.
 
 
    Implementation
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3d7db21..8718ef2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -57,6 +57,11 @@ if COND_ARITHMETIC
   arithmetic/where.sh: noisechisel/noisechisel.sh.log
   arithmetic/or.sh: noisechisel/noisechisel.sh.log
 endif
+if COND_BUILDPROG
+  MAYBE_BUILDPROG_TESTS = buildprog/simpleio.sh
+
+  buildprog/simpleio.sh: mkprof/mosaic1.sh.log
+endif
 if COND_CONVERTT
   MAYBE_CONVERTT_TESTS = convertt/fitstotxt.sh convertt/fitstojpeg.sh  \
   convertt/blankch.sh convertt/jpegtotxt.sh convertt/fitstojpegcmyk.sh \
@@ -206,12 +211,12 @@ lib/multithread.sh: mkprof/mosaic1.sh.log
 
 # Final Tests
 # ===========
-TESTS = prepconf.sh lib/versioncpp.sh lib/multithread.sh                \
-  $(MAYBE_ARITHMETIC_TESTS) $(MAYBE_CONVERTT_TESTS)                     \
-  $(MAYBE_CONVOLVE_TESTS) $(MAYBE_COSMICCAL_TESTS) $(MAYBE_CROP_TESTS)  \
-  $(MAYBE_FITS_TESTS) $(MAYBE_MKCATALOG_TESTS) $(MAYBE_MKNOISE_TESTS)   \
-  $(MAYBE_MKPROF_TESTS) $(MAYBE_NOISECHISEL_TESTS)                      \
-  $(MAYBE_STATISTICS_TESTS) $(MAYBE_SUBTRACTSKY_TESTS)                  \
+TESTS = prepconf.sh lib/versioncpp.sh lib/multithread.sh                   \
+  $(MAYBE_ARITHMETIC_TESTS) $(MAYBE_BUILDPROG_TESTS)                       \
+  $(MAYBE_CONVERTT_TESTS) $(MAYBE_CONVOLVE_TESTS) $(MAYBE_COSMICCAL_TESTS) \
+  $(MAYBE_CROP_TESTS) $(MAYBE_FITS_TESTS) $(MAYBE_MKCATALOG_TESTS)         \
+  $(MAYBE_MKNOISE_TESTS) $(MAYBE_MKPROF_TESTS) $(MAYBE_NOISECHISEL_TESTS)  \
+  $(MAYBE_STATISTICS_TESTS) $(MAYBE_SUBTRACTSKY_TESTS)                     \
   $(MAYBE_TABLE_TESTS) $(MAYBE_WARP_TESTS)
 
 
diff --git a/tests/buildprog/simpleio.c b/tests/buildprog/simpleio.c
new file mode 100644
index 0000000..7e889d1
--- /dev/null
+++ b/tests/buildprog/simpleio.c
@@ -0,0 +1,56 @@
+/*********************************************************************
+A test program to multithreaded building using Gnuastro's helpers.
+
+Original author:
+     Mohammad Akhlaghi <address@hidden>
+Contributing author(s):
+Copyright (C) 2017, Free Software Foundation, Inc.
+
+Gnuastro is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+Gnuastro is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
+**********************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "gnuastro/fits.h"
+
+int
+main(int argc, char *argv[])
+{
+  gal_data_t *image;
+  char *outname="simpleio.fits";
+
+  /* We need two arguments, note that the system also gives the executable
+     name, so argc is one more than the number of arguments. */
+  if(argc!=3)
+    {
+      fprintf(stderr, "this program only accepts two arguments");
+      return EXIT_FAILURE;
+    }
+
+  /* Read the image into memory as a float32 data type. */
+  image=gal_fits_img_read(argv[1], argv[2], -1);
+
+  /* Let the user know. */
+  printf("%s (hdu %s) is read into memory.\n", argv[1], argv[2]);
+
+  /* Save the image in memory into another file. */
+  gal_fits_img_write(image, outname, NULL, "BuildProgram's Simpleio");
+
+  /* Let the user know. */
+  printf("%s created.\n", outname);
+
+  /* Clean up and return. */
+  gal_data_free(image);
+  return EXIT_SUCCESS;
+}
diff --git a/tests/buildprog/simpleio.sh b/tests/buildprog/simpleio.sh
new file mode 100755
index 0000000..da4c7b1
--- /dev/null
+++ b/tests/buildprog/simpleio.sh
@@ -0,0 +1,54 @@
+# Make labeled regions on an image with blank pixels.
+#
+# See the Tests subsection of the manual for a complete explanation
+# (in the Installing gnuastro section).
+#
+# Original author:
+#     Mohammad Akhlaghi <address@hidden>
+# Contributing author(s):
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.  This file is offered as-is,
+# without any warranty.
+
+
+
+
+
+# Preliminaries
+# =============
+#
+# Set the variabels (The executable is in the build tree). Do the
+# basic checks to see if the executable is made or if the defaults
+# file exists (basicchecks.sh is in the source tree).
+img=psf.fits
+prog=buildprog
+execname=../bin/$prog/ast$prog
+source=$topsrc/tests/$prog/simpleio.c
+
+
+
+
+
+# Skip?
+# =====
+#
+# If the dependencies of the test don't exist, then skip it. There are two
+# types of dependencies:
+#
+#   - The executable was not made (for example due to a configure option),
+#
+#   - The input data was not made (for example the test that created the
+#     data file failed).
+if [ ! -f $img      ]; then echo "$img does not exist";       exit 77; fi
+if [ ! -f $source   ]; then echo "$source does not exist";    exit 77; fi
+if [ ! -f $execname ]; then echo "$execname does not exist."; exit 77; fi
+
+
+
+
+
+# Actual test script
+# ==================
+$execname $source $img 1



reply via email to

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