Line data Source code
1 1 : /** 2 : * @file compiler.c 3 : * 4 : * @brief The ROOT-Sim compiler 5 : * 6 : * This is the ROOT-Sim compiler, a compiler wrapper which allows 7 : * to setup all necessary includes and configurations to run with 8 : * a parallel or distributed simulation. This is targeting low 9 : * level C models. 10 : * 11 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com> 12 : * SPDX-License-Identifier: GPL-3.0-only 13 : */ 14 : #include <stdlib.h> 15 : #include <stdio.h> 16 : #include <string.h> 17 : 18 : #ifndef ROOTSIM_OPTIMIZATION_OPTIONS 19 : /// The optimization options to be used when compiling models 20 : /** This macro is filled in at build time */ 21 1 : #define ROOTSIM_OPTIMIZATION_OPTIONS "" 22 : #endif 23 : 24 : #ifndef ROOTSIM_CC 25 : /// The path of the C compiler to use when compiling models 26 : /** This macro is filled in at build time */ 27 1 : #define ROOTSIM_CC "" 28 : #endif 29 : 30 : #ifndef ROOTSIM_LIB_DIR 31 : /// The path of the installed ROOT-Sim libraries 32 : /** This macro is filled in at build time */ 33 1 : #define ROOTSIM_LIB_DIR "" 34 : #endif 35 : 36 : #ifndef ROOTSIM_INC_DIR 37 : /// The path of the installed ROOT-Sim headers 38 : /** This macro is filled in at build time */ 39 1 : #define ROOTSIM_INC_DIR "" 40 : #endif 41 : 42 0 : static const char cmd_line_prefix[] = 43 : ROOTSIM_CC " " 44 : ROOTSIM_OPTIMIZATION_OPTIONS " " 45 : "-I" ROOTSIM_INC_DIR " " 46 : "-Xclang -load " 47 : "-Xclang " ROOTSIM_LIB_DIR "librootsim-llvm.so" 48 : ; 49 0 : 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 : 57 : /** 58 : * @brief The main entry point of the custom compiler 59 : * @param argc The count of command line arguments, as per ISO C standard 60 : * @param argv The list of command line arguments, as per ISO C standard 61 : */ 62 1 : 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 :