Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Build C code for server, client or TMS
0003  *   
0004  * @file compile_c.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 /*---------------------------Includes-----------------------------------*/
0035 
0036 #include <unistd.h>
0037 #include <stdio.h>
0038 #include <errno.h>
0039 #include <stdlib.h>
0040 #include <atmi.h>
0041 
0042 #include <ubf.h>
0043 #include <ferror.h>
0044 #include <fieldtable.h>
0045 #include <fdatatype.h>
0046 
0047 #include <ndrstandard.h>
0048 #include <ndebug.h>
0049 #include <errno.h>
0050 #include "buildtools.h"
0051 #include <typed_view.h>
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 
0059 /**
0060  * Invoke C compiler.
0061  * @param buildmode this is build mode. Either server,client or tms, see COMPILE_ consts
0062  * @param verbose if set, print build command
0063  * @param cfile generate file name to build (where the main lives)
0064  * @param 
0065  */
0066 expublic int ndrx_compile_c(int buildmode, int verbose, char *cfile, char *ofile, 
0067         char *firstfiles, char *lastfiles, ndrx_rm_def_t *p_rmdef)
0068 {
0069     int ret=EXSUCCEED;
0070     char *env_cc, *env_cflags, *env_ndrx_home;
0071     char build_cmd[NDRX_BPATH_MAX+1] = {EXEOS};
0072     char default_compiler[NDRX_BPATH_MAX+1] = "cc";
0073     char *build_final_cmd;
0074     char ndrx_lib[NDRX_BPATH_MAX+1]={EXEOS};
0075     char ndrx_inc[NDRX_BPATH_MAX+1]={EXEOS};
0076     char tmp[NDRX_BPATH_MAX+1];
0077     char *saveptr1;
0078     char *p;
0079     env_cc = getenv("CC");
0080     
0081     if (NULL!=env_cc && env_cc[0]!=0)
0082     {
0083         build_final_cmd = env_cc;
0084     }
0085     else
0086     {
0087         NDRX_LOG(log_debug, "Use default cc");
0088         build_final_cmd = default_compiler;
0089     }
0090     
0091     env_cflags = getenv("CFLAGS");
0092     
0093     env_ndrx_home = getenv(CONF_NDRX_HOME);
0094 
0095     if (NULL!=env_ndrx_home && env_ndrx_home[0]!=0)
0096     {
0097         snprintf(ndrx_inc, sizeof(ndrx_inc), "-I%s/include", env_ndrx_home);
0098         
0099 #ifdef SYS64BIT
0100         /* check the lib64... */
0101         snprintf(ndrx_lib, sizeof(ndrx_lib), "-L%s/lib64", env_ndrx_home);
0102         
0103         /* fix the chk path... */
0104         if (!ndrx_file_exists(ndrx_lib+2))
0105         {
0106             snprintf(ndrx_lib, sizeof(ndrx_lib), "-L%s/lib", env_ndrx_home);
0107         }
0108 #else
0109         snprintf(ndrx_lib, sizeof(ndrx_lib), "-L%s/lib", env_ndrx_home);
0110 #endif
0111     }
0112     
0113     NDRX_LOG(log_debug, "Build command set to: [%s]", build_final_cmd);
0114     
0115     NDRX_STRCPY_SAFE(build_cmd, build_final_cmd);
0116     
0117     if (NULL!=env_cflags)
0118     {
0119         NDRX_LOG(log_debug, "CFLAGS=[%s]", env_cflags);
0120         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0121         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), env_cflags);
0122     }
0123     
0124     /* add outfile */
0125     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -o ");
0126     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), ofile);
0127     
0128     /* add C file to build */
0129     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0130     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), cfile);
0131     
0132     if (EXEOS!=ndrx_inc[0])
0133     {
0134         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0135         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), ndrx_inc);
0136     }
0137     
0138     if (EXEOS!=ndrx_lib[0])
0139     {
0140         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0141         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), ndrx_lib);
0142     }
0143     
0144     if (EXEOS!=firstfiles[0])
0145     {
0146         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0147         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), firstfiles);
0148     }
0149     
0150     /* Add RM libraries if any... */
0151     if (NULL!=p_rmdef && EXEOS!=p_rmdef->libnames[0])
0152     {
0153         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0154         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), p_rmdef->libnames);
0155     }
0156     
0157     /* Add any Enduro/X libraries required... */
0158     if (COMPILE_SRV == buildmode)
0159     {   
0160         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -latmisrvinteg -latmi -lubf -lnstd");
0161     }
0162     else if (COMPILE_CLT == buildmode)
0163     {
0164         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -latmicltbld -latmi -lubf -lnstd");
0165     }
0166     else if (COMPILE_TMS == buildmode)
0167     {        
0168         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), 
0169                 " -ltms -latmisrvinteg -latmi -lubf -lnstd");
0170     }
0171     
0172     /* add any platform specific library */
0173     NDRX_STRCPY_SAFE(tmp, NDRX_RT_LIB);
0174     
0175     p=strtok_r(tmp, ";", &saveptr1);
0176     
0177     while (NULL!=p)
0178     {
0179         /* "Crun" is Sun Studio compiler specific, thus exclude as user
0180          * might use GNU...
0181          */
0182         if (0!=strcmp(p, "Crun"))
0183         {
0184             NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -l");
0185             NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), p);
0186         }
0187         
0188         p=strtok_r(NULL, ";", &saveptr1);
0189     }
0190 
0191 #ifdef EX_OS_AIX
0192     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -lbsd");
0193 #endif
0194     
0195     NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " -lm -lc -lpthread");
0196     
0197     /* Add last files. */
0198     if (EXEOS!=lastfiles[0])
0199     {
0200         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), " ");
0201         NDRX_STRCAT_S(build_cmd, sizeof(build_cmd), lastfiles);
0202     }
0203     
0204     NDRX_LOG(log_debug, "build_cmd: [%s]", build_cmd);
0205     
0206     if (verbose)
0207     {
0208         fprintf(stderr, "%s\n", build_cmd);
0209     }
0210     
0211     /* exec shell ... */
0212     if (EXSUCCEED!=(ret=system(build_cmd)))
0213     {
0214         _Nset_error_fmt(NEEXEC, "Failed to execute compiler [%s]: %d", 
0215                 build_cmd, ret);
0216         NDRX_LOG(log_error, "Failed to execute compiler [%s]: %d", 
0217                 build_cmd, ret);
0218         ret = EXFAIL;
0219     }
0220     
0221     return ret;
0222 }
0223 
0224 /* vim: set ts=4 sw=4 et smartindent: */