xref: /libCEED/interface/ceed-jit-tools.c (revision ee5a26f213b810c411fd8b16edb331d78b70003c)
1 // Copyright (c) 2017-2022, 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/ceed.h>
9 #include <ceed/backend.h>
10 #include <ceed/jit-tools.h>
11 #include <ceed-impl.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 ceed                  A 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 opend
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   int ierr;
29 
30   // Sometimes we have path/to/file.h:function_name
31   // Create tempory file path without name, if needed
32   char *source_file_path_only;
33   char *last_colon = strrchr(source_file_path, ':');
34   if (last_colon) {
35     size_t source_file_path_length = (last_colon - source_file_path + 1);
36 
37     ierr = CeedCalloc(source_file_path_length, &source_file_path_only);
38     CeedChk(ierr);
39     strncpy(source_file_path_only, source_file_path, source_file_path_length - 1);
40   } else {
41     source_file_path_only = (char *)source_file_path;
42   }
43 
44   // Debug
45   CeedDebug256(ceed, 1, "Checking for source file: ");
46   CeedDebug256(ceed, 255, "%s\n", source_file_path_only);
47 
48   // Check for valid file path
49   FILE *source_file;
50   source_file = fopen(source_file_path_only, "rb");
51   *is_valid = !!source_file;
52 
53   if (*is_valid) {
54     // Debug
55     CeedDebug256(ceed, 1, "Found JiT source file: ");
56     CeedDebug256(ceed, 255, "%s\n", source_file_path_only);
57 
58     fclose(source_file);
59   }
60 
61   // Free temp file path, if used
62   if (last_colon) {
63     ierr = CeedFree(&source_file_path_only); CeedChk(ierr);
64   }
65 
66   return CEED_ERROR_SUCCESS;
67 }
68 
69 /**
70   @brief Load source file into initalized string buffer, including full text
71            of local files in place of `#include "local.h"`
72 
73   @param ceed                  A Ceed object for error handling
74   @param[in]  source_file_path Absolute path to source file
75   @param[out] buffer           String buffer for source file contents
76 
77   @return An error code: 0 - success, otherwise - failure
78 
79   @ref Backend
80 **/
81 static inline int CeedLoadSourceToInitalizedBuffer(Ceed ceed,
82     const char *source_file_path, char **buffer) {
83   int ierr;
84   FILE *source_file;
85   long file_size, file_offset = 0;
86   char *temp_buffer;
87 
88   // Debug
89   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
90   CeedDebug256(ceed, 1, "Current source file: ");
91   CeedDebug256(ceed, 255, "%s\n", source_file_path);
92   CeedDebug256(ceed, 1, "Current buffer:\n");
93   CeedDebug256(ceed, 255, "%s\n", *buffer);
94 
95   // Read file to temporary buffer
96   source_file = fopen(source_file_path, "rb");
97   if (!source_file)
98     // LCOV_EXCL_START
99     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s",
100                      source_file_path);
101   // LCOV_EXCL_STOP
102   // -- Compute size of source
103   fseek(source_file, 0L, SEEK_END);
104   file_size = ftell(source_file);
105   rewind(source_file);
106   //  -- Allocate memory for entire source file
107   ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr);
108   // -- Copy the file into the buffer
109   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
110     // LCOV_EXCL_START
111     fclose(source_file);
112     ierr = CeedFree(&temp_buffer); CeedChk(ierr);
113     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s",
114                      source_file_path);
115     // LCOV_EXCL_STOP
116   }
117   fclose(source_file);
118 
119   // Search for headers to include
120   const char *first_hash = strchr(temp_buffer, '#');
121   while (first_hash) {
122     // -- Check for 'include' keyword
123     const char *next_e = strchr(first_hash, 'e');
124     char keyword[8] = "";
125     if (next_e)
126       strncpy(keyword, &next_e[-6], 7);
127     bool is_hash_include = !strcmp(keyword, "include");
128     // ---- Spaces allowed in '#  include <header.h>'
129     if (next_e)
130       for (CeedInt i = 1; first_hash - next_e + i < -6; i++)
131         is_hash_include &= first_hash[i] == ' ';
132     if (is_hash_include) {
133       // -- Copy into buffer all preceding #
134       long current_size = strlen(*buffer);
135       long copy_size = first_hash - &temp_buffer[file_offset];
136       ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
137       strncpy(&(*buffer)[current_size], "\n", 2);
138       strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
139       strncpy(&(*buffer)[current_size + copy_size], "", 1);
140       // -- Load local "header.h"
141       char *next_quote = strchr(first_hash, '"');
142       char *next_new_line = strchr(first_hash, '\n');
143       bool is_local_header = is_hash_include && next_quote
144                              && (next_new_line - next_quote > 0);
145       if (is_local_header) {
146         // ---- Build source path
147         char *include_source_path;
148         long root_length = strrchr(source_file_path, '/') - source_file_path;
149         long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
150         ierr = CeedCalloc(root_length + include_file_name_len + 2,
151                           &include_source_path); CeedChk(ierr);
152         strncpy(include_source_path, source_file_path, root_length + 1);
153         strncpy(&include_source_path[root_length + 1], &next_quote[1],
154                 include_file_name_len);
155         strncpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
156         // ---- Recursive call to load source to buffer
157         ierr = CeedLoadSourceToInitalizedBuffer(ceed, include_source_path, buffer);
158         CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path);
159         CeedChk(ierr);
160         ierr = CeedFree(&include_source_path); CeedChk(ierr);
161       }
162       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
163     }
164     // -- Next hash
165     first_hash = strchr(&first_hash[1], '#');
166   }
167   // Copy rest of source file into buffer
168   long current_size = strlen(*buffer);
169   long copy_size = strlen(&temp_buffer[file_offset]);
170   ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
171   strncpy(&(*buffer)[current_size], "\n", 2);
172   strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
173   strncpy(&(*buffer)[current_size + copy_size + 1], "", 1);
174 
175   // Cleanup
176   ierr = CeedFree(&temp_buffer); CeedChk(ierr);
177 
178   // Debug
179   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
180   CeedDebug256(ceed, 1, "Current source file: ");
181   CeedDebug256(ceed, 255, "%s\n", source_file_path);
182   CeedDebug256(ceed, 1, "Final buffer:\n");
183   CeedDebug256(ceed, 255, "%s\n", *buffer);
184 
185   return CEED_ERROR_SUCCESS;
186 }
187 
188 /**
189   @brief Initalize and load source file into string buffer, including full text
190            of local files in place of `#include "local.h"`.
191          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
192 
193   @param ceed                  A Ceed object for error handling
194   @param[in]  source_file_path Absolute path to source file
195   @param[out] buffer           String buffer for source file contents
196 
197   @return An error code: 0 - success, otherwise - failure
198 
199   @ref Backend
200 **/
201 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path,
202                            char **buffer) {
203   int ierr;
204 
205   // Initalize buffer
206   ierr = CeedCalloc(1, buffer); CeedChk(ierr);
207 
208   // Load to initalized buffer
209   ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer);
210   CeedChk(ierr);
211 
212   return CEED_ERROR_SUCCESS;
213 }
214 
215 /**
216   @brief Build an absolute filepath from a base filepath and an absolute filepath.
217            This helps construct source file paths for `CeedLoadSourceToBuffer()`.
218          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
219 
220   @param ceed                     A Ceed object for error handling
221   @param[in]  base_file_path      Absolute path to current file
222   @param[in]  relative_file_path  Relative path to target file
223   @param[out] new_file_path       String buffer for absolute path to target file
224 
225   @return An error code: 0 - success, otherwise - failure
226 
227   @ref Backend
228 **/
229 int CeedPathConcatenate(Ceed ceed, const char *base_file_path,
230                         const char *relative_file_path, char **new_file_path) {
231   int ierr;
232   char *last_slash = strrchr(base_file_path, '/');
233   size_t base_length = (last_slash - base_file_path + 1),
234          relative_length = strlen(relative_file_path),
235          new_file_path_length = base_length + relative_length + 1;
236 
237   ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr);
238   strncpy(*new_file_path, base_file_path, base_length);
239   strncpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
240 
241   return CEED_ERROR_SUCCESS;
242 }
243 
244 /**
245   @brief Find the relative filepath to an installed JiT file
246 
247   @param[in]  absolute_file_path Absolute path to installed JiT file
248   @param[out] relative_file_path Relative path to installed JiT file
249 
250   @return An error code: 0 - success, otherwise - failure
251 
252   @ref Backend
253 **/
254 int CeedGetJitRelativePath(const char *absolute_file_path,
255                            const char **relative_file_path) {
256   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
257 
258   if (!*relative_file_path)
259     // LCOV_EXCL_START
260     return CeedError(NULL, CEED_ERROR_MAJOR,
261                      "Couldn't find relative path including "
262                      "'ceed/jit-source' for: %s", absolute_file_path);
263   // LCOV_EXCL_STOP
264 
265   return CEED_ERROR_SUCCESS;
266 }
267 
268 /**
269   @brief Build an absolute filepath to a JiT file
270 
271   @param ceed                    A Ceed object for error handling
272   @param[in]  relative_file_path Relative path to installed JiT file
273   @param[out] absolute_file_path String buffer for absolute path to target file
274 
275   @return An error code: 0 - success, otherwise - failure
276 
277   @ref Backend
278 **/
279 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path,
280                            char **absolute_file_path) {
281   int ierr;
282 
283   // Debug
284   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
285   CeedDebug256(ceed, 1, "Relative JiT source file: ");
286   CeedDebug256(ceed, 255, "%s\n", relative_file_path);
287 
288 
289   for (CeedInt i = 0; i < ceed->num_jit_source_roots; i++) {
290     bool is_valid;
291 
292     // Debug
293     CeedDebug256(ceed, 1, "Checking JiT root: ");
294     CeedDebug256(ceed, 255, "%s\n", ceed->jit_source_roots[i]);
295 
296     // Build  and check absolute path with current root
297     ierr = CeedPathConcatenate(ceed, ceed->jit_source_roots[i],
298                                relative_file_path, absolute_file_path);
299     CeedChk(ierr);
300     ierr = CeedCheckFilePath(ceed, *absolute_file_path, &is_valid); CeedChk(ierr);
301 
302     if (is_valid) {
303       return CEED_ERROR_SUCCESS;
304     } else {
305       ierr = CeedFree(absolute_file_path); CeedChk(ierr);
306     }
307   }
308 
309   // LCOV_EXCL_START
310   return CeedError(ceed, CEED_ERROR_MAJOR,
311                    "Couldn't find matching JiT source file: %s",
312                    relative_file_path);
313   // LCOV_EXCL_STOP
314 }
315