xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-jit-tools.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33d3250a0SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53d3250a0SJeremy L Thompson //
6*3d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73d3250a0SJeremy L Thompson 
83d3250a0SJeremy L Thompson #include <ceed/ceed.h>
93d3250a0SJeremy L Thompson #include <ceed/backend.h>
103d3250a0SJeremy L Thompson #include <ceed/jit-tools.h>
113d3250a0SJeremy L Thompson #include <stdbool.h>
123d3250a0SJeremy L Thompson #include <stdio.h>
133d3250a0SJeremy L Thompson #include <string.h>
143d3250a0SJeremy L Thompson 
153d3250a0SJeremy L Thompson /**
163d3250a0SJeremy L Thompson   @brief Load source file into initalized string buffer, including full text
173d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`
183d3250a0SJeremy L Thompson 
193d3250a0SJeremy L Thompson   @param ceed                   A Ceed object for error handling
203d3250a0SJeremy L Thompson   @param[in]  source_file_path  Absolute path to source file
213d3250a0SJeremy L Thompson   @param[out] buffer            String buffer for source file contents
223d3250a0SJeremy L Thompson 
233d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
243d3250a0SJeremy L Thompson 
253d3250a0SJeremy L Thompson   @ref Backend
263d3250a0SJeremy L Thompson **/
273d3250a0SJeremy L Thompson static inline int CeedLoadSourceToInitalizedBuffer(Ceed ceed,
283d3250a0SJeremy L Thompson     const char *source_file_path, char **buffer) {
293d3250a0SJeremy L Thompson   int ierr;
303d3250a0SJeremy L Thompson   FILE *source_file;
313d3250a0SJeremy L Thompson   long file_size, file_offset = 0;
323d3250a0SJeremy L Thompson   char *temp_buffer;
333d3250a0SJeremy L Thompson 
343d3250a0SJeremy L Thompson   // Debug
353d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
363d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
373d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
383d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current buffer:\n");
393d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
403d3250a0SJeremy L Thompson 
413d3250a0SJeremy L Thompson   // Read file to temporary buffer
423d3250a0SJeremy L Thompson   source_file = fopen(source_file_path, "rb");
433d3250a0SJeremy L Thompson   if (!source_file)
443d3250a0SJeremy L Thompson     // LCOV_EXCL_START
453d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s",
463d3250a0SJeremy L Thompson                      source_file_path);
473d3250a0SJeremy L Thompson   // LCOV_EXCL_STOP
483d3250a0SJeremy L Thompson   // -- Compute size of source
493d3250a0SJeremy L Thompson   fseek(source_file, 0L, SEEK_END);
503d3250a0SJeremy L Thompson   file_size = ftell(source_file);
513d3250a0SJeremy L Thompson   rewind(source_file);
523d3250a0SJeremy L Thompson   //  -- Allocate memory for entire source file
533d3250a0SJeremy L Thompson   ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr);
543d3250a0SJeremy L Thompson   // -- Copy the file into the buffer
553d3250a0SJeremy L Thompson   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
563d3250a0SJeremy L Thompson     // LCOV_EXCL_START
573d3250a0SJeremy L Thompson     fclose(source_file);
583d3250a0SJeremy L Thompson     ierr = CeedFree(&temp_buffer); CeedChk(ierr);
593d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s",
603d3250a0SJeremy L Thompson                      source_file_path);
613d3250a0SJeremy L Thompson     // LCOV_EXCL_STOP
623d3250a0SJeremy L Thompson   }
633d3250a0SJeremy L Thompson   fclose(source_file);
643d3250a0SJeremy L Thompson 
653d3250a0SJeremy L Thompson   // Search for headers to include
663d3250a0SJeremy L Thompson   const char *first_hash = strchr(temp_buffer, '#');
673d3250a0SJeremy L Thompson   while (first_hash) {
683d3250a0SJeremy L Thompson     // -- Check for 'include' keyword
693d3250a0SJeremy L Thompson     const char *next_e = strchr(first_hash, 'e');
703d3250a0SJeremy L Thompson     char keyword[8] = "";
713d3250a0SJeremy L Thompson     if (next_e)
723d3250a0SJeremy L Thompson       strncpy(keyword, &next_e[-6], 7);
733d3250a0SJeremy L Thompson     bool is_hash_include = !strcmp(keyword, "include");
743d3250a0SJeremy L Thompson     // ---- Spaces allowed in '#  include <header.h>'
753d3250a0SJeremy L Thompson     if (next_e)
763d3250a0SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_e + i < -6; i++)
773d3250a0SJeremy L Thompson         is_hash_include &= first_hash[i] == ' ';
783d3250a0SJeremy L Thompson     if (is_hash_include) {
793d3250a0SJeremy L Thompson       // -- Copy into buffer all preceding #
803d3250a0SJeremy L Thompson       long current_size = strlen(*buffer);
813d3250a0SJeremy L Thompson       long copy_size = first_hash - &temp_buffer[file_offset];
823d3250a0SJeremy L Thompson       ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
833d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size], "\n", 2);
843d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
853d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + copy_size], "", 1);
863d3250a0SJeremy L Thompson       // -- Load local "header.h"
873d3250a0SJeremy L Thompson       char *next_quote = strchr(first_hash, '"');
883d3250a0SJeremy L Thompson       char *next_new_line = strchr(first_hash, '\n');
893d3250a0SJeremy L Thompson       bool is_local_header = is_hash_include && next_quote
903d3250a0SJeremy L Thompson                              && (next_new_line - next_quote > 0);
913d3250a0SJeremy L Thompson       if (is_local_header) {
923d3250a0SJeremy L Thompson         // ---- Build source path
933d3250a0SJeremy L Thompson         char *include_source_path;
943d3250a0SJeremy L Thompson         long root_length = strrchr(source_file_path, '/') - source_file_path;
953d3250a0SJeremy L Thompson         long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
963d3250a0SJeremy L Thompson         ierr = CeedCalloc(root_length + include_file_name_len + 2,
973d3250a0SJeremy L Thompson                           &include_source_path); CeedChk(ierr);
983d3250a0SJeremy L Thompson         strncpy(include_source_path, source_file_path, root_length + 1);
993d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + 1], &next_quote[1],
1003d3250a0SJeremy L Thompson                 include_file_name_len);
1013d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
1023d3250a0SJeremy L Thompson         // ---- Recursive call to load source to buffer
1033d3250a0SJeremy L Thompson         ierr = CeedLoadSourceToInitalizedBuffer(ceed, include_source_path, buffer);
1043d3250a0SJeremy L Thompson         CeedChk(ierr);
1053d3250a0SJeremy L Thompson         ierr = CeedFree(&include_source_path); CeedChk(ierr);
1063d3250a0SJeremy L Thompson       }
1073d3250a0SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
1083d3250a0SJeremy L Thompson     }
1093d3250a0SJeremy L Thompson     // -- Next hash
1103d3250a0SJeremy L Thompson     first_hash = strchr(&first_hash[1], '#');
1113d3250a0SJeremy L Thompson   }
1123d3250a0SJeremy L Thompson   // Copy rest of source file into buffer
1133d3250a0SJeremy L Thompson   long current_size = strlen(*buffer);
1143d3250a0SJeremy L Thompson   long copy_size = strlen(&temp_buffer[file_offset]);
1153d3250a0SJeremy L Thompson   ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
1163d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size], "\n", 2);
1173d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
1183d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + copy_size + 1], "", 1);
1193d3250a0SJeremy L Thompson 
1203d3250a0SJeremy L Thompson   // Cleanup
1213d3250a0SJeremy L Thompson   ierr = CeedFree(&temp_buffer); CeedChk(ierr);
1223d3250a0SJeremy L Thompson 
1233d3250a0SJeremy L Thompson   // Debug
1243d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
1253d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
1263d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
1273d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Final buffer:\n");
1283d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
1293d3250a0SJeremy L Thompson 
1303d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1313d3250a0SJeremy L Thompson }
1323d3250a0SJeremy L Thompson 
1333d3250a0SJeremy L Thompson /**
1343d3250a0SJeremy L Thompson   @brief Initalize and load source file into string buffer, including full text
1353d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`.
1363d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
1373d3250a0SJeremy L Thompson 
1383d3250a0SJeremy L Thompson   @param ceed                   A Ceed object for error handling
1393d3250a0SJeremy L Thompson   @param[in]  source_file_path  Absolute path to source file
1403d3250a0SJeremy L Thompson   @param[out] buffer            String buffer for source file contents
1413d3250a0SJeremy L Thompson 
1423d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1433d3250a0SJeremy L Thompson 
1443d3250a0SJeremy L Thompson   @ref Backend
1453d3250a0SJeremy L Thompson **/
1463d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path,
1473d3250a0SJeremy L Thompson                            char **buffer) {
1483d3250a0SJeremy L Thompson   int ierr;
1493d3250a0SJeremy L Thompson 
1503d3250a0SJeremy L Thompson   // Initalize buffer
1513d3250a0SJeremy L Thompson   ierr = CeedCalloc(1, buffer); CeedChk(ierr);
1523d3250a0SJeremy L Thompson 
1533d3250a0SJeremy L Thompson   // Load to initalized buffer
1543d3250a0SJeremy L Thompson   ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer);
1553d3250a0SJeremy L Thompson   CeedChk(ierr);
1563d3250a0SJeremy L Thompson 
1573d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1583d3250a0SJeremy L Thompson }
159437930d1SJeremy L Thompson 
160437930d1SJeremy L Thompson /**
161437930d1SJeremy L Thompson   @brief Build an absolute filepath from a base filepath and an absolute filepath.
162437930d1SJeremy L Thompson            This helps construct source file paths for `CeedLoadSourceToBuffer()`.
163437930d1SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
164437930d1SJeremy L Thompson 
165437930d1SJeremy L Thompson   @param ceed                     A Ceed object for error handling
166437930d1SJeremy L Thompson   @param[in]  base_file_path      Absolute path to current file
167437930d1SJeremy L Thompson   @param[in]  relative_file_path  Relative path to target file
168437930d1SJeremy L Thompson   @param[out] new_file_path       String buffer for absolute path to target file
169437930d1SJeremy L Thompson 
170437930d1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
171437930d1SJeremy L Thompson 
172437930d1SJeremy L Thompson   @ref Backend
173437930d1SJeremy L Thompson **/
174437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path,
175437930d1SJeremy L Thompson                         const char *relative_file_path, char **new_file_path) {
176437930d1SJeremy L Thompson   int ierr;
177437930d1SJeremy L Thompson   char *last_slash = strrchr(base_file_path, '/');
178437930d1SJeremy L Thompson   size_t base_length = (last_slash - base_file_path + 1),
179437930d1SJeremy L Thompson          relative_length = strlen(relative_file_path),
180437930d1SJeremy L Thompson          new_file_path_length = base_length + relative_length + 1;
181437930d1SJeremy L Thompson 
182437930d1SJeremy L Thompson   ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr);
183437930d1SJeremy L Thompson   memcpy(*new_file_path, base_file_path, base_length);
184437930d1SJeremy L Thompson   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
185437930d1SJeremy L Thompson 
186437930d1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
187437930d1SJeremy L Thompson }
188