[Cmake] Comments, questions from my first CMake'd app

Ian Main ian-cmake at stemwinder.org
Tue Mar 18 17:05:29 EST 2003


First off I'd like to thank the authors for their efforts! Thanks!
It can be a real pain dealing with all these different build systems.

So, I've got a few projects that I am thinking of moving to CMake
from auto*.  The first one is a utility library for C, kind of like
glib.

I am in the process of converting everything that auto* does to
be done by CMake.

I've had a relatively easy go of it in general, and have gotten
most things working.

I had a few little problems with some things.  The return from

INCLUDE(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)

will set HAVE_SYS_TYPES_H to 1 if it exists, or nothing if it
does not.  This makes it hard to use in a config.h.in style
arrangement, as you have to do funky #if stuff.  I am
currently using:

#if 0 at HAVE_SYS_TYPES_H@
#define HAVE_SYS_TYPES_H 1
#endif

which will make the first line '#if 0' or '#if 01' depending
on the result.  This works but it's a bit hackish.  Note that
you can't just do:

#define HAVE_SYS_TYPES_H @HAVE_SYS_TYPES_H@ because then when
you test for it you have to use #if still, and you end up 
with the same problem.

It would be much better if that macro returned 1 if found, and
0 if not found.  Same for the library tests etc.

Of course, I may be missing something obvious, please tell me
if I am! :)

The problem I am currently stumped on is getting a 'config'
shell script to install as executable on unix.  I am generating
a roy-config shell script from roy-config.in, which when run
outputs the CFLAGS, libs etc needed to link with roy (just
like gtk, glib etc. use).

INSTALL_FILES of course installs as mode 644, and I can't
get INSTALL_PROGRAMS to install it.  I am guessing this could
be a problem for anyone doing any kind of scripts that
require that they be installed executable.

The last thing I am unsure of is how to do custom build commands.
I have a set of pod files that I use to generate manpages.. not
sure how to go about that one.

If I can get this all working, I may write a tutorial for CMake
as it seems there is a lack of documentation.  The reference is
good but you're kind of left using your imagination a lot as to
how things should be used, and what the most portable way to do
things is.

I'll insert my main build file here just to get comments from folks
to see if there are better ways to do things.

In general CMake is pretty nice.  It's certainly much better than
auto*, although I am not sure why you decided on a macro language
instead of a "real" scripting language (even just a tiny/simple one).

Thanks!


    Ian


    ----

# Roy cmake build

PROJECT(ROY)

SET(BUILD_SHARED_LIBS "ON" CACHE BOOL "Whether or not to build libraries as shared.")


# Check size of various types..
INCLUDE(${CMAKE_ROOT}/Modules/CheckTypeSize.cmake)

CHECK_TYPE_SIZE("char" SIZEOF_CHAR)
CHECK_TYPE_SIZE("short int" SIZEOF_SHORT_INT)
CHECK_TYPE_SIZE("int" SIZEOF_INT)
CHECK_TYPE_SIZE("long int" SIZEOF_LONG_INT)
CHECK_TYPE_SIZE("long long int" SIZEOF_LONG_LONG_INT)

# Check for various functions..
INCLUDE(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)

CHECK_FUNCTION_EXISTS(popen HAVE_POPEN)
CHECK_FUNCTION_EXISTS(vsnprintf HAVE_VSNPRINTF)
CHECK_FUNCTION_EXISTS(snprintf HAVE_SNPRINTF)

# Easy way to check for includes 
INCLUDE(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)

CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE(sys/time.h HAVE_SYS_TIME_H)
CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
CHECK_INCLUDE_FILE(netdb.h HAVE_NETDB_H)
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
CHECK_INCLUDE_FILE(errno.h HAVE_ERRNO_H)

# Check for libs
INCLUDE(${CMAKE_ROOT}/Modules/CheckLibraryExists.cmake)

# zlib
CHECK_LIBRARY_EXISTS(z deflate "/lib;/usr/lib;/usr/local/lib;/usr/pkg/lib" HAVE_ZLIB)
# socket
CHECK_LIBRARY_EXISTS(socket socket "/lib;/usr/lib;/usr/local/lib;/usr/pkg/lib" HAVE_SOCKETLIB)
# nsl
CHECK_LIBRARY_EXISTS(nsl gethostbyname "/lib;/usr/lib;/usr/local/lib;/usr/pkg/lib" HAVE_NSLLIB)


# Setup the SOCKET_LIBS variable - this one is only used in
# roy-config.in, and is for compat with auto*
SET(SOCKET_LIBS "" CACHE INTERNAL "Socket libraries")

# Setup the SOCKET_LIBRARIES variable - this one is used by
# the build system
SET(SOCKET_LIBRARIES "" CACHE INTERNAL "Socket libraries")

IF(${HAVE_SOCKETLIB} MATCHES 1)
    SET(SOCKET_LIBS "-lsocket")
    SET(SOCKET_LIBRARIES "socket")
ENDIF(${HAVE_SOCKETLIB} MATCHES 1)

IF(${HAVE_NSLLIB} MATCHES 1)
    SET(SOCKET_LIBS "${SOCKET_LIBS} -lnsl")
    SET(SOCKET_LIBRARIES "${SOCKET_LIBS} nsl")
ENDIF(${HAVE_NSLLIB} MATCHES 1)


# Generate and install a roy-config.h
CONFIGURE_FILE(${ROY_SOURCE_DIR}/roy/roy-config.h.cmake.in
               ${ROY_BINARY_DIR}/roy/roy-config.h
               ESCAPE_QUOTES)
INSTALL_FILES(/include/roy/ FILES ${ROY_BINARY_DIR}/roy/roy-config.h)

# Generate and install roy-config
SET(prefix ${CMAKE_INSTALL_PREFIX} CACHE INTERNAL "prefix for roy-config.in")
CONFIGURE_FILE(${ROY_SOURCE_DIR}/roy-config.in
               ${ROY_BINARY_DIR}/roy-config
               ESCAPE_QUOTES)

#IF(UNIX)
#    EXEC_PROGRAM(chmod ARGS "755 roy-config")
#ENDIF(UNIX)

# This is broken as it doesn't install it executable..
INSTALL_FILES(/bin/ FILES ${ROY_BINARY_DIR}/roy-config)

# Make sure everyone can find roy.h and roy-config.h
INCLUDE_DIRECTORIES(${ROY_SOURCE_DIR}
                    ${ROY_BINARY_DIR})

# Install main roy.h here
INSTALL_FILES(/include/ FILES roy.h)

SUBDIRS(roy tests)







More information about the CMake mailing list