ComputationalRadiationPhysics / imresh

Shrink-Wrap Phase Reconstruction Algorithm
MIT License
3 stars 2 forks source link

CMake add_definitions bug in conjunction with FindCUDA #50

Open mxmlnkn opened 8 years ago

mxmlnkn commented 8 years ago

try checking out dev (de14cd9c0c1cb23c3ea9575b8e4ee3d7fc49a93d), add following code to src/imresh/libs/cudacommon.hpp:

#ifdef USE_SPLASH
#   warning "USE_SPLASH defined!"
#else
#   warning "USE_SPLASH NOT defined!"
#endif
#ifdef USE_PNG
#   warning "USE_PNG defined!"
#else
#   warning "USE_PNG NOT defined!"
#endif
#ifdef IMRESH_DEBUG
#   warning "IMRESH_DEBUG defined!"
#else
#   warning "IMRESH_DEBUG NOT defined!"
#endif
#ifdef USE_FFTW
#   warning "USE_FFTW defined!"
#else
#   warning "USE_FFTW NOT defined!"
#endif
#error "!"

Now compile with:

mkdir build
cd build
cmake ..                                \
  -DCMAKE_CXX_COMPILER=$(which g++-4.9) \
  -DCMAKE_C_COMPILER=$(which gcc-4.9)   \
  -DUSE_PNG=ON                          \
  -DUSE_FFTW=ON                         \
  -DUSE_SPLASH=ON                       \
  -DBUILD_DOC=OFF                       \
  -DIMRESH_DEBUG=ON
make VERBOSE=1

The output is:

/usr/bin/nvcc -M -D__CUDACC__ imresh/src/imresh/libs/cudacommon.cu -o imresh/build/CMakeFiles/imresh.dir/src/imresh/libs/imresh_generated_cudacommon.cu.o.NVCC-depend -ccbin /usr/bin/gcc-4.9 -m64 --std c++11 -DIMRESH_DEBUG -DUSE_SPLASH -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -D_FORTIFY_SOURCE=2 -DUSE_FFTW -Xcompiler ,\"-Wall\",\"-Wextra\",\"-Wshadow\",\"-Wno-unused-parameter\",\"-O2\",\"-g\",\"-fPIC\",\"-pthread\",\"-fopenmp\",\"-g\" -Xcompiler -Wall,-Wextra,-Wshadow -G -lineinfo -Xptxas=-v -arch=sm_30 -DNVCC -Iimresh/src/imresh -I/usr/include -I/usr/include -Iimresh/src/imresh -I$PNGWRITER_ROOT/include -I/usr/include/freetype2 -I$SPLASH_ROOT/include -I/usr/include/hdf5/serial
[  5%] Building NVCC (Device) object CMakeFiles/imresh.dir/src/imresh/libs/imresh_generated_cudacommon.cu.o
In file included from imresh/src/imresh/libs/cudacommon.cu:26:0:
imresh/src/imresh/libs/cudacommon.hpp:33:9: warning: #warning "USE_PNG NOT defined!" [-Wcpp]
     #   warning "USE_PNG NOT defined!"
         ^
imresh/src/imresh/libs/cudacommon.hpp:36:9: warning: #warning "IMRESH_DEBUG defined!" [-Wcpp]
     #   warning "IMRESH_DEBUG defined!"
         ^
imresh/src/imresh/libs/cudacommon.hpp:41:9: warning: #warning "USE_FFTW defined!" [-Wcpp]
     #   warning "USE_FFTW defined!"
         ^
imresh/src/imresh/libs/cudacommon.hpp:45:6: error: #error "!"
     #error "!"
      ^
CMake Error at imresh_generated_cudacommon.cu.o.cmake:207 (message):
  Error generating
  imresh/build/CMakeFiles/imresh.dir/src/imresh/libs/./imresh_generated_cudacommon.cu.o

So for some reason only IMRESH_DEBUG and USE_FFTW are defined, but not USE_PNG. Note the difference:

add_definitions("-DUSE_PNG ${PNGwriter_DEFINITIONS}")
add_definitions("-DUSE_SPLASH" ${Splash_DEFINITIONS})

Changing this to

add_definitions("-DUSE_PNG" ${PNGwriter_DEFINITIONS})

will make it work.

Note also this minimal-non-working example:

cmake_minimum_required(VERSION 3.5)
find_package( CUDA )
add_definitions( "-D IMRESH_DEBUG" )
add_definitions( "-DIMRESH_DEBUG2" )
SET_SOURCE_FILES_PROPERTIES( main.cpp PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ )
cuda_add_library( mimi main.cpp )
target_link_libraries( mimi )

Use the following main.cpp

#ifdef IMRESH_DEBUG
#   error "IMRESH_DEBUG defined!"
#endif
int main ( void ){ return 0; }

Here is an excerpt from the output:

/usr/bin/nvcc ~/cmakeIsysBug/main.cpp -x=cu -c -o ~/cmakeIsysBug/build/CMakeFiles/mimi.dir//./mimi_generated_main.cpp.o -ccbin /usr/bin/gcc-4.9 -m64 -DIMRESH_DEBUG2 -Xcompiler ,\"-g\" -DNVCC -I/usr/include -I/usr/include

So especially there is no compile error, because IMRESH_DEBUG won't be defined, but IMRESH_DEBUG2 is. This means CMake has a bug where it misinterprets definitions containing spaces. Note that the space after -D is completely fine!

Comparing the above to this minimal-working CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
add_definitions( "-D IMRESH_DEBUG" )
add_definitions( "-DIMRESH_DEBUG2" )
add_library( mimi main.cpp )
target_link_libraries( mimi )

the output will be:

[ 50%] Building CXX object CMakeFiles/mimi.dir/main.cpp.o
/usr/bin/g++-4.9   -DIMRESH_DEBUG2  -D IMRESH_DEBUG -o CMakeFiles/mimi.dir/main.cpp.o -c ~/cmakeIsysBug/main.cpp
~/cmakeIsysBug/main.cpp:3:5: error: #error "IMRESH_DEBUG defined!"
 #   error "IMRESH_DEBUG defined!"
     ^

Meaning the bug seems to be inside FindCUDA.

I'm only kinda surprised that the missing -D USE_PNG didn't lead to any notable errors Oo. Maybe I'm overlooking something again.

Ferruck commented 8 years ago

I noticed strange behavior of CMake in combination with definitions and the correct/incorrect setting of "'s as well. But it is somehow indeterministic, especially between even minor version changes. So in my opinion this will be a very difficult but to track down.

mxmlnkn commented 8 years ago

For reference. Also I'm struggling with warning messages from alpaka and halt and possibly other libraries in the future flooding output, see here.