Is `meson.source_root()` the project root directory? - build

Is the directory return by meson.source_root() the project root directory (with the root level meson.build file) or is it the path of the current meson.build file being processed?

The project root directory is obtained with the following meson syntax:
meson.source_root()
The current source directory can be obtained with the following meson syntax:
meson.current_source_dir()

Related

Installing spdlog by copying contents of include directory

I'm trying to use spdlog as a logging library, but I do not want to use my package manager (apt) to install it. I have copied the spdlog directory under include to the root of my project directory, but when I try to compile I get include errors because the #include directives in the spdlog headers are all absolute (relative to the project root), instead of relative, gcc cannot find the files.
0:10: fatal error: spdlog/common.h: No such file or directory
#include "spdlog/common.h"
Short of changing all the includes in the spdlog library to relative, how can I use spdlog in my project? I'm also using CMake to compile my project; I have a build directory in my project root and from within it I call cmake .. and then make to compile; is there something I need to add to my CMakeLists.txt file to include spdlog?
You need to add the project root directory into the cmake include_directories, like this:
include_directories(
/absolute/path/
)
Or in the project root directory, edit CMakeLists.txt and add this:
include_directories(
.
)

Meson project: can I have part of the source located elsewhere?

In a Meson project, how can I compile files (i.e. not just headers) located in an directory which is not in my project tree?
E.g.:
MyProj/
src/
meson.build
ExternalCode/
src/
file1.h
file1.cpp
include_directories is just for headers...
If I use ".." in the files path, I get this error:
meson.build:10:0: ERROR: Subdir contains ..
If you want to build ExternalCode as a part of your project, then I recommend fetch somehow this directory inside your project, e.g. using symbolic link and place meson.build file alongside. So, your project layout will look as:
MyProj/
meson.build
src/
meson.build
external/
ExternalCode -> link to ...
meson.build
Then, make aware meson of all sub-directories in the project placing this in top level meson.build file:
subdir('src')
subdir('external')
yes but the external file must be picked up in the following way:
e.g.
source = files(file1.cpp)
put in meson.build inside
ExternalCode/src
folder

using cmake to make header files descendants of the project's source directory

As in Google C++ Style Guide is mentioned all of a project's header files should be listed as descendants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). How can I do that in my project that is shortly described below.
My project directory hierarchy is like this:
GraphicsEngine
header_files.h
source_files.cc
CMakeLists.txt (1)
Light
CMakeLists.txt (2)
header_files.h
source_files.cc
Camera
CMakeLists.txt (3)
header_files.h
source_files.cc
Core
CMakeLists.txt (4)
header_files.h
source_files.cc
These are contents of CMakeLists.txt files:
CMakeLists.txt (1)
add_library(GraphicsEngineLib source_files.cc)
target_link_libraries(GraphicsEngineLib LightLib CameraLib CoreLib)
add_subdirectory(Light)
add_subdirectory(Camera)
add_subdirectory(Core)
CMakeLists.txt (2)
add_library(LightLib source_files.cc)
CMakeLists.txt (3)
add_library(CameraLib source_files.cc)
CMakeLists.txt (4)
add_library(CoreLib source_files.cc)
Now when for example I want to include header files from Camera folder in to files in Core folder, I have to use ../Camera/header_file.h but I want to use GraphicsEngine/Camera/header_file.h. How can I do this?
What I have done in the past is to set this in the top level CMakeLists.txt (which should be in your GraphicsEngine directory):
SET(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/..")
INCLUDE_DIRECTORIES(
${PROJECT_ROOT}
)
where according to this, CMAKE_CURRENT_SOURCE_DIR is
this is the directory where the currently processed CMakeLists.txt is located in
Note that by defining Project_Root in this way, your GraphicsEngine project can also #include headers from sister projects to GraphicsEngine.
Hope this helps.

SCons out of source build only works for executable program, not for object files

I have the following SCons configuration:
current_directory
|-<.cpp files>
|-<.h files>
|-SConstruct
|-SConscript
|-bin
|-<empty>
I want to build my source files and put the executable and the object files into the bin directory.
This is what I have in my SConstruct file:
SConscript('SConscript', variant_dir='bin', duplicate=0)
While in the SConsript file I have:
debug_environment.Program(target = 'SsaTest', src_files, LIBS=libraries, LIBPATH=libraries_path)
When I build using scons command I get the SsaTest executable in the bin directory (as desired), but the object files are left in the current directory.
How can I have the .o files be built in the bin directory as well?
Many thanks.
EDIT: Complete SConscript file (forgive me for the xxxs)
import os
# This is correctly Exported()
Import('debug_flags')
# Paths to header files
headers_paths = ['#/../../xxx/include/',
'#/../../../xxx/include/',
'#/../../xxx/include/',
'#/../../xxx/include/',
'#/../../xxx/include/']
# Path to source files
src_folder = '#./'
# Source files list
src_files = ['xxx.cpp',
'xxx.cpp',
'xxx.cpp',
'xxx.cpp',
'xxx.cpp']
# Prepend the relative path to each source file name
src_files = [src_folder + filename for filename in src_files]
libraries = ['xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx']
libraries_path = ['#/../../xxx/lib',
'#/../../../xxx/bin',
'#/../lib',
'#/../../xxx/lib',
'#/../../xxx/lib',
'#/../../xxx/lib']
# Debug environment
debug_environment = Environment(CC = 'g++', CCFLAGS=debug_flags, ENV = os.environ, CPPPATH=headers_paths);
# Executable build command
debug_environment.Program(target = 'SsaTest', src_files, LIBS=libraries, LIBPATH=libraries_path)
Using '#' with source files not recommended, because you have your situation, scons can't correctly process it with variant dirs and how result create object files in directory where sources placed.
So, i tryed to build your example with same configuration and have no troubles:
#SConsctruct
SConscript('SConscript', variant_dir='bin', duplicate=0)
#SConscript
src_files = Glob('*.cpp')
debug_environment = Environment()
debug_environment.Program('SsaTest', src_files)
So, all object files are generated in bin directory.
Finally, you have no troubles with relation dirs with sources files then using variant dirs. But include dirs are depended from variant dirs.
Configuration for example :
build
app
--SConscript
--src
----*.h
----*.cpp
SConstruct
#SConstruct
rootEnv = Environment()
Export('rootEnv')
SConscript('app/SConscript', variant_dir='build', duplicate=0)
You SConscript will be looking like it:
Import('rootEnv')
env = rootEnv.Clone()
env.Append(CPPPATH = ['#app/src'])
env.Program('app', Glob('src/*.cpp'))
'#app/src' - where # is very important when using variant dir, because if would be app/src, build command will be looking: '-Ibuild/app/src' (adding variant dir before include path). But adding '#' command will be looking correctly : '-Iapp/src'.
One thing that sticks out in your SConscript is how you are prepending the path to each source file with #./.
# Path to source files
src_folder = '#./'
Why do you use the dot in that path, its not necessary? Try with the following #/ like you do with the rest of the paths, like this:
# Path to source files
src_folder = '#/'
Another option would be to put the source files and the respective SConscript in its own subdirectory. Its not real clear why you have a SConstruct, SConscript, and the source files all in one directory. Either create a subdir, or consider removing the SConscript if its not necessary.
In the SConscript() function call in the SConstruct, refer to the variant_dir as "#bin" and not "bin". Not sure if this will help, but its better practice.
Ive seen this behaviour before using the Repository() SCons function to reference the source files as mentioned here.
Also, this is off-topic, but if your include and library paths (headers_paths and libraries_path variables) are outside of the project directory structure, you may consider using absolute paths instead. Personally I find it rather ugly to use relative paths with several ../ paths.

Have CMake recursively scan folders?

How do I set up CMake to recursively scan a given directory and determine the list of source files?
My project is a shared library. I have a folder structure similar to this:
/
src/ # Source files in an arbitrary tree
include/ # Headers, tree mirrors that of the src/ folder
examples/ # Executable code examples that link against the library
CMakeLists.txt
I want CMake to recursively scan src and include and determine the list of source and header files in my project, regardless of the directory structure. I also want to avoid:
Polluting the src/ and include/ directories with endless CMakeLists.txt files
Having to change and adapt the scripts every time I change my folder structure
It is fine for each example to have their own build script, however.
CMake provides the following command for recursive files globing:
file(GLOB_RECURSE variable [RELATIVE path]
[FOLLOW_SYMLINKS] [globbing expressions]...)
Command documentation: http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:file