Optionally search relative to the current source file to find headers.

This commit is contained in:
Curle 2023-12-06 18:21:31 +00:00
parent bc787c3adb
commit bfebf647eb
2 changed files with 26 additions and 3 deletions

View File

@ -12,6 +12,10 @@
#include <limits.h>
#include <sys/stat.h>
#if defined(__GNUC__) || defined(APPLE)
#include <errno.h>
#endif
/**
* The function of the importer is to read in definitions from a file, and store
* them into the symbol tables.
@ -50,6 +54,10 @@
// Read in the string that we know must be there.
char* Module = strdup(CurrentIdentifier);
// Two strategies for finding the module; either it's relative to cwd, or it's relative to source.
bool FoundImport = false;
// Check the cwd first.
// Figure out the working directory
char CWD[PATH_MAX];
@ -65,8 +73,23 @@
// Stat the file to see if it exists
struct stat FileInfo;
if (stat(ModulePath, &FileInfo) != 0)
DieMessage("Unable to access the imported module", ModulePath);
if (stat(ModulePath, &FileInfo) != 0) {
free(ModulePath);
char SourcePath[PATH_MAX + 1];
realpath(CurrentFile->SourceName, SourcePath);
char* SourceFolderLength = strrchr(SourcePath, '/');
*(SourceFolderLength + 1) = '\0';
size_t SourcePathLength = strlen(SourcePath);
ModulePath = malloc(SourcePathLength + sizeof(Module) + 1);
strcpy(ModulePath, SourcePath);
strcpy(ModulePath + SourcePathLength, Module);
printf("Scanning %s for module definitions.\n", ModulePath);
if (stat(ModulePath, &FileInfo) != 0)
DieMessage("Unable to access the imported module", ModulePath);
}
// At this point, the file exists and we have the path.
// Save the current file, so that we can restore it later

View File

@ -1,4 +1,4 @@
import "tests/import/defs.eh"
import "import/defs.eh"
long num[100];
int :: main() {