Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Generate code for C lang
0003  * Using "tmnull_switch" when XA switch is not available for compatilibty..
0004  *
0005  * @file codegen_c.c
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2018, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL or Mavimax's license for commercial use.
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 <atmi.h>
0042 
0043 #include <ubf.h>
0044 #include <ferror.h>
0045 #include <fieldtable.h>
0046 #include <fdatatype.h>
0047 
0048 #include <ndrstandard.h>
0049 #include <ndebug.h>
0050 #include <errno.h>
0051 #include "buildtools.h"
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 /**
0061  * Generate C code for build server / build transaction manager
0062  * @param f file where to write the code (already open for w)
0063  * @param cfile - Auto generated buildeserver main code
0064  * @param thread_option - 
0065  * @param p_svcnm_lst - list of Srevice names
0066  * @param p_funcnm_lst - list of Function names
0067  * @param p_rmdef - resource manager definition
0068  * @param nomain - do not generate main if 1
0069  * @return EXSUCCEED/EXFAIL
0070  */
0071 expublic int ndrx_buildsrv_generate_code(FILE **f, char *cfile, int thread_option, 
0072                                         bs_svcnm_lst_t *p_svcnm_lst, 
0073                                         bs_svcnm_lst_t *p_funcnm_lst,
0074                                         ndrx_rm_def_t *p_rmdef, int nomain)
0075 {
0076     int ret = EXSUCCEED;
0077     bs_svcnm_lst_t *bs, *bst;
0078 
0079     NDRX_LOG(log_info, "C-Code to compile: [%s]", cfile);
0080     
0081     fprintf(*f, "/* Buildserver auto-generated code */\n");
0082     fprintf(*f, "/*---------------------------Includes-----------------------------------*/\n");
0083     fprintf(*f, "#include <stdio.h>\n");
0084     fprintf(*f, "#include <stdlib.h>\n");
0085     fprintf(*f, "#include <atmi.h>\n");
0086     fprintf(*f, "#include <ndrstandard.h>\n");
0087     fprintf(*f, "#include <string.h>\n");
0088     fprintf(*f, "#include <unistd.h>\n");
0089     fprintf(*f, "#include <xa.h>\n");
0090     
0091     fprintf(*f, "/*---------------------------Externs------------------------------------*/\n");
0092     fprintf(*f, "/* Buildserver auto generated extern service list */\n");
0093     
0094     /* Support #612: */
0095     fprintf(*f, "#ifdef __cplusplus\n");
0096     fprintf(*f, "    extern \"C\"\n");
0097     fprintf(*f, "{\n");
0098     fprintf(*f, "#endif\n");
0099 
0100     /* Generate definitions */
0101     if (EXEOS!=p_rmdef->structname[0])
0102     {
0103         fprintf(*f, "extern struct xa_switch_t %s;\n", p_rmdef->structname);
0104     }
0105     
0106     EXHASH_ITER(hh, p_funcnm_lst, bs, bst)
0107     {
0108         fprintf(*f, "extern void %s (TPSVCINFO *);\n", bs->funcnm);
0109     }
0110     
0111     fprintf(*f, "/*---------------------------Macros-------------------------------------*/\n");
0112     fprintf(*f, "/*---------------------------Enums--------------------------------------*/\n");
0113     fprintf(*f, "/*---------------------------Typedefs-----------------------------------*/\n");
0114     fprintf(*f, "/*---------------------------Globals------------------------------------*/\n");
0115     fprintf(*f, "/*---------------------------Statics------------------------------------*/\n");
0116     fprintf(*f, "/* Auto generated system advertise table */\n");
0117     fprintf(*f, "expublic struct tmdsptchtbl_t ndrx_G_tmdsptchtbl[] = {\n");
0118     
0119     EXHASH_ITER(hh, p_svcnm_lst, bs, bst)
0120     {
0121         fprintf(*f, "    {\"%s\",\"%s\",(void (*)(TPSVCINFO *)) %s, 0, 0 },\n", 
0122                                 bs->svcnm, bs->funcnm, bs->funcnm);
0123     }
0124         
0125     /* list empty functions */
0126     EXHASH_ITER(hh, p_funcnm_lst, bs, bst)
0127     {
0128         if (bs->funcreg)
0129         {
0130             fprintf(*f, "    {\"%s\",\"%s\",(void (*)(TPSVCINFO *)) %s, 0, 0 },\n", 
0131                                     bs->svcnm, bs->funcnm, bs->funcnm);
0132         }
0133     }
0134     
0135     fprintf(*f, "    { NULL, NULL, NULL, 0, 0 }\n");
0136     fprintf(*f, "};\n");
0137 
0138     fprintf(*f, "/*---------------------------Prototypes---------------------------------*/\n");
0139     fprintf(*f, "\n");
0140     
0141     /* if not no main requested... */
0142     if (!nomain)
0143     {
0144         fprintf(*f, "/* Buildserver main  */\n");
0145         fprintf(*f, "int main( int argc, char** argv )\n");
0146         fprintf(*f, "{\n");
0147         fprintf(*f, "    _tmbuilt_with_thread_option=%d;\n",thread_option);
0148         fprintf(*f, "    struct tmsvrargs_t tmsvrargs =\n");
0149         fprintf(*f, "    {\n");
0150         fprintf(*f, "        &%s,\n", (EXEOS!=p_rmdef->structname[0]?p_rmdef->structname:"tmnull_switch"));
0151         fprintf(*f, "        &ndrx_G_tmdsptchtbl[0],\n");
0152         fprintf(*f, "        0,\n");
0153         fprintf(*f, "        tpsvrinit,\n");
0154         fprintf(*f, "        tpsvrdone,\n");
0155         fprintf(*f, "        NULL,\n");
0156         fprintf(*f, "        NULL,\n");
0157         fprintf(*f, "        NULL,\n");
0158         fprintf(*f, "        NULL,\n");
0159         fprintf(*f, "        NULL,\n");
0160         fprintf(*f, "        tpsvrthrinit,\n");
0161         fprintf(*f, "        tpsvrthrdone\n");
0162         fprintf(*f, "    };\n");
0163         fprintf(*f, "#ifdef TMMAINEXIT\n");
0164         fprintf(*f, "#include \"mainexit.h\"\n");
0165         fprintf(*f, "#endif\n");
0166         fprintf(*f, "    return( _tmstartserver( argc, argv, &tmsvrargs ));\n");
0167         fprintf(*f, "}\n");
0168     }
0169     
0170     fprintf(*f, "#ifdef __cplusplus\n");
0171     fprintf(*f, "} // extern \"C\"\n");
0172     fprintf(*f, "#endif\n");
0173     
0174 out:
0175 
0176     if (NULL!=*f)
0177     {
0178         NDRX_FCLOSE(*f);
0179         *f = NULL;
0180     }
0181 
0182     return ret;
0183 }
0184 
0185 /**
0186  * Generate build client C code
0187  * @param f file open for write
0188  * @param cfile Auto generated buildclient main code
0189  * @param p_rmdef resource manager definition
0190  * @return EXSUCCEED/EXFAIL
0191  */
0192 expublic int ndrx_buildclt_generate_code(FILE **f, char *cfile, ndrx_rm_def_t *p_rmdef)
0193 {
0194     int ret = EXSUCCEED;
0195 
0196     fprintf(*f, "/* Buildclient auto-generated code */\n");
0197     fprintf(*f, "/*---------------------------Includes-----------------------------------*/\n");
0198     fprintf(*f, "#include <atmi.h>\n");
0199     fprintf(*f, "#include <xa.h>\n");
0200     
0201     /* Support #612: */
0202     fprintf(*f, "#ifdef __cplusplus\n");
0203     fprintf(*f, "    extern \"C\"\n");
0204     fprintf(*f, "{\n");
0205     fprintf(*f, "#endif\n");
0206     
0207     /* we shall export the "ndrx_G_tmsvrargs" in case if running on windows... */
0208     fprintf(*f, "#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n");
0209     fprintf(*f, "#define NDRX_API_EXPORT __declspec(dllexport)\n");
0210     fprintf(*f, "#else\n");
0211     fprintf(*f, "#define NDRX_API_EXPORT\n");
0212     fprintf(*f, "#endif\n");
0213 
0214     /* Generate definitions */
0215     if (EXEOS!=p_rmdef->structname[0])
0216     {
0217         fprintf(*f, "extern struct xa_switch_t %s;\n", p_rmdef->structname);
0218     }
0219     /* from libatmibld library */
0220     fprintf(*f, "extern struct xa_switch_t * ndrx_xa_builtin_get(void);\n");
0221 
0222     fprintf(*f, "expublic NDRX_API_EXPORT struct xa_switch_t *ndrx_G_p_xaswitch = &%s;\n",
0223                 (EXEOS!=p_rmdef->structname[0]?p_rmdef->structname:"tmnull_switch"));
0224     
0225     fprintf(*f, "struct xa_switch_t * ndrx_switch_pull(void)\n");
0226     fprintf(*f, "{\n");
0227     fprintf(*f, "    return ndrx_xa_builtin_get();\n");
0228     fprintf(*f, "}\n");
0229     
0230     
0231     fprintf(*f, "#ifdef __cplusplus\n");
0232     fprintf(*f, "} // extern \"C\"\n");
0233     fprintf(*f, "#endif\n");
0234     
0235 out:
0236 
0237     if (NULL!=*f)
0238     {
0239         NDRX_FCLOSE(*f);
0240         *f=NULL;
0241     }
0242 
0243     return ret;
0244 }
0245 
0246 /* vim: set ts=4 sw=4 et smartindent: */