xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-jit-tools.c (revision 7cbc98ee6c81dcfdc133295c4388d23367d79bb6)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 **/
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 /**
64*7cbc98eeSJeremy L Thompson   @brief Trim all `/./` and `/../` out of filepath
65*7cbc98eeSJeremy L Thompson 
66*7cbc98eeSJeremy L Thompson   @param[in]   ceed                     `Ceed` object for error handling
67*7cbc98eeSJeremy L Thompson   @param[in]   source_file_path         Absolute path to source file
68*7cbc98eeSJeremy L Thompson   @param[out]  trimmed_source_file_path Filepath trimmed of all `/./` and `/../`
69*7cbc98eeSJeremy L Thompson 
70*7cbc98eeSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71*7cbc98eeSJeremy L Thompson 
72*7cbc98eeSJeremy L Thompson   @ref Backend
73*7cbc98eeSJeremy L Thompson **/
74*7cbc98eeSJeremy L Thompson static int CeedTrimPath(Ceed ceed, const char *source_file_path, char **trimmed_source_file_path) {
75*7cbc98eeSJeremy L Thompson   CeedCall(CeedStringAllocCopy(source_file_path, trimmed_source_file_path));
76*7cbc98eeSJeremy L Thompson 
77*7cbc98eeSJeremy L Thompson   char *first_dot = strchr(*trimmed_source_file_path, '.');
78*7cbc98eeSJeremy L Thompson 
79*7cbc98eeSJeremy L Thompson   while (first_dot) {
80*7cbc98eeSJeremy L Thompson     char keyword[5] = "";
81*7cbc98eeSJeremy L Thompson 
82*7cbc98eeSJeremy L Thompson     // -- Check for /./ and covert to /
83*7cbc98eeSJeremy L Thompson     if (first_dot != *trimmed_source_file_path && strlen(first_dot) > 2) memcpy(keyword, &first_dot[-1], 3);
84*7cbc98eeSJeremy L Thompson     bool is_here = !strcmp(keyword, "/./");
85*7cbc98eeSJeremy L Thompson 
86*7cbc98eeSJeremy L Thompson     if (is_here) {
87*7cbc98eeSJeremy L Thompson       for (CeedInt i = 0; first_dot[i - 1]; i++) first_dot[i] = first_dot[i + 2];
88*7cbc98eeSJeremy L Thompson     } else {
89*7cbc98eeSJeremy L Thompson       // -- Check for /foo/../ and convert to /
90*7cbc98eeSJeremy L Thompson       if (first_dot != *trimmed_source_file_path && strlen(first_dot) > 3) memcpy(keyword, &first_dot[-1], 4);
91*7cbc98eeSJeremy L Thompson       bool is_up_one = !strcmp(keyword, "/../");
92*7cbc98eeSJeremy L Thompson 
93*7cbc98eeSJeremy L Thompson       if (is_up_one) {
94*7cbc98eeSJeremy L Thompson         char *last_slash = &first_dot[-2];
95*7cbc98eeSJeremy L Thompson 
96*7cbc98eeSJeremy L Thompson         while (last_slash[0] != '/' && last_slash != *trimmed_source_file_path) last_slash--;
97*7cbc98eeSJeremy L Thompson         CeedCheck(last_slash != *trimmed_source_file_path, ceed, CEED_ERROR_MAJOR, "Malformed source path %s", source_file_path);
98*7cbc98eeSJeremy L Thompson         for (CeedInt i = 0; first_dot[i - 1]; i++) last_slash[i] = first_dot[i + 2];
99*7cbc98eeSJeremy L Thompson       }
100*7cbc98eeSJeremy L Thompson     }
101*7cbc98eeSJeremy L Thompson     first_dot = strchr(&first_dot[1], '.');
102*7cbc98eeSJeremy L Thompson   }
103*7cbc98eeSJeremy L Thompson   return CEED_ERROR_SUCCESS;
104*7cbc98eeSJeremy L Thompson }
105*7cbc98eeSJeremy L Thompson 
106*7cbc98eeSJeremy L Thompson /**
107509d4af6SJeremy L Thompson   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
108509d4af6SJeremy L Thompson     This also updates the `num_file_paths` and `source_file_paths`.
109509d4af6SJeremy L Thompson     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
1103d3250a0SJeremy L Thompson 
111ca94c3ddSJeremy L Thompson   @param[in]     ceed             `Ceed` object for error handling
1123d3250a0SJeremy L Thompson   @param[in]     source_file_path Absolute path to source file
113509d4af6SJeremy L Thompson   @param[in,out] num_file_paths   Number of files already included
114509d4af6SJeremy L Thompson   @param[in,out] file_paths       Paths of files already included
1153d3250a0SJeremy L Thompson   @param[out]    buffer           String buffer for source file contents
1163d3250a0SJeremy L Thompson 
1173d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1183d3250a0SJeremy L Thompson 
1193d3250a0SJeremy L Thompson   @ref Backend
1203d3250a0SJeremy L Thompson **/
121509d4af6SJeremy L Thompson int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_files, char ***file_paths, char **buffer) {
1223d3250a0SJeremy L Thompson   FILE *source_file;
1233d3250a0SJeremy L Thompson   long  file_size, file_offset = 0;
1243d3250a0SJeremy L Thompson   char *temp_buffer;
1253d3250a0SJeremy L Thompson 
1263d3250a0SJeremy L Thompson   // Debug
12723d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
12823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
12913f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", source_file_path);
13023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current buffer:\n");
13113f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", *buffer);
1323d3250a0SJeremy L Thompson 
1333d3250a0SJeremy L Thompson   // Read file to temporary buffer
1343d3250a0SJeremy L Thompson   source_file = fopen(source_file_path, "rb");
1356574a04fSJeremy L Thompson   CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path);
1363d3250a0SJeremy L Thompson   // -- Compute size of source
1373d3250a0SJeremy L Thompson   fseek(source_file, 0L, SEEK_END);
1383d3250a0SJeremy L Thompson   file_size = ftell(source_file);
1393d3250a0SJeremy L Thompson   rewind(source_file);
1403d3250a0SJeremy L Thompson   //  -- Allocate memory for entire source file
1412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(file_size + 1, &temp_buffer));
1423d3250a0SJeremy L Thompson   // -- Copy the file into the buffer
1433d3250a0SJeremy L Thompson   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
1443d3250a0SJeremy L Thompson     // LCOV_EXCL_START
1453d3250a0SJeremy L Thompson     fclose(source_file);
1462b730f8bSJeremy L Thompson     CeedCall(CeedFree(&temp_buffer));
1472b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path);
1483d3250a0SJeremy L Thompson     // LCOV_EXCL_STOP
1493d3250a0SJeremy L Thompson   }
1503d3250a0SJeremy L Thompson   fclose(source_file);
1513d3250a0SJeremy L Thompson 
1523d3250a0SJeremy L Thompson   // Search for headers to include
1533d3250a0SJeremy L Thompson   const char *first_hash = strchr(temp_buffer, '#');
1541c66c397SJeremy L Thompson 
1553d3250a0SJeremy L Thompson   while (first_hash) {
156509d4af6SJeremy L Thompson     // -- Check for 'pragma' keyword
157509d4af6SJeremy L Thompson     const char *next_m     = strchr(first_hash, 'm');
158509d4af6SJeremy L Thompson     char        keyword[8] = "";
159509d4af6SJeremy L Thompson 
160509d4af6SJeremy L Thompson     if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6);
161509d4af6SJeremy L Thompson     bool is_hash_pragma = !strcmp(keyword, "pragma");
162509d4af6SJeremy L Thompson 
163509d4af6SJeremy L Thompson     // ---- Spaces allowed in '#  pragma'
164509d4af6SJeremy L Thompson     if (next_m) {
165509d4af6SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_m + i < -5; i++) {
166509d4af6SJeremy L Thompson         is_hash_pragma &= first_hash[i] == ' ';
167509d4af6SJeremy L Thompson       }
168509d4af6SJeremy L Thompson     }
169509d4af6SJeremy L Thompson     if (is_hash_pragma) {
170509d4af6SJeremy L Thompson       // -- Check if '#pragma once'
171509d4af6SJeremy L Thompson       char *next_o         = strchr(first_hash, 'o');
172509d4af6SJeremy L Thompson       char *next_new_line  = strchr(first_hash, '\n');
173509d4af6SJeremy L Thompson       bool  is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4);
174509d4af6SJeremy L Thompson 
175509d4af6SJeremy L Thompson       // -- Copy into buffer, omitting last line if #pragma once
176509d4af6SJeremy L Thompson       long current_size = strlen(*buffer);
177509d4af6SJeremy L Thompson       long copy_size    = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1));
178509d4af6SJeremy L Thompson 
179509d4af6SJeremy L Thompson       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
180509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size], "\n", 2);
181509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
182509d4af6SJeremy L Thompson       memcpy(&(*buffer)[current_size + copy_size], "", 1);
183509d4af6SJeremy L Thompson 
184509d4af6SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
185509d4af6SJeremy L Thompson     }
186509d4af6SJeremy L Thompson 
1873d3250a0SJeremy L Thompson     // -- Check for 'include' keyword
1883d3250a0SJeremy L Thompson     const char *next_e = strchr(first_hash, 'e');
1891c66c397SJeremy L Thompson 
190c9c2c079SJeremy L Thompson     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
1913d3250a0SJeremy L Thompson     bool is_hash_include = !strcmp(keyword, "include");
1921c66c397SJeremy L Thompson 
1933d3250a0SJeremy L Thompson     // ---- Spaces allowed in '#  include <header.h>'
194c9c2c079SJeremy L Thompson     if (next_e) {
195c9c2c079SJeremy L Thompson       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
1963d3250a0SJeremy L Thompson         is_hash_include &= first_hash[i] == ' ';
197c9c2c079SJeremy L Thompson       }
198c9c2c079SJeremy L Thompson     }
1993d3250a0SJeremy L Thompson     if (is_hash_include) {
2003d3250a0SJeremy L Thompson       // -- Copy into buffer all preceding #
2013d3250a0SJeremy L Thompson       long current_size = strlen(*buffer);
2023d3250a0SJeremy L Thompson       long copy_size    = first_hash - &temp_buffer[file_offset];
2031c66c397SJeremy L Thompson 
2042b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
205d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size], "\n", 2);
206d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
207d602d780SJeremy L Thompson       memcpy(&(*buffer)[current_size + copy_size], "", 1);
2083d3250a0SJeremy L Thompson       // -- Load local "header.h"
2093d3250a0SJeremy L Thompson       char *next_quote        = strchr(first_hash, '"');
2103d3250a0SJeremy L Thompson       char *next_new_line     = strchr(first_hash, '\n');
2112b730f8bSJeremy L Thompson       bool  is_local_header   = is_hash_include && next_quote && (next_new_line - next_quote > 0);
212c9c2c079SJeremy L Thompson       char *next_left_chevron = strchr(first_hash, '<');
213509d4af6SJeremy L Thompson       bool  is_ceed_header    = next_left_chevron && (next_new_line - next_left_chevron > 0) &&
2142b730f8bSJeremy L Thompson                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
2152b730f8bSJeremy L Thompson                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
2161c66c397SJeremy L Thompson 
217c9c2c079SJeremy L Thompson       if (is_local_header || is_ceed_header) {
2183d3250a0SJeremy L Thompson         // ---- Build source path
219430da97aSJeremy L Thompson         bool  is_included = false;
2203d3250a0SJeremy L Thompson         char *include_source_path;
2211c66c397SJeremy L Thompson 
222c9c2c079SJeremy L Thompson         if (is_local_header) {
2233d3250a0SJeremy L Thompson           long root_length           = strrchr(source_file_path, '/') - source_file_path;
2243d3250a0SJeremy L Thompson           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
2251c66c397SJeremy L Thompson 
2262b730f8bSJeremy L Thompson           CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path));
227d602d780SJeremy L Thompson           memcpy(include_source_path, source_file_path, root_length + 1);
2282b730f8bSJeremy L Thompson           memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len);
229d602d780SJeremy L Thompson           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
230c9c2c079SJeremy L Thompson         } else {
231c9c2c079SJeremy L Thompson           char *next_right_chevron = strchr(first_hash, '>');
232c9c2c079SJeremy L Thompson           char *ceed_relative_path;
233c9c2c079SJeremy L Thompson           long  ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
2341c66c397SJeremy L Thompson 
2352b730f8bSJeremy L Thompson           CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path));
236c9c2c079SJeremy L Thompson           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
23722070f95SJeremy L Thompson           CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path));
2382b730f8bSJeremy L Thompson           CeedCall(CeedFree(&ceed_relative_path));
239c9c2c079SJeremy L Thompson         }
2403d3250a0SJeremy L Thompson         // ---- Recursive call to load source to buffer
241*7cbc98eeSJeremy L Thompson         char *trimmed_include_source_path;
242*7cbc98eeSJeremy L Thompson 
243*7cbc98eeSJeremy L Thompson         CeedCall(CeedTrimPath(ceed, include_source_path, &trimmed_include_source_path));
244*7cbc98eeSJeremy L Thompson         for (CeedInt i = 0; i < *num_files; i++) is_included |= !strcmp(trimmed_include_source_path, (*file_paths)[i]);
245430da97aSJeremy L Thompson         if (!is_included) {
246*7cbc98eeSJeremy L Thompson           CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", trimmed_include_source_path);
247*7cbc98eeSJeremy L Thompson           CeedCall(CeedLoadSourceToInitializedBuffer(ceed, trimmed_include_source_path, num_files, file_paths, buffer));
248509d4af6SJeremy L Thompson           CeedCall(CeedRealloc(*num_files + 1, file_paths));
249*7cbc98eeSJeremy L Thompson           CeedCall(CeedStringAllocCopy(trimmed_include_source_path, &(*file_paths)[*num_files]));
250430da97aSJeremy L Thompson           (*num_files)++;
251430da97aSJeremy L Thompson         }
2522b730f8bSJeremy L Thompson         CeedCall(CeedFree(&include_source_path));
253*7cbc98eeSJeremy L Thompson         CeedCall(CeedFree(&trimmed_include_source_path));
2543d3250a0SJeremy L Thompson       }
2553d3250a0SJeremy L Thompson       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
2563d3250a0SJeremy L Thompson     }
2573d3250a0SJeremy L Thompson     // -- Next hash
2583d3250a0SJeremy L Thompson     first_hash = strchr(&first_hash[1], '#');
2593d3250a0SJeremy L Thompson   }
2603d3250a0SJeremy L Thompson   // Copy rest of source file into buffer
2613d3250a0SJeremy L Thompson   long current_size = strlen(*buffer);
2623d3250a0SJeremy L Thompson   long copy_size    = strlen(&temp_buffer[file_offset]);
2631c66c397SJeremy L Thompson 
2642b730f8bSJeremy L Thompson   CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
265d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size], "\n", 2);
266d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
267d602d780SJeremy L Thompson   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
2683d3250a0SJeremy L Thompson 
2693d3250a0SJeremy L Thompson   // Cleanup
2702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&temp_buffer));
2713d3250a0SJeremy L Thompson 
2723d3250a0SJeremy L Thompson   // Debug
27323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
27423d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
27513f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", source_file_path);
27623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n");
27713f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", *buffer);
2783d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2793d3250a0SJeremy L Thompson }
2803d3250a0SJeremy L Thompson 
2813d3250a0SJeremy L Thompson /**
282509d4af6SJeremy L Thompson   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
283509d4af6SJeremy L Thompson     This also initializes and populates the `num_file_paths` and `source_file_paths`.
284509d4af6SJeremy L Thompson     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
285430da97aSJeremy L Thompson 
286430da97aSJeremy L Thompson   @param[in]     ceed             `Ceed` object for error handling
287430da97aSJeremy L Thompson   @param[in]     source_file_path Absolute path to source file
288509d4af6SJeremy L Thompson   @param[in,out] num_file_paths   Number of files already included
289509d4af6SJeremy L Thompson   @param[in,out] file_paths       Paths of files already included
290430da97aSJeremy L Thompson   @param[out]    buffer           String buffer for source file contents
291430da97aSJeremy L Thompson 
292430da97aSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
293430da97aSJeremy L Thompson 
294430da97aSJeremy L Thompson   @ref Backend
295430da97aSJeremy L Thompson **/
296509d4af6SJeremy L Thompson int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
297509d4af6SJeremy L Thompson   // Ensure defaults were set
298509d4af6SJeremy L Thompson   *num_file_paths = 0;
299509d4af6SJeremy L Thompson   *file_paths     = NULL;
300430da97aSJeremy L Thompson 
301509d4af6SJeremy L Thompson   // Initialize
302509d4af6SJeremy L Thompson   CeedCall(CeedCalloc(1, buffer));
303509d4af6SJeremy L Thompson 
304509d4af6SJeremy L Thompson   // And load source
305509d4af6SJeremy L Thompson   CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer));
306430da97aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
307430da97aSJeremy L Thompson }
308430da97aSJeremy L Thompson 
309430da97aSJeremy L Thompson /**
310ea61e9acSJeremy L Thompson   @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`.
311509d4af6SJeremy L Thompson     User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer.
312509d4af6SJeremy L Thompson     Caller is responsible for freeing the string buffer with @ref CeedFree().
3133d3250a0SJeremy L Thompson 
314ca94c3ddSJeremy L Thompson   @param[in]  ceed             `Ceed` object for error handling
3153d3250a0SJeremy L Thompson   @param[in]  source_file_path Absolute path to source file
3163d3250a0SJeremy L Thompson   @param[out] buffer           String buffer for source file contents
3173d3250a0SJeremy L Thompson 
3183d3250a0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3193d3250a0SJeremy L Thompson 
3203d3250a0SJeremy L Thompson   @ref Backend
3213d3250a0SJeremy L Thompson **/
3222b730f8bSJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
323509d4af6SJeremy L Thompson   char  **file_paths     = NULL;
324509d4af6SJeremy L Thompson   CeedInt num_file_paths = 0;
3253d3250a0SJeremy L Thompson 
326509d4af6SJeremy L Thompson   // Load
327509d4af6SJeremy L Thompson   CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer));
328509d4af6SJeremy L Thompson 
329509d4af6SJeremy L Thompson   // Cleanup
330509d4af6SJeremy L Thompson   for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
331509d4af6SJeremy L Thompson   CeedCall(CeedFree(&file_paths));
3323d3250a0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3333d3250a0SJeremy L Thompson }
334437930d1SJeremy L Thompson 
335437930d1SJeremy L Thompson /**
336437930d1SJeremy L Thompson   @brief Build an absolute filepath from a base filepath and an absolute filepath.
3374385fb7fSSebastian Grimberg 
338ca94c3ddSJeremy L Thompson   This helps construct source file paths for @ref CeedLoadSourceToBuffer().
3394385fb7fSSebastian Grimberg 
340ca94c3ddSJeremy L Thompson   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
341437930d1SJeremy L Thompson 
342ca94c3ddSJeremy L Thompson   @param[in]  ceed               `Ceed` object for error handling
343437930d1SJeremy L Thompson   @param[in]  base_file_path     Absolute path to current file
344437930d1SJeremy L Thompson   @param[in]  relative_file_path Relative path to target file
345437930d1SJeremy L Thompson   @param[out] new_file_path      String buffer for absolute path to target file
346437930d1SJeremy L Thompson 
347437930d1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
348437930d1SJeremy L Thompson 
349437930d1SJeremy L Thompson   @ref Backend
350437930d1SJeremy L Thompson **/
3512b730f8bSJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) {
352437930d1SJeremy L Thompson   char  *last_slash  = strrchr(base_file_path, '/');
3532b730f8bSJeremy L Thompson   size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path),
354437930d1SJeremy L Thompson          new_file_path_length = base_length + relative_length + 1;
355437930d1SJeremy L Thompson 
3562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(new_file_path_length, new_file_path));
357d602d780SJeremy L Thompson   memcpy(*new_file_path, base_file_path, base_length);
358d602d780SJeremy L Thompson   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
359437930d1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
360437930d1SJeremy L Thompson }
3616eb0d8b4SJeremy L Thompson 
3626eb0d8b4SJeremy L Thompson /**
363032e71eaSJeremy L Thompson   @brief Find the relative filepath to an installed JiT file
3646eb0d8b4SJeremy L Thompson 
365032e71eaSJeremy L Thompson   @param[in]  absolute_file_path Absolute path to installed JiT file
36691e18578SJeremy L Thompson   @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path
3676eb0d8b4SJeremy L Thompson 
3686eb0d8b4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3696eb0d8b4SJeremy L Thompson 
3706eb0d8b4SJeremy L Thompson   @ref Backend
3716eb0d8b4SJeremy L Thompson **/
3722b730f8bSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) {
373032e71eaSJeremy L Thompson   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
3746574a04fSJeremy L Thompson   CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path);
3756eb0d8b4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3766eb0d8b4SJeremy L Thompson }
377032e71eaSJeremy L Thompson 
378032e71eaSJeremy L Thompson /**
379032e71eaSJeremy L Thompson   @brief Build an absolute filepath to a JiT file
380032e71eaSJeremy L Thompson 
381ca94c3ddSJeremy L Thompson   @param[in]  ceed               `Ceed` object for error handling
382032e71eaSJeremy L Thompson   @param[in]  relative_file_path Relative path to installed JiT file
38391e18578SJeremy L Thompson   @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller
384032e71eaSJeremy L Thompson 
385032e71eaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
386032e71eaSJeremy L Thompson 
387032e71eaSJeremy L Thompson   @ref Backend
388032e71eaSJeremy L Thompson **/
389f8608ea8SJed Brown int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) {
3906155f12fSJeremy L Thompson   Ceed ceed_parent;
391032e71eaSJeremy L Thompson 
392032e71eaSJeremy L Thompson   // Debug
39323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
39423d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: ");
39513f886e9SJeremy L Thompson   CeedDebug(ceed, "%s\n", relative_file_path);
396032e71eaSJeremy L Thompson 
3972b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(ceed, &ceed_parent));
3986155f12fSJeremy L Thompson   for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) {
399ee5a26f2SJeremy L Thompson     bool is_valid;
400ee5a26f2SJeremy L Thompson 
401032e71eaSJeremy L Thompson     // Debug
40223d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: ");
40313f886e9SJeremy L Thompson     CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]);
404032e71eaSJeremy L Thompson 
405ee5a26f2SJeremy L Thompson     // Build and check absolute path with current root
40622070f95SJeremy L Thompson     CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, (char **)absolute_file_path));
4072b730f8bSJeremy L Thompson     CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid));
408032e71eaSJeremy L Thompson 
4092b730f8bSJeremy L Thompson     if (is_valid) return CEED_ERROR_SUCCESS;
41091e18578SJeremy L Thompson     // LCOV_EXCL_START
4112b730f8bSJeremy L Thompson     else CeedCall(CeedFree(absolute_file_path));
41291e18578SJeremy L Thompson     // LCOV_EXCL_STOP
413032e71eaSJeremy L Thompson   }
414032e71eaSJeremy L Thompson   // LCOV_EXCL_START
4152b730f8bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path);
416032e71eaSJeremy L Thompson   // LCOV_EXCL_STOP
417032e71eaSJeremy L Thompson }
418