xref: /libCEED/interface/ceed-jit-tools.c (revision bef6543fdd46db23de9cc545e1f4ad88e3fc2e5d)
1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <ceed/jit-tools.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 /**
17   @brief Check if valid file exists at path given
18 
19   @param[in]  ceed             `Ceed` object for error handling
20   @param[in]  source_file_path Absolute path to source file
21   @param[out] is_valid         Boolean flag indicating if file can be opened
22 
23   @return An error code: 0 - success, otherwise - failure
24 
25   @ref Backend
26 **/
27 int CeedCheckFilePath(Ceed ceed, const char *source_file_path, bool *is_valid) {
28   // Sometimes we have path/to/file.h:function_name
29   // Create temporary file path without name, if needed
30   char *source_file_path_only;
31   char *last_colon = strrchr(source_file_path, ':');
32 
33   if (last_colon) {
34     size_t source_file_path_length = (last_colon - source_file_path + 1);
35 
36     CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only));
37     memcpy(source_file_path_only, source_file_path, source_file_path_length - 1);
38   } else {
39     source_file_path_only = (char *)source_file_path;
40   }
41 
42   // Debug
43   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking for source file: ");
44   CeedDebug(ceed, "%s\n", source_file_path_only);
45 
46   // Check for valid file path
47   FILE *source_file;
48   source_file = fopen(source_file_path_only, "rb");
49   *is_valid   = source_file;
50 
51   if (*is_valid) {
52     // Debug
53     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Found JiT source file: ");
54     CeedDebug(ceed, "%s\n", source_file_path_only);
55     fclose(source_file);
56   }
57 
58   // Free temp file path, if used
59   if (last_colon) CeedCall(CeedFree(&source_file_path_only));
60   return CEED_ERROR_SUCCESS;
61 }
62 
63 /**
64   @brief Normalize a filepath
65 
66   @param[in]   ceed                        `Ceed` object for error handling
67   @param[in]   source_file_path            Absolute path to source file
68   @param[out]  normalized_source_file_path Normalized filepath
69 
70   @return An error code: 0 - success, otherwise - failure
71 
72   @ref Backend
73 **/
74 static int CeedNormalizePath(Ceed ceed, const char *source_file_path, char **normalized_source_file_path) {
75   CeedCall(CeedStringAllocCopy(source_file_path, normalized_source_file_path));
76 
77   char *first_dot = strchr(*normalized_source_file_path, '.');
78 
79   while (first_dot) {
80     char *search_from = first_dot + 1;
81     char  keyword[5]  = "";
82 
83     // -- Check for /./ and covert to /
84     if (first_dot != *normalized_source_file_path && strlen(first_dot) > 2) memcpy(keyword, &first_dot[-1], 3);
85     bool is_here = !strcmp(keyword, "/./");
86 
87     if (is_here) {
88       for (CeedInt i = 0; first_dot[i - 1]; i++) first_dot[i] = first_dot[i + 2];
89       search_from = first_dot;
90     } else {
91       // -- Check for /foo/../ and convert to /
92       if (first_dot != *normalized_source_file_path && strlen(first_dot) > 3) memcpy(keyword, &first_dot[-1], 4);
93       bool is_up_one = !strcmp(keyword, "/../");
94 
95       if (is_up_one) {
96         char *last_slash = &first_dot[-2];
97 
98         while (last_slash[0] != '/' && last_slash != *normalized_source_file_path) last_slash--;
99         CeedCheck(last_slash != *normalized_source_file_path, ceed, CEED_ERROR_MAJOR, "Malformed source path %s", source_file_path);
100         for (CeedInt i = 0; first_dot[i + 1]; i++) last_slash[i] = first_dot[i + 2];
101         search_from = last_slash;
102       }
103     }
104     first_dot = strchr(search_from, '.');
105   }
106   return CEED_ERROR_SUCCESS;
107 }
108 
109 /**
110   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
111     This also updates the `num_file_paths` and `source_file_paths`.
112     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
113 
114   @param[in]     ceed             `Ceed` object for error handling
115   @param[in]     source_file_path Absolute path to source file
116   @param[in,out] num_file_paths   Number of files already included
117   @param[in,out] file_paths       Paths of files already included
118   @param[out]    buffer           String buffer for source file contents
119 
120   @return An error code: 0 - success, otherwise - failure
121 
122   @ref Backend
123 **/
124 int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
125   FILE *source_file;
126   long  file_size, file_offset = 0;
127   char *temp_buffer;
128 
129   // Debug
130   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
131   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
132   CeedDebug(ceed, "%s\n", source_file_path);
133 
134   // Read file to temporary buffer
135   source_file = fopen(source_file_path, "rb");
136   CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path);
137   // -- Compute size of source
138   fseek(source_file, 0L, SEEK_END);
139   file_size = ftell(source_file);
140   rewind(source_file);
141   //  -- Allocate memory for entire source file
142   CeedCall(CeedCalloc(file_size + 1, &temp_buffer));
143   // -- Copy the file into the buffer
144   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
145     // LCOV_EXCL_START
146     fclose(source_file);
147     CeedCall(CeedFree(&temp_buffer));
148     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path);
149     // LCOV_EXCL_STOP
150   }
151   fclose(source_file);
152 
153   // Search for headers to include
154   const char *first_hash = strchr(temp_buffer, '#');
155 
156   while (first_hash) {
157     // -- Check for 'pragma' keyword
158     const char *next_m     = strchr(first_hash, 'm');
159     char        keyword[8] = "";
160 
161     if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6);
162     bool is_hash_pragma = !strcmp(keyword, "pragma");
163 
164     // ---- Spaces allowed in '#  pragma'
165     if (next_m) {
166       for (CeedInt i = 1; first_hash - next_m + i < -5; i++) {
167         is_hash_pragma &= first_hash[i] == ' ';
168       }
169     }
170     if (is_hash_pragma) {
171       // -- Check if '#pragma once'
172       char *next_o         = strchr(first_hash, 'o');
173       char *next_new_line  = strchr(first_hash, '\n');
174       bool  is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4);
175 
176       // -- Copy into buffer, omitting last line if #pragma once
177       long current_size = strlen(*buffer);
178       long copy_size    = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1));
179 
180       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
181       memcpy(&(*buffer)[current_size], "\n", 2);
182       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
183       memcpy(&(*buffer)[current_size + copy_size], "", 1);
184 
185       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
186     }
187 
188     // -- Check for 'include' keyword
189     const char *next_e = strchr(first_hash, 'e');
190 
191     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
192     bool is_hash_include = !strcmp(keyword, "include");
193 
194     // ---- Spaces allowed in '#  include <header.h>'
195     if (next_e) {
196       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
197         is_hash_include &= first_hash[i] == ' ';
198       }
199     }
200     if (is_hash_include) {
201       // -- Copy into buffer all preceding #
202       long current_size = strlen(*buffer);
203       long copy_size    = first_hash - &temp_buffer[file_offset];
204 
205       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
206       memcpy(&(*buffer)[current_size], "\n", 2);
207       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
208       memcpy(&(*buffer)[current_size + copy_size], "", 1);
209       // -- Load local "header.h"
210       char *next_quote        = strchr(first_hash, '"');
211       char *next_new_line     = strchr(first_hash, '\n');
212       bool  is_local_header   = is_hash_include && next_quote && (next_new_line - next_quote > 0);
213       char *next_left_chevron = strchr(first_hash, '<');
214       bool  is_ceed_header    = next_left_chevron && (next_new_line - next_left_chevron > 0) &&
215                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
216                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
217       bool is_std_header =
218           next_left_chevron && (next_new_line - next_left_chevron > 0) &&
219           (!strncmp(next_left_chevron, "<std", 4) || !strncmp(next_left_chevron, "<math.h>", 8) || !strncmp(next_left_chevron, "<ceed", 5));
220 
221       if (is_local_header || is_ceed_header) {
222         // ---- Build source path
223         bool  is_included = false;
224         char *include_source_path;
225 
226         if (is_local_header) {
227           long root_length           = strrchr(source_file_path, '/') - source_file_path;
228           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
229 
230           CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path));
231           memcpy(include_source_path, source_file_path, root_length + 1);
232           memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len);
233           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
234         } else {
235           char *next_right_chevron = strchr(first_hash, '>');
236           char *ceed_relative_path;
237           long  ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
238 
239           CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path));
240           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
241           CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path));
242           CeedCall(CeedFree(&ceed_relative_path));
243         }
244         // ---- Recursive call to load source to buffer
245         char *normalized_include_source_path;
246 
247         CeedCall(CeedNormalizePath(ceed, include_source_path, &normalized_include_source_path));
248         for (CeedInt i = 0; i < *num_file_paths; i++) is_included |= !strcmp(normalized_include_source_path, (*file_paths)[i]);
249         if (!is_included) {
250           CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", normalized_include_source_path);
251           CeedCall(CeedLoadSourceToInitializedBuffer(ceed, normalized_include_source_path, num_file_paths, file_paths, buffer));
252           CeedCall(CeedRealloc(*num_file_paths + 1, file_paths));
253           CeedCall(CeedStringAllocCopy(normalized_include_source_path, &(*file_paths)[*num_file_paths]));
254           (*num_file_paths)++;
255         }
256         CeedCall(CeedFree(&include_source_path));
257         CeedCall(CeedFree(&normalized_include_source_path));
258       } else if (!is_std_header) {
259         long header_copy_size = next_new_line - first_hash + 1;
260 
261         CeedCall(CeedRealloc(current_size + copy_size + header_copy_size + 2, buffer));
262         memcpy(&(*buffer)[current_size + copy_size], "\n", 2);
263         memcpy(&(*buffer)[current_size + copy_size + 1], first_hash, header_copy_size);
264         memcpy(&(*buffer)[current_size + copy_size + header_copy_size], "", 1);
265       }
266       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
267     }
268     // -- Next hash
269     first_hash = strchr(&first_hash[1], '#');
270   }
271   // Copy rest of source file into buffer
272   long current_size = strlen(*buffer);
273   long copy_size    = strlen(&temp_buffer[file_offset]);
274 
275   CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
276   memcpy(&(*buffer)[current_size], "\n", 2);
277   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
278   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
279 
280   // Cleanup
281   CeedCall(CeedFree(&temp_buffer));
282 
283   // Debug
284   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
285   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
286   CeedDebug(ceed, "%s\n", source_file_path);
287   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n");
288   CeedDebug(ceed, "%s\n", *buffer);
289   return CEED_ERROR_SUCCESS;
290 }
291 
292 /**
293   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
294     This also initializes and populates the `num_file_paths` and `source_file_paths`.
295     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
296 
297   @param[in]     ceed             `Ceed` object for error handling
298   @param[in]     source_file_path Absolute path to source file
299   @param[in,out] num_file_paths   Number of files already included
300   @param[in,out] file_paths       Paths of files already included
301   @param[out]    buffer           String buffer for source file contents
302 
303   @return An error code: 0 - success, otherwise - failure
304 
305   @ref Backend
306 **/
307 int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
308   // Ensure defaults were set
309   *num_file_paths = 0;
310   *file_paths     = NULL;
311 
312   // Initialize
313   CeedCall(CeedCalloc(1, buffer));
314 
315   // And load source
316   CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer));
317   return CEED_ERROR_SUCCESS;
318 }
319 
320 /**
321   @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`.
322     User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer.
323     Caller is responsible for freeing the string buffer with @ref CeedFree().
324 
325   @param[in]  ceed             `Ceed` object for error handling
326   @param[in]  source_file_path Absolute path to source file
327   @param[out] buffer           String buffer for source file contents
328 
329   @return An error code: 0 - success, otherwise - failure
330 
331   @ref Backend
332 **/
333 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
334   char  **file_paths     = NULL;
335   CeedInt num_file_paths = 0;
336 
337   // Load
338   CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer));
339 
340   // Cleanup
341   for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
342   CeedCall(CeedFree(&file_paths));
343   return CEED_ERROR_SUCCESS;
344 }
345 
346 /**
347   @brief Build an absolute filepath from a base filepath and an absolute filepath.
348 
349   This helps construct source file paths for @ref CeedLoadSourceToBuffer().
350 
351   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
352 
353   @param[in]  ceed               `Ceed` object for error handling
354   @param[in]  base_file_path     Absolute path to current file
355   @param[in]  relative_file_path Relative path to target file
356   @param[out] new_file_path      String buffer for absolute path to target file
357 
358   @return An error code: 0 - success, otherwise - failure
359 
360   @ref Backend
361 **/
362 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) {
363   char  *last_slash  = strrchr(base_file_path, '/');
364   size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path),
365          new_file_path_length = base_length + relative_length + 1;
366 
367   CeedCall(CeedCalloc(new_file_path_length, new_file_path));
368   memcpy(*new_file_path, base_file_path, base_length);
369   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
370   return CEED_ERROR_SUCCESS;
371 }
372 
373 /**
374   @brief Find the relative filepath to an installed JiT file
375 
376   @param[in]  absolute_file_path Absolute path to installed JiT file
377   @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path
378 
379   @return An error code: 0 - success, otherwise - failure
380 
381   @ref Backend
382 **/
383 int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) {
384   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
385   CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path);
386   return CEED_ERROR_SUCCESS;
387 }
388 
389 /**
390   @brief Build an absolute filepath to a JiT file
391 
392   @param[in]  ceed               `Ceed` object for error handling
393   @param[in]  relative_file_path Relative path to installed JiT file
394   @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller
395 
396   @return An error code: 0 - success, otherwise - failure
397 
398   @ref Backend
399 **/
400 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) {
401   const char **jit_source_dirs;
402   CeedInt      num_source_dirs;
403 
404   // Debug
405   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
406   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: ");
407   CeedDebug(ceed, "%s\n", relative_file_path);
408 
409   CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_source_dirs, &jit_source_dirs));
410   for (CeedInt i = 0; i < num_source_dirs; i++) {
411     bool is_valid;
412 
413     // Debug
414     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: ");
415     CeedDebug(ceed, "%s\n", jit_source_dirs[i]);
416 
417     // Build and check absolute path with current root
418     CeedCall(CeedPathConcatenate(ceed, jit_source_dirs[i], relative_file_path, (char **)absolute_file_path));
419     CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid));
420 
421     if (is_valid) {
422       CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs));
423       return CEED_ERROR_SUCCESS;
424     }
425     // LCOV_EXCL_START
426     else
427       CeedCall(CeedFree(absolute_file_path));
428     // LCOV_EXCL_STOP
429   }
430   // LCOV_EXCL_START
431   return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path);
432   // LCOV_EXCL_STOP
433 }
434