xref: /libCEED/interface/ceed-jit-tools.c (revision 437930d19388999b5cc2d76e2fe0d14f58fb41f3)
13d3250a0SJeremy L Thompson // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
23d3250a0SJeremy L Thompson // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
33d3250a0SJeremy L Thompson // reserved. See files LICENSE and NOTICE for details.
43d3250a0SJeremy L Thompson //
53d3250a0SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
63d3250a0SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
73d3250a0SJeremy L Thompson // element discretizations for exascale applications. For more information and
83d3250a0SJeremy L Thompson // source code availability see http://github.com/ceed.
93d3250a0SJeremy L Thompson //
103d3250a0SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
113d3250a0SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
123d3250a0SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
133d3250a0SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
143d3250a0SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
153d3250a0SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
163d3250a0SJeremy L Thompson 
173d3250a0SJeremy L Thompson #include <ceed/ceed.h>
183d3250a0SJeremy L Thompson #include <ceed/backend.h>
193d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
203d3250a0SJeremy L Thompson #include <stdbool.h>
213d3250a0SJeremy L Thompson #include <stdio.h>
223d3250a0SJeremy L Thompson #include <string.h>
233d3250a0SJeremy L Thompson 
243d3250a0SJeremy L Thompson /**
253d3250a0SJeremy L Thompson   @brief Load source file into initalized string buffer, including full text
263d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`
273d3250a0SJeremy L Thompson 
283d3250a0SJeremy L Thompson   @param ceed                   A Ceed object for error handling
293d3250a0SJeremy L Thompson   @param[in]  source_file_path  Absolute path to source file
303d3250a0SJeremy L Thompson   @param[out] buffer            String buffer for source file contents
313d3250a0SJeremy L Thompson 
323d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
333d3250a0SJeremy L Thompson 
343d3250a0SJeremy L Thompson   @ref Backend
353d3250a0SJeremy L Thompson **/
363d3250a0SJeremy L Thompson static inline int CeedLoadSourceToInitalizedBuffer(Ceed ceed,
373d3250a0SJeremy L Thompson     const char *source_file_path, char **buffer) {
383d3250a0SJeremy L Thompson   int ierr;
393d3250a0SJeremy L Thompson   FILE *source_file;
403d3250a0SJeremy L Thompson   long file_size, file_offset = 0;
413d3250a0SJeremy L Thompson   char *temp_buffer;
423d3250a0SJeremy L Thompson 
433d3250a0SJeremy L Thompson   // Debug
443d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
453d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
463d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
473d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current buffer:\n");
483d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
493d3250a0SJeremy L Thompson 
503d3250a0SJeremy L Thompson   // Read file to temporary buffer
513d3250a0SJeremy L Thompson   source_file = fopen(source_file_path, "rb");
523d3250a0SJeremy L Thompson   if (!source_file)
533d3250a0SJeremy L Thompson     // LCOV_EXCL_START
543d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s",
553d3250a0SJeremy L Thompson                      source_file_path);
563d3250a0SJeremy L Thompson   // LCOV_EXCL_STOP
573d3250a0SJeremy L Thompson   // -- Compute size of source
583d3250a0SJeremy L Thompson   fseek(source_file, 0L, SEEK_END);
593d3250a0SJeremy L Thompson   file_size = ftell(source_file);
603d3250a0SJeremy L Thompson   rewind(source_file);
613d3250a0SJeremy L Thompson   //  -- Allocate memory for entire source file
623d3250a0SJeremy L Thompson   ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr);
633d3250a0SJeremy L Thompson   // -- Copy the file into the buffer
643d3250a0SJeremy L Thompson   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
653d3250a0SJeremy L Thompson     // LCOV_EXCL_START
663d3250a0SJeremy L Thompson     fclose(source_file);
673d3250a0SJeremy L Thompson     ierr = CeedFree(&temp_buffer); CeedChk(ierr);
683d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s",
693d3250a0SJeremy L Thompson                      source_file_path);
703d3250a0SJeremy L Thompson     // LCOV_EXCL_STOP
713d3250a0SJeremy L Thompson   }
723d3250a0SJeremy L Thompson   fclose(source_file);
733d3250a0SJeremy L Thompson 
743d3250a0SJeremy L Thompson   // Search for headers to include
753d3250a0SJeremy L Thompson   const char *first_hash = strchr(temp_buffer, '#');
763d3250a0SJeremy L Thompson   while (first_hash) {
773d3250a0SJeremy L Thompson     // -- Check for 'include' keyword
783d3250a0SJeremy L Thompson     const char *next_e = strchr(first_hash, 'e');
793d3250a0SJeremy L Thompson     char keyword[8] = "";
803d3250a0SJeremy L Thompson     if (next_e)
813d3250a0SJeremy L Thompson       strncpy(keyword, &next_e[-6], 7);
823d3250a0SJeremy L Thompson     bool is_hash_include = !strcmp(keyword, "include");
833d3250a0SJeremy L Thompson     // ---- Spaces allowed in '#  include <header.h>'
843d3250a0SJeremy L Thompson     if (next_e)
853d3250a0SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_e + i < -6; i++)
863d3250a0SJeremy L Thompson         is_hash_include &= first_hash[i] == ' ';
873d3250a0SJeremy L Thompson     if (is_hash_include) {
883d3250a0SJeremy L Thompson       // -- Copy into buffer all preceding #
893d3250a0SJeremy L Thompson       long current_size = strlen(*buffer);
903d3250a0SJeremy L Thompson       long copy_size = first_hash - &temp_buffer[file_offset];
913d3250a0SJeremy L Thompson       ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
923d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size], "\n", 2);
933d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
943d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + copy_size], "", 1);
953d3250a0SJeremy L Thompson       // -- Load local "header.h"
963d3250a0SJeremy L Thompson       char *next_quote = strchr(first_hash, '"');
973d3250a0SJeremy L Thompson       char *next_new_line = strchr(first_hash, '\n');
983d3250a0SJeremy L Thompson       bool is_local_header = is_hash_include && next_quote
993d3250a0SJeremy L Thompson                              && (next_new_line - next_quote > 0);
1003d3250a0SJeremy L Thompson       if (is_local_header) {
1013d3250a0SJeremy L Thompson         // ---- Build source path
1023d3250a0SJeremy L Thompson         char *include_source_path;
1033d3250a0SJeremy L Thompson         long root_length = strrchr(source_file_path, '/') - source_file_path;
1043d3250a0SJeremy L Thompson         long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
1053d3250a0SJeremy L Thompson         ierr = CeedCalloc(root_length + include_file_name_len + 2,
1063d3250a0SJeremy L Thompson                           &include_source_path); CeedChk(ierr);
1073d3250a0SJeremy L Thompson         strncpy(include_source_path, source_file_path, root_length + 1);
1083d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + 1], &next_quote[1],
1093d3250a0SJeremy L Thompson                 include_file_name_len);
1103d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
1113d3250a0SJeremy L Thompson         // ---- Recursive call to load source to buffer
1123d3250a0SJeremy L Thompson         ierr = CeedLoadSourceToInitalizedBuffer(ceed, include_source_path, buffer);
1133d3250a0SJeremy L Thompson         CeedChk(ierr);
1143d3250a0SJeremy L Thompson         ierr = CeedFree(&include_source_path); CeedChk(ierr);
1153d3250a0SJeremy L Thompson       }
1163d3250a0SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
1173d3250a0SJeremy L Thompson     }
1183d3250a0SJeremy L Thompson     // -- Next hash
1193d3250a0SJeremy L Thompson     first_hash = strchr(&first_hash[1], '#');
1203d3250a0SJeremy L Thompson   }
1213d3250a0SJeremy L Thompson   // Copy rest of source file into buffer
1223d3250a0SJeremy L Thompson   long current_size = strlen(*buffer);
1233d3250a0SJeremy L Thompson   long copy_size = strlen(&temp_buffer[file_offset]);
1243d3250a0SJeremy L Thompson   ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
1253d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size], "\n", 2);
1263d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
1273d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + copy_size + 1], "", 1);
1283d3250a0SJeremy L Thompson 
1293d3250a0SJeremy L Thompson   // Cleanup
1303d3250a0SJeremy L Thompson   ierr = CeedFree(&temp_buffer); CeedChk(ierr);
1313d3250a0SJeremy L Thompson 
1323d3250a0SJeremy L Thompson   // Debug
1333d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
1343d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
1353d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
1363d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Final buffer:\n");
1373d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
1383d3250a0SJeremy L Thompson 
1393d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1403d3250a0SJeremy L Thompson }
1413d3250a0SJeremy L Thompson 
1423d3250a0SJeremy L Thompson /**
1433d3250a0SJeremy L Thompson   @brief Initalize and load source file into string buffer, including full text
1443d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`.
1453d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
1463d3250a0SJeremy L Thompson 
1473d3250a0SJeremy L Thompson   @param ceed                   A Ceed object for error handling
1483d3250a0SJeremy L Thompson   @param[in]  source_file_path  Absolute path to source file
1493d3250a0SJeremy L Thompson   @param[out] buffer            String buffer for source file contents
1503d3250a0SJeremy L Thompson 
1513d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1523d3250a0SJeremy L Thompson 
1533d3250a0SJeremy L Thompson   @ref Backend
1543d3250a0SJeremy L Thompson **/
1553d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path,
1563d3250a0SJeremy L Thompson                            char **buffer) {
1573d3250a0SJeremy L Thompson   int ierr;
1583d3250a0SJeremy L Thompson 
1593d3250a0SJeremy L Thompson   // Initalize buffer
1603d3250a0SJeremy L Thompson   ierr = CeedCalloc(1, buffer); CeedChk(ierr);
1613d3250a0SJeremy L Thompson 
1623d3250a0SJeremy L Thompson   // Load to initalized buffer
1633d3250a0SJeremy L Thompson   ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer);
1643d3250a0SJeremy L Thompson   CeedChk(ierr);
1653d3250a0SJeremy L Thompson 
1663d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1673d3250a0SJeremy L Thompson }
168*437930d1SJeremy L Thompson 
169*437930d1SJeremy L Thompson /**
170*437930d1SJeremy L Thompson   @brief Build an absolute filepath from a base filepath and an absolute filepath.
171*437930d1SJeremy L Thompson            This helps construct source file paths for `CeedLoadSourceToBuffer()`.
172*437930d1SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
173*437930d1SJeremy L Thompson 
174*437930d1SJeremy L Thompson   @param ceed                     A Ceed object for error handling
175*437930d1SJeremy L Thompson   @param[in]  base_file_path      Absolute path to current file
176*437930d1SJeremy L Thompson   @param[in]  relative_file_path  Relative path to target file
177*437930d1SJeremy L Thompson   @param[out] new_file_path       String buffer for absolute path to target file
178*437930d1SJeremy L Thompson 
179*437930d1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
180*437930d1SJeremy L Thompson 
181*437930d1SJeremy L Thompson   @ref Backend
182*437930d1SJeremy L Thompson **/
183*437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path,
184*437930d1SJeremy L Thompson                         const char *relative_file_path, char **new_file_path) {
185*437930d1SJeremy L Thompson   int ierr;
186*437930d1SJeremy L Thompson   char *last_slash = strrchr(base_file_path, '/');
187*437930d1SJeremy L Thompson   size_t base_length = (last_slash - base_file_path + 1),
188*437930d1SJeremy L Thompson          relative_length = strlen(relative_file_path),
189*437930d1SJeremy L Thompson          new_file_path_length = base_length + relative_length + 1;
190*437930d1SJeremy L Thompson 
191*437930d1SJeremy L Thompson   ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr);
192*437930d1SJeremy L Thompson   memcpy(*new_file_path, base_file_path, base_length);
193*437930d1SJeremy L Thompson   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
194*437930d1SJeremy L Thompson 
195*437930d1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
196*437930d1SJeremy L Thompson }
197