The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
compiler.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 #ifndef ROOTSIM_OPTIMIZATION_OPTIONS
19 
21 #define ROOTSIM_OPTIMIZATION_OPTIONS ""
22 #endif
23 
24 #ifndef ROOTSIM_CC
25 
27 #define ROOTSIM_CC ""
28 #endif
29 
30 #ifndef ROOTSIM_LIB_DIR
31 
33 #define ROOTSIM_LIB_DIR ""
34 #endif
35 
36 #ifndef ROOTSIM_INC_DIR
37 
39 #define ROOTSIM_INC_DIR ""
40 #endif
41 
42 static const char cmd_line_prefix[] =
43  ROOTSIM_CC " "
45  "-I" ROOTSIM_INC_DIR " "
46  "-Xclang -load "
47  "-Xclang " ROOTSIM_LIB_DIR "librootsim-llvm.so"
48 ;
49 static const char cmd_line_suffix[] =
50  " -Wl,--as-needed "
51  ROOTSIM_LIB_DIR "librootsim.a "
52  ROOTSIM_LIB_DIR "librootsim-mods.a "
53  "-lm "
54  "-lpthread"
55 ;
56 
62 int main(int argc, char **argv)
63 {
64  (void) argc;
65  ++argv;
66  size_t tot_size = sizeof(cmd_line_prefix) + sizeof(cmd_line_suffix) - 1;
67  char **argv_tmp = argv;
68  while (*argv_tmp) {
69  tot_size += strlen(*argv_tmp) + 1;
70  ++argv_tmp;
71  }
72 
73  char *cmd_line = malloc(tot_size);
74  if (cmd_line == NULL) {
75  fprintf(stderr, "Unable to allocate memory!");
76  return -1;
77  }
78 
79  char *ptr = cmd_line;
80  memcpy(ptr, cmd_line_prefix, sizeof(cmd_line_prefix) - 1);
81  ptr += sizeof(cmd_line_prefix) - 1;
82 
83  while (*argv) {
84  *ptr = ' ';
85  ++ptr;
86 
87  size_t l = strlen(*argv);
88  memcpy(ptr, *argv, l);
89  ptr += l;
90 
91  ++argv;
92  }
93 
94  memcpy(ptr, cmd_line_suffix, sizeof(cmd_line_suffix));
95 
96  if (system(cmd_line)) {
97  free(cmd_line);
98  fprintf(stderr, "Unable to run " ROOTSIM_CC "\n");
99  return -1;
100  }
101  free(cmd_line);
102  return 0;
103 }
104 
ROOTSIM_OPTIMIZATION_OPTIONS
#define ROOTSIM_OPTIMIZATION_OPTIONS
The optimization options to be used when compiling models.
Definition: compiler.c:21
main
int main(int argc, char **argv)
The main entry point of the custom compiler.
Definition: compiler.c:62
ROOTSIM_INC_DIR
#define ROOTSIM_INC_DIR
The path of the installed ROOT-Sim headers.
Definition: compiler.c:39
ROOTSIM_LIB_DIR
#define ROOTSIM_LIB_DIR
The path of the installed ROOT-Sim libraries.
Definition: compiler.c:33
ROOTSIM_CC
#define ROOTSIM_CC
The path of the C compiler to use when compiling models.
Definition: compiler.c:27