xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-jit-tools.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 
86eb0d8b4SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/jit-tools.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 /**
17ee5a26f2SJeremy L Thompson   @brief Check if valid file exists at path given
18ee5a26f2SJeremy L Thompson 
19ca94c3ddSJeremy L Thompson   @param[in]  ceed             `Ceed` object for error handling
20ee5a26f2SJeremy L Thompson   @param[in]  source_file_path Absolute path to source file
21ea61e9acSJeremy L Thompson   @param[out] is_valid         Boolean flag indicating if file can be opened
22ee5a26f2SJeremy L Thompson 
23ee5a26f2SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
24ee5a26f2SJeremy L Thompson 
25ee5a26f2SJeremy L Thompson   @ref Backend
26ee5a26f2SJeremy L Thompson **/
CeedCheckFilePath(Ceed ceed,const char * source_file_path,bool * is_valid)27ee5a26f2SJeremy L Thompson int CeedCheckFilePath(Ceed ceed, const char *source_file_path, bool *is_valid) {
28ee5a26f2SJeremy L Thompson   // Sometimes we have path/to/file.h:function_name
29ea61e9acSJeremy L Thompson   // Create temporary file path without name, if needed
30ee5a26f2SJeremy L Thompson   char *source_file_path_only;
31ee5a26f2SJeremy L Thompson   char *last_colon = strrchr(source_file_path, ':');
321c66c397SJeremy L Thompson 
33ee5a26f2SJeremy L Thompson   if (last_colon) {
34ee5a26f2SJeremy L Thompson     size_t source_file_path_length = (last_colon - source_file_path + 1);
35ee5a26f2SJeremy L Thompson 
362b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only));
37d602d780SJeremy L Thompson     memcpy(source_file_path_only, source_file_path, source_file_path_length - 1);
38ee5a26f2SJeremy L Thompson   } else {
39ee5a26f2SJeremy L Thompson     source_file_path_only = (char *)source_file_path;
40ee5a26f2SJeremy L Thompson   }
41ee5a26f2SJeremy L Thompson 
42ee5a26f2SJeremy L Thompson   // Debug
4323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking for source file: ");
4413f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", source_file_path_only);
45ee5a26f2SJeremy L Thompson 
46ee5a26f2SJeremy L Thompson   // Check for valid file path
47ee5a26f2SJeremy L Thompson   FILE *source_file;
48ee5a26f2SJeremy L Thompson   source_file = fopen(source_file_path_only, "rb");
491c66c397SJeremy L Thompson   *is_valid   = source_file;
50ee5a26f2SJeremy L Thompson 
51ee5a26f2SJeremy L Thompson   if (*is_valid) {
52ee5a26f2SJeremy L Thompson     // Debug
5323d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Found JiT source file: ");
5413f886e9SJeremy L Thompson     CeedDebug(ceed, "%s\n", source_file_path_only);
55ee5a26f2SJeremy L Thompson     fclose(source_file);
56ee5a26f2SJeremy L Thompson   }
57ee5a26f2SJeremy L Thompson 
58ee5a26f2SJeremy L Thompson   // Free temp file path, if used
592b730f8bSJeremy L Thompson   if (last_colon) CeedCall(CeedFree(&source_file_path_only));
60ee5a26f2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
61ee5a26f2SJeremy L Thompson }
62ee5a26f2SJeremy L Thompson 
63ee5a26f2SJeremy L Thompson /**
6417afdf5cSJames Wright   @brief Normalize a filepath
657cbc98eeSJeremy L Thompson 
667cbc98eeSJeremy L Thompson   @param[in]   ceed                        `Ceed` object for error handling
677cbc98eeSJeremy L Thompson   @param[in]   source_file_path            Absolute path to source file
6817afdf5cSJames Wright   @param[out]  normalized_source_file_path Normalized filepath
697cbc98eeSJeremy L Thompson 
707cbc98eeSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
717cbc98eeSJeremy L Thompson 
727cbc98eeSJeremy L Thompson   @ref Backend
737cbc98eeSJeremy L Thompson **/
CeedNormalizePath(Ceed ceed,const char * source_file_path,char ** normalized_source_file_path)7417afdf5cSJames Wright static int CeedNormalizePath(Ceed ceed, const char *source_file_path, char **normalized_source_file_path) {
7517afdf5cSJames Wright   CeedCall(CeedStringAllocCopy(source_file_path, normalized_source_file_path));
767cbc98eeSJeremy L Thompson 
7717afdf5cSJames Wright   char *first_dot = strchr(*normalized_source_file_path, '.');
787cbc98eeSJeremy L Thompson 
797cbc98eeSJeremy L Thompson   while (first_dot) {
8042c7156fSJeremy L Thompson     char *search_from = first_dot + 1;
817cbc98eeSJeremy L Thompson     char  keyword[5]  = "";
827cbc98eeSJeremy L Thompson 
837cbc98eeSJeremy L Thompson     // -- Check for /./ and covert to /
8417afdf5cSJames Wright     if (first_dot != *normalized_source_file_path && strlen(first_dot) > 2) memcpy(keyword, &first_dot[-1], 3);
857cbc98eeSJeremy L Thompson     bool is_here = !strcmp(keyword, "/./");
867cbc98eeSJeremy L Thompson 
877cbc98eeSJeremy L Thompson     if (is_here) {
887cbc98eeSJeremy L Thompson       for (CeedInt i = 0; first_dot[i - 1]; i++) first_dot[i] = first_dot[i + 2];
8942c7156fSJeremy L Thompson       search_from = first_dot;
907cbc98eeSJeremy L Thompson     } else {
917cbc98eeSJeremy L Thompson       // -- Check for /foo/../ and convert to /
9217afdf5cSJames Wright       if (first_dot != *normalized_source_file_path && strlen(first_dot) > 3) memcpy(keyword, &first_dot[-1], 4);
937cbc98eeSJeremy L Thompson       bool is_up_one = !strcmp(keyword, "/../");
947cbc98eeSJeremy L Thompson 
957cbc98eeSJeremy L Thompson       if (is_up_one) {
967cbc98eeSJeremy L Thompson         char *last_slash = &first_dot[-2];
977cbc98eeSJeremy L Thompson 
9817afdf5cSJames Wright         while (last_slash[0] != '/' && last_slash != *normalized_source_file_path) last_slash--;
9917afdf5cSJames Wright         CeedCheck(last_slash != *normalized_source_file_path, ceed, CEED_ERROR_MAJOR, "Malformed source path %s", source_file_path);
1006e25802eSJames Wright         for (CeedInt i = 0; first_dot[i + 1]; i++) last_slash[i] = first_dot[i + 2];
10142c7156fSJeremy L Thompson         search_from = last_slash;
1027cbc98eeSJeremy L Thompson       }
1037cbc98eeSJeremy L Thompson     }
10442c7156fSJeremy L Thompson     first_dot = strchr(search_from, '.');
1057cbc98eeSJeremy L Thompson   }
1067cbc98eeSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1077cbc98eeSJeremy L Thompson }
1087cbc98eeSJeremy L Thompson 
1097cbc98eeSJeremy L Thompson /**
110509d4af6SJeremy L Thompson   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
111509d4af6SJeremy L Thompson     This also updates the `num_file_paths` and `source_file_paths`.
112509d4af6SJeremy L Thompson     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
1133d3250a0SJeremy L Thompson 
114ca94c3ddSJeremy L Thompson   @param[in]     ceed             `Ceed` object for error handling
1153d3250a0SJeremy L Thompson   @param[in]     source_file_path Absolute path to source file
116509d4af6SJeremy L Thompson   @param[in,out] num_file_paths   Number of files already included
117509d4af6SJeremy L Thompson   @param[in,out] file_paths       Paths of files already included
1183d3250a0SJeremy L Thompson   @param[out]    buffer           String buffer for source file contents
1193d3250a0SJeremy L Thompson 
1203d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1213d3250a0SJeremy L Thompson 
1223d3250a0SJeremy L Thompson   @ref Backend
1233d3250a0SJeremy L Thompson **/
CeedLoadSourceToInitializedBuffer(Ceed ceed,const char * source_file_path,CeedInt * num_file_paths,char *** file_paths,char ** buffer)12417afdf5cSJames Wright int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
1253d3250a0SJeremy L Thompson   FILE *source_file;
1263d3250a0SJeremy L Thompson   long  file_size, file_offset = 0;
1273d3250a0SJeremy L Thompson   char *temp_buffer;
1283d3250a0SJeremy L Thompson 
1293d3250a0SJeremy L Thompson   // Debug
13023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
13123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
13213f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", source_file_path);
1333d3250a0SJeremy L Thompson 
1343d3250a0SJeremy L Thompson   // Read file to temporary buffer
1353d3250a0SJeremy L Thompson   source_file = fopen(source_file_path, "rb");
1366574a04fSJeremy L Thompson   CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path);
1373d3250a0SJeremy L Thompson   // -- Compute size of source
1383d3250a0SJeremy L Thompson   fseek(source_file, 0L, SEEK_END);
1393d3250a0SJeremy L Thompson   file_size = ftell(source_file);
1407d0543c0SJeremy L Thompson   fseek(source_file, 0L, SEEK_SET);
1413d3250a0SJeremy L Thompson   //  -- Allocate memory for entire source file
1423b9caef5SJeremy L Thompson   {
1433b9caef5SJeremy L Thompson     const int ierr = CeedCalloc(file_size + 1, &temp_buffer);
1443b9caef5SJeremy L Thompson 
1453b9caef5SJeremy L Thompson     // Close stream before error handling, if necessary
1463b9caef5SJeremy L Thompson     if (ierr != CEED_ERROR_SUCCESS) fclose(source_file);
1473b9caef5SJeremy L Thompson     CeedCall(ierr);
1483b9caef5SJeremy L Thompson   }
1493d3250a0SJeremy L Thompson   // -- Copy the file into the buffer
1503d3250a0SJeremy L Thompson   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
1513d3250a0SJeremy L Thompson     // LCOV_EXCL_START
1523d3250a0SJeremy L Thompson     fclose(source_file);
1532b730f8bSJeremy L Thompson     CeedCall(CeedFree(&temp_buffer));
1542b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path);
1553d3250a0SJeremy L Thompson     // LCOV_EXCL_STOP
1563d3250a0SJeremy L Thompson   }
1573d3250a0SJeremy L Thompson   fclose(source_file);
1583d3250a0SJeremy L Thompson 
1593d3250a0SJeremy L Thompson   // Search for headers to include
1603d3250a0SJeremy L Thompson   const char *first_hash = strchr(temp_buffer, '#');
1611c66c397SJeremy L Thompson 
1623d3250a0SJeremy L Thompson   while (first_hash) {
163509d4af6SJeremy L Thompson     // -- Check for 'pragma' keyword
164509d4af6SJeremy L Thompson     const char *next_m     = strchr(first_hash, 'm');
165509d4af6SJeremy L Thompson     char        keyword[8] = "";
166509d4af6SJeremy L Thompson 
167509d4af6SJeremy L Thompson     if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6);
168509d4af6SJeremy L Thompson     bool is_hash_pragma = !strcmp(keyword, "pragma");
169509d4af6SJeremy L Thompson 
170509d4af6SJeremy L Thompson     // ---- Spaces allowed in '#  pragma'
171509d4af6SJeremy L Thompson     if (next_m) {
172509d4af6SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_m + i < -5; i++) {
173509d4af6SJeremy L Thompson         is_hash_pragma &= first_hash[i] == ' ';
174509d4af6SJeremy L Thompson       }
175509d4af6SJeremy L Thompson     }
176509d4af6SJeremy L Thompson     if (is_hash_pragma) {
177509d4af6SJeremy L Thompson       // -- Check if '#pragma once'
178509d4af6SJeremy L Thompson       char *next_o         = strchr(first_hash, 'o');
179509d4af6SJeremy L Thompson       char *next_new_line  = strchr(first_hash, '\n');
180509d4af6SJeremy L Thompson       bool  is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4);
181509d4af6SJeremy L Thompson 
182509d4af6SJeremy L Thompson       // -- Copy into buffer, omitting last line if #pragma once
183509d4af6SJeremy L Thompson       long current_size = strlen(*buffer);
184509d4af6SJeremy L Thompson       long copy_size    = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1));
185509d4af6SJeremy L Thompson 
186509d4af6SJeremy L Thompson       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
187509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size], "\n", 2);
188509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
189509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size + copy_size], "", 1);
190509d4af6SJeremy L Thompson 
191509d4af6SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
192509d4af6SJeremy L Thompson     }
193509d4af6SJeremy L Thompson 
1943d3250a0SJeremy L Thompson     // -- Check for 'include' keyword
1953d3250a0SJeremy L Thompson     const char *next_e = strchr(first_hash, 'e');
1961c66c397SJeremy L Thompson 
197c9c2c079SJeremy L Thompson     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
1983d3250a0SJeremy L Thompson     bool is_hash_include = !strcmp(keyword, "include");
1991c66c397SJeremy L Thompson 
2003d3250a0SJeremy L Thompson     // ---- Spaces allowed in '#  include <header.h>'
201c9c2c079SJeremy L Thompson     if (next_e) {
202c9c2c079SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
2033d3250a0SJeremy L Thompson         is_hash_include &= first_hash[i] == ' ';
204c9c2c079SJeremy L Thompson       }
205c9c2c079SJeremy L Thompson     }
2063d3250a0SJeremy L Thompson     if (is_hash_include) {
2073d3250a0SJeremy L Thompson       // -- Copy into buffer all preceding #
2083d3250a0SJeremy L Thompson       long current_size = strlen(*buffer);
2093d3250a0SJeremy L Thompson       long copy_size    = first_hash - &temp_buffer[file_offset];
2101c66c397SJeremy L Thompson 
2112b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
212d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size], "\n", 2);
213d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
214d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size + copy_size], "", 1);
2153d3250a0SJeremy L Thompson       // -- Load local "header.h"
2163d3250a0SJeremy L Thompson       char *next_quote        = strchr(first_hash, '"');
2173d3250a0SJeremy L Thompson       char *next_new_line     = strchr(first_hash, '\n');
2182b730f8bSJeremy L Thompson       bool  is_local_header   = is_hash_include && next_quote && (next_new_line - next_quote > 0);
219c9c2c079SJeremy L Thompson       char *next_left_chevron = strchr(first_hash, '<');
220509d4af6SJeremy L Thompson       bool  is_ceed_header    = next_left_chevron && (next_new_line - next_left_chevron > 0) &&
2212b730f8bSJeremy L Thompson                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
2222b730f8bSJeremy L Thompson                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
223daaf13a4SJeremy L Thompson       bool is_std_header =
224daaf13a4SJeremy L Thompson           next_left_chevron && (next_new_line - next_left_chevron > 0) &&
225daaf13a4SJeremy L Thompson           (!strncmp(next_left_chevron, "<std", 4) || !strncmp(next_left_chevron, "<math.h>", 8) || !strncmp(next_left_chevron, "<ceed", 5));
2261c66c397SJeremy L Thompson 
227c9c2c079SJeremy L Thompson       if (is_local_header || is_ceed_header) {
2283d3250a0SJeremy L Thompson         // ---- Build source path
229430da97aSJeremy L Thompson         bool  is_included = false;
2303d3250a0SJeremy L Thompson         char *include_source_path;
2311c66c397SJeremy L Thompson 
232c9c2c079SJeremy L Thompson         if (is_local_header) {
2333d3250a0SJeremy L Thompson           long root_length           = strrchr(source_file_path, '/') - source_file_path;
2343d3250a0SJeremy L Thompson           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
2351c66c397SJeremy L Thompson 
2362b730f8bSJeremy L Thompson           CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path));
237d602d780SJeremy L Thompson           memcpy(include_source_path, source_file_path, root_length + 1);
2382b730f8bSJeremy L Thompson           memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len);
239d602d780SJeremy L Thompson           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
240c9c2c079SJeremy L Thompson         } else {
241c9c2c079SJeremy L Thompson           char *next_right_chevron = strchr(first_hash, '>');
242c9c2c079SJeremy L Thompson           char *ceed_relative_path;
243c9c2c079SJeremy L Thompson           long  ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
2441c66c397SJeremy L Thompson 
2452b730f8bSJeremy L Thompson           CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path));
246c9c2c079SJeremy L Thompson           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
24722070f95SJeremy L Thompson           CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path));
2482b730f8bSJeremy L Thompson           CeedCall(CeedFree(&ceed_relative_path));
249c9c2c079SJeremy L Thompson         }
2503d3250a0SJeremy L Thompson         // ---- Recursive call to load source to buffer
25117afdf5cSJames Wright         char *normalized_include_source_path;
2527cbc98eeSJeremy L Thompson 
25317afdf5cSJames Wright         CeedCall(CeedNormalizePath(ceed, include_source_path, &normalized_include_source_path));
25417afdf5cSJames Wright         for (CeedInt i = 0; i < *num_file_paths; i++) is_included |= !strcmp(normalized_include_source_path, (*file_paths)[i]);
255430da97aSJeremy L Thompson         if (!is_included) {
25617afdf5cSJames Wright           CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", normalized_include_source_path);
25717afdf5cSJames Wright           CeedCall(CeedLoadSourceToInitializedBuffer(ceed, normalized_include_source_path, num_file_paths, file_paths, buffer));
25817afdf5cSJames Wright           CeedCall(CeedRealloc(*num_file_paths + 1, file_paths));
25917afdf5cSJames Wright           CeedCall(CeedStringAllocCopy(normalized_include_source_path, &(*file_paths)[*num_file_paths]));
26017afdf5cSJames Wright           (*num_file_paths)++;
261430da97aSJeremy L Thompson         }
2622b730f8bSJeremy L Thompson         CeedCall(CeedFree(&include_source_path));
26317afdf5cSJames Wright         CeedCall(CeedFree(&normalized_include_source_path));
264daaf13a4SJeremy L Thompson       } else if (!is_std_header) {
265daaf13a4SJeremy L Thompson         long header_copy_size = next_new_line - first_hash + 1;
266daaf13a4SJeremy L Thompson 
267daaf13a4SJeremy L Thompson         CeedCall(CeedRealloc(current_size + copy_size + header_copy_size + 2, buffer));
268daaf13a4SJeremy L Thompson         memcpy(&(*buffer)[current_size + copy_size], "\n", 2);
269daaf13a4SJeremy L Thompson         memcpy(&(*buffer)[current_size + copy_size + 1], first_hash, header_copy_size);
270daaf13a4SJeremy L Thompson         memcpy(&(*buffer)[current_size + copy_size + header_copy_size], "", 1);
2713d3250a0SJeremy L Thompson       }
2723d3250a0SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
2733d3250a0SJeremy L Thompson     }
2743d3250a0SJeremy L Thompson     // -- Next hash
2753d3250a0SJeremy L Thompson     first_hash = strchr(&first_hash[1], '#');
2763d3250a0SJeremy L Thompson   }
2773d3250a0SJeremy L Thompson   // Copy rest of source file into buffer
2783d3250a0SJeremy L Thompson   long current_size = strlen(*buffer);
2793d3250a0SJeremy L Thompson   long copy_size    = strlen(&temp_buffer[file_offset]);
2801c66c397SJeremy L Thompson 
2812b730f8bSJeremy L Thompson   CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
282d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size], "\n", 2);
283d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
284d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
2853d3250a0SJeremy L Thompson 
2863d3250a0SJeremy L Thompson   // Cleanup
2872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&temp_buffer));
2883d3250a0SJeremy L Thompson 
2893d3250a0SJeremy L Thompson   // Debug
29023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
29123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
29213f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", source_file_path);
29323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n");
29413f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", *buffer);
2953d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2963d3250a0SJeremy L Thompson }
2973d3250a0SJeremy L Thompson 
2983d3250a0SJeremy L Thompson /**
299509d4af6SJeremy L Thompson   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
300509d4af6SJeremy L Thompson     This also initializes and populates the `num_file_paths` and `source_file_paths`.
301509d4af6SJeremy L Thompson     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
302430da97aSJeremy L Thompson 
303430da97aSJeremy L Thompson   @param[in]     ceed             `Ceed` object for error handling
304430da97aSJeremy L Thompson   @param[in]     source_file_path Absolute path to source file
305509d4af6SJeremy L Thompson   @param[in,out] num_file_paths   Number of files already included
306509d4af6SJeremy L Thompson   @param[in,out] file_paths       Paths of files already included
307430da97aSJeremy L Thompson   @param[out]    buffer           String buffer for source file contents
308430da97aSJeremy L Thompson 
309430da97aSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
310430da97aSJeremy L Thompson 
311430da97aSJeremy L Thompson   @ref Backend
312430da97aSJeremy L Thompson **/
CeedLoadSourceAndInitializeBuffer(Ceed ceed,const char * source_file_path,CeedInt * num_file_paths,char *** file_paths,char ** buffer)313509d4af6SJeremy L Thompson int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
314509d4af6SJeremy L Thompson   // Ensure defaults were set
315509d4af6SJeremy L Thompson   *num_file_paths = 0;
316509d4af6SJeremy L Thompson   *file_paths     = NULL;
317430da97aSJeremy L Thompson 
318509d4af6SJeremy L Thompson   // Initialize
319509d4af6SJeremy L Thompson   CeedCall(CeedCalloc(1, buffer));
320509d4af6SJeremy L Thompson 
321509d4af6SJeremy L Thompson   // And load source
322509d4af6SJeremy L Thompson   CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer));
323430da97aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
324430da97aSJeremy L Thompson }
325430da97aSJeremy L Thompson 
326430da97aSJeremy L Thompson /**
327ea61e9acSJeremy L Thompson   @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`.
328509d4af6SJeremy L Thompson     User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer.
329509d4af6SJeremy L Thompson     Caller is responsible for freeing the string buffer with @ref CeedFree().
3303d3250a0SJeremy L Thompson 
331ca94c3ddSJeremy L Thompson   @param[in]  ceed             `Ceed` object for error handling
3323d3250a0SJeremy L Thompson   @param[in]  source_file_path Absolute path to source file
3333d3250a0SJeremy L Thompson   @param[out] buffer           String buffer for source file contents
3343d3250a0SJeremy L Thompson 
3353d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3363d3250a0SJeremy L Thompson 
3373d3250a0SJeremy L Thompson   @ref Backend
3383d3250a0SJeremy L Thompson **/
CeedLoadSourceToBuffer(Ceed ceed,const char * source_file_path,char ** buffer)3392b730f8bSJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
340509d4af6SJeremy L Thompson   char  **file_paths     = NULL;
341509d4af6SJeremy L Thompson   CeedInt num_file_paths = 0;
3423d3250a0SJeremy L Thompson 
343509d4af6SJeremy L Thompson   // Load
344509d4af6SJeremy L Thompson   CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer));
345509d4af6SJeremy L Thompson 
346509d4af6SJeremy L Thompson   // Cleanup
347509d4af6SJeremy L Thompson   for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
348509d4af6SJeremy L Thompson   CeedCall(CeedFree(&file_paths));
3493d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3503d3250a0SJeremy L Thompson }
351437930d1SJeremy L Thompson 
352437930d1SJeremy L Thompson /**
353437930d1SJeremy L Thompson   @brief Build an absolute filepath from a base filepath and an absolute filepath.
3544385fb7fSSebastian Grimberg 
355ca94c3ddSJeremy L Thompson   This helps construct source file paths for @ref CeedLoadSourceToBuffer().
3564385fb7fSSebastian Grimberg 
357ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
358437930d1SJeremy L Thompson 
359ca94c3ddSJeremy L Thompson   @param[in]  ceed               `Ceed` object for error handling
360437930d1SJeremy L Thompson   @param[in]  base_file_path     Absolute path to current file
361437930d1SJeremy L Thompson   @param[in]  relative_file_path Relative path to target file
362437930d1SJeremy L Thompson   @param[out] new_file_path      String buffer for absolute path to target file
363437930d1SJeremy L Thompson 
364437930d1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
365437930d1SJeremy L Thompson 
366437930d1SJeremy L Thompson   @ref Backend
367437930d1SJeremy L Thompson **/
CeedPathConcatenate(Ceed ceed,const char * base_file_path,const char * relative_file_path,char ** new_file_path)3682b730f8bSJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) {
369437930d1SJeremy L Thompson   char  *last_slash  = strrchr(base_file_path, '/');
3702b730f8bSJeremy L Thompson   size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path),
371437930d1SJeremy L Thompson          new_file_path_length = base_length + relative_length + 1;
372437930d1SJeremy L Thompson 
3732b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(new_file_path_length, new_file_path));
374d602d780SJeremy L Thompson   memcpy(*new_file_path, base_file_path, base_length);
375d602d780SJeremy L Thompson   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
376437930d1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
377437930d1SJeremy L Thompson }
3786eb0d8b4SJeremy L Thompson 
3796eb0d8b4SJeremy L Thompson /**
380032e71eaSJeremy L Thompson   @brief Find the relative filepath to an installed JiT file
3816eb0d8b4SJeremy L Thompson 
382032e71eaSJeremy L Thompson   @param[in]  absolute_file_path Absolute path to installed JiT file
38391e18578SJeremy L Thompson   @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path
3846eb0d8b4SJeremy L Thompson 
3856eb0d8b4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3866eb0d8b4SJeremy L Thompson 
3876eb0d8b4SJeremy L Thompson   @ref Backend
3886eb0d8b4SJeremy L Thompson **/
CeedGetJitRelativePath(const char * absolute_file_path,const char ** relative_file_path)3892b730f8bSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) {
390032e71eaSJeremy L Thompson   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
3916574a04fSJeremy L Thompson   CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path);
3926eb0d8b4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3936eb0d8b4SJeremy L Thompson }
394032e71eaSJeremy L Thompson 
395032e71eaSJeremy L Thompson /**
396032e71eaSJeremy L Thompson   @brief Build an absolute filepath to a JiT file
397032e71eaSJeremy L Thompson 
398ca94c3ddSJeremy L Thompson   @param[in]  ceed               `Ceed` object for error handling
399032e71eaSJeremy L Thompson   @param[in]  relative_file_path Relative path to installed JiT file
40091e18578SJeremy L Thompson   @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller
401032e71eaSJeremy L Thompson 
402032e71eaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
403032e71eaSJeremy L Thompson 
404032e71eaSJeremy L Thompson   @ref Backend
405032e71eaSJeremy L Thompson **/
CeedGetJitAbsolutePath(Ceed ceed,const char * relative_file_path,const char ** absolute_file_path)406f8608ea8SJed Brown int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) {
407b13efd58SJeremy L Thompson   const char **jit_source_dirs;
408b13efd58SJeremy L Thompson   CeedInt      num_source_dirs;
409032e71eaSJeremy L Thompson 
410032e71eaSJeremy L Thompson   // Debug
41123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
41223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: ");
41313f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", relative_file_path);
414032e71eaSJeremy L Thompson 
415b13efd58SJeremy L Thompson   CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_source_dirs, &jit_source_dirs));
416b13efd58SJeremy L Thompson   for (CeedInt i = 0; i < num_source_dirs; i++) {
417ee5a26f2SJeremy L Thompson     bool is_valid;
418ee5a26f2SJeremy L Thompson 
419032e71eaSJeremy L Thompson     // Debug
42023d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: ");
421b13efd58SJeremy L Thompson     CeedDebug(ceed, "%s\n", jit_source_dirs[i]);
422032e71eaSJeremy L Thompson 
423ee5a26f2SJeremy L Thompson     // Build and check absolute path with current root
424b13efd58SJeremy L Thompson     CeedCall(CeedPathConcatenate(ceed, jit_source_dirs[i], relative_file_path, (char **)absolute_file_path));
4252b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid));
426032e71eaSJeremy L Thompson 
427b13efd58SJeremy L Thompson     if (is_valid) {
428b13efd58SJeremy L Thompson       CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs));
429b13efd58SJeremy L Thompson       return CEED_ERROR_SUCCESS;
430b13efd58SJeremy L Thompson     }
43191e18578SJeremy L Thompson     // LCOV_EXCL_START
432b13efd58SJeremy L Thompson     else
433b13efd58SJeremy L Thompson       CeedCall(CeedFree(absolute_file_path));
43491e18578SJeremy L Thompson     // LCOV_EXCL_STOP
435032e71eaSJeremy L Thompson   }
436032e71eaSJeremy L Thompson   // LCOV_EXCL_START
4372b730f8bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path);
438032e71eaSJeremy L Thompson   // LCOV_EXCL_STOP
439032e71eaSJeremy L Thompson }
440