xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-jit-tools.c (revision 032e71eaa1e750e7a66514b4e3c9cd7b57405aab)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33d3250a0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53d3250a0SJeremy L Thompson //
63d8e8822SJeremy 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>
116eb0d8b4SJeremy L Thompson #include <ceed-impl.h>
123d3250a0SJeremy L Thompson #include <stdbool.h>
133d3250a0SJeremy L Thompson #include <stdio.h>
143d3250a0SJeremy L Thompson #include <string.h>
153d3250a0SJeremy L Thompson 
163d3250a0SJeremy L Thompson /**
173d3250a0SJeremy L Thompson   @brief Load source file into initalized string buffer, including full text
183d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`
193d3250a0SJeremy L Thompson 
203d3250a0SJeremy L Thompson   @param ceed                  A Ceed object for error handling
213d3250a0SJeremy L Thompson   @param[in]  source_file_path Absolute path to source file
223d3250a0SJeremy L Thompson   @param[out] buffer           String buffer for source file contents
233d3250a0SJeremy L Thompson 
243d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
253d3250a0SJeremy L Thompson 
263d3250a0SJeremy L Thompson   @ref Backend
273d3250a0SJeremy L Thompson **/
283d3250a0SJeremy L Thompson static inline int CeedLoadSourceToInitalizedBuffer(Ceed ceed,
293d3250a0SJeremy L Thompson     const char *source_file_path, char **buffer) {
303d3250a0SJeremy L Thompson   int ierr;
313d3250a0SJeremy L Thompson   FILE *source_file;
323d3250a0SJeremy L Thompson   long file_size, file_offset = 0;
333d3250a0SJeremy L Thompson   char *temp_buffer;
343d3250a0SJeremy L Thompson 
353d3250a0SJeremy L Thompson   // Debug
363d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
373d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
383d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
393d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current buffer:\n");
403d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
413d3250a0SJeremy L Thompson 
423d3250a0SJeremy L Thompson   // Read file to temporary buffer
433d3250a0SJeremy L Thompson   source_file = fopen(source_file_path, "rb");
443d3250a0SJeremy L Thompson   if (!source_file)
453d3250a0SJeremy L Thompson     // LCOV_EXCL_START
463d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s",
473d3250a0SJeremy L Thompson                      source_file_path);
483d3250a0SJeremy L Thompson   // LCOV_EXCL_STOP
493d3250a0SJeremy L Thompson   // -- Compute size of source
503d3250a0SJeremy L Thompson   fseek(source_file, 0L, SEEK_END);
513d3250a0SJeremy L Thompson   file_size = ftell(source_file);
523d3250a0SJeremy L Thompson   rewind(source_file);
533d3250a0SJeremy L Thompson   //  -- Allocate memory for entire source file
543d3250a0SJeremy L Thompson   ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr);
553d3250a0SJeremy L Thompson   // -- Copy the file into the buffer
563d3250a0SJeremy L Thompson   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
573d3250a0SJeremy L Thompson     // LCOV_EXCL_START
583d3250a0SJeremy L Thompson     fclose(source_file);
593d3250a0SJeremy L Thompson     ierr = CeedFree(&temp_buffer); CeedChk(ierr);
603d3250a0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s",
613d3250a0SJeremy L Thompson                      source_file_path);
623d3250a0SJeremy L Thompson     // LCOV_EXCL_STOP
633d3250a0SJeremy L Thompson   }
643d3250a0SJeremy L Thompson   fclose(source_file);
653d3250a0SJeremy L Thompson 
663d3250a0SJeremy L Thompson   // Search for headers to include
673d3250a0SJeremy L Thompson   const char *first_hash = strchr(temp_buffer, '#');
683d3250a0SJeremy L Thompson   while (first_hash) {
693d3250a0SJeremy L Thompson     // -- Check for 'include' keyword
703d3250a0SJeremy L Thompson     const char *next_e = strchr(first_hash, 'e');
713d3250a0SJeremy L Thompson     char keyword[8] = "";
723d3250a0SJeremy L Thompson     if (next_e)
733d3250a0SJeremy L Thompson       strncpy(keyword, &next_e[-6], 7);
743d3250a0SJeremy L Thompson     bool is_hash_include = !strcmp(keyword, "include");
753d3250a0SJeremy L Thompson     // ---- Spaces allowed in '#  include <header.h>'
763d3250a0SJeremy L Thompson     if (next_e)
773d3250a0SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_e + i < -6; i++)
783d3250a0SJeremy L Thompson         is_hash_include &= first_hash[i] == ' ';
793d3250a0SJeremy L Thompson     if (is_hash_include) {
803d3250a0SJeremy L Thompson       // -- Copy into buffer all preceding #
813d3250a0SJeremy L Thompson       long current_size = strlen(*buffer);
823d3250a0SJeremy L Thompson       long copy_size = first_hash - &temp_buffer[file_offset];
833d3250a0SJeremy L Thompson       ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
843d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size], "\n", 2);
853d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
863d3250a0SJeremy L Thompson       strncpy(&(*buffer)[current_size + copy_size], "", 1);
873d3250a0SJeremy L Thompson       // -- Load local "header.h"
883d3250a0SJeremy L Thompson       char *next_quote = strchr(first_hash, '"');
893d3250a0SJeremy L Thompson       char *next_new_line = strchr(first_hash, '\n');
903d3250a0SJeremy L Thompson       bool is_local_header = is_hash_include && next_quote
913d3250a0SJeremy L Thompson                              && (next_new_line - next_quote > 0);
923d3250a0SJeremy L Thompson       if (is_local_header) {
933d3250a0SJeremy L Thompson         // ---- Build source path
943d3250a0SJeremy L Thompson         char *include_source_path;
953d3250a0SJeremy L Thompson         long root_length = strrchr(source_file_path, '/') - source_file_path;
963d3250a0SJeremy L Thompson         long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
973d3250a0SJeremy L Thompson         ierr = CeedCalloc(root_length + include_file_name_len + 2,
983d3250a0SJeremy L Thompson                           &include_source_path); CeedChk(ierr);
993d3250a0SJeremy L Thompson         strncpy(include_source_path, source_file_path, root_length + 1);
1003d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + 1], &next_quote[1],
1013d3250a0SJeremy L Thompson                 include_file_name_len);
1023d3250a0SJeremy L Thompson         strncpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
1033d3250a0SJeremy L Thompson         // ---- Recursive call to load source to buffer
1043d3250a0SJeremy L Thompson         ierr = CeedLoadSourceToInitalizedBuffer(ceed, include_source_path, buffer);
105a0154adeSJed Brown         CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path);
1063d3250a0SJeremy L Thompson         CeedChk(ierr);
1073d3250a0SJeremy L Thompson         ierr = CeedFree(&include_source_path); CeedChk(ierr);
1083d3250a0SJeremy L Thompson       }
1093d3250a0SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
1103d3250a0SJeremy L Thompson     }
1113d3250a0SJeremy L Thompson     // -- Next hash
1123d3250a0SJeremy L Thompson     first_hash = strchr(&first_hash[1], '#');
1133d3250a0SJeremy L Thompson   }
1143d3250a0SJeremy L Thompson   // Copy rest of source file into buffer
1153d3250a0SJeremy L Thompson   long current_size = strlen(*buffer);
1163d3250a0SJeremy L Thompson   long copy_size = strlen(&temp_buffer[file_offset]);
1173d3250a0SJeremy L Thompson   ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
1183d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size], "\n", 2);
1193d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
1203d3250a0SJeremy L Thompson   strncpy(&(*buffer)[current_size + copy_size + 1], "", 1);
1213d3250a0SJeremy L Thompson 
1223d3250a0SJeremy L Thompson   // Cleanup
1233d3250a0SJeremy L Thompson   ierr = CeedFree(&temp_buffer); CeedChk(ierr);
1243d3250a0SJeremy L Thompson 
1253d3250a0SJeremy L Thompson   // Debug
1263d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
1273d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Current source file: ");
1283d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", source_file_path);
1293d3250a0SJeremy L Thompson   CeedDebug256(ceed, 1, "Final buffer:\n");
1303d3250a0SJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", *buffer);
1313d3250a0SJeremy L Thompson 
1323d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1333d3250a0SJeremy L Thompson }
1343d3250a0SJeremy L Thompson 
1353d3250a0SJeremy L Thompson /**
1363d3250a0SJeremy L Thompson   @brief Initalize and load source file into string buffer, including full text
1373d3250a0SJeremy L Thompson            of local files in place of `#include "local.h"`.
1383d3250a0SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
1393d3250a0SJeremy L Thompson 
1403d3250a0SJeremy L Thompson   @param ceed                  A Ceed object for error handling
1413d3250a0SJeremy L Thompson   @param[in]  source_file_path Absolute path to source file
1423d3250a0SJeremy L Thompson   @param[out] buffer           String buffer for source file contents
1433d3250a0SJeremy L Thompson 
1443d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1453d3250a0SJeremy L Thompson 
1463d3250a0SJeremy L Thompson   @ref Backend
1473d3250a0SJeremy L Thompson **/
1483d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path,
1493d3250a0SJeremy L Thompson                            char **buffer) {
1503d3250a0SJeremy L Thompson   int ierr;
1513d3250a0SJeremy L Thompson 
1523d3250a0SJeremy L Thompson   // Initalize buffer
1533d3250a0SJeremy L Thompson   ierr = CeedCalloc(1, buffer); CeedChk(ierr);
1543d3250a0SJeremy L Thompson 
1553d3250a0SJeremy L Thompson   // Load to initalized buffer
1563d3250a0SJeremy L Thompson   ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer);
1573d3250a0SJeremy L Thompson   CeedChk(ierr);
1583d3250a0SJeremy L Thompson 
1593d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1603d3250a0SJeremy L Thompson }
161437930d1SJeremy L Thompson 
162437930d1SJeremy L Thompson /**
163437930d1SJeremy L Thompson   @brief Build an absolute filepath from a base filepath and an absolute filepath.
164437930d1SJeremy L Thompson            This helps construct source file paths for `CeedLoadSourceToBuffer()`.
165437930d1SJeremy L Thompson          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
166437930d1SJeremy L Thompson 
167437930d1SJeremy L Thompson   @param ceed                     A Ceed object for error handling
168437930d1SJeremy L Thompson   @param[in]  base_file_path      Absolute path to current file
169437930d1SJeremy L Thompson   @param[in]  relative_file_path  Relative path to target file
170437930d1SJeremy L Thompson   @param[out] new_file_path       String buffer for absolute path to target file
171437930d1SJeremy L Thompson 
172437930d1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
173437930d1SJeremy L Thompson 
174437930d1SJeremy L Thompson   @ref Backend
175437930d1SJeremy L Thompson **/
176437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path,
177437930d1SJeremy L Thompson                         const char *relative_file_path, char **new_file_path) {
178437930d1SJeremy L Thompson   int ierr;
179437930d1SJeremy L Thompson   char *last_slash = strrchr(base_file_path, '/');
180437930d1SJeremy L Thompson   size_t base_length = (last_slash - base_file_path + 1),
181437930d1SJeremy L Thompson          relative_length = strlen(relative_file_path),
182437930d1SJeremy L Thompson          new_file_path_length = base_length + relative_length + 1;
183437930d1SJeremy L Thompson 
184437930d1SJeremy L Thompson   ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr);
185437930d1SJeremy L Thompson   memcpy(*new_file_path, base_file_path, base_length);
186437930d1SJeremy L Thompson   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
187437930d1SJeremy L Thompson 
188437930d1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
189437930d1SJeremy L Thompson }
1906eb0d8b4SJeremy L Thompson 
1916eb0d8b4SJeremy L Thompson /**
192*032e71eaSJeremy L Thompson   @brief Find the relative filepath to an installed JiT file
1936eb0d8b4SJeremy L Thompson 
194*032e71eaSJeremy L Thompson   @param[in]  absolute_file_path Absolute path to installed JiT file
195*032e71eaSJeremy L Thompson   @param[out] relative_file_path Relative path to installed JiT file
1966eb0d8b4SJeremy L Thompson 
1976eb0d8b4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1986eb0d8b4SJeremy L Thompson 
1996eb0d8b4SJeremy L Thompson   @ref Backend
2006eb0d8b4SJeremy L Thompson **/
201*032e71eaSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path,
202*032e71eaSJeremy L Thompson                            const char **relative_file_path) {
203*032e71eaSJeremy L Thompson   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
2046eb0d8b4SJeremy L Thompson 
205*032e71eaSJeremy L Thompson   if (!*relative_file_path)
206*032e71eaSJeremy L Thompson     // LCOV_EXCL_START
207*032e71eaSJeremy L Thompson     return CeedError(NULL, CEED_ERROR_MAJOR,
208*032e71eaSJeremy L Thompson                      "Couldn't find relative path including "
209*032e71eaSJeremy L Thompson                      "'ceed/jit-source' for: %s", absolute_file_path);
210*032e71eaSJeremy L Thompson   // LCOV_EXCL_STOP
2116eb0d8b4SJeremy L Thompson 
2126eb0d8b4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2136eb0d8b4SJeremy L Thompson }
214*032e71eaSJeremy L Thompson 
215*032e71eaSJeremy L Thompson /**
216*032e71eaSJeremy L Thompson   @brief Build an absolute filepath to a JiT file
217*032e71eaSJeremy L Thompson 
218*032e71eaSJeremy L Thompson   @param ceed                    A Ceed object for error handling
219*032e71eaSJeremy L Thompson   @param[in]  relative_file_path Relative path to installed JiT file
220*032e71eaSJeremy L Thompson   @param[out] absolute_file_path String buffer for absolute path to target file
221*032e71eaSJeremy L Thompson 
222*032e71eaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
223*032e71eaSJeremy L Thompson 
224*032e71eaSJeremy L Thompson   @ref Backend
225*032e71eaSJeremy L Thompson **/
226*032e71eaSJeremy L Thompson int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path,
227*032e71eaSJeremy L Thompson                            char **absolute_file_path) {
228*032e71eaSJeremy L Thompson   int ierr;
229*032e71eaSJeremy L Thompson 
230*032e71eaSJeremy L Thompson   // Debug
231*032e71eaSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
232*032e71eaSJeremy L Thompson   CeedDebug256(ceed, 1, "Relative JiT source file: ");
233*032e71eaSJeremy L Thompson   CeedDebug256(ceed, 255, "%s\n", relative_file_path);
234*032e71eaSJeremy L Thompson 
235*032e71eaSJeremy L Thompson 
236*032e71eaSJeremy L Thompson   for (CeedInt i = 0; i < ceed->num_jit_source_roots; i++) {
237*032e71eaSJeremy L Thompson     // Debug
238*032e71eaSJeremy L Thompson     CeedDebug256(ceed, 1, "Checking JiT root: ");
239*032e71eaSJeremy L Thompson     CeedDebug256(ceed, 255, "%s\n", ceed->jit_source_roots[i]);
240*032e71eaSJeremy L Thompson 
241*032e71eaSJeremy L Thompson     // Build absolute path with current root
242*032e71eaSJeremy L Thompson     ierr = CeedPathConcatenate(ceed, ceed->jit_source_roots[i],
243*032e71eaSJeremy L Thompson                                relative_file_path, absolute_file_path);
244*032e71eaSJeremy L Thompson     CeedChk(ierr);
245*032e71eaSJeremy L Thompson 
246*032e71eaSJeremy L Thompson     // Temporarily mask function name if included
247*032e71eaSJeremy L Thompson     char *last_colon = strrchr(*absolute_file_path, ':');
248*032e71eaSJeremy L Thompson     if (last_colon) {
249*032e71eaSJeremy L Thompson       *last_colon = '\0';
250*032e71eaSJeremy L Thompson     }
251*032e71eaSJeremy L Thompson 
252*032e71eaSJeremy L Thompson     // Debug
253*032e71eaSJeremy L Thompson     CeedDebug256(ceed, 1, "Checking for source file: ");
254*032e71eaSJeremy L Thompson     CeedDebug256(ceed, 255, "%s\n", *absolute_file_path);
255*032e71eaSJeremy L Thompson 
256*032e71eaSJeremy L Thompson     // Check for valid file path
257*032e71eaSJeremy L Thompson     FILE *source_file;
258*032e71eaSJeremy L Thompson     source_file = fopen((const char *)*absolute_file_path, "rb");
259*032e71eaSJeremy L Thompson     if (source_file) {
260*032e71eaSJeremy L Thompson       // Debug
261*032e71eaSJeremy L Thompson       CeedDebug256(ceed, 1, "Found JiT source file: ");
262*032e71eaSJeremy L Thompson       CeedDebug256(ceed, 255, "%s\n", *absolute_file_path);
263*032e71eaSJeremy L Thompson 
264*032e71eaSJeremy L Thompson       // Restore function name if included
265*032e71eaSJeremy L Thompson       if (last_colon) {
266*032e71eaSJeremy L Thompson         *last_colon = ':';
267*032e71eaSJeremy L Thompson       }
268*032e71eaSJeremy L Thompson       fclose(source_file);
269*032e71eaSJeremy L Thompson       return CEED_ERROR_SUCCESS;
270*032e71eaSJeremy L Thompson     } else {
271*032e71eaSJeremy L Thompson       ierr = CeedFree(absolute_file_path); CeedChk(ierr);
272*032e71eaSJeremy L Thompson     }
273*032e71eaSJeremy L Thompson   }
274*032e71eaSJeremy L Thompson 
275*032e71eaSJeremy L Thompson   // LCOV_EXCL_START
276*032e71eaSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_MAJOR,
277*032e71eaSJeremy L Thompson                    "Couldn't find matching JiT source file: %s",
278*032e71eaSJeremy L Thompson                    relative_file_path);
279*032e71eaSJeremy L Thompson   // LCOV_EXCL_STOP
280*032e71eaSJeremy L Thompson }
281