52 lines
1.3 KiB
C
52 lines
1.3 KiB
C
// Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and
|
|
// other CEED contributors. All Rights Reserved.
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// Relocatable replacement for libCEED's installed JIT source-root object.
|
|
// Upstream normally compiles the installation prefix into libceed. Native
|
|
// source bundles here are shared libraries, so derive ../include from the
|
|
// loaded library instead. Emscripten has no runtime compiler/JIT filesystem.
|
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
const char *CeedJitSourceRootDefault = "";
|
|
|
|
#else
|
|
|
|
#ifndef _GNU_SOURCE
|
|
#define _GNU_SOURCE
|
|
#endif
|
|
|
|
#include <dlfcn.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifndef PATH_MAX
|
|
#define PATH_MAX 4096
|
|
#endif
|
|
|
|
static char ceed_jit_source_root[PATH_MAX];
|
|
const char *CeedJitSourceRootDefault = ceed_jit_source_root;
|
|
|
|
__attribute__((constructor)) static void CeedSetRelocatableJitSourceRoot(void) {
|
|
Dl_info info;
|
|
char library_path[PATH_MAX];
|
|
|
|
if (!dladdr((void *)&CeedJitSourceRootDefault, &info) || !info.dli_fname) {
|
|
return;
|
|
}
|
|
if (snprintf(library_path, sizeof library_path, "%s", info.dli_fname) < 0) {
|
|
return;
|
|
}
|
|
char *separator = strrchr(library_path, '/');
|
|
if (!separator) {
|
|
return;
|
|
}
|
|
*separator = '\0';
|
|
(void)snprintf(ceed_jit_source_root, sizeof ceed_jit_source_root,
|
|
"%s/../include/", library_path);
|
|
}
|
|
|
|
#endif
|