Using CMake generator expressions with GYP - cmake

I'm working on Node.js native module which contains C and C++ sources. node-gyp is used to build the module.
As I want only one warning rises error in C code I use the following lines in binding.gyp:
"cflags!": [ "-Werror"],
"cflags": [ "-Werror=implicit-function-declaration" ],
This works fine while compiling C code but produces the following warning on each C++ source file:
cc1plus: warning: ‘-Werror=’ argument ‘-Werror=implicit-function-declaration’ is not valid for C++
I found this answer - Apply C-specific gcc options to C/C++ mixed library - which solves the same problem when using 'pure' CMake. Unfortunately I didn't found if it is possible and how to add this condition correctly to GYP configuration file - maybe using variables and conditions? Please, let me know if it's solvable. Thanks.

I found solution to the problem in my question and I'm posting an answer just in case somebody will have the same kind of a problem.
Original improper configuration in binding.gyp was as follows:
"cflags!": [ "-Werror"],
"cflags": [ "-Werror=implicit-function-declaration" ],
Correct configuration for my requirements is:
"cflags!": [ "-Werror"],
"cflags_c": [ "-Werror=implicit-function-declaration" ],
To avoid the warning in C++ we just need to add required flag to C-special flags cflag_c.
Solution was obtained while studying my_module.target.mk file in my project which contains the following comments (thanks to developers!):
# Flags passed to all source files.
CFLAGS_Release := \
# Flags passed to only C files.
CFLAGS_C_Release := \
# Flags passed to only C++ files.
CFLAGS_CC_Release := \
Thus it's seemed obvious, but I still didn't find clear reference in CMake and GYP documentation on these flags. I'm asking please to provide me with corresponding links if you know them or you will find them - I should know where is my mistake in searching docs to avoid them in the future.

Related

CMake hunter error: `Unexpected MSVC_VERSION`

First of all, thank you for taking your time to help!
System: Windows 10 x64
cmake version: 3.24.1
Visual Studio 2022 v17.3.3
Console with admin privileges: Developer Command Prompt for VS 2022
Perl (Strawberry Perl): perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x64-multi-thread
I am trying to compile kawpowminer from https://github.com/RavenCommunity/kawpowminer.
I already found some other solutions to similar errors but they haven't helped me.
Looked into those files (every single one printed out in the error) and found the lines with "hunter_add_package" as the cause of those errors.
I also changed 2019 to 2022 and 15 to 17 in those config files and edited the paths if needed.
I cloned at C:\Users\MEuser\kp the described git-project from "https://github.com/RavenCommunity/kawpowminer".
This is my script, the last line is causing the error. No errors are printed out before.
mkdir kp
cd kp
git clone https://github.com/RavenCommunity/kawpowminer
cd kawpowminer
git submodule update --init --recursive
cd build
cmake -G "Visual Studio 16 2019" -A X64 -H. -Bbuild -DETHASHCL=ON -DETHASHCUDA=ON -DAPICORE=ON ..
cd build
cmake --build . --config Release
Console: Developer Command Prompt for VS 2022
CMake Warning: Ignoring extra path from command line:
"C:/Users/Meuser/k2/kawpowminer/build"
-- [cable ] Cable 0.2.14 initialized
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- [cable ] Configurations: Release;RelWithDebInfo
-- [hunter] Calculating Toolchain-SHA1
-- [hunter] Calculating Config-SHA1
-- [hunter] HUNTER_ROOT: C:/.hunter
-- [hunter] [ Hunter-ID: e14bc15 | Toolchain-ID: b0b5bc7 | Config-ID: 196746b ]
[hunter ** INTERNAL **] Unexpected MSVC_VERSION: '1933' [hunter **
INTERNAL **] [Directory:C:/Users/MEuser/k2/kawpowminer]
--lol------------------------- ERROR ------------------------lol--
https://docs.hunter.sh/en/latest/reference/errors/error.internal.html
------------------------------------------------------------------
CMake Error at
C:/.hunter/_Base/Download/Hunter/0.23.214/e14bc15/Unpacked/cmake/modules/hunter_error_page.cmake:12
(message): Call Stack (most recent call first):
C:/.hunter/_Base/Download/Hunter/0.23.214/e14bc15/Unpacked/cmake/modules/hunter_internal_error.cmake:13 (hunter_error_page)
C:/.hunter/_Base/Download/Hunter/0.23.214/e14bc15/Unpacked/cmake/modules/hunter_setup_msvc.cmake:73
(hunter_internal_error)
C:/.hunter/_Base/Download/Hunter/0.23.214/e14bc15/Unpacked/cmake/modules/hunter_finalize.cmake:117
(hunter_setup_msvc)
C:/.hunter/_Base/Download/Hunter/0.23.214/e14bc15/Unpacked/cmake/modules/hunter_add_package.cmake:23
(hunter_finalize) CMakeLists.txt:56 (hunter_add_package)
-- Configuring incomplete, errors occurred!
I did found similar errors in the internet and on stack but nothing really helped me.
I also searched through the files and the config (CMakeLists.txt) and researched some lines, but I really didnt found anything interesting, similar or working.
(it's 5am in my country, please don't assume I didn't researched a lot or anything, I am just lost)
I also tried different includes for the HunterGate like
include("cmake/HunterGate.make")
HunterGate(
URL "https://github.com/ruslo/hunter/archive/v0.23.33.tar.gz"
SHA1 "abcf4b9b77b5a0160abc1da1cdda35f64b14196a"
)
But at this point I have to admit, I don't know what I am actually doing...
Investigating your error message,
[hunter ** INTERNAL **] Unexpected MSVC_VERSION: '1933' [hunter **
INTERNAL **] [Directory:C:/Users/MEuser/k2/kawpowminer]
I'm pretty sure this is fixed by this hunter PR ("Add support for Visual Studio 17 2022" #484), which added the following to :/cmake/modules/hunter_setup_msvc.cmake:
# ...
string(REGEX MATCH "^193[0-9]$" _vs_17_2022 "${MSVC_VERSION}")
# ...
elseif(_vs_17_2022)
set(HUNTER_MSVC_VERSION "17")
set(HUNTER_MSVC_YEAR "2022")
# ...
The solution is to update your hunter version to get this fix.
I think the Hunter first step docs should be all the information you need. Just change the HunterGate URL to use v0.23.317.
You get a gold star from me for providing so many details in your question (I think you were just missing what hunter version you're using).
If I could add a bit more to my answer, I'd say that reading long error messages to find the important parts and googling key phrases is useful (a skill? / a helpful trick?). If this is truly the cause of your issue, with luck, it's possible to find the answer yourself in less than 5 minutes. I just read your error message, and googled:
hunter "Unexpected MSVC_VERSION"
(the quotes means to look for an exact match)
, which led me to hunter_setup_msvc.cmake in the old hunter repo, and then I just went to the new repo, went to the file, and (by luck) the GitHub banner for most recent commit said "tnixeu add msvc17_2022 to huter_setup_msvc.cmake (#484)".
* Bonus link to wikipedia on MSVC versions.

Recompiling and Problems with cmake

first sry for my gramma or my faults, i'm german but i'll try my best ;)
OK, now straight to the problem:
I'm trying to recompile my source but everytime, i got some faults.
So becouse my knowledge of cmake isn't so far, i hope to find help from you.
The project is about an azerothcore privat wow server and it's first to add thoes modules (CMakeList.txt's below) and to update the core itself.
Everytime, while trying "make -j 4; make install" the latest fail the console shows is
CMake Error at CMakeLists.txt:5 (CU_SET_PATH):
Unknown CMake command "CU_SET_PATH".
-- Configuring incomplete, errors occurred!
See also "/home/wajmanor/azerothcore/build/CMakeFiles/CMakeOutput.log".
Makefile:132: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Thoses helps i found with google everytime want to see the log-file, and that's why i attached it already ;):
CMakeOutput.log
For the Advice (thx to Tsyvarev ), i add the both CMakeList.txt's that could couse the problem:
Mod Nr. 1
cmake_minimum_required(VERSION 3.10)
AC_ADD_SCRIPT("${CMAKE_CURRENT_LIST_DIR}/src/WorldChat.cpp")
AC_ADD_SCRIPT_LOADER("WorldChat" "${CMAKE_CURRENT_LIST_DIR}/src/loader.h")
AC_ADD_CONFIG_FILE("${CMAKE_CURRENT_LIST_DIR}/conf/WorldChat.conf.dist")
Mod Nr. 2
project(GuildHouse)
cmake_minimum_required(VERSION 3.16)
CU_SET_PATH("CMAKE_guildmaster_DIR" "${CMAKE_CURRENT_LIST_DIR}")
AC_ADD_SCRIPT("${CMAKE_CURRENT_LIST_DIR}/src/guildmaster.cpp")
AC_ADD_SCRIPT_LOADER("guildmaster" "${CMAKE_CURRENT_LIST_DIR}/src/loader.h")
Hope someone could help me....

clang-tidy report error unknown argument when contain other compiler options

I have a project, I built it with intel compiler. I want use the clang-tidy to help detect code problems.
I am using CMake to generate compile_commands.json and I'm getting the follow error when I using clang-tidy:
$ run-clang-tidy
# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 warning and 1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]
warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [clang-diagnostic-unknown-warning-option]
Actually, there is a very simliar question exist: clang-tidy reporting unknown warnings
However, when I try to using the method refered above, there is no help. The warning can be suppressed but error still exist.
$ run-clang-tidy -extra-arg=-Wno-unknown-warning-option
# output
# ...
clang-tidy-6.0 -header-filter=^/home/xuhui/temp/build/.* -extra-arg=-Wno-unknown-warning-option -p=/home/xuhui/temp/build /home/xuhui/temp/main.cpp
1 error generated.
Error while processing /home/xuhui/temp/main.cpp.
error: unknown argument: '-w2' [clang-diagnostic-error]
How can I deal with the error?
-w2 options is used to control warning in intel compiler.
Although the problem occurs to me because of the intel compiler, but may be other compiler's options can also leads to the problem.
Appendix
The follow code snippets can help reproduce the problem.
// CMakeLists.txt
SET(CMAKE_CXX_COMPILER "icc")
SET(CMAKE_CXX_COMPILER "icpc")
project(test)
# leads to warning, can be settled by refer link
add_compile_options("-Wno-maybe-uninitialized")
# leads to error, can not be settled by refer link
add_compile_options("-w2")
add_executable(a.out main.cpp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
// main.cpp
#include <iostream>
int main()
{
std::cout << "hello!" << std::endl;
return 0;
}
the above code can generate compile_commands.json like follows:
[
{
"directory": "/home/xuhui/temp/build",
"command": "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc -Wno-maybe-uninitialized -w2 -o CMakeFiles/a.out.dir/main.o -c /home/xuhui/temp/main.cpp",
"file": "/home/xuhui/temp/main.cpp"
}
]
Thanks for your time.
This is not a clang-tidy error per se. Clang-diagnostic-error is basically a compiler error. Clang has made unknown arguments a hard error some time ago and it cannot be degraded to a warning. There used to be -Qunused-arguments but that doesn't work in Clang 11 AFAIK.
You will have to remove the argument before passing the compile commands to clang-tidy, I suggest CMake - remove a compile flag for a single translation unit.
#pablo285 already give the perfect answer.
He said that:
have to remove the argument before passing the compile commands to
clang-tidy
He already provide a link to demonstate how to modify CMakeLists.txt to remove argument.
Also, we can do some modification directly on compile_commands.json to remove argument.
A script to tidy code can be written as follows:
# tidy_code.sh
cd build
cmake ..
# do modification on compile_commands.json to remove argument which clang can not recognized
# replace '-w2' to ' '
sed -i 's/-w2/ /g' compile_commands.json
# using clang tidy
run-clang-tidy -checks='*' -extra-arg=-Wno-unknown-warning-option

Undefined symbol for HDF5 when installing with cmake in Linux environment

I have been able to install and start a program with CMake (with HDF5) but once I access the "about" drop-down link for said program it crashes with the following error:
python: symbol lookup error: /nobackup/<user id>/program-devel/dist_linux/bin_linux/libprogramDLL.so: undefined symbol: h5lib_MP_h5get_libversion_f_
I believe it is an issue with linking static libraries but I am, unfortunately, quite new to CMake and unable to isolate the root problem. I know that this "symbol" is tied somehow to a libhdf5_fortran.a and this is listed in my Cache with:
$ grep -rnw '/nobackup/<user id>/program-devel/build' -e "libhdf5_fortran"
/nobackup/<user id>/program-devel/build/CMakeCache.txt:234:ToolkitLib_LIB_DEPENDS:STATIC=general;SomeLib;general;libz.a;general;libhdf5.a;general;libhdf5_fortran.a;
I’m not sure if this might be where the problem is or not but this is from the ToolKit file - CMakeLists.txt.
if (${USE_HDF5})
#link_directories (${HDF5_DIRECTORY}/lib)
if (WIN32)
target_link_libraries(ToolkitLib libszip libzlib libhdf5 libhdf5_f90cstub libhdf5_fortran)
else ()
# doesn't seem to work on Linux for some reason....
#target_link_libraries(ToolkitLib libsz libz libhdf5 libhdf5_fortran)
# ... try this ... seems to be getting the shared libs...not the static ones...
# set(HDF5_LINK_FLAGS "-L${HDF5_DIRECTORY}/lib -lz -lhdf5_fortran -lhdf5")
# SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${HDF5_LINK_FLAGS}")
# SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${HDF5_LINK_FLAGS}")
# ... doesn't work either ...
target_link_libraries(ToolkitLib libz.a libhdf5.a libhdf5_fortran.a)
endif()
endif()
The last 4 "target_link_library" elements are in directories I've added to my LD_LIBRARY_PATH (although, I've heard this isn't the preferred approach). There is also a final output line when I issue gmake install that might be relevant:
-- Set runtime path of "/nobackup/<user id>/program-devel/dist_linux/bin_linux/libProgramDLL.so" to "$ORIGIN/"
It seems I fixed my problem by adding a --with-pic on the hdf5 installation bash scripts. I also had to remove a -standard-semantics from the CMAKE_Fortran_FLAGS in the src directory. Those two things along with the help you provided fixed my problem. Thanks a lot Pierre de Buyl!

How can I determine MAGICKCORE_QUANTUM_DEPTH and MAGICKCORE_HDRI_ENABLE during compilation?

I have a library that interfaces against ImageMagick 6. During compilation I get the below compilation warnings (promoted to errors by me).
I am aware that explicitly defining these values during compilation using -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=0 will solve the issue (on my specific installation), however, as I am writing my CMake configuration files to be as portable as I can make them, this feels way to brittle and I really hope there is a better way.
Which brings me back to my question: Is there a way to determine MAGICKCORE_HDRI_ENABLE and MAGICKCORE_QUANTUM_DEPTH using cmake, bash or similar for the specific version of the library I am linking against?
/usr/include/ImageMagick-6/magick/magick-config.h:29:3: error: #warning "you should set MAGICKCORE_QUANTUM_DEPTH to sensible default set it to configure time default" [-Werror=cpp]
# warning "you should set MAGICKCORE_QUANTUM_DEPTH to sensible default set it to configure time default"
^
/usr/include/ImageMagick-6/magick/magick-config.h:30:3: error: #warning "this is an obsolete behavior please fix your makefile" [-Werror=cpp]
# warning "this is an obsolete behavior please fix your makefile"
^
/usr/include/ImageMagick-6/magick/magick-config.h:52:3: error: #warning "you should set MAGICKCORE_HDRI_ENABLE to sensible default set it to configure time default" [-Werror=cpp]
# warning "you should set MAGICKCORE_HDRI_ENABLE to sensible default set it to configure time default"
^
/usr/include/ImageMagick-6/magick/magick-config.h:53:3: error: #warning "this is an obsolete behavior please fix yours makefile" [-Werror=cpp]
# warning "this is an obsolete behavior please fix yours makefile"
^
cc1plus: all warnings being treated as errors
While writing the question I came across an answer to this. I'll summarize it here as the other questions regarding this angle it slightly differently.
Imagemagick ships with an utility called Magick++-config on my installation (Ubuntu 16.04) I found this utility under /usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16/Magick++-config. Below is the cmake code snipped I ended up using to extract the relevant build options.
find_package(ImageMagick 6.7 COMPONENTS Magick++ MagickCore)
if(ImageMagick_FOUND)
# Find Imagemagick Library directory
get_filename_component(MAGICK_LIB_DIR ${ImageMagick_MagickCore_LIBRARY} DIRECTORY)
# Find where Magick++-config lives
file(GLOB_RECURSE MAGICK_CONFIG FOLLOW_SYMLINKS ${MAGICK_LIB_DIR}/Magick++-config)
# Ask about CXX and lib flags/locations
set(MAGICK_CONFIG ${MAGICK_CONFIG} CACHE string "Path to Magick++-config utility")
execute_process(COMMAND "${MAGICK_CONFIG}" "--cxxflags" OUTPUT_VARIABLE MAGICK_CXX_FLAGS)
execute_process(COMMAND "${MAGICK_CONFIG}" "--libs" OUTPUT_VARIABLE MAGICK_LD_FLAGS)
# Add these to cache
set(MAGICK_CXX_FLAGS "${MAGICK_CXX_FLAGS}" CACHE string "ImageMagick configuration specific compilation flags." )
set(MAGICK_LD_FLAGS "${MAGICK_LD_FLAGS}" CACHE string "ImageMagick configuration specific linking flags.")
# Split into list:
string(REGEX MATCHALL "([^\ ]+)" MAGICK_CXX_FLAGS "${MAGICK_CXX_FLAGS}")
string(REGEX MATCHALL "([^\ ]+)" MAGICK_LD_FLAGS "${MAGICK_LD_FLAGS}")
# Remove trailing whitespace (CMAKE warns about this)
string(STRIP "${MAGICK_CXX_FLAGS}" MAGICK_CXX_FLAGS)
string(STRIP "${MAGICK_LD_FLAGS}" MAGICK_LD_FLAGS)
target_compile_options(<project> ${MAGICK_CXX_FLAGS})
target_link_libraries(<project> ${MAGICK_LD_FLAGS})
endif(ImageMagick_FOUND)
Source