Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Generate main() for the XATMI server
0003  *
0004  * @file buildserver.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 
0035 /*---------------------------Includes-----------------------------------*/
0036 
0037 #include <unistd.h>
0038 #include <stdio.h>
0039 #include <errno.h>
0040 #include <stdlib.h>
0041 #include <ndrx_config.h>
0042 #include <atmi.h>
0043 #include <atmi_int.h>
0044 #include <sys_unix.h>
0045 #include <ctype.h>
0046 #include <limits.h>
0047 
0048 #include <ubf.h>
0049 #include <ferror.h>
0050 #include <fieldtable.h>
0051 #include <fdatatype.h>
0052 
0053 #include <ndrstandard.h>
0054 #include <ndebug.h>
0055 #include "buildtools.h"
0056 
0057 /*---------------------------Externs------------------------------------*/
0058 /*---------------------------Macros-------------------------------------*/
0059 /*---------------------------Enums--------------------------------------*/
0060 /*---------------------------Typedefs-----------------------------------*/
0061 /*---------------------------Globals------------------------------------*/
0062 /*---------------------------Statics------------------------------------*/
0063 /**
0064  * print help for the command
0065  * @param name name of the program, argv[0]
0066  */
0067 exprivate void print_help(char *name)
0068 {
0069     fprintf(stderr, "Usage: %s [options] -o filename \n", name);
0070     fprintf(stderr, "Options:\n");
0071     fprintf(stderr, "  -n               Do not generate main() entry\n");
0072     fprintf(stderr, "  -o <filename>    Output compiled file name\n");
0073     fprintf(stderr, "  -r <rm_name>     Resource manager name to be searched in $NDRX_HOME/udataobj/RM.\n");
0074     fprintf(stderr, "                   If not set, null switch is used.\n");
0075     fprintf(stderr, "  -k               Keep generated source file\n");
0076     fprintf(stderr, "  -v               Verbose mode (print build command)\n");
0077     fprintf(stderr, "  -h               Print this help\n");   
0078 }
0079 
0080 /**
0081  * Main entry point for view compiler
0082  */
0083 int main(int argc, char **argv)
0084 {
0085     int ret = EXSUCCEED;
0086     int c;
0087     char ofile[NDRX_BPATH_MAX+1]="";
0088     char cfile[NDRX_BPATH_MAX+1]="ndrx_tms_XXXXXX.c";
0089     int keep_main=EXFALSE;
0090     char firstfiles[NDRX_BPATH_MAX+1] = {EXEOS};
0091     char lastfiles[NDRX_BPATH_MAX+1] = {EXEOS};
0092     int nomain = EXFALSE;
0093     int verbose = EXFALSE;
0094     FILE *out_fptr = NULL;
0095     ndrx_rm_def_t rmdef;
0096 
0097     NDRX_BANNER("BUILDTMS Tool");
0098     
0099     /* clear any error... */
0100     _Nunset_error();
0101     
0102     memset(&rmdef, 0, sizeof(rmdef));
0103 
0104     while ((c = getopt (argc, argv, "ho:r:vk")) != -1)
0105     {
0106         switch (c)
0107         {
0108             case 'h':
0109                 print_help(argv[0]);
0110                 return 0; /*<<<< RETURN ! */
0111                 break;
0112             case 'o':
0113                 NDRX_STRCPY_SAFE(ofile, optarg);
0114                 NDRX_LOG(log_debug, "ofile: [%s]", ofile);
0115                 break;
0116             case 'r':    
0117                 if ( EXFAIL == (ret=ndrx_get_rm_name(optarg, &rmdef)))
0118                 {
0119                     /* error is set */
0120                     NDRX_LOG(log_error, 
0121                          "Failed to parse resource manager: [%s], check -r", optarg);
0122                     EXFAIL_OUT(ret);
0123                 }
0124                 else if (EXTRUE!=ret)
0125                 {
0126                     NDRX_LOG(log_error, 
0127                          "Resource manager not defined: [%s], check -r", optarg);
0128                     /* set error */
0129                     _Nset_error_fmt(NEINVAL, "Resource manager [%s] not found "
0130                             "in udataobj/RM files", optarg);
0131                     EXFAIL_OUT(ret);
0132                 }
0133                 
0134                 ret = EXSUCCEED;
0135 
0136                 break;
0137             case 'k':
0138                 keep_main=EXTRUE;
0139                 break;
0140             case 'v':
0141                 NDRX_LOG(log_debug, "running in verbose mode");
0142                 verbose = EXTRUE;
0143                 break;
0144             case '?':
0145             default:
0146             
0147                 print_help(argv[0]);
0148                 return EXFAIL; /*<<<< RETURN ! */
0149         }
0150     }
0151     
0152     if (EXEOS==ofile[0])
0153     {
0154         print_help(argv[0]);
0155         return EXFAIL; /*<<<< RETURN ! */
0156     }
0157     
0158     if ( NULL==(out_fptr=ndrx_mkstemps(cfile, 2, 0) ))
0159     {
0160         int err = errno;
0161         NDRX_LOG(log_error, "Failed with error %s", strerror(err));
0162         _Nset_error_fmt(NEUNIX, "Failed to create temporary file: %s", 
0163                 strerror(err));
0164         EXFAIL_OUT(ret);
0165     }
0166 
0167     if (EXSUCCEED!=ndrx_buildsrv_generate_code(&out_fptr, cfile, EXFALSE, 
0168                                                NULL, 
0169                                                NULL,
0170                                                &rmdef, nomain))
0171     {
0172         NDRX_LOG(log_error, "Failed to generate code!");
0173         EXFAIL_OUT(ret);
0174     }
0175 
0176     if (EXSUCCEED!=ndrx_compile_c(COMPILE_TMS, verbose, cfile, ofile, 
0177             firstfiles, lastfiles, &rmdef))
0178     {
0179         NDRX_LOG(log_error, "Failed to build");
0180         EXFAIL_OUT(ret);
0181     }
0182     
0183 out:
0184 
0185     /* cleanup hash lists... */
0186     if (EXSUCCEED!=ret)
0187     {
0188         if (!_Nis_error())
0189         {
0190             _Nset_error_fmt(NESYSTEM, "Generic error - see logs.");
0191         }
0192         
0193         /* print error */
0194         fprintf(stderr, "%s: %s\n", argv[0], ndrx_Nstrerror2(Nerror));
0195     }
0196 
0197     if (NULL!=out_fptr)
0198     {
0199         NDRX_FCLOSE(out_fptr);
0200     }
0201 
0202     if (EXFALSE == keep_main)
0203     {
0204         unlink(cfile);
0205     }
0206 
0207 
0208     return ret;
0209 }
0210 
0211 /* vim: set ts=4 sw=4 et smartindent: */