CMAKE passing variable into included cmake sub-file - c++

I'm currently configuring build of my project and decided to split CMakeLists.txt into subfiles which are responsible for a single build sub-task (like testing, packaging, compiling, etc...). The problem is the cmake subfiles needs info about how to do their tasks. So I decided to require some variables to be set before including the module. It looks like
CpackMylib.cmake:
if(NOT DEFINED MYLIB_PACKAGE_NAME)
message(FATAL_ERROR "MYLIB_PACKAGE_NAME variable must be set")
endif()
if(NOT DEFINED MYLIB_VERSION_MAJOR)
message(FATAL_ERROR "MYLIB_VERSION_MAJOR variable must be set")
endif()
if(NOT DEFINED MYLIB_VERSION_MINOR)
message(FATAL_ERROR "MYLIB_VERSION_MINOR variable must be set")
endif()
if(NOT DEFINED MYLIB_VERSION_PATCH)
message(FATAL_ERROR "MYLIB_VERSION_PATCH variable must be set")
endif()
set(CPACK_PACKAGE_VERSION_MAJOR ${MYLIB_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${MYLIB_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${MYLIB_VERSION_PATCH})
set(CPACK_DEBIAN_PACKAGE_NAME "${MYLIB_PACKAGE_NAME}-${MYLIB_VERSION_MAJOR}")
set(CPACK_DEBIAN_PACKAGE_FILE_NAME ${MYLIB_PACKAGE_NAME}-${MYLIB_VERSION_MAJOR})
#other settings...
include(CPack)
And the root CMakeLists.txt:
set(MYLIB_VERSION_MAJOR 0)
set(MYLIB_VERSION_MINOR 0)
set(MYLIB_VERSION_PATCH 1)
set(MYLIB_PACKAGE_NAME ${PROJECT_NAME})
include(cmake/CpackMylib.cmake)
So I require to define project-specific variables that will be used for creation packages using CPack. I'm new to CMake and this just seems natural to do things that way. But I'm not sure if it is sort of common approach when dealing with CMake.
Does it make sense to define all variable the CMake submodules needs and provide them in the root CMakeLists.txt?

Related

CMAKE_CXX_SOURCE_FILE_EXTENSIONS not working with thrust/cuda

Thrust allows for one to specify different backends at cmake configure time via the THRUST_DEVICE_SYSTEM flag. My problem is that I have a bunch of .cu files that I want to be compiled as regular c++ files when a user runs cmake with -DTHRUST_DEVICE_SYSTEM=OMP (for example). If I change the extension of the .cu files to .cpp they compile fine (indicating that I just need tell cmake to use the c++ compiler on the .cu files). But if I add .cu to CMAKE_CXX_SOURCE_FILE_EXTENSIONS then I get a CMake Error: Cannot determine link language for target "cuda_kernels". Here's a minimal cmake example:
cmake_minimum_required(VERSION 3.19)
project(kernels LANGUAGES C CXX Fortran)
set(KERNELS_USE_OMP OFF)
if ("${THRUST_DEVICE_SYSTEM}" STREQUAL "OMP")
set(KERNELS_USE_OMP ON)
endif()
# verify CUDA support
include(CheckLanguage)
check_language(CUDA)
if (CMAKE_CUDA_COMPILER AND NOT KERNELS_USE_OMP)
enable_language(CUDA)
else()
list(PREPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS "cu;CU")
endif()
message(STATUS "${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")
find_package(Thrust REQUIRED CONFIG)
thrust_create_target(Thrust FROM_OPTIONS)
add_library(cuda_kernels my_kernels.cu)
target_link_libraries(cuda_kernels Thrust)
The output of the message command on my system is: -- cu;CU;C;M;c++;cc;cpp;cxx;mm;mpp;CPP;ixx;cppm
Why is cmake not respecting my CMAKE_CXX_SOURCE_FILE_EXTENSIONS changes?
Why is cmake not respecting my CMAKE_CXX_SOURCE_FILE_EXTENSIONS changes?
The extension-to-language for <LANG> is set as soon as <LANG> is enabled by inspecting the value of the CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS variable when the language detection module exits.
Unfortunately, there is no blessed way to override this list for CXX as it is hard-coded in Modules/CMakeCXXCompiler.cmake.in.
Perhaps the best way of working around the actual error would be to use the LANGUAGE source file property to tell CMake how to compile the individual CUDA files, like so:
cmake_minimum_required(VERSION 3.19)
project(kernels LANGUAGES CXX)
find_package(Thrust REQUIRED)
thrust_create_target(Thrust FROM_OPTIONS)
thrust_is_cuda_system_found(USE_CUDA)
if (USE_CUDA)
enable_language(CUDA)
endif()
set(cuda_kernel_sources my_kernels.cu)
add_library(cuda_kernels ${cuda_kernel_sources})
target_link_libraries(cuda_kernels PRIVATE Thrust)
if (NOT USE_CUDA)
set_source_files_properties(
${cuda_kernel_sources}
PROPERTIES
LANGUAGE CXX
)
endif ()
This will certainly be friendlier to other projects that might try to add_subdirectory yours.
However, if we want to be very naughty, we can do this:
cmake_minimum_required(VERSION 3.19)
project(kernels LANGUAGES NONE)
###
# Hacky language extension override
function(add_cuda_extensions variable access value current_list_file stack)
if (NOT cu IN_LIST value)
list(PREPEND "${variable}" "cu" "CU")
set("${variable}" "${${variable}}" PARENT_SCOPE)
endif ()
endfunction()
# verify CUDA support
include(CheckLanguage)
check_language(CUDA)
if (CMAKE_CUDA_COMPILER AND NOT THRUST_DEVICE_SYSTEM STREQUAL "OMP")
enable_language(CUDA)
enable_language(CXX)
else()
variable_watch(CMAKE_CXX_SOURCE_FILE_EXTENSIONS add_cuda_extensions)
enable_language(CXX)
endif()
###
# Normal project code starts here
message(STATUS "${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")
find_package(Thrust REQUIRED)
thrust_create_target(Thrust FROM_OPTIONS)
add_library(cuda_kernels my_kernels.cu)
target_link_libraries(cuda_kernels PRIVATE Thrust)
This waits for the platform module to try to write CMAKE_CXX_SOURCE_FILE_EXTENSIONS and then any time it's accessed, quickly inserts the cu and CU extensions to the list.

How to use find_package to locate project targets in the same build-tree?

If one has a workspace with multiple CMake packages (i.e. a project with its own CMakeLists.txt), and a top-level CMakeLists.txt building each package in a common build directory through add_subdirectory, is it possible to use find_package from within a package to specify build dependency to another package?
The packages are properly configured to be located by find_package (the targets are exported), but generated on their own subdirectory in the build location (e.g. build/pkg1/kg1Config.cmake) and thus CMake complains that pkg1Config.cmake file can't be found.
EDIT:
Currently I'm checking manually if pkg1 is in the build tree or is available externally:
if (TARGET pkg1)
# if in the build tree / being built
if (DEFINED pkg1_SOURCE_DIR)
set(pkg1_INCLUDE_DIR ${pkg1_SOURCE_DIR}/include)
message(STATUS "pkg1_INCLUDE_DIR: ${pkg1_INCLUDE_DIR}")
else()
message(FATAL_ERROR "pkg1 was NOT FOUND! can't build without it")
endif()
else ()
# search if installed on the system
find_package(pkg1)
if (pkg1_FOUND AND DEFINED pkg1_INCLUDE_DIR)
message(STATUS "pkg1_INCLUDE_DIR: ${pkg1_INCLUDE_DIR}")
else()
message(FATAL_ERROR "pkg1 was NOT FOUND! can't build without it")
endif()
endif()

Integrate CMake subproject with another

I've wrote a C++ library MyLib and I'd like to integrate it with another project ExternPro. So in ExternPro I wrote CMakeLists.txt like this:
add_subdirectory(MyLib)
ADD_EXECUTABLE(test test.cpp)
include_directories(${MyLib_INCLUDE_DIRS})
target_link_libraries(test ${MyLib_LIBRARIES})
To set variables MyLib_LIBRARIES and MyLib_INCLUDE_DIRS I wrote:
set(MyLib_LIBRARIES ${PROJECT_SOURCE_DIR}/src/MyLib.a CACHE INTERNAL "")
set(MyLib_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "")
But something wrong "No rule to make target MyLib/src/MyLib.a, needed by test. Stop."
So my question is, how should I wrote CMakeLists.txt correctly so cmake could help me build MyLib first and then take care of dependencies of ExternPro?
If those are two separate projects, I normally use "CMake find scripts" to reference one library from the other: http://www.vtk.org/Wiki/CMake:How_To_Find_Libraries#Writing_find_modules
But I normally use slightly different find script than described there (FindMyLibrary.cmake):
# Find MyLibrary installation
#
# This module needs following variables specified (e.g. through cmake -Dvar=)
# MyLibrary_ROOT_DIR - root directory of the library installation
#
# This module defines the following variables:
# MyLibrary_INCLUDE_DIRS - Where to find the public headers
# MyLibrary_LIBRARIES - List of mandatory and optional libraries
# MyLibrary_FOUND - True if an installation was found
#
# Configuration variables for tis module:
# MyLibrary_USE_STATIC_LIBS - Set to ON to force the use of the static
# libraries. Default is OFF.
# If MyLibrary_ROOT_DIR was defined in the environment, use it.
if(NOT MyLibrary_ROOT_DIR AND NOT $ENV{MyLibrary_ROOT_DIR} STREQUAL "")
set(MyLibrary_ROOT_DIR $ENV{MyLibrary_ROOT_DIR})
endif()
if(NOT MyLibrary_ROOT_DIR)
set(MyLibrary_ROOT_DIR /usr)
endif()
message(STATUS "Using MyLibrary_ROOT_DIR: ${MyLibrary_ROOT_DIR}")
find_path(MyLibrary_INCLUDE_DIRS
NAMES mylib/mylib.hpp
PATHS ${MyLibrary_ROOT_DIR}
PATH_SUFFIXES include)
# Here we set the default components
if(NOT MyLibrary_FIND_COMPONENTS)
set(MyLibrary_FIND_COMPONENTS mylibrary)
endif()
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
if(MyLibrary_USE_STATIC_LIBS)
set(_mylib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endif()
foreach(COMPONENT ${MyLibrary_FIND_COMPONENTS})
find_library(MyLibrary_${COMPONENT}_LIBRARY
NAMES ${COMPONENT}
HINTS ${MyLibrary_ROOT_DIR}
PATH_SUFFIXES lib64 lib
NO_DEFAULT_PATH)
set(MyLibrary_LIBRARIES ${MyLibrary_LIBRARIES} ${MyLibrary_${COMPONENT}_LIBRARY})
endforeach()
# Restore the original find library ordering
if(MyLibrary_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_mylib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
endif()
# handle the QUIETLY and REQUIRED arguments and set MyLibrary_FOUND to
# TRUE if all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
MyLibrary "Could NOT find MyLibrary: set MyLibrary_ROOT_DIR to a proper location"
MyLibrary_LIBRARIES
MyLibrary_INCLUDE_DIRS)
mark_as_advanced(MyLibrary_INCLUDE_DIRS MyLibrary_LIBRARIES)
Then used like this:
find_package(MyLibrary REQUIRED)
include_directories(SYSTEM ${MyLibrary_INCLUDE_DIRS})
target_link_libraries(${TARGET}
${MyLibrary_LIBRARIES}
)
Basically, it goes like this:
The library is built and installed (make install) to either the default location (e.g. /usr), or some other location (usual in development).
The FindMyLibrary.cmake is part of the library installation (for RPM the library devel package) and installs as well (into ${instdir}/share/cmake/Modules, for example).
The dependent project then adds the path to the CMAKE_MODULE_PATH and uses the find script to find the public headers and libraries as installed.
The advantage is that this way you can either use it during the development (when you have the library sources and build the library), or without the library sources as well (with just the library and headers - the devel package - installed in the system).
Similar technique is normally used by Boost etc. (the find scripts provided already by the CMake).
Instead of path to the library, use library target for target_link_libraries.
Assuming your library project contains
add_library(MyLib ...)
Linking of executable in main project should be performed with
target_link_libraries(test MyLib)
First of all this doesn't work because variables set in a subdirectory are not set for the parent directory.
So to solve this properly you should define MyLib like:
add_library(MyLib ...)
target_include_directories(MyLib INTERFACE ${PROJECT_SOURCE_DIR}/include)
And for the ExternPro you just need to link to MyLib:
target_link_libraries(test MyLib)
This will automatically add the include directory to test and link MyLib properly.

Check for optionally targets in cmake that are not in the correct order

i'm currently working on a large software project which uses cmake as build system. But i have a problem to check if another target exists (or will exist).
For example there is root CMakeLists.txt and two modules that can optionally added to the software project as subfolders.
.
├── A
│   └── CMakeLists.txt
├── B
│   └── CMakeLists.txt
└── CMakeLists.txt
In the root CMakeLists these modules are added with the add_subdirectory command:
cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
project(root)
add_subdirectory(./A)
add_subdirectory(./B)
In some cases i want to check in module A if module B exists and add an define to the compile options in module A:
cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
project(A)
add_libary(A a.cpp a.hpp)
if (TARGET B)
target_compile_definitions(A PUBLIC HAVE_B)
endif()
The
if(TARGET target-name)
command will return false because this will only work if the modules are added in the right order to the root CMakeLists.txt.
Is there another check in cmake that doesn't depend on the order of the targets?
Greetings
Perry
This can be solved using a macro.
The idea is that each subdirectory must have an associated top-level variable that other subdirectories can check, so they must be declared on the top CMakeLists.txt file. Also all variables must be defined before the start of the add_subdirectory rules.
So our macro will take as parameters the list of subdirectories, first output the list of variable declarations, and then output the list of subdirectories.
Here is the code for such a macro:
macro(declare_modules)
foreach(module ${ARGN})
set(TARGET_${module} yes)
endforeach()
foreach(module ${ARGN})
add_subdirectory(./${module})
endforeach()
endmacro(declare_modules)
It can be used like this:
declare_modules(A B C)
And it will expand into the following code:
foreach(module A B C)
set(TARGET_${module} yes)
endforeach()
foreach(module A B C)
add_subdirectory(./${module})
endforeach()
This will result into the variables TARGET_A, TARGET_B, and TARGET_C to be declared and set to yes, and they will be available from all modules.
Adding new modules is just a matter of adding new parameters to the declare_modules call.
Because the root CMakeLists.txt file have to know the project's subdirectories, I normally put my optional settings there:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
project(root CXX)
if (EXISTS "B/CMakeLists.txt")
set(HAVE_B 1)
endif()
add_subdirectory(A)
if (HAVE_B)
add_subdirectory(B)
endif()
A/CMakeLists.txt:
add_library(A a.cpp a.hpp)
if (HAVE_B)
target_link_libraries(A B)
endif()
B/CMakeLists.txt:
add_library(B b.cpp b.hpp)
target_compile_definitions(B PUBLIC HAVE_B)
target_include_directories(B PUBLIC .)
There are a lot of possibile places you could set the compiler definitions and necessary include directories. In this example I've choosen to propagade both with target_compile_definitions(... PUBLIC ...) and target_include_directories(... PUBLIC ...). Then you just have to setup the dependency with target_link_libraries() and CMake handles the rest (and target_link_libraries() does accept forward declarations).
There is no available $<TARGET_EXISTS:...> generator expression where you could write this:
add_libary(A a.cpp a.hpp)
target_compile_definitions(A PUBLIC $<<TARGET_EXISTS:B>:HAVE_B=1>)
Although I am proposing this to cmake, here. As a workaround, you can create shadow targets to track the targets from other directories:
# Custom property to check if target exists
define_property(TARGET PROPERTY "INTERFACE_TARGET_EXISTS"
BRIEF_DOCS "True if target exists"
FULL_DOCS "True if target exists"
)
# Create shadow target to notify that the target exists
macro(shadow_notify TARGET)
if(NOT TARGET _shadow_target_${TARGET})
add_library(_shadow_target_${TARGET} INTERFACE IMPORTED GLOBAL)
endif()
set_target_properties(_shadow_target_${TARGET} PROPERTIES INTERFACE_TARGET_EXISTS 1)
endmacro()
# Check if target exists by querying the shadow target
macro(shadow_exists OUT TARGET)
if(NOT TARGET _shadow_target_${TARGET})
add_library(_shadow_target_${TARGET} INTERFACE IMPORTED GLOBAL)
set_target_properties(_shadow_target_${TARGET} PROPERTIES INTERFACE_TARGET_EXISTS 0)
endif()
set(${OUT} "$<TARGET_PROPERTY:_shadow_target_${TARGET},INTERFACE_TARGET_EXISTS>")
endmacro()
This creates two functions, and a custom property to track if the target exists or not. The shadow_notify will create a shadow target if it doesn't exists and then set the INTERFACE_TARGET_EXISTS property to true if it does exists.
The shadow_exists target will create a generator expression to query if the target exists. It does this by creating a new shadow target if doesn't exists with the INTERFACE_TARGET_EXISTS property set to 0. Then it will create a generator expression that will query the INTERFACE_TARGET_EXISTS property on the shadow target. The shadow target will always exists, and if the actual target exists then the property will be set to 1 by shadow_notify.
So then you can write this in your cmake:
add_libary(A a.cpp a.hpp)
# Notify that target A exists
shadow_notify(A)
# Check if target B exists
shadow_exists(HAS_B B)
target_compile_definitions(A PUBLIC $<${HAS_B}:HAVE_B=1>)
When you create library B you will need to call shadow_notify(B) as well.
If you don't like these extra shadow_notify calls, you could also override add_library to do it for you:
macro(add_library LIB)
_add_library(${LIB} ${ARGN})
if(NOT TARGET _shadow_target_${TARGET})
_add_library(_shadow_target_${TARGET} INTERFACE IMPORTED GLOBAL)
endif()
set_target_properties(_shadow_target_${TARGET} PROPERTIES INTERFACE_TARGET_EXISTS 1)
endmacro()

OpenCV_FOUND to FALSE so package "OpenCV" is considered to be NOT FOUND

I am having problems at the moment trying to get my cmake to see my opencv.
I have installed opencv and can run the some of the sample problems and some give the same error as the the error I get in my cmake file (when running the sample programs through terminal)
I have tried to change the environment variable path as described in
http://answers.opencv.org/question/35125/cmake-linking-error-opencv_found-to-false-ubuntu/
My bashrc file now looks like
CMAKE_PREFIX_PATH=/home/durham/Desktop/OpenCV/opencv-2.4.9:$CMAKE_PREFIX_PATH
CPATH=/home/durham/Desktop/OpenCV/opencv-2.4.9/include:$CPATH LD_LIBRARY_PATH=/home/durham/Desktop/OpenCV/opencv-2.4.9/lib:$LD_LIBRARY_PATH PATH=/home/durham/Desktop/OpenCV/opencv-2.4.9bin:$PATH
PKG_CONFIG_PATH=/home/durham/Desktop/OpenCV/opencv-2.4.9/lib/pkgconfig:$PKG_CONFIG_PATH
PYTHONPATH=/home/durham/Desktop/OpenCV/opencv-2.4.9/lib/python2.7/dist-packages:$PYTHONPATH
and the contents of /etc/ld.so.conf are
include /etc/ld.so.conf.d/*.conf
include /home/durham/Desktop/OpenCV/opencv-2.4.9
The cmake file I am trying to run looks like this
cmake_minimum_required(VERSION 2.6)
if(POLICY CMP0020) cmake_policy(SET CMP0020 NEW) endif(POLICY CMP0020)
SET(CMAKE_VERBOSE_MAKEFILE TRUE) SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/config)
ADD_DEFINITIONS(-DQT_THREAD_SUPPORT -D_REENTRANT -DQT_NO_DEBUG
-DIQRMODULE)
SET(QT_MT_REQUIRED TRUE) find_package(Qt5Widgets) FIND_PACKAGE(OpenCV REQUIRED)
IF(NOT DEFINED IQR_INCLUDE_DIR) set (IQR_INCLUDE_DIR "/usr/include/iqr") #default for linux ENDIF(NOT DEFINED IQR_INCLUDE_DIR)
IF(NOT EXISTS ${IQR_INCLUDE_DIR}) message(STATUS "not exists IQR_INCLUDE_DIR: ${IQR_INCLUDE_DIR}") set (IQR_INCLUDE_DIR $ENV{IQR_INCLUDE_DIR} CACHE PATH "" FORCE) IF(NOT EXISTS ${IQR_INCLUDE_DIR})
message(STATUS "IQR_INCLUDE_DIR set to ${IQR_INCLUDE_DIR}")
message(FATAL_ERROR "Please specify iqr include directory using IQR_INCLUDE_DIR env. variable") ENDIF(NOT EXISTS ${IQR_INCLUDE_DIR}) ENDIF(NOT EXISTS ${IQR_INCLUDE_DIR})
IF(WIN32) IF(NOT DEFINED IQR_LIB_DIR)
set (IQR_LIB_DIR $ENV{IQR_LIB_DIR} CACHE PATH "" FORCE) ENDIF(NOT DEFINED IQR_LIB_DIR)
IF(NOT EXISTS ${IQR_LIB_DIR})
message(FATAL_ERROR "Please specify phidgets include directory using IQR_LIB_DIR env. variable") ENDIF(NOT EXISTS ${IQR_LIB_DIR}) ENDIF(WIN32)
SET(libSrc
moduleArDroneBottomCamera.cpp
)
INCLUDE_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${IQR_INCLUDE_DIR} ${QT_INCLUDE_DIR} ${OPENCV_INCLUDE_DIR} ardrone_sdk/ ardrone_sdk/VP_SDK/ ardrone_sdk/VLIB/Stages/ ardrone_sdk/VP_SDK/VP_Os/ ardrone_sdk/VP_SDK/VP_Os/linux/ ardrone_sdk/VP_SDK/VP_Stages/ )
ADD_SUBDIRECTORY(ardrone_sdk)
ADD_LIBRARY(moduleArDroneBottomCamera SHARED ${libSrc})
IF(WIN32) set(IQR_LIBS "${IQR_LIB_DIR}/libIqrItem.dll") ENDIF(WIN32)
TARGET_LINK_LIBRARIES (moduleArDroneBottomCamera ${OPENCV_LIBRARIES} pc_ardrone ${QT_LIBRARIES} ${IQR_LIBS} )
qt5_use_modules(moduleArDroneBottomCamera Core Widgets Network)
SET_TARGET_PROPERTIES(moduleArDroneBottomCamera PROPERTIES PREFIX "")
IF(UNIX) set (IQR_INSTALL_DIR $ENV{HOME}) ENDIF(UNIX)
IF(WIN32) set (IQR_INSTALL_DIR $ENV{USERPROFILE}) ENDIF(WIN32)
INSTALL(TARGETS moduleArDroneBottomCamera LIBRARY DESTINATION ${IQR_INSTALL_DIR}/iqr/lib/Modules RUNTIME DESTINATION ${IQR_INSTALL_DIR}/iqr/lib/Modules )
But when I try to generate this using the cmake gui I get the following output (cant post images yet so its in the link)
http://postimg.org/image/4e553z6rh/
I am running Ubuntu 14.04. Any suggestions?
Thanks
D
Fast and dirty solution: try to install opencv (You know, make && sudo make install). After installation header files should be inc /usr/local/include and library files should be in /usr/local/lib .
The problem might lay somewhere ind FindOpenCV.cmake file, so You might as well try to understand what it is doing and maybe fix it - CMake syntax is fairly straightforward.
It might check in a default instalation location instead where it lies right now, or some rarely used environment variable.