Meson absolute path from include_directories - meson-build

In Meson, is it possible to get a string absolute path from an object created by a call to include_directories?
My use case:
include_dirs = include_directories('include')
lib = library(
'mylib',
source_files,
include_directories: include_dirs
)
run_target('analyze',
command: ['static_analysis',
source_files,
'-I', include_dirs.to_abs_path(),
]
)
include_dirs.to_abs_path() does not exist, but I wonder if something similar is possible. Alternatively, files() exists(as used for source_files here); is there a directories()?

You probably found out how to achieve this by now, but if anyone is looking for the same thing, you can get the root directory by calling
dir_base = meson.current_source_dir()
This returns the directory of the current meson build file.
Then you could construct the include path by doing
dir_include = join_paths(dir_base, 'include')

Related

How to add symbolic links for MODULE in cmake like for shared lib?

I'm using cmake-3.16, and for other technical reason, I must use MODULE to make a shared lib(*.so) in Linux instead of using SHARED.
But with MODULE, cmake does NOT produce target file with name like "libDummy.so.x.x.x", and automatically create a symbolic link with name like "libDummy.so".
So I manually use OUTPUT_NAME_RELEASE to declare the target name as following:
add_library( Dummy MODULE Dummy.cpp )
set_target_properties( Dummy PROPERTIES
PREFIX ""
SUFFIX ""
OUTPUT_NAME_RELEASE "Dummy.so.${PROJECT_VERSION}"
OUTPUT_NAME_DEBUG "Dummy.so.${PROJECT_VERSION}"
)
install( TARGETS Dummy LIBRARY DESTINATION somewhere )
But I don't know how to add a symbolic link for it.
I found the cmake command:
file( CREATE_LINK "Origin.so.0.1.2" "Symlnk.so" RESULT act_res SYMBOLIC )
looks like doing my requirement.
But I don't Known its syntax. I searched in cmake documents, there are very less comments about it.
How to refer the target name of the original Module in the command file( CREATE_LINK ),
and how to make the symbolic link with relative path,
and how to store the symblink into the same folder of the module?
Thx!

Read a file or print a message in CMake

I'm trying to read a file's content and set a variable on the condition whether a file exists relative to the CMakeLists.txt script file. For example, I want to conditionally set an environment variable with the content of a file that resides on disk, and if it's not there I want to print a helpful message.
if (EXISTS pkgconfig-environment)
file(READ pkgconfig-environment LOCAL_PKG_CONFIG_PATH)
set(ENV{PKG_CONFIG_PATH} ${LOCAL_PKG_CONFIG_PATH})
else()
message("
I hope you know what you're doing with your pkg-config.
")
endif ()
The logic above never detects the file pkgconfig-environment, and it instead always prints the message. The file can be read into a cmake variable, but only if it exists.
There are two problems: first, file(READ ...) will fail the build sometimes because the file doesn't exist (I don't care if it's a directory and it fails. That's not my use case). Second, the parameter expected in the call if(EXISTS path) should probably be an absolute path, but I wanted the file to be tested for existence relative to the CMakeLists.txt script file.
Given how clearly the documentation states that exists-checks are supposed to be absolute paths, it leads me to think there's some way to determine the absolute path of a file from a relative path near the CMakeLists.txt.
To get the full path to the directory containing the current CMakeLists.txt file, use ${CMAKE_CURRENT_LIST_DIR}:
if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/pkgconfig-environment)
file(READ ${CMAKE_CURRENT_LIST_DIR}/pkgconfig-environment LOCAL_PKG_CONFIG_PATH)
set(ENV{PKG_CONFIG_PATH} ${LOCAL_PKG_CONFIG_PATH})
else()
message("
I hope you know what you're doing with your pkg-config.
")
endif ()

Why CMake doubles the path?

I am using UseLATEX, with commands
set(MainFile "Demo.tex")
set(InputFiles ${MainFile} Main.tex OtherFiles.tex)
then later I use it like
ADD_LATEX_DOCUMENT( ${MyFileName}
INPUTS "${InputFiles}" )
and everything works fine. If I change to
file(GLOB_RECURSE InputFiles src/*.tex)
then I receive messages with a list of files I wanted to put into InputFiles,
but preceeded with
"Could not find input file ${CMAKE_SOURCE_DIR}/${CMAKE_SOURCE_DIR}/OtherFiles.tex"
and of course that path does not exist. What is wrong?
Turning my comment into answer
Haven't worked with ADD_LATEX_DOCUMENT(), but it seems it appends the current directory itself and would need relative paths.
Just change your file(GLOB ...) command to output relative paths:
file(GLOB_RECURSE InputFiles RELATIVE "${CMAKE_SOURCE_DIR}" src/*.tex)

cmake: Configure_file variable replacement

I am using cmake's PackageConfigHelpers'
configure_package_config_file(
Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
....
PATH_VARS
my_paths
)
If my_path were to consist of multiple paths, such as:
set(my_paths path1 path2)
the config file will end up prefixing only path1 and I will end up with:
${PACKAGE_PREFIX_DIR}path1;path2.
which results into path2 not being locatable. Is there any way of fixing this while still using the function provided by PackageConfigHelpers?
Each path should be assigned to its own variable, and these variables should be enumarated for PATH_VARS option:
set(path1_var <...> CACHE ...)
set(path2_var <...> CACHE ...)
configure_package_config_file(
"Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
....
PATH_VARS
path1_var
path2_var
)
Each variable should be used in Config.cmake.in for specific type of deliverables.
From the documentation for configure_package_config_file:
The variables <var1> to <varN> given as PATH_VARS are the variables which contain install destinations. For each of them the macro will create a helper variable PACKAGE_<var...>. These helper variables must be used in the FooConfig.cmake.in file for setting the installed location. They are calculated by CONFIGURE_PACKAGE_CONFIG_FILE() so that they are always relative to the installed location of the package.

How to change the reference path for get_filename_component in cmake?

I'm using get_filename_component in cmake to get the absolute path of a possibly relative path given in a variable.
And I want to do an out-of-tree/out-of-source-build.
It seems to me that get_filename_component is using CMAKE_SOURCE_DIR as reference-path.
Is there a way to change that or to workaround it?
One way I tried is to prefix my potential relative path with ${CMAKE_BINARY_DIR} but that stops working of the path given is not relative.
Assuming your relative path is always relative to CMAKE_BINARY_DIR, then you can handle this pretty easily using if(IS_ABSOLUTE ...):
if(NOT IS_ABSOLUTE ${MyPath})
set(MyAbsPath ${CMAKE_BINARY_DIR}/${MyPath})
endif()
If the subject file or dir exists at CMake run time, then you can always do a find_file call, passing the possible NAMES and PATHS. If the file exists and is found, the resulting variable will hold the full path to the file.
Or you can use the if(EXISTS ...) signature of if to check for the existence or not of the given file.