CMake fails looking up mingw headers - cmake

Trying to change FindSDL.make to work with SDL2. The problem is that using mingw it doesn't find SDL2_INCLUDE_DIR, even though SDL2 is installed.
I've change the whole file, but the problems is in this line:
find_path(SDL2_INCLUDE_DIR
NAMES
SDL.h
PATH_SUFFIXES
include/SDL2
)
Thought it should look up MinGW folder, but it looks like it doesn't.
Actually, even this one doesn't work:
find_path(TEST_PATH stdio.h)
So what is the proper way to use find_path under mingw on Windows? Can it be related to mingw install path (preferred one is C:\MinGW, but I've installed it to E:\Programs\MinGW)?
Should I always explicitly write -DCMAKE_INCLUDE_PATH=<my path to mingw>\include or use environment variable that points to mingw folder and use it as a HINT to find_path?

Related

CMake find_library can't find an existing library on MSVC

I made a project and tried to build it under GCC (Linux) and MinGW (Windows) using CMake. Now I'm trying to do the same under Visual Studio 2019 (for reasons) and it seems that CMake that is shipped with VS can't find libraries installed with MSVC.
I'm using a script which unfortunately I can't find again on the Net, so I can't give you a link to it. But I tried to isolate what is causing the problem. So, here is the code:
if(MY_LIB_PATH)
set(MYLIB_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH)
endif()
find_library(MY_LIBRARY
NAMES myLibrary
HINTS
ENV MY_ENV
${MYLIB_NO_DEFAULT_PATH_CMD}
PATH_SUFFIXES lib lib/x64
PATHS ${MY_LIB_PATH}
DOC ""
)
(the code is much complicated than that, but that is what it looks after all variable substitutions)
If I don't set the MY_LIB_PATH variable it can't find the library (which is located in the lib/x64 folder in the MSVC installation folder). But when I call CMake like this:
cmake "-DMY_LIB_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64"
it "miraculously" finds it.
Yes it works but...wasn't CMake made to avoid writing all that path in the first place? Besides, there is yet another problem: there are many such libraries in my project, all of which use the same above code for finding, and I have to write all that path multiple times, so CMake can find them.
Either the authors of my CMake script did something wrong or I installed my libraries not where CMake is expecting them.

How to use CMake with cygwin/mingw toolchain?

What is the current process to use the mingw toolchain that is included with cygwin?
There use to be a -mno-cygwin option used with gcc.
The mingw versions have x86_64-w64-mingw32- or i686-w64-mingw32- added to their exe names. Currently I just use these directly but I want to use cmake.
Cygwin also includes cmake. How would I configure it to use the mingw toolset that is included with cygwin? I also see some Qt5 *.cmake modules included in the Qt5 libraries for mingw.
Thanks.
I've found a temporary workaround. Just define these two environment variables:
set CC=/usr/bin/x86_64-w64-mingw32-gcc.exe
set CXX=/usr/bin/x86_64-w64-mingw32-g++.exe
Define them before running the cmake that is included with cygwin.
Seems to work.

How to set RPATH in CMAKE?

I wish to build and install a software locally to the $HOME/.local/ path instead of a system-wide /usr/ folder. The software uses CMAKE for compilation.
After installation, the software binaries and libraries get stored in $HOME/.local/bin/ and $HOME/.local/lib/, respectively. However, when I try to run the program, it throws an error that the required library is not found (which, by the way, is present in $HOME/.local/lib/).
The program works fine if I set the $LD_LIBRARY_PATH to $HOME/.local/lib. But I don't want to do this. Hence, instead of this, I would like to know how to specify the RPATH variable (which would point to $HOME/.local/lib) while compiling the software using CMAKE.
Kindly help.
I am using the following two lines in the CMakefile
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
(the first one is required only if you use MacOSX)
you may also use:
set(CMAKE_BUILD_RPATH "/my/libs/location")
specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). The entries will not be used for binaries in the install tree. See also the CMAKE_INSTALL_RPATH variable.
CMAKE_INSTALL_RPATH is a predefined list, so I can see scenarios where it would be better to do
list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib )
If you include( GNUInstallDirs ) you could also
list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_LIBDIR} )
I am a CMake novice though, so if someone sees an issue with the above please let me know.

cmake find_path where path includes version numbers

I'm trying to write a FindQwt.cmake module. I've googled the existing modules and none of them work for my installation.
My module's find path call currently looks like this:
find_path ( QWT_INCLUDE_DIR
NAMES qwt_global.h
HINTS ${QT_INCLUDE_DIR}
PATHS
/opt
/usr/include
/usr/local
/usr/local/include
"$ENV{LIB_DIR}/include"
"$ENV{INCLUDE}"
PATH_SUFFIXES qwt
)
The actual qwt_global.h file resides at the path: /opt/qwt-6.1.2/src/qwt_global.h
I can get this to work if I add the path suffix qwt-6.1.2/src, but it seems to me like it's going to defeat the purpose of having a find module if I need to hard code every version into it (Assume I'm checking later in the module that the versions are compatible and don't care which version is used within the compatible set).
I've tried qwt* and qwt*/src in the PATH_SUFFIXES, but to no avail.
It seems like this would be a common problem. Does anyone know how to fix this find_path call to be robust to having version numbers in the path?
EDIT: I'm using cmake 3.0.2
You can use FILE(GLOB ...) for this.
file(GLOB QWT_SEARCH_PATHS "/opt/qwt-*" "/usr/include/qwt-*")
find_path(QWT_INCLUDE_DIR
NAMES qwt_global.h
PATHS ${QWT_SEARCH_PATHS})
For a cleaner implementation, build a list of directories, then iterate the list to append the "qwt-*" glob.

Add temporarily path to pkg-config within CMake script?

For external libraries the user can specify a non-standard location by adding the path to the CMAKE_FLAGS or by adding -DMYLIB_ROOT. Within the CMake script I want to find the library's pkg-config pc file. Because the pc file is not in the standard folder, it is not found by pkg-config with FindPkgConfig's pkg_search_module.
I tried to add the user-given path to the PKG_CONFIG_PATH but it seemed to be ignored:
include(FindPkgConfig)
set(PKG_CONFIG_PATH "${PKG_CONFIG_PATH}:${MYLIB_ROOT}/lib/pkgconfig")
pkg_search_module(PKG_MYLIB mylib)
if(${PKG_MYLIB_FOUND})
...
When I call pkg-config from the terminal with the modified PKG_CONFIG_PATH set, it find the pc file. What am I doing wrong? How can I get pkg_search_module working? I'd like to avoid calling pkg-config directly from CMake.
Maybe the following will do the job
set( ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${MYLIB_ROOT}/lib/pkgconfig" )
This is a known issue and a ticket exists in CMake's bugtracker, but it is backlocked due to lack of developer interest. I guess one has to provide a patch first...
Edit: According to the bugtracker the feature has been implemented and is part of CMake 3.1.