dependency and option in meson project - meson-build

I have a project with a similar folder trees:
├── meson.build (1)
├── meson_options.txt
├── README.md
└── src
└── mysub
├── meson.build (2)
└── mesonTest.c
the meson.options.txt contains
option('avx_opt', type : 'combo', choices : ['avx2', 'avx512'], value : 'avx512')
the mysub project is a dependency of the main proj
so the meson.build (1) :
project(
'my_proj',
'c',
version : '1.1.0',
default_options : ['buildtype=plain','warning_level=3'],
subproject_dir : 'src'
)
project_source_files = [
''
]
message('## source root : ' + meson.project_source_root() + ' ##')
project_dependencies = [
dependency('mysub', fallback : ['mysub', 'mysub_dep']),
]
build_args = [
]
# ===================================================================
# ======
# Target
# ======
build_args += [
'-DPROJECT_NAME=' + meson.project_name(),
'-DPROJECT_VERSION=' + meson.project_version(),
]
the meson.build (2) of the mysub proj is:
project(
'mysub',
'c',
version : '1.1.0',
default_options : ['warning_level=3']
)
project_description = 'mysub binary'
project_source_files = [
'mesonTest.c'
]
project_headers = [
]
avx_type = get_option('avx_opt')
if (avx_type == 'avx512')
build_args_avx512 = [
'-mavx512f',
'-mavx512cd',
'-mavx512vl',
'-mavx512bw',
'-mavx512dq',
'-DNEWLDPC=1'
]
else
build_args_avx512 = [
'-DNEWLDPC=0'
]
endif
project_target = executable(
meson.project_name(),
project_source_files,
install : true,
c_args : build_args,
link_args : '-Wl,--allow-shlib-undefined',
)
# =======
# Project
# =======
# Make this library usable as a Meson subproject.
project_dep = declare_dependency(
include_directories: public_headers,
link_with : project_target
)
set_variable(meson.project_name() + '_dep', project_dep)
# Make this library usable from the system's
# package manager.
install_headers(project_headers, subdir : meson.project_name())
pkg_mod = import('pkgconfig')
pkg_mod.generate(
name : meson.project_name(),
filebase : meson.project_name(),
description : project_description,
subdirs : meson.project_name(),
# libraries : project_target,
)
I have tried to configure in the following way:
meson builddir -Davx_opt=avx512
or
meson builddir -Davx_opt:mysub=avx512
but in both case I got:
The Meson build system
Version: 0.59.1
Source dir: /home/roccasal/wsEclipse/Intel/mesonTest/proj
Build dir: /home/roccasal/wsEclipse/Intel/mesonTest/proj/builddir
Build type: native build
Project name: my_proj
Project version: 1.1.0
C compiler for the host machine: cc (gcc 8.5.0 "cc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)")
C linker for the host machine: cc ld.bfd 2.30-108
Host machine cpu family: x86_64
Host machine cpu: x86_64
Message: ## source root : /home/roccasal/wsEclipse/Intel/mesonTest/proj ##
Found pkg-config: /usr/bin/pkg-config (1.4.2)
Found CMake: /usr/bin/cmake (3.20.2)
Run-time dependency mysub found: NO (tried pkgconfig and cmake)
Looking for a fallback subproject for the dependency mysub
Executing subproject mysub
mysub| Project name: mysub
mysub| Project version: 1.1.0
mysub| C compiler for the host machine: cc (gcc 8.5.0 "cc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)")
mysub| C linker for the host machine: cc ld.bfd 2.30-108
src/mysub/meson.build:32:0: ERROR: Tried to access unknown option "avx_opt".
what is wrong in the meson build configuration?
the meson ver used is 0.59.1
//thanks

Check Build-options page in reference manual:
To change values in subprojects prepend the name of the subproject and
a colon:
$ meson configure -Dsubproject:option=newvalue
Thus, try create new build dir with:
meson builddir -Dmysub:avx_opt=avx512
or configure existing with:
meson configure builddir -Dmysub:avx_opt=avx512
To make it working you also need this option defined in meson_options.txt in every subproject that uses it, but to simplify configuration you can as #dcbaker suggested use yielding, i.e. update option definition for the main project:
option('avx_opt', ...., yield : true)
This will give you possibility to configure it the same way for main and subprojects with just:
meson configure builddir -Davx_opt=avx512
Also, (I guess it's just typo in question) file with options should have name meson_options.txt (with underscore).

Related

Trying to create & use a conan package: lib dir not set

I created a conan package for a 3rd party tool. I'm trying to test it, and it doesn't seem to add the lib dir so the linker can't find the package's lib. Not sure how to debug it.
Here's the conanfile.py for the package ("hjson"):
class HjsonConan(ConanFile):
name = "hjson"
version = "2.2"
url = "https://github.com/hjson/hjson-cpp"
license="MIT"
# Optional metadata
description = "hjson C++ json library"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False], "version": "ANY"}
default_options = {"shared": False, "fPIC": True, "version": "2.2"}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = ("hjson-src/*")
def source(self):
# unpacks into ./hjson-src
get(self,
f"https://github.com/hjson/hjson-cpp/archive/refs/tags/{self.options.version}.tar.gz",
destination='hjson-src',
strip_root=True)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
# Sets source=., build=build/Release, generators=build/generators
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
# Here is where to set CMake variables ("-DXYZ")
tc.variables["HJSON_ENABLE_INSTALL"] = 1
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder='hjson-src')
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["hjson"]
# these are the defaults, here for clarity
self.cpp_info.includedirs = ["include"]
self.cpp_info.libdirs = ["lib"]
Running conan create . works; it populates a subdir of my ~/.conan with the lib and headers and other metadata.
So I try to use it, with this conanfile:
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import cross_building
class HJsonTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
# VirtualBuildEnv and VirtualRunEnv can be avoided if "tools.env.virtualenv:auto_use" is defined
# (it will be defined in Conan 2.0)
generators = "CMakeDeps", "CMakeToolchain", "VirtualBuildEnv", "VirtualRunEnv"
apply_env = False
test_type = "explicit"
def requirements(self):
self.requires("hjson/2.2")
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def layout(self):
cmake_layout(self)
def test(self):
if not cross_building(self):
cmd = os.path.join(self.cpp.build.bindirs[0], "example")
self.run(cmd, env="conanrun")
with this CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)
find_package(hjson CONFIG REQUIRED)
add_executable(example src/example.cpp)
set_property(TARGET example PROPERTY CXX_STANDARD 20)
include_directories(${hjson_INCLUDE_DIRS})
link_directories(${hjson_LIB_DIRS})
target_link_libraries(example hjson)
but the link fails because it's not passing -L<conanpath> on the link line. Poking around, it looks like hjson_LIB_DIRS is not set in the generated build/generators/hjson-config.cmake:
set(hjson_VERSION_STRING "2.2")
set(hjson_INCLUDE_DIRS ${hjson_INCLUDE_DIRS_RELEASE} )
set(hjson_INCLUDE_DIR ${hjson_INCLUDE_DIRS_RELEASE} )
set(hjson_LIBRARIES ${hjson_LIBRARIES_RELEASE} )
set(hjson_DEFINITIONS ${hjson_DEFINITIONS_RELEASE} )
So where do I go to fix that? I did set the cpp_info.libdirs in the package, so not sure why it's not getting through to the generated config.
The created conan package looks like this, btw:
~/.conan/data/hjson/2.2/_/_/package/37c9d02f...91ef
├── conaninfo.txt
├── conanmanifest.txt
├── include
│ └── hjson
│ └── hjson.h
└── lib
├── hjson
│ ├── hjson-config-version.cmake
│ ├── hjson-config.cmake
│ ├── hjson-release.cmake
│ └── hjson.cmake
└── libhjson.a

How to call a bash script from cmake and pass a generator dependent string as argument?

While using the Unix Makefiles generator I have added the following to a CMakeLists.txt file:
add_custom_target(maintainer-clean
# The current directory is CMAKE_CURRENT_BINARY_DIR.
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/cwm4/scripts/cmake_maintainer_clean.sh $(MAKE) \"${GITACHE_PACKAGES}\"
)
This cmake_maintainer_clean.sh script is make specific, and it needs to use $(MAKE) in the generated Makefile when calling the script.
However, when switching to the generator Ninja this custom command is put as-is in the build.ninja file, causing the $ of the $(MAKE) to cause problems (ninja refuses to run any target, failing to parse build.ninja).
Therefore, I wish to make this generator-specific. How can I use $(MAKE) as first argument to the script when the generator is Unix Makefiles and something else, without a $ - e.g. "ninja" - when the generator is Ninja?
Can I do something like:
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/cwm4/scripts/cmake_maintainer_clean.sh $<UNIX:$(MAKE),ninja> \"${GITACHE_PACKAGES}\"
?
I would make separate presets for each generator, so you can associate a particular environment variable with it. Here is how the CMakePresets.json file may look like:
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "base",
"binaryDir": "${sourceDir}/build",
"hidden": true
},
{
"name": "Ninja",
"inherits": "base",
"displayName": "Ninja Config",
"generator": "Ninja",
"environment": {
"SCRIPT_ARG": "Ninja"
}
},
{
"name": "Make",
"inherits": "base",
"displayName": "Make Config",
"generator": "Unix Makefiles",
"environment": {
"SCRIPT_ARG": "Make"
}
}
]
}
Where the SCRIPT_ARG can be read later inside of the CMakeLists.txt configuration:
cmake_minimum_required(VERSION 3.20)
project(Hello)
add_executable(Hello main.cpp)
add_custom_target(my-script
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/script.sh $ENV{SCRIPT_ARG}
)
add_dependencies(Hello my-script)
Providing script.sh is as simple as this:
#!/bin/sh
echo "Hello from script, $1!"
You will end up with the following output during build phase:
% cmake --preset Make
...
% cmake --build build
Hello from script, Make!
[ 0%] Built target my-script
...
Corresponding output for Ninja generator would be:
% cmake --preset Ninja
...
% cmake --build build
Hello from script, Ninja!
[ 0%] Built target my-script
...
If you need something more complex than an environment variable, you can introduce condition statement based on value of CMAKE_GENERATOR:
cmake_minimum_required(VERSION 3.20)
project(Hello)
add_executable(Hello main.cpp)
if(CMAKE_GENERATOR STREQUAL Ninja)
add_custom_target(my-script COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/script.sh $ENV{SCRIPT_ARG})
elseif(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
add_custom_target(my-script COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/another_script.sh)
endif()
add_dependencies(Hello my-script)
The provided solutions work at build step, and for the part whether it's possible to distinguish between generators during build system generation phase (e.g. with use of generator expressions) i don't think it's possible, because the configuration is generator-agnostic at this point.

Yocto include cmake project with custom steps

I am trying to include this simple cmake-based project to my image: https://github.com/MatrixOrbital/HTT-Utility
The steps to build in Linux are:
mkdir build
cd build
cmake ..
make
I am trying to reproduce these steps within my Yocto recipe. The generated binary (./build/htt_util) should be installed in /usr/bin.
So far with the help of devtool and some manual tuning I ended up with this recipe:
LICENSE = "MIT & Unknown"
LIC_FILES_CHKSUM = "file://LICENSE;md5=ff75ee274f4c77abeee3db089083fec7 \
file://hidapi/LICENSE.txt;md5=7c3949a631240cb6c31c50f3eb696077"
SRC_URI = "git://github.com/MatrixOrbital/HTT-Utility.git;protocol=https"
SRC_URI += "file://0001-Adding-ctype.patch;"
PATCHTOOL = "git"
# Modify these as desired
PV = "1.0+git${SRCPV}"
SRCREV = "2045d5eacc67b89a02dafe41edfd032179333aee"
S = "${WORKDIR}/git"
inherit cmake
# Specify any options you want to pass to cmake using EXTRA_OECMAKE:
EXTRA_OECMAKE = ""
DEPENDS += "udev"
What should I add to my recipe to achieve the goal of generating a binary and installing into /usr/bin?
I have been trying to play with:
do_configure() {
...
}
do_compile() {
...
}
do_install() {
...
}
But so far I did not manage to do anything useful.
Any help would be appreciated.
do_install() {
install -m 0644 mybinary ${D}${bindir}
}
FILES_${PN} = " \
${bindir} \
"

How to use Facebook BUCK with DTrace files?

So, if you open https://github.com/airbnb/BuckSample
And will try to install with cocoaPods https://github.com/ReactiveCocoa/ReactiveObjC
and after you'll add a new BUCK build rule like
apple_third_party_lib(
name = "ReactiveObjC",
visibility = ["PUBLIC"],
srcs = glob([
"ReactiveObjC/**/*.m",
]),
exported_headers = glob([
"ReactiveObjC/**/*.h",
]),
frameworks = [
"$PLATFORM_DIR/Developer/Library/Frameworks/Foundation.framework",
],
)
buck build //Pods:ReactiveObjC will fail with error like this
Pods/ReactiveObjC/ReactiveObjC/RACPassthroughSubscriber.m:12:9: fatal error: 'RACSignalProvider.h' file not found
If we go further we'll see that RACSignalProvider.h is not in the Pod sources, but there is RACSignalProvider.d which is DTrace source file.
When we try to compile it with XCode we can see that there is an extra step before compiling actual framwork
CompileDTraceScript /*user folder*/Pods/ReactiveObjC/ReactiveObjC/RACSignalProvider.d (in target 'ReactiveObjC' from project 'Pods')
cd /*user folder*/Pods
/usr/sbin/dtrace -h -s /*user folder*/Pods/ReactiveObjC/ReactiveObjC/RACSignalProvider.d -o /*user folder*/Library/Developer/Xcode/DerivedData/Odnoklassniki-gsukbcogkxolydbhlpglswzdhhpg/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/ReactiveObjC.build/DerivedSources/RACSignalProvider.h
which is not happening when we run buck build
Is there something missing from config? Or this just not supported by BUCK?
So the answer is, you need to use a genrule() to process your DTrace files
It should look like
genrule(
name = "ReactiveObjC_DTrace",
srcs = [
"ReactiveObjC/ReactiveObjC/RACSignalProvider.d",
"ReactiveObjC/ReactiveObjC/RACCompoundDisposableProvider.d",
],
bash =
"""
mkdir -p $OUT
/usr/sbin/dtrace -h -s $SRCDIR/ReactiveObjC/ReactiveObjC/RACSignalProvider.d -o $OUT/RACSignalProvider.h
/usr/sbin/dtrace -h -s $SRCDIR/ReactiveObjC/ReactiveObjC/RACCompoundDisposableProvider.d -o $OUT/RACCompoundDisposableProvider.h
""",
out = "ReactiveObjC_DTrace",
visibility = ["PUBLIC"]
)
And then modify your ReactiveObjC rule to look like
apple_third_party_lib(
name = "ReactiveObjC",
visibility = ["PUBLIC"],
srcs = glob([
"ReactiveObjC/**/*.m",
]),
deps = [
"//Pods:ReactiveObjC_DTrace",
],
exported_headers = glob([
"ReactiveObjC/**/*.h",
"$(location :ReactiveObjC_DTrace)/**/*.h"
]),
frameworks = [
"$PLATFORM_DIR/Developer/Library/Frameworks/Foundation.framework",
],
)

Can't find package X11 on WSL

I'm trying to build an opensource project on WSL using cmake.
I'm getting first these two errors which I don't understand :
CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineSystem.cmake:185 (configure_file):
configure_file Problem configuring file
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
The CXX compiler identification is GNU 9.3.0
CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineCXXCompiler.cmake:210 (configure_file):
configure_file Problem configuring file
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
and for X11:
Searching X11
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find X11 (missing: X11_X11_LIB)
Call Stack (most recent call first):
/usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.16/Modules/FindX11.cmake:366 (find_package_handle_standard_args)
CMakeLists.txt:18 (FIND_PACKAGE)
Here is the cmakelist.txt provided :
#CMake for the gard Project
project(GARD)
cmake_minimum_required(VERSION 2.4.0)
#Set the path to the Cmake Modules
SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules/")
MESSAGE("Path to Cmake Modules is " ${CMAKE_MODULE_PATH})
MESSAGE("The source directory of the project is " ${GARD_SOURCE_DIR})
MESSAGE("")
MESSAGE("STEP 1 : SEARCHING FOR NEEDED MODULES")
MESSAGE("")
MESSAGE("Searching X11")
FIND_PACKAGE(X11 REQUIRED)
IF(X11_FOUND)
MESSAGE(" X11 include path: " ${X11_INCLUDE_DIR})
MESSAGE(" X11 library path: " ${X11_LIBRARIES})
ENDIF(X11_FOUND)
MESSAGE("Searching LAPACK AND BLAS")
find_package(LAPACK)
IF(LAPACK_FOUND)
MESSAGE(" LAPACK library path: " ${LAPACK_LIBRARIES})
MESSAGE(" BLAS library path: " ${BLAS_LIBRARIES})
ENDIF(LAPACK_FOUND)
MESSAGE("Searching CIMG")
find_package(CImg)
IF(CIMG_FOUND)
MESSAGE(" CIMG include path: " ${CIMG_INCLUDE_DIR})
ENDIF(CIMG_FOUND)
MESSAGE("Searching pthread")
FIND_LIBRARY(PTHREADS_LIBRARY pthread
PATHS /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu
)
IF(PTHREADS_LIBRARY)
MESSAGE(" Pthread library path: " ${PTHREADS_LIBRARY})
ENDIF(PTHREADS_LIBRARY)
MESSAGE("Searching Graphviz")
find_package(GraphViz)
IF(GRAPHVIZ_FOUND)
MESSAGE(" Graphviz library path: " ${GRAPHVIZ_LIBRARIES})
MESSAGE(" Graphviz include path: " ${GRAPHVIZ_INCLUDE_DIRS})
SET(GRAPHVIZ_SUPPORT "#define GRAPHVIZ_SUPPORT")
ENDIF(GRAPHVIZ_FOUND)
CONFIGURE_FILE(GraphVizConfig.h.cmake ${CMAKE_SOURCE_DIR}/include/core/GraphVizConfig.h)
find_package(OpenMP)
if(OPENMP_FOUND)
message("openMP found")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
MESSAGE("Searching FLANN")
find_package(Flann)
IF(FLANN_FOUND)
MESSAGE(" FLANN include path: " ${FLANN_INCLUDE_DIR})
include_directories(${FLANN_INCLUDE_DIR})
SET(FLANN_SUPPORT "#define FLANN_SUPPORT")
ELSE(FLANN_FOUND)
MESSAGE(" FLANN include path not found")
SET(FLANN_SUPPORT "")
ENDIF(FLANN_FOUND)
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
set(AVAILABLE_CORES "#define AVAILABLE_CORES ${N}")
else(NOT N EQUAL 0)
set(AVAILABLE_CORES "#define AVAILABLE_CORES 1")
endif()
CONFIGURE_FILE(FlannConfig.h.cmake ${CMAKE_SOURCE_DIR}/include/core/FlannConfig.h)
MESSAGE("")
MESSAGE("")
MESSAGE("STEP 2 : SET INCLUDE AND LIBRARY PATHS")
MESSAGE("")
SET(GARD_INCLUDE_DIR ${GARD_SOURCE_DIR}/include)
IF(LAPACK_FOUND)
SET(LINK_LIBRARIES ${X11_LIBRARIES} ${PTHREADS_LIBRARY} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${GRAPHVIZ_LIBRARIES})
SET(CIMG_USE_LAPACK "#define cimg_use_lapack")
ELSE()
SET(LINK_LIBRARIES ${X11_LIBRARIES} ${PTHREADS_LIBRARY} ${GRAPHVIZ_LIBRARIES})
ENDIF()
CONFIGURE_FILE(CimgConfig.h.cmake ${CMAKE_SOURCE_DIR}/include/core/CimgConfig.h)
MESSAGE("")
MESSAGE("STEP 3 : Documentation generation")
MESSAGE("")
MESSAGE("Searching DOXYGEN")
INCLUDE(${CMAKE_MODULE_PATH}/FindDoxygen.cmake)
IF(DOXYGEN_FOUND)
MESSAGE("DOXYGEN Dir " ${DOXYGEN_DIR})
MESSAGE("GENERATING DOCUMENTATION")
SET(ENV{GARD_INCLUDE_DIR} "${GARD_SOURCE_DIR}/include")
EXEC_PROGRAM("cd docs;${DOXYGEN_DIR}/doxygen doc.txt"
OUTPUT_VARIABLE MY_OUTPUT
)
ENDIF(DOXYGEN_FOUND)
MESSAGE(STATUS "Link libraries: ${LINK_LIBRARIES}")
MESSAGE("")
MESSAGE("STEP 4 : COMPILATION")
MESSAGE("")
add_subdirectory (${GARD_SOURCE_DIR}/examples)
# add_subdirectory (${GARD_SOURCE_DIR}/operators)
Can you help please? I'm new to cmake and WSL. I'm running WSL on Windows 10 and I've installed the X11 library with " sudo apt-get install libx11-dev " . The command 'cmake .' is running in the bash terminal and looks like :
ananass#DESKTOP-R1E6G2S:/mnt/c/Users/ananass/gard$