PIPS
f95split_file.c
Go to the documentation of this file.
1 /*
2 
3  $Id: f95split_file.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23  */
24 /*
25  * procedures used in both PIPS top-level, wpips and tpips.
26  *
27  * problems to use those procedures with wpips: show_message() and
28  * update_props() .
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "pips_config.h"
32 #endif
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 
44 #include "genC.h"
45 #include "linear.h"
46 #include "ri.h"
47 #include "ri-util.h"
48 #include "parser_private.h"
49 
50 #include "resources.h"
51 #include "database.h"
52 
53 #include "misc.h"
54 #include "pipsdbm.h"
55 
56 char * f95split( char * dir_name, char * file_name, FILE ** out ) {
57 
58  FILE *fd;
59 
60 
61  debug_on( "SYNTAX_DEBUG_LEVEL" );
62 
63  /*fprintf(stderr,
64  "\n-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"
65  "+ Starting gfc parser in PIPS. -\n"
66  "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"
67  );*/
68 
69  // string callees_filename = get_resource_file_name( DBR_CALLEES, module );
70  // Create if it doesn't exist
71  // close( open( callees_filename, O_CREAT, S_IRWXU ) );
72 
73 
74  /*
75  *
76  */
77 
78  // Create xxx.database/Program if it doesn't exist Yet
79  string program_dirname = get_resource_file_name( "", "" );
80  mkdir( program_dirname, S_IRWXU );
81  free( program_dirname );
82 
83  /*
84  * Dump entities
85  */
86  string entities_filename = get_resource_file_name( DBR_ENTITIES, "" );
87  fd = (FILE *) safe_fopen( (char *) entities_filename, "w" );
89  safe_fclose( fd, (char *) entities_filename );
90 
91  /*
92  * directory where module are precompiled
93  */
94  char *compiled_dir_name = get_resource_file_name( "", "/Precompiled" );
95 
96  // Check if source file exist
97  /* string source_filename =
98  strdup( concatenate( dir, "/", db_get_file_resource( DBR_SOURCE_FILE,
99  module,
100  true ), NULL ) );
101  */
102  // "char **argv" for gfc2pips :-)
103  char* gfc2pips_args[] = { "gfc2pips", "-Wall",// "-Werror",
104  // We give it where to output...
105  "-pips-entities",
106  entities_filename,
107  "-o","/dev/null",
108  //parsedcode_filename,
109  file_name,
110  // ... and where to read inputs
111  "-auxbase",
112  //source_filename,
113  dir_name,
114  /* dump_parse_tree is the gfc pass that have been
115  * hacked to call the gfc2pips stuff
116  */
117  "-fdump-parse-tree",
118  "-quiet",
119  "-I",compiled_dir_name,
120  /* we may have non-standard file extensions
121  * (e.g. .f_initial) and gfortran will not be able
122  * to know what it is so we force the language input
123  */
124  // "-x",
125  // "f95",
126  "-cpp",
127  // "-quiet",// "-Werror",
128  /* I don't know what the following stuff is ... */
129  /*"-fcray-pointer",*/
130  "-ffree-form",
131  //"-fdefault-double-8",
132  //"-fdefault-integer-8",
133  //"-fdefault-real-8",
134 
135  // Argv must be null terminated
136  NULL };
137 
138  // we will fork now in order to call GFC
139  int statut;
140  pid_t child_pid = fork( );
141 
142  // Now we have two process runing the same code
143  // We differentiate them with fork() return value
144 
145  if ( child_pid == -1 ) {
146  // Error
147  perror( "Fork" );
148  return false;
149  } else if ( child_pid == 0 ) {
150  // in the child
151 
152 
153  pips_debug( 2, "build module %s\n", file_name );
154 
155  // MAIN CALL TO GFC
156  char** arg = gfc2pips_args;
157  ifdebug(1) {
158  fprintf( stderr, "execvp : " );
159  while ( *arg ) {
160  fprintf( stderr, " %s", *arg );
161  arg++;
162  }
163  fprintf( stderr, "\n" );
164  }
165  execvp( "gfc2pips", gfc2pips_args );
166  // No return from exec
167  pips_user_error("gfc2pips is not installed, did you compile PIPS with"
168  " Fortran95 support ?\n");
169  exit( -1 );
170  } else {
171  // in the Father
172 
173  // Wait that gfc has done the job
174  if ( waitpid( child_pid, &statut, 0 ) == -1 ) {
175  // Error in waitpid
176  perror( "waitpid" );
177  return "Erreur in wait pid";
178  } else {
179  // Child has correctly finished
180 
181  // Check the gfc2pips return code
182  if ( statut != EXIT_SUCCESS ) {
183  fprintf(stderr,"error code %d\n",statut);
184  return "gfc2pips return an error";
185  }
186  }
187  }
188 
189  // If we are, everything was done properly :-)
190 
191 
192  // Reload entities
193 
194  // We have to close it and re-open since gfc2pips has written into it.
195  fclose( *out );
196 
197  //out will be close in caller
198  *out = safe_fopen( entities_filename, "r" );
199 
200  gen_read_tabulated( *out, 0 );
201 // safe_fclose( fp, entities_filename );
202 
203 
204 
205  /* This debug_off() occurs too late since pipsdbm has been called
206  * before. Initially, the parser was designed to parse more than
207  * one subroutine/function/program at a time. */
208  debug_off( );
209 
210 
211 
212  return NULL;
213 
214 }
215 
static FILE * out
Definition: alias_check.c:128
char * f95split(char *dir_name, char *file_name, FILE **out)
f95split_file.c
Definition: f95split_file.c:56
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
int gen_write_tabulated(FILE *fd, int domain)
GEN_WRITE_TABULATED writes the tabulated object TABLE on FD.
Definition: genClib.c:1866
int gen_read_tabulated(FILE *file, int create_p)
GEN_READ_TABULATED reads FILE to update the Gen_tabulated_ table.
Definition: genClib.c:2334
void free(void *)
list gfc2pips_args(gfc_namespace *ns)
Retrieve the list of names of every argument of the function, if any.
Definition: gfc2pips.c:847
#define EXIT_SUCCESS
NetBSD 5.0 mis-defines NULL.
Definition: stdlib.in.h:103
string get_resource_file_name(const char *rname, const char *oname)
allocate a full file name for the given resource.
Definition: lowlevel.c:187
#define debug_on(env)
Definition: misc-local.h:157
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define debug_off()
Definition: misc-local.h:160
#define exit(code)
Definition: misc-local.h:54
#define pips_user_error
Definition: misc-local.h:147
#define entity_domain
newgen_syntax_domain_defined
Definition: ri.h:410
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
#define ifdebug(n)
Definition: sg.c:47
static string file_name