[CMake] Hi and some newbie questions

Jack Kelly endgame.dos at gmail.com
Thu Sep 13 17:17:10 EDT 2007


Goswin von Brederlow wrote:
> Hi everyone.
> 
> A friend of mine keeps cursing about autotools and suggests cmake
> instead. So I looked into it for a small project. Like all newbies I
> run into a lot of unknowns and I'm somewhat stuck now.
> 
> My CMakeLists.txt looks like below. My questions are in the comments.
> 
> ----------------------------------------------------------------------
> project (MOOSE)
> 
> # Building subdirectories recursively not done to mimik original
> # Makefile, not because I don't know how.
> #add_subdirectory (lib)
> 
> # How do I specify those more correctly? I need/want those flags and I
> # probably want them for all or most subdirs.
> ADD_DEFINITIONS(-fno-builtin -g -O2 -W -Wall -Werror -Wredundant-decls -Wno-format -Wstrict-prototypes -Wnested-externs -Wpointer-arith -Winline -D__XEN_INTERFACE_VERSION__=0x00030203 -m64 -mno-red-zone -fpic -fno-reorder-blocks -fno-asynchronous-unwind-tables -Wno-unused)
# I'm assuming CFLAGS here, but the principle is the same for C++, just
# use CXX instead of C. They look like GCC flags, so I wrapped them in a
# test:
IF(CMAKE_COMPILER_IS_GNUCC)
   SET(CMAKE_C_FLAGS "-fno-builtin ...")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
# Note also that if you are using CMAKE_BUILD_TYPE, then you can also
# set variables such as CMAKE_C_FLAGS_DEBUG to set flags on a per
# build-type basis.

# One way would be to have a file i486.cmake:
SET(ARCH i486)
# and whatever else...

# A file called x86_64.cmake:
SET(ARCH x86_64)
# ...

# Put this in the CMakeLists.txt:
INCLUDE(${PLATFORM})

# and invoke cmake like so:
# cmake -DPLATFORM=x86_64 /path/to/build

> # Again, how do I specify those that I don't have to repeat it in subdirs?
> include_directories (${MOOSE_SOURCE_DIR}/include)
> include_directories (${MOOSE_SOURCE_DIR}/include/x86)
# I think if you put these before the ADD_SUBDIRECTORY() calls then they
# will be propagated.

> # How do I put the architecture there? I need x86_64 or i486 or fail.
> include_directories (${MOOSE_SOURCE_DIR}/include/x86/x86_64)
# If you can detect or otherwise get the architecture into a variable
# (even just set it up so that the user has to input it). Not sure
# how to do that, though...
INCLUDE_DIRECTORIES(${MOOSE_SOURCE_DIR}/include/x86/${ARCH})

> add_library (minios STATIC events.c gnttab.c hypervisor.c kernel.c mm.c sched.c time.c lib/math.c lib/printf.c lib/string.c lib/xmalloc.c xenbus/xenbus.c console/console.c console/xencons_ring.c  arch/x86/mm.c arch/x86/sched.c arch/x86/setup.c arch/x86/traps.c)
> 
> # There would be multiple dirs here: lib, console, arch, ...
> #link_directories (.)
# As with INCLUDE_DIRECTORIES, you should be able to put this above the
# ADD_SUBIRECTORY() if you need it to propagate.

> # Now here is my first big problem. CMake doesn't know how to handle
> # any of these:
> #add_executable (mini-os.gz mini-os.elf x86_64.o)
# I don't think ADD_EXECUTABLE is what you're looking for here.
# If you can set up the build steps with ADD_CUSTOM_COMMAND you should
# be able to add those with an ADD_CUSTOM_TARGET

> # And it can't figure out that mini-os.elf is a normal elf binary just
> # from the static library.
> #target_link_libraries (mini-os.elf minios)
> 
> # How do I make a rule for assembler files? Doesn't seem to be preset.
> ADD_CUSTOM_COMMAND (OUTPUT x86_64.o
>                     COMMAND gcc -D__ASSEMBLY__ -Iinclude  -Iinclude/x86     -Iinclude/x86/x86_64 -c x86_64.S -o x86_64.o
>                     COMMENT Assembler pass) 
> 
> # Or define my own linker script?
> ADD_CUSTOM_COMMAND (OUTPUT mini-os.elf
>                     COMMAND ld -N -T minios-x86_64.lds -m elf_x86_64 x86_64.o -L. -lminios -o mini-os.elf
>                     DEPENDS x86_64.o minios
>                     COMMENT Link kernel into shape)
> 
> # And a rule to gzip the result?
> ADD_CUSTOM_COMMAND (OUTPUT mini-os.gz
>                     COMMAND gzip -f -9 -c mini-os.elf >mini-os.gz
>                     DEPENDS mini-os.elf
>                     COMMENT compress kernel)
# These look reasonable, apart from the fact that you should be looking
# for the executables with FIND_PROGRAM because not everyone will have
# the right versions at the front of the system path. Just allows a user
# override, that's all. Are they giving you a specific problem?

# You may also want to add explicit paths
# (like gcc -c ${MOOSE_SOURCE_DIR}/x86_64.S) and see if it's possible to
# do away with that redirection in the gzip.

> # And last why can't I add a custom command to the all target as in
> #ADD_CUSTOM_TARGET (all DEPENDS mini-os.gz)
> # or
> #ADD_CUSTOM_COMMAND (TARGET all PRE_LINK COMMAND make mini-os.gz)
> 
> ADD_CUSTOM_TARGET (mini-os DEPENDS mini-os.gz)
ADD_CUSTOM_TARGET(mini-os ALL DEPENDS mini-os.gz VERBATIM)
# The ALL will force it to be rebuilt even if you just run `make'.
# The VERBATIM is just a style thing, it's recommended: (man cmake)

# Use  of  VERBATIM  is  recommended as it enables correct
# behavior. When VERBATIM is not given the  behavior  is  platform
# specific.  In the future VERBATIM may be enabled by default. The
# only reason it is an option is to  preserve  compatibility  with
# older CMake code.

# -- Jack


More information about the CMake mailing list