Internal LLVM syntax errors when following Pass tutorial using CMake - cmake

I am attempting to follow the tutorial here for developing a "Hello, World" LLVM pass - I am using the guidelines linked by that tutorial here for doing so out of the LLVM source directory. However, when I attempt to follow this tutorial, CMake reports a number of errors internal to LLVM itself.
I have the following directory structure:
HelloWorld/
CMakeLists.txt
HelloWorld/
CMakeLists.txt
HelloWorld.cpp
My HelloWorld.cpp, and the two CMakeLists.txt are copy and pasted directly from the tutorials linked to above.
I run CMake HelloWorld and it successfully generates a CMake configuration. However, when I run make. I get numerous errors reported from within the LLVM codebase itself.
[ 50%] Building CXX object CMakeFiles/LLVMPassName.dir/Vectorize.cpp.o
In file included from /Volumes/andromeda/HelloWorld/HelloWorld.cpp:1:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/Pass.h:377:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/PassSupport.h:27:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/PassRegistry.h:20:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm-c/Core.h:18:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm-c/Support.h:17:
/usr/local/Cellar/llvm/3.6.2/include/llvm/Support/DataTypes.h:57:3: error: "Must #define
__STDC_LIMIT_MACROS before #including Support/DataTypes.h"
# error "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"
^
/usr/local/Cellar/llvm/3.6.2/include/llvm/Support/DataTypes.h:61:3: error: "Must #define
__STDC_CONSTANT_MACROS before " "#including Support/DataTypes.h"
# error "Must #define __STDC_CONSTANT_MACROS before " \
The list goes on and on and all of them refer to errors in LLVM header files. This is a clean install of LLVM using Homebrew. To get linking to work, I had to set CPLUS_INCLUDE_PATH to the Homebrew include directory for LLVM.
My first thought was that CMake was attempting to use a different compiler (Clang vs. GCC or vice versa), but setting CMAKE_CXX_COMPILER to point to either my clang or g++ installation did not help.
Does anyone have any ideas for what might be the problem here?
After following the link provided by #oak in the comments, I was able to get rid of the first two Support/DataType errors. However, many of the errors still remain.
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/Pass.h:377:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/PassSupport.h:27:
In file included from /usr/local/Cellar/llvm/3.6.2/include/llvm/PassRegistry.h:21:
/usr/local/Cellar/llvm/3.6.2/include/llvm/ADT/DenseMap.h:543:63: error: a space is required
between consecutive right angle brackets (use '> >')
typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
^
/usr/local/Cellar/llvm/3.6.2/include/llvm/ADT/DenseMap.h:694:63: error: a space is required
between consecutive right angle brackets (use '> >')
typename BucketT = detail::DenseMapPair<KeyT, ValueT>>

So, after much research, it turns out that there is an inconsistency with how LLVM and CMake support out-of-source builds. The LLVM binaries are built with -fno-rtti, so CMake will complain about missing symbols unless it is also uses -fno-rtti when compiling the LLVM pass.
I fixed all of my troubles (including those solved by the temporary fix proposed by Oak) by adding SET(CMAKE_CXX_FLAGS "-Wall -fno-rtti") to my CMakeLists.txt file in the innermost directory.
This was inspired by this question also on StackOverflow.

Related

add_subdirectory for cmake tensorflow lite fails on subsequent runs

I am trying to include tensorflow-lite in a project per the minimal example here: Build TensorFlow Lite with CMake. Specifically, I'm trying to add_subdirectory the CMakeLists.txt for tflite, as recommended.
This works when the project is first built, but if I change my top-level CMakeLists.txt for any reason (adding a test or linking something to a different target, say), then the build fails with the following:
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at build/abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Standard libraries to link to explicitly: none
-- The Fortran compiler identification is GNU 9.4.0
-- Could NOT find CLANG_FORMAT: Found unsuitable version "0.0", but required is exact version "9" (found CLANG_FORMAT_EXECUTABLE-NOTFOUND)
--
-- Configured Eigen 3.4.90
--
-- Proceeding with version: 2.0.6.v2.0.6
-- CMAKE_CXX_FLAGS: -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow -faligned-new -Werror=implicit-fallthrough=2 -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter -fsigned-char
CMake Error at build/cpuinfo/CMakeLists.txt:262 (ADD_SUBDIRECTORY):
ADD_SUBDIRECTORY not given a binary directory but the given source
directory "/opt/------/workspace/------/build/clog-source"
is not a subdirectory of
"/opt/------/workspace/------/build/cpuinfo". When
specifying an out-of-tree source a binary directory must be explicitly
specified.
CMake Error at build/cpuinfo/CMakeLists.txt:265 (SET_PROPERTY):
SET_PROPERTY could not find TARGET clog. Perhaps it has not yet been
created.
-- Configuring incomplete, errors occurred!
See also "/opt/------/workspace/------/build/CMakeFiles/CMakeOutput.log".
See also "/opt/------/workspace/------/build/CMakeFiles/CMakeError.log".
(Those dashes aren't real paths, they're just to mask sensitive info.)
Just to reiterate, it DOES configure and build correctly the first time, but reruns of cmake .. will fail.
I've tried this on a couple of platforms and got the same result. You should be able to reproduce with a minimal CMakeLists.txt containing add_subdirectory and some helloworld target to link tensorflow-lite.
I tried giving the add_subdirectory for clog a binary location but just got a cascade of new errors, and at that point my CMake know-how definitely runs out.
The issue is due to the same cmake variable CLOG_SOURCE_DIR used in both xnnpack and cpuinfo.
In xnnpack, it uses a cmake script to download, configure and build the clog.
In cpuinfo, it add as a subdirectory.
So they use different ways to add dependency. We need to fix this issue in either xnnpack or cpuinfo but as a walkaround, we can change the CLOG_SOURCE_DIR in either project to a different name (e.g. CLOG_SOURCE_DIR1) then this problem will go away.

Unknown CMake Command QT4_ADD_RESOURCE

Very new to make files here, so please forgive me if this is a noob question. I can't seem to find the answer on the internet.
I've forked a public repository from github, and it repository itself is supposed to be download, make and run, but I've already fixed a half dozen errors in their make files trying to get it running. Now I'm down to this one, which seems to be something to do with CMake not finding the right QT4 command.
When I'm in a build folder, the instructions (according to the owner of the repository) says to type "cmake .." - which is an obvious reference to CMakeList.txt in the parent folder. The file is hosted on Github, the line that throws the error is line 94, it says:
QT4_ADD_RESOURCES(OFFNAO_RES_SRCS ${OFFNAO_RES})
I am trying to build this program on Ubuntu 14.04.5 (because that's the OS it was originally built on, I can work on upgrading that after it is working).
The error text it spits out is:
CMake Error at CMakeLists.txt:94(QT$_ADD_RESOURCES):
Unknown CMake command "QT4_ADD_RESOURCES".
-- Configuring incomplete, errors occurred!
I have installed qt4-dev-tools (which also installs all qt4 libraries).
I can't seem to find how to get CMake to recognise QT4 and its commands.
I'm new to CMake and this is the first time I've come across QT4 so I don't know what I'm looking for. Happy to provide any more info if needed. All help is appreciated.
EDIT:
Here's the contents of the offnao CMakeList.txt:
cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR)
PROJECT(OFFNAO)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${CTC_DIR}/libnaoqi/include)
INCLUDE_DIRECTORIES(${CTC_DIR}/zlib/include)
INCLUDE_DIRECTORIES(${CTC_DIR}/../sysroot_legacy/usr/include)
SET(OFFNAO_CXX_SRCS
utils/OverlayPainter.cpp
// contents skipped for brevity
tabs/teamBallTab.cpp
)
SET(OFFNAO_MOC
readers/reader.hpp
// contents skipped for brevity
tabs/teamBallTab.hpp
)
if(CMAKE_TOOLCHAIN_FILE)
list(APPEND OFFNAO_CXX_SRCS tabs/cameraTab.cpp)
list(APPEND OFFNAO_MOC tabs/cameraTab.hpp)
endif(CMAKE_TOOLCHAIN_FILE)
SET(OFFNAO_UI
visualiser.ui
ConnectionBar.ui
tabs/LogTab.ui
tabs/LogsTab.ui
)
SET(OFFNAO_RES
resources/visualiser_resources.qrc
)
# build cxx files for resources
QT4_ADD_RESOURCES(OFFNAO_RES_SRCS ${OFFNAO_RES})
# build ui_XXX files from the XML-style .ui files
QT4_WRAP_UI(OFFNAO_UI_SRCS ${OFFNAO_UI})
# this moc's the above variable and appends to the cxx sources
QT4_WRAP_CPP(OFFNAO_MOC_SRCS ${OFFNAO_MOC})
ADD_EXECUTABLE(offnao.bin ${OFFNAO_CXX_SRCS} ${OFFNAO_RES_SRCS} ${OFFNAO_MOC_SRCS} ${OFFNAO_UI_SRCS})
cotire(offnao.bin)
set_source_files_properties(
tabs/graphTab.cpp
tabs/plots.cpp
tabs/walkTab.cpp
tabs/zmpTab.cpp
main.cpp
visualiser.cpp
${OFFNAO_MOC_SRCS} #too lazy to split and list them
PROPERTIES COMPILE_FLAGS "-I${QWT_INCLUDE_DIR}")
find_library ( QGLVIEWER_LIBRARY NAMES QGLViewer qglviewer-qt4 )
find_package ( OpenGL REQUIRED )
find_package ( PNG REQUIRED )
TARGET_LINK_LIBRARIES(
offnao.bin
${QT_LIBRARIES}
${QWT_LIBRARY}
${QGLVIEWER_LIBRARY}
Furthermore, the README that states to build this is rather vague, it doesn't actually say what folder to run these commands from. It states:
Welcome to Off-Nao, the rUNSWift debugging toolsuite.
To build this project, either:
You get lucky and bin/build_setup.sh just works :D
(verified under Ubuntu 14.04.1 LTS both natively and in VMs;
but builds successfully then segfaults at runtime in libGL.so.1
under fresh download of 14.04.3 LTS # 15/9/2015
according to gdb - so much for Ubuntu being stable).
---OR---
You need Qt4 and probably a bunch of other things like QGLViewer to build it natively:
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./offnao
Once you have performed the steps above once, in future you only need to:
$ make
$ ./offnao
In the 'build' directory
I do get the segfault as mentioned in the README at LibGL.so.1 so I've attempted to follow the instruction below it. I can only get the the "cmake .." stage and that's where I get the error.
Reformulating my previous comment as answer:
To use macros QT4_ADD_RESOURCES, QT4_WRAP_UI, QT4_WRAP_CPP and others you need to call find_package(Qt4 REQUIRED) first in your CMakeLists.txt. See the documentation for the FindQt4.cmake module (https://cmake.org/cmake/help/v3.0/module/FindQt4.html).

How do I get a verbose output for CMake?

I would like to investigate why I have this error:
$ cmake ..
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc
-- Check for working C compiler: /cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.6.2/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc" is not
able to compile a simple test program.
Unfortunately after the error:
I have no idea of what CMake did. I don't have a verbose log of the command it executed.
The CMakeFiles/cmTC_e4aa4.dir was cleaned after the error, so I have no possibility to explore the issue myself.
How should I investigate such an error?
I tried to use the --debug-trycompile option. This time CMake creates a CMakeTmp folder which makes perfectly without errors. However, I still have this CMakeFiles/cmTC_e4aa4.dir that generates errors and even with the option CMake unlinks the folder.
Getting a Verbose Log
The try_compile() calls that CMake does in the beginning to test the compiler, gives a detailed error output on the console and writes it to
[your binary output directory]/CMakeFiles/CMakeError.log
I've checked the source code again and there is no CMake option that would give more a more detailed output for CMake's internal try_compile() calls.
You could just force the output to standard output by adding some variable_watch() calls to your main CMakeLists.txt before your project() call like:
variable_watch(__CMAKE_C_COMPILER_OUTPUT)
variable_watch(__CMAKE_CXX_COMPILER_OUTPUT)
Keeping the Temporary Files
To keep the temporary file of try_compile, add --debug-trycompile to the cmake command line.
But be aware that the multiple compiler tests at the beginning overwrite the artifacts of previous ones:
It may however change the results of the try-compiles as old junk from a previous try-compile may cause a different test to either pass or fail incorrectly. This option is best used for one try-compile at a time, and only when debugging.
References
How to keep generated temporary files?
CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
For me, none of the log files in my output directory contained useful information from try_compile(), even when using --debug-trycompile.
I ended up using the OUTPUT_VARIABLE option to capture and then print the output like this:
try_compile(<options> OUTPUT_VARIABLE TRY_COMPILE_OUTPUT)
message(WARNING ${TRY_COMPILE_OUTPUT})

Why does CMake ignore exported CXX and CC environment variables?

I am running a CMake (3.4.3) like this as explained in the CMake FAQ's:
export CC="cc_args.py $PWD/../bin/gcc"
export CXX="cc_args.py $PWD/../bin/g++"
cmake -DCMAKE_BUILD_TYPE=Debug ..
However when I print CMAKE_CXX_COMPILER and CMAKE_C_COMPILER it still points to the system's default compilers in /usr/bin. It only works when I explicitly read-in the environment variables like this:
IF (NOT $ENV{CC} STREQUAL "")
SET(CMAKE_C_COMPILER $ENV{CC})
ENDIF ()
IF (NOT $ENV{CXX} STREQUAL "")
SET(CMAKE_CXX_COMPILER $ENV{CXX})
ENDIF ()
But even then the building fails with this message:
/bin/sh: 1: /home/peterg/bin/cc_args.py /home/peterg/Code/build/../bin/g++: not found
However I am certain that all paths are correct since executing just the path between the two colons outputs this as expected:
g++: fatal error: no input files
compilation terminated.
Update:
It seems the compiling process does not like spaces in the compiler paths. I've now created two scripts (one for GCC and one for CC) which wrap the commands and propagate the arguments and that seems to work. But it still seems I am doing something fundamentally wrong because CMake would also not accept the exported CC=proxy_script_cc.sh and GCC=proxy_script_gcc.sh variables without spaces by itself.
Turning my comment into an answer
Problem
I've given you code a try and could reproduce your problem
CMake Error at [...]/cmake-3.5/Modules/CMakeDetermineCXXCompiler.cmake:56 (message):
Could not find compiler set in environment variable CXX:
cc_args.py [... PWD ...]/../bin/g++.
If I look at CMakeDetermineCXXCompiler.cmake code and at get_filename_component() documentation, it just means that it didn't find cc_args.py in "the system search path" or relative to your binary output directory.
Solution
So it does work when you give a full path or a relative path to your binary output dir with something like
export CC="../cc_args.py ../bin/gcc"
export CXX="../cc_args.py ../bin/g++"
Alternative
CMake does allow to define "launcher scripts" e.g. with CMAKE_<LANG>_COMPILER_LAUNCHER
$ cmake -DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_COMPILER_LAUNCHER=../cc_args.py
-DCMAKE_CXX_COMPILER_LAUNCHER=../cc_args.py
..
References
How to Use CCache with CMake?
Save and reprint warnings for successfully-compiled files on subsequent builds?
Pass -DCMAKE_CXX_COMPILER=<path/to/compiler> to your CMake call. That's less error prone compared to fiddling with shell variables.

Compiling a CMake project against libraries in a non-standard location

I have two projects using CMake.
The first is a shared library. It compiles and installs fine. Currently, it is still necessary to build 'debug' releases of it. So presently it is installed under ~/localdebug. That folder looks like the root of a filesystem with a 'include' and 'lib' directory. The same concept as '/usr/local'.
The second is a program. It needs to compile and link against my library in ~/localdebug. The CMakeLists.txt file for it looks like this:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_C_FLAGS "-std=gnu99")
#add_definitions(-pg)
find_library(SANDGROUSE_LIB NAMES sandgrouse)
add_library(http_parser http_parser.c)
add_executable(rsva11001adapter main.c rsva11001.c)
target_link_libraries(rsva11001adapter http_parser ${SANDGROUSE_LIB})
I run the following to set up the make files:
cmake --debug-output -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH="/home/ericu/localdebug" ..
Based on the CMake wiki, setting DCMAKE_PREFIX_PATH does exactly what I want.
CMAKE_PREFIX_PATH
(since CMake 2.6.0) This is used when searching for include files, binaries, or libraries using either the FIND_PACKAGE(), FIND_PATH(), FIND_PROGRAM(), or FIND_LIBRARY() commands. For each path in the CMAKE_PREFIX_PATH list, CMake will check "PATH/include" and "PATH" when FIND_PATH() is called, "PATH/bin" and "PATH" when FIND_PROGRAM() is called, and "PATH/lib" and "PATH" when FIND_LIBRARY() is called. See the documentation for FIND_PACKAGE(), FIND_LIBRARY(), FIND_PATH(), and FIND_PROGRAM() for more details.
However, when I do a 'make VERBOSE=1' this is what I get:
cd /home/ericu/rsva11001adapter/build/src && /usr/bin/gcc -std=gnu99 -g -o CMakeFiles/rsva11001adapter.dir/main.c.o -c /home/ericu/rsva11001adapter/src/main.c
/home/ericu/rsva11001adapter/src/main.c:19:31: fatal error: sandgrouse/server.h: No such file or directory
compilation terminated.
So, it does not seem that CMake is finding things in CMAKE_PREFIX_PATH. It obviously is not adding -I variables to the compiler invocations either.
An inspection of CMakeCache.txt makes it seem as though it has no idea what the variable is:
// No help, variable specified on the command line.
CMAKE_PREFIX_PATH:UNINITIALIZED=/home/ericu/localdebug
I've been working on this for over an hour. I'm nearly at the point of giving up using CMake if it is this difficult to use a non-standard library with it.
You should instruct CMake to add -I flags when compiling your library:
find_path(SANDGROUSE_INCLUDE_DIR sandgrouse/server.h)
include_directories(${SANDGROUSE_INCLUDE_DIR}
Place these lines before add_library() invocation.