Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

Dabbling a bit in cmake waters

cmake: it sounds like a good idea to me. I write a CMakeLists.txt file, which contains a list of my source files and I specify the target type. Then cmake creates the appropriate Makefile or Xcode project or even Visual Studio project depending on the platform it is run on.

But it’s usually the little things that make me wonder.

First off, this is on Fedora 21 Linux I am trying to generate a “Unix Makefile” for a C library from

cmake_minimum_required (VERSION 3.0)

project (mulle-objc-runtime)

set (CMAKE_C_FLAGS "-Wno-parantheses")

add_library(foo
src/foo.c
)

thusly

$ cmake -G "Unix Makefiles" 
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is unknown
-- 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
CMake Error at CMakeLists.txt:3 (project):
  No CMAKE_CXX_COMPILER could be found.

So it didn’t find a C++ compiler. Maybe it is so - maybe not so. But there is also no Pascal compiler. Why does it care ?

Let’s use true as a C++ compiler…

$ cmake -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=/bin/true
-- The CXX compiler identification is unknown
-- Check for working CXX compiler: /bin/true
-- Check for working CXX compiler: /bin/true -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Configuring done
-- Generating done

OK, nice. Now make compiles, links and ranlibs my library. Excellent

But the next disappointment is right around the corner. make clean doesn’t work and neither does cmake clean to remove all the files cmake generated.

Simulate cmake clean with rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake

Oh well. Lets try to set a compiler flag, that llvm (OS X) understands and gcc (Linux) doesn’t. I suspect, that this won’t work, because that would mean a heck of a lot of mapping effort. But hope springs eternal. So I edit set (CMAKE_C_FLAGS "-Wno-parantheses") to set (CMAKE_C_FLAGS "-fno-color-diagnostic"). And try again and of course it doesn’t work.

I didn’t expect it to work and it is OK that it doesn’t work. But it makes me wonder about cmake’s actual usefulness to me in real life, when compiler flags won’t translate.

It seems the solution is to write a lot of if (compiler==) code inside your CMakeFiles.txt.

cmake_minimum_required (VERSION 3.0)

project (mulle-objc-runtime)

# dont forget to wipe all cmake produced files before
# changing this


if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
   set (CMAKE_C_FLAGS "-Wno-parantheses")
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
   set (CMAKE_C_FLAGS "-fno-color-diagnostics")
endif()


add_library(foo
src/foo.c
)

The syntax has this kind of sick LISPy flair…

cmake: will use, won’t love.


Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: