how cmake Error in compiling for riscv64-unknown-elf-g++ - cmake

The error looks like the below when I cmake .. from the terminal:
(base) k:~ cd /Users/yuli/Documents/version3/cpp/
(base) k:~ ls
CMakeLists.txt src
riscv-gnu-toolchain test
(base) k:~ mkdir build
(base) k:~ cd build
(base) k:~ cmake ..
-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
RegularExpression::compile(): Nested *?+.
RegularExpression::compile(): Error in compile.
CMake Error at CMakeLists.txt:10 (if):
if given arguments:
"/Library/Developer/CommandLineTools/usr/bin/c++" "MATCHES" ".*riscv64-unknown-elf-g++"
Regular expression ".*riscv64-unknown-elf-g++" cannot compile
-- Configuring incomplete, errors occurred!
See also "/Users/yuli/Documents/version3/cpp/build/CMakeFiles/CMakeOutput.log".
I tried "STREQUAL" instead of MATCHES but didn't work. any idea what might be wrong in here?
CMakeLists.txt as follows:
cmake_minimum_required(VERSION 3.13)
project(ofdmchain)
set(CMAKE_CXX_FLAGS "-std=c++14")
add_definitions(-DCOMPILES_ON_PC -Wall -Wextra)
configure_file(${CMAKE_SOURCE_DIR}/src/config.hpp.in ${CMAKE_BINARY_DIR}/config.hpp)
if("${CMAKE_CXX_COMPILER}" MATCHES ".*riscv64-unknown-elf-g++")
message(STATUS "Compiling RISCV")
SET(RISCV 1)
else()
message(STATUS "Compiling X86")
SET(RISCV 0)
endif()
add_library(ofdm
src/transmitter.cpp
src/configuration.cpp
src/ofdm.hpp
src/datatypes.hpp
)
target_include_directories(ofdm PRIVATE ${CMAKE_SOURCE_DIR}/../reference_matlab)
target_include_directories(ofdm PRIVATE ${CMAKE_SOURCE_DIR}/src/)
if (NOT ${RISCV})
add_executable(unit_tests
test/catch_main.cpp
test/test_sanity.cpp
test/test_utilities.cpp
test/test_transmitter.cpp
)
target_include_directories(unit_tests PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(unit_tests ofdm)
endif()
I have also tried adding ++ where g++ exits as well as - where - exists. And also have tired "STREQUAL" instead of "MATCHES" it didn't work either. It could be the problem of c++ in the PATH?

CMake "internal error"
RegularExpression::compile(): Nested *?+.
is about the last two + characters: in regular expressions such characters has special meaning.
Ways for make + character (and other special characters) to be treated literally:
Escape the character with \. Note, that escape character by itself should be escaped in CMake string, so you need to write it twice:
if("${CMAKE_CXX_COMPILER}" MATCHES ".*riscv64-unknown-elf-g\\+\\+")
Put the character into square brackets ([]). Inside [] all special characters loose their special meaning:
if("${CMAKE_CXX_COMPILER}" MATCHES ".*riscv64-unknown-elf-g[+][+]")
Note also, that in certain if conditions one doesn't need to explicitly dereference the variable: CMake does that by itself.
So, it is possible to write
if(CMAKE_CXX_COMPILER MATCHES ".*riscv64-unknown-elf-g[+][+]")
See more in the documentation for if command.

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
...

Finding GLib with CMake: target_include_directories called with invalid arguments

I have this following (minimal) CMakeLists.txt supposed to find GLib via pkg-config and add the libs to the foo target:
cmake_minimum_required(VERSION 2.10 FATAL_ERROR)
project(foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLib REQUIRED glib-2.0)
add_executable(foo foo.cpp)
message(WARNING "libs:" ${GLIB_LIBRARIES})
message(WARNING "includes:" ${GLIB_INCLUDE_DIRS})
target_link_libraries(foo PUBLIC ${GLIB_LIBRARIES})
target_include_directories(foo PUBLIC ${GLIB_INCLUDE_DIRS})
No matter what I try, I get (note the Found glib-2.0 part):
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.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
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'glib-2.0'
-- Found glib-2.0, version 2.56.1
CMake Warning at CMakeLists.txt:6 (message):
libs:
CMake Warning at CMakeLists.txt:7 (message):
includes:
CMake Error at CMakeLists.txt:9 (target_include_directories):
target_include_directories called with invalid arguments
-- Configuring incomplete, errors occurred!
See also "/tmp/aaa/CMakeFiles/CMakeOutput.log".
and I cannot see, reading the CMake reference what arguments are invalid (note: this question is different from cmake target_include_directories called with invalid arguments). I also looked at CMake's FindPkgConfig documentation which gives glib as an example and I am not able to reproduce it (${GLIB_VERSION}). I tried GLIB_ and GLIB2_ prefixes and all I get is empty strings.
The messages show the variables are empty, though pkg-config reports values correctly:
$ pkg-config glib-2.0 --cflags --libs
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
The CMake version I have is 2.10.
Can someone shed light on the issue?
I think your problem is the casing of GLIB_INCLUDE_DIRS and GLIB_LIBRARIES. They should be GLib_INCLUDE_DIRS and GLib_LIBRARIES since you specified "GLib" as the first argument of pkg_check_modules. I'm guessing that the target_include_directories() doesn't like not getting any arguments after the
PUBLIC (although it works for me in cmake 3.5).

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

add_custom_command not working

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

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 .