tags:

views:

40

answers:

1

I'm currently working on using cmake to build some projects, with the main platforms being Visual C++, MinGW GCC and Linux GCC. When building with GCC, I need to specify the -Wno-invalid-offsetof compiler option.

My current fix is as follows...

if (   "${CMAKE_GENERATOR}" MATCHES "^Visual Studio"
    OR "${CMAKE_GENERATOR}" MATCHES "^NMake"
   )
    set (CPPLIB_COMPILER_OPTS "")
else ()
    set (CPPLIB_COMPILER_OPTS "-Wno-invalid-offsetof")
endif ()

...

set_target_properties(sh_core PROPERTIES COMPILE_FLAGS "${CPPLIB_COMPILER_OPTS}")
# repeated for all targets

This works, but assuming that all generators other than the visual studio ones will build with gcc is obviously unsafe. For a start, there are IIRC generators for Borland compilers. More importantly, using make doesn't always mean using gcc.

Other compilers I'm likely to use are llvm-gcc and clang. Fortunately, I think even clang supports gcc-compatible options. But this logic is only good for as long as the relevant code is never released.

Cmake appears to check for available compilers and generate a makefile specifically for that compiler (raising the question - why not at least have the option of building the project directly, without the need for a middle-man like make?).

That being the case, I was hoping to be able to test directly for gcc in my CMakeLists.txt files. So far, though, I can't find an appropriate variable to test or any other obvious solution.

Is this possible?

+4  A: 

To create a portable build system, it is best to not test for platforms, but to test for features.

Instead of testing "if Windows then do this", test for "if the -Wno-invalid-offsetof flag works then use it". You can do that with the CheckCCompilerFlag module, for example:

include(CheckCCompilerFlag)
check_c_compiler_flag(-Wno-invalid-offsetof HAS_NO_INVALID_OFFSETOF)
if (HAS_NO_INVALID_OFFSETOF)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-invalid-offsetof")
endif()

For C++ there is a similar CheckCXXCompilerFlag with a check_cxx_compiler_flag(flag var) command.

rq
That's perfect! One irrelevant thing, though... there's not much point checking that option for C, as there's no such thing as an invalid offsetof in C - it only happens in C++.
Steve314
I didn't know that. In fact I didn't know what the option was for but it makes perfect sense, as C only has POD types http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Warning-Options.html. And CMake ignores the compiler warning during its test - "if it compiles it works".
rq