add_custom_command not working - cmake

I am trying to understand add_custom_command usage, I wrote a minimal make file (CMakeLists.txt)
like shown below
When I run the make file using
$cmake.
$make
I don't see perfecthash.cpp being created and the build breaks
cmake_minimum_required(VERSION 2.8)
# Call add_custom_command() with appropriate arguments for generate output file
# Note, that *gperf* will work in the build tree,
# so for file in the source tree full path should be used.
function(gperf_generate_new input output)
MESSAGE("debugging function")
add_custom_command(
OUTPUT ${output}
COMMAND gperf -L c++ ${input} > ${output}
DEPENDS ${input}
COMMENT "printing ${output}" # Just for nice message during build
)
endfunction()
# Generate *example.hpp* file ...
gperf_generate_new(command_options.new.gperf pefecthash.hpp)
# ... for use it in executable
add_custom_target(my_target
ALL # Force target to be built with default build target.
DEPENDS perfecthash.hpp
)
$cmake .
$make
give the below error
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
debugging function
-- Configuring done
-- Generating done
-- Build files have been written to: /home/srinivas
Scanning dependencies of target my_target
make[2]: *** No rule to make target `perfecthash.hpp', needed by `CMakeFiles/my_target'. Stop.
make[1]: *** [CMakeFiles/my_target.dir/all] Error 2
make: *** [all] Error 2

Just a typo:
gperf_generate_new(command_options.new.gperf pefecthash.hpp)
...
DEPENDS perfecthash.hpp

Related

How can I pass variables with space to cmake through CLI?

There is an option defined in a third party CMake file.
SET(PHYSX_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -ffunction-sections -fdata-sections -fno-strict-aliasing ${GCC_WARNINGS}" CACHE INTERNAL "PhysX CXX")
I am trying to pass custom flags to CMAKE_CXX_FLAGS:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS="-Wno-restrict -Wno-class-memaccess"
Because I have space between the two GCC warning flags, the final result ends up breaking the string:
CXX_FLAGS = "-Wno-restrict ... # Quote is not closed
and I get an error about unterminated string
WHen I use no quotes around the flag CMake variable value:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS=-Wno-restrict -Wno-class-memaccess
The final result ignores the value after space, which makes sense since space will be treated as a separate variable.
Is there something that I can do to set values with space into CMake variable?
Thanks #KamilCuk,
My problem was with the Python script that was triggering the commands. I had the following line in Python:
subprocess.run(parsedCmdLine.split(' '), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
Luckily, Python has a lexical utility module that resembles shell:
import shlex
subprocess.run(shlex.split(parsedCmdLine.split), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
You need to quote the whole -D parameter, like:
$ cmake ../ '-DCMAKE_CXX_FLAGS=-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer'
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/baiyanh/arena/cmake/tut/build
Then the compile looks like:
$ cmake --build . -v
...
[ 50%] Building CXX object CMakeFiles/tut.dir/tut.cpp.o
/usr/bin/c++ -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -o CMakeFiles/tut.dir/tut.cpp.o -c /home/baiyanh/arena/cmake/tut/tut.cpp
...

Is it possible to call a custom CMake function when library is linked?

Let's say I have a library A and a project B which uses A.
project(libA)
function(calledWhenLink target)
message(STATUS Hi ${target}) # Should print "Hi exeB"
endfunction()
add_library(libA INTERFACE)
project(exeB)
find_package(A REQUIRED)
target_link_library(exeB libA)
Is it possible to automatically call calledWhenLink() when the executable links to the library ?
Reasoning: As said in a comment, the original problem is that VTK needs to call CMake vtk_module_autoinit. My library uses VTK, and it seems better for all the samples to automatically call this when linked to the lib instead of copy paste the code in each CMakeLists. The problem is that vtk_module_autoinit is not working when called with the lib target instead ot the exe in my tests.
Yes, it is possible to run arbitrary CMake code at build time. However, it is not at all clear what you're really trying to do, so this might not be a good approach.
Here's a minimal example:
cmake_minimum_required(VERSION 3.21)
project(example)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/post-link.cmake" [[
message(STATUS "Hi ${target}")
]])
add_executable(exeB main.cpp)
add_custom_command(
TARGET exeB POST_BUILD
COMMAND "${CMAKE_COMMAND}"
-Dtarget=exeB
-P "${CMAKE_CURRENT_BINARY_DIR}/post-link.cmake"
)
Test interaction:
alex#alex-ubuntu:~/test$ cmake -G Ninja -S . -B build
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/build
alex#alex-ubuntu:~/test$ cmake --build build/ --verbose
[1/2] /usr/bin/c++ -MD -MT CMakeFiles/exeB.dir/main.cpp.o -MF CMakeFiles/exeB.dir/main.cpp.o.d -o CMakeFiles/exeB.dir/main.cpp.o -c /home/alex/test/main.cpp
[2/2] : && /usr/bin/c++ CMakeFiles/exeB.dir/main.cpp.o -o exeB && cd /home/alex/test/build && /usr/bin/cmake -Dtarget=exeB -P /home/alex/test/build/post-link.cmake
-- Hi exeB

Having problems with mingw32-make to make googletest

This is my current environment.
Windows 10 64bit, MinGW, CMake 3.15.2, Python 3.7.
I downloaded googletest from https://github.com/google/googletest.
I ran cmake and the following is the output.
C:\googletest-master>cd build
C:\googletest-master\build>cmake ../ -G "MinGW Makefiles"
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: C:/Users/sangjin/AppData/Local/Programs/Python/Python37-32/python.exe (found version "3.7.4")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/googletest-master/build
But when I ran mingw32-make I got the following error message.
C:\googletest-master\build>mingw32-make
Scanning dependencies of target gtest
[ 12%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
In file included from C:\googletest-master\googletest\src\gtest-all.cc:41:
C:/googletest-master/googletest/src/gtest.cc:86:11: fatal error: crtdbg.h: No such file or directory
# include <crtdbg.h> // NOLINT
^~~~~~~~~~
compilation terminated.
googletest\CMakeFiles\gtest.dir\build.make:62: recipe for target 'googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj' failed
mingw32-make[2]: *** [googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj] Error 1
CMakeFiles\Makefile2:171: recipe for target 'googletest/CMakeFiles/gtest.dir/all' failed
mingw32-make[1]: *** [googletest/CMakeFiles/gtest.dir/all] Error 2
Makefile:139: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
C:\googletest-master\build>
Can anyone help me fix this problem.
This version of google test doesn't have that issue, try to build it
https://github.com/google/googletest/tree/216c37f057ae0fff38062984c890df912f40ccf6
Here is a solution for
mingw32-base version 2013072200
mingw32-gcc-g++ version 6.3.0-1
gtest version 1.10.0
In ...\googletest\src\gtest.cc
first at (original) line 86, make header inclusion conditional to MSC toolchain using #ifdef _MSC_VER ... #endif preprocessor macros.
#ifdef _MSC_VER
# include <crtdbg.h> // NOLINT
# include <debugapi.h> // NOLINT
#endif // _MSC_VER
then at (original) line 4913, make assertion redirection also conditional.
#ifdef _MSC_VER
// In debug mode, the Windows CRT can crash with an assertion over invalid
// input (e.g. passing an invalid file descriptor). The default handling
// for these assertions is to pop up a dialog and wait for user input.
// Instead ask the CRT to dump such assertions to stderr non-interactively.
if (!IsDebuggerPresent()) {
(void)_CrtSetReportMode(_CRT_ASSERT,
_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
(void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
}
#endif // _MSC_VER
With theese changes, unit tests build and execute as expected.

CMake collect and process all source files in top directory

I am trying to collect and process all source files in the top directory, to deal with the fact that variables are not passed to sub directories. I have cobbled together the following CMakeLists.txt files:
cmake_minimum_required(VERSION 2.8)
project(main)
enable_language(Fortran)
enable_testing()
set (CMAKE_Fortran_COMPILER ifort)
set (CMAKE_Fortran_FLAGS " -g -C -fixed")
set (CMAKE_Fortran95_FLAGS " -openmp ")
# function to collect all the sources from sub-directories
# into a single list
function(add_sources)
get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY SRCS_LIST
BRIEF_DOCS "List of source files"
FULL_DOCS "List of source files to be compiled in one library")
endif()
# make absolute paths
set(SRCS)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND SRCS "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}")
endfunction(add_sources)
add_sources(SRCS main.f95)
add_subdirectory(sub)
# preprocess sources
set(PREP_SRCS)
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(s IN LISTS SRCS)
file(RELATIVE_PATH rs "${CMAKE_CURRENT_SOURCE_DIR}" "${s}")
string(REGEX REPLACE "f95$" "f" o "${CMAKE_CURRENT_BINARY_DIR}/${rs}")
add_custom_command(
OUTPUT "${o}"
COMMAND ${CMAKE_COMMAND} -E copy "${s}" "${o}"
DEPENDS "${s}"
if(${s} MATCHES "f95$")
set_source_files_properties( ${o} PROPERTIES
COMPILE_FLAGS ${CMAKE_Fortran95_FLAGS})
endif(${s} MATCHES "f95$")
COMMENT "Creating ${o}"
VERBATIM
)
list(APPEND PREP_SRCS "${o}")
endforeach()
message(${PREP_SRCS})
add_executable(main ${PREP_SRCS})
SET_TARGET_PROPERTIES(main PROPERTIES LINKER_LANGUAGE Fortran)
SET_TARGET_PROPERTIES(main PROPERTIES
LINK_FLAGS " ")
and in sub/
add_sources(SRCS saxpy2.f95 saxpy_noomp.f)
which cmakes correctly:
[baron#ray:1034]$ cmake ..
-- The C compiler identification is AppleClang 7.0.2.7000181
-- The CXX compiler identification is AppleClang 7.0.2.7000181
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The Fortran compiler identification is Intel 15.0.1.20141022
-- Check for working Fortran compiler: /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort
-- Check for working Fortran compiler: /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort supports Fortran 90
-- Checking whether /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort supports Fortran 90 -- yes
/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/SRCS/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/main.f/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/SRCS/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/saxpy2.f/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/saxpy_noomp.f
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/baron/teach/comps/ompstuff/new_new_cmake_test/build
[baron#ray:1035]$ make
make[2]: *** No rule to make target `../)', needed by `sub/saxpy_noomp.f'. Stop.
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
[baron#ray:1036]$
but obviously something subtle is missing. TIA

Why does Cmake Always Choose GCC?

For reasons that I cannot completely understand, Cmake awlays chooses the GNU compiler toolset when compiling software.
My enviroment looks like this:
which cc
/opt/cray/xt-asyncpe/4.9/bin/cc
which CC
/opt/cray/xt-asyncpe/4.9/bin/CC
echo $CC
/opt/cray/xt-asyncpe/4.9/bin/cc
echo $CXX
/opt/cray/xt-asyncpe/4.9/bin/CC
but when I use cmake I get this
Using existing /opt/cmake/2.8.4/bin/cmake
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
And it builds all the software with g++ commands. Why is this going on? How does one set the compiler?
You can also set the env vars CC and CXX much like autotools.
CC=cc CXX=CC cmake ...
Make sure you start with an empty build tree.
I'm not sure why CMake favours GCC.
However, to set the compilers, use:
cmake <path to CMakeLists.txt> -DCMAKE_C_COMPILER=/opt/cray/xt-asyncpe/4.9/bin/cc -DCMAKE_CXX_COMPILER=/opt/cray/xt-asyncpe/4.9/bin/CC
These values will be cached, so subsequent runs of CMake (if required) can simply be invoked by:
cmake .