[libcamera-devel] [PATCH 05/16] libcamera: Separate source and build path helpers

Kieran Bingham kieran.bingham at ideasonboard.com
Fri Jun 25 16:16:03 CEST 2021


Hi Laurent,

On 25/06/2021 13:18, Laurent Pinchart wrote:
> Hi Kieran,
> 
> Thank you for the patch.
> 
> On Fri, Jun 25, 2021 at 02:35:28AM +0100, Kieran Bingham wrote:
>> The libcameraSourcePath and libcameraBuildPath helper functions are
>> internal and specific to libcamera needs while operating with the meson
>> build system.
>>
>> In preparation for the upcoming move of utils to a common library, move
>> these helpers out of utils and into their own build unit.
>>
>> Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
>> ---
>>  include/libcamera/internal/meson.build    |   1 +
>>  include/libcamera/internal/source-paths.h |  19 ++++
>>  src/libcamera/meson.build                 |   1 +
>>  src/libcamera/source-paths.cpp            | 129 ++++++++++++++++++++++
>>  src/libcamera/utils.cpp                   | 102 -----------------
>>  5 files changed, 150 insertions(+), 102 deletions(-)
>>  create mode 100644 include/libcamera/internal/source-paths.h
>>  create mode 100644 src/libcamera/source-paths.cpp
>>
>> diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
>> index f019cfb3b0de..e5088177fe8e 100644
>> --- a/include/libcamera/internal/meson.build
>> +++ b/include/libcamera/internal/meson.build
>> @@ -39,6 +39,7 @@ libcamera_internal_headers = files([
>>      'process.h',
>>      'pub_key.h',
>>      'semaphore.h',
>> +    'source-paths.h',
>>      'sysfs.h',
>>      'thread.h',
>>      'timer.h',
>> diff --git a/include/libcamera/internal/source-paths.h b/include/libcamera/internal/source-paths.h
>> new file mode 100644
>> index 000000000000..2591ebb1081b
>> --- /dev/null
>> +++ b/include/libcamera/internal/source-paths.h
>> @@ -0,0 +1,19 @@
>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
>> +/*
>> + * Copyright (C) 2021, Google Inc.
>> + *
>> + * source-paths.h - Identify libcamera source and build paths
>> + */
>> +#ifndef __LIBCAMERA_INTERNAL_SOURCE_PATHS_H__
>> +#define __LIBCAMERA_INTERNAL_SOURCE_PATHS_H__
>> +
>> +#include <string>
>> +
>> +namespace libcamera::utils {
>> +
>> +std::string libcameraBuildPath();
>> +std::string libcameraSourcePath();
>> +
>> +} /* namespace libcamera::utils */
>> +
>> +#endif /* __LIBCAMERA_INTERNAL_SOURCE_PATHS_H__ */
>> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
>> index 387d20843203..e792094f5798 100644
>> --- a/src/libcamera/meson.build
>> +++ b/src/libcamera/meson.build
>> @@ -46,6 +46,7 @@ libcamera_sources = files([
>>      'request.cpp',
>>      'semaphore.cpp',
>>      'signal.cpp',
>> +    'source-paths.cpp',
>>      'stream.cpp',
>>      'sysfs.cpp',
>>      'thread.cpp',
>> diff --git a/src/libcamera/source-paths.cpp b/src/libcamera/source-paths.cpp
>> new file mode 100644
>> index 000000000000..b39f5f853e39
>> --- /dev/null
>> +++ b/src/libcamera/source-paths.cpp
>> @@ -0,0 +1,129 @@
>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */
>> +/*
>> + * Copyright (C) 2021, Google Inc.
>> + *
>> + * source-paths.cpp - Identify libcamera source and build paths
>> + */
>> +
>> +#include "libcamera/internal/source-paths.h"
>> +
>> +#include <dlfcn.h>
>> +#include <elf.h>
>> +#include <link.h>
>> +#include <sys/stat.h>
>> +
>> +#include "libcamera/internal/utils.h"
>> +
>> +/**
>> + * \file source-paths.h
>> + * \brief Identify the build and source path of a not-yet-installed library
>> + */
>> +
>> +/* musl doesn't declare _DYNAMIC in link.h, declare it manually. */
>> +extern ElfW(Dyn) _DYNAMIC[];
>> +
>> +namespace libcamera::utils {
>> +
>> +/**
>> + * \brief Check if libcamera is installed or not
>> + *
>> + * Utilise the build_rpath dynamic tag which is stripped out by meson at
>> + * install time to determine at runtime if the library currently executing
>> + * has been installed or not.
>> + *
>> + * \return True if libcamera is installed, false otherwise
>> + */
>> +bool isLibcameraInstalled()
>> +{
>> +	/*
>> +	 * DT_RUNPATH (DT_RPATH when the linker uses old dtags) is removed on
>> +	 * install.
>> +	 */
>> +	for (const ElfW(Dyn) *dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn) {
>> +		if (dyn->d_tag == DT_RUNPATH || dyn->d_tag == DT_RPATH)
>> +			return false;
>> +	}
>> +
>> +	return true;
>> +}
>> +
>> +/**
>> + * \brief Retrieve the path to the build directory
>> + *
>> + * During development, it is useful to run libcamera binaries directly from the
>> + * build directory without installing them. This function helps components that
>> + * need to locate resources in the build tree, such as IPA modules or IPA proxy
>> + * workers, by providing them with the path to the root of the build directory.
>> + * Callers can then use it to complement or override searches in system-wide
>> + * directories.
>> + *
>> + * If libcamera has been installed, the build directory path is not available
>> + * and this function returns an empty string.
>> + *
>> + * \return The path to the build directory if running from a build, or an empty
>> + * string otherwise
>> + */
>> +std::string libcameraBuildPath()
>> +{
>> +	if (isLibcameraInstalled())
>> +		return std::string();
>> +
>> +	Dl_info info;
>> +
>> +	/* Look up our own symbol. */
>> +	int ret = dladdr(reinterpret_cast<void *>(libcameraBuildPath), &info);
>> +	if (ret == 0)
>> +		return std::string();
>> +
>> +	std::string path = dirname(info.dli_fname) + "/../../";
>> +
>> +	char *real = realpath(path.c_str(), nullptr);
>> +	if (!real)
>> +		return std::string();
>> +
>> +	path = real;
>> +	free(real);
>> +
>> +	return path + "/";
>> +}
>> +
>> +/**
>> + * \brief Retrieve the path to the source directory
>> + *
>> + * During development, it is useful to run libcamera binaries directly from the
>> + * build directory without installing them. This function helps components that
>> + * need to locate resources in the source tree, such as IPA configuration
>> + * files, by providing them with the path to the root of the source directory.
>> + * Callers can then use it to complement or override searches in system-wide
>> + * directories.
>> + *
>> + * If libcamera has been installed, the source directory path is not available
>> + * and this function returns an empty string.
>> + *
>> + * \return The path to the source directory if running from a build directory,
>> + * or an empty string otherwise
>> + */
>> +std::string libcameraSourcePath()
>> +{
>> +	std::string path = libcameraBuildPath();
>> +	if (path.empty())
>> +		return std::string();
>> +
>> +	path += "source";
>> +
>> +	char *real = realpath(path.c_str(), nullptr);
>> +	if (!real)
>> +		return std::string();
>> +
>> +	path = real;
>> +	free(real);
>> +
>> +	struct stat statbuf;
>> +	int ret = stat(path.c_str(), &statbuf);
> 
> The manpage of stat(à also lists sys/types.h as an include, and
> realpath() lists stdlib.h.
> 
>> +	if (ret < 0 || (statbuf.st_mode & S_IFMT) != S_IFDIR)
>> +		return std::string();
>> +
>> +	return path + "/";
>> +}
>> +
>> +} /* namespace libcamera::utils */
>> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
>> index 49b8fc9e211f..42f82d6158dd 100644
>> --- a/src/libcamera/utils.cpp
>> +++ b/src/libcamera/utils.cpp
>> @@ -351,108 +351,6 @@ std::string toAscii(const std::string &str)
>>  	return ret;
>>  }
>>  
>> -/**
>> - * \brief Check if libcamera is installed or not
>> - *
>> - * Utilise the build_rpath dynamic tag which is stripped out by meson at
>> - * install time to determine at runtime if the library currently executing
>> - * has been installed or not.
>> - *
>> - * \return True if libcamera is installed, false otherwise
>> - */
>> -bool isLibcameraInstalled()
>> -{
>> -	/*
>> -	 * DT_RUNPATH (DT_RPATH when the linker uses old dtags) is removed on
>> -	 * install.
>> -	 */
>> -	for (const ElfW(Dyn) *dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn) {
>> -		if (dyn->d_tag == DT_RUNPATH || dyn->d_tag == DT_RPATH)
>> -			return false;
>> -	}
> 
> You can drop _DYNAMIC at the beginning of the file, as well as inclusion
> of dlfcn.h, elf.h and link.h. I think limits.h, sys/stat.h and
> sys/types.h can be dropped too.
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

Done, thanks.


> 
>> -
>> -	return true;
>> -}
>> -
>> -/**
>> - * \brief Retrieve the path to the build directory
>> - *
>> - * During development, it is useful to run libcamera binaries directly from the
>> - * build directory without installing them. This function helps components that
>> - * need to locate resources in the build tree, such as IPA modules or IPA proxy
>> - * workers, by providing them with the path to the root of the build directory.
>> - * Callers can then use it to complement or override searches in system-wide
>> - * directories.
>> - *
>> - * If libcamera has been installed, the build directory path is not available
>> - * and this function returns an empty string.
>> - *
>> - * \return The path to the build directory if running from a build, or an empty
>> - * string otherwise
>> - */
>> -std::string libcameraBuildPath()
>> -{
>> -	if (isLibcameraInstalled())
>> -		return std::string();
>> -
>> -	Dl_info info;
>> -
>> -	/* Look up our own symbol. */
>> -	int ret = dladdr(reinterpret_cast<void *>(libcameraBuildPath), &info);
>> -	if (ret == 0)
>> -		return std::string();
>> -
>> -	std::string path = dirname(info.dli_fname) + "/../../";
>> -
>> -	char *real = realpath(path.c_str(), nullptr);
>> -	if (!real)
>> -		return std::string();
>> -
>> -	path = real;
>> -	free(real);
>> -
>> -	return path + "/";
>> -}
>> -
>> -/**
>> - * \brief Retrieve the path to the source directory
>> - *
>> - * During development, it is useful to run libcamera binaries directly from the
>> - * build directory without installing them. This function helps components that
>> - * need to locate resources in the source tree, such as IPA configuration
>> - * files, by providing them with the path to the root of the source directory.
>> - * Callers can then use it to complement or override searches in system-wide
>> - * directories.
>> - *
>> - * If libcamera has been installed, the source directory path is not available
>> - * and this function returns an empty string.
>> - *
>> - * \return The path to the source directory if running from a build directory,
>> - * or an empty string otherwise
>> - */
>> -std::string libcameraSourcePath()
>> -{
>> -	std::string path = libcameraBuildPath();
>> -	if (path.empty())
>> -		return std::string();
>> -
>> -	path += "source";
>> -
>> -	char *real = realpath(path.c_str(), nullptr);
>> -	if (!real)
>> -		return std::string();
>> -
>> -	path = real;
>> -	free(real);
>> -
>> -	struct stat statbuf;
>> -	int ret = stat(path.c_str(), &statbuf);
>> -	if (ret < 0 || (statbuf.st_mode & S_IFMT) != S_IFDIR)
>> -		return std::string();
>> -
>> -	return path + "/";
>> -}
>> -
>>  /**
>>   * \fn alignDown(unsigned int value, unsigned int alignment)
>>   * \brief Align \a value down to \a alignment
> 


More information about the libcamera-devel mailing list