Skip to content

Commit

Permalink
backend: Allow local builds to use the src backend_default_config.json
Browse files Browse the repository at this point in the history
If an application links to a local install of libpisp.so, the backend
will fail to find backend_default_config.json in the data install path.

Fix this by testing if libpisp is installed, and if not, use the build
path to find backend_default_config.json, which is now copied to the
specific location by the build scripts.

This functionality needs linkage with libdl as well as a custom rpath
that gets stripped with a meson install step.

Signed-off-by: Naushir Patuck <[email protected]>
  • Loading branch information
naushir committed Jan 12, 2024
1 parent 5da8c47 commit e46acc1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
55 changes: 53 additions & 2 deletions src/libpisp/backend/backend_default_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*/
#include "backend.hpp"

#include <dlfcn.h>
#include <elf.h>
#include <fstream>
#include <link.h>
#include <string>
#include <vector>

Expand All @@ -25,6 +28,40 @@ using json = nlohmann::json;
namespace
{

// Check if the RPATH or RUNPATH definitions exist in the ELF file. If they don't exist, it means that meson has
// stripped them out when doing the install step.
//
// Source: https://stackoverflow.com/questions/2836330/is-there-a-programmatic-way-to-inspect-the-current-rpath-on-linux
bool is_installed()
{
const ElfW(Dyn) *dyn = _DYNAMIC;
for (; dyn->d_tag != DT_NULL; dyn++)
{
if (dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH)
return false;
}
return true;
}

std::string source_path()
{
Dl_info dl_info;
std::string path;

if (dladdr(reinterpret_cast<void *>(source_path), &dl_info))
path = dl_info.dli_fname;

if (path.empty())
return {};

auto const pos = path.find_last_of('/');
if (pos == std::string::npos)
return {};

path.erase(pos, path.length() - pos);
return path;
}

void initialise_debin(pisp_be_debin_config &debin, const json &root)
{
constexpr unsigned int num_coefs = sizeof(debin.coeffs) / sizeof(debin.coeffs[0]);
Expand Down Expand Up @@ -269,8 +306,22 @@ void BackEnd::InitialiseSharpen(pisp_be_sharpen_config &sharpen, pisp_be_sh_fc_c

void BackEnd::initialiseDefaultConfig(const std::string &filename)
{
std::string file = filename.empty() ? std::string(PISP_BE_CONFIG_DIR) + "/" + "backend_default_config.json"
: filename;
std::string file(filename);

if (file.empty())
{
if (!is_installed())
{
std::string path = source_path();
if (path.empty())
throw std::runtime_error("BE: Could not determine the local source path");

file = path + "/libpisp/backend/backend_default_config.json";
}
else
file = std::string(PISP_BE_CONFIG_DIR) + "/" + "backend_default_config.json";
}

std::ifstream ifs(file);
if (!ifs.good())
throw std::runtime_error("BE: Could not find config json file: " + file);
Expand Down
8 changes: 8 additions & 0 deletions src/libpisp/backend/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ install_headers(backend_headers, subdir: backend_include_dir)
install_data('backend_default_config.json',
install_dir : config_install_dir)

# Copy the json config file to the build directory for running locally.
custom_target('Default config to build dir',
input : 'backend_default_config.json',
output : 'backend_default_config.json',
command : ['cp', '@INPUT@', '@OUTPUT@'],
install : false,
build_by_default : true)

subdir('tiling')
4 changes: 3 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pisp_sources += backend_sources

pisp_deps = [
dependency('nlohmann_json', fallback : ['nlohmann_json', 'nlohmann_json_dep']),
dependency('threads')
dependency('threads'),
dependency('dl'),
]

logging_dep = dependency('boost', modules : ['log', 'log_setup', 'thread', 'system'], required : get_option('logging'))
Expand Down Expand Up @@ -54,6 +55,7 @@ libpisp = library(
include_directories : include_directories(inc_dirs),
name_prefix : '',
install : true,
build_rpath : meson.project_source_root(),
dependencies : pisp_deps,
)

Expand Down

0 comments on commit e46acc1

Please sign in to comment.