Maik Klein
Maik Klein

Reputation: 16148

How to configure external cmake libraries?

What I wanted to do is call

add_subdirectory(ext/oglplus)

and be done with it. Unfortunately it is not that simple. There is a huge buildscript which detects various opengl settings. So I tried the following

ExternalProject_Add(liboglplus
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ext/oglplus
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/ext/oglplus/configure.py --use-glew
    BUILD_COMMAND  ${MAKE})

The problem that I have is don't really want to build it like that. It also doesn't build correctly because for some reason it wants to install the library and because there is no install target it will abort the compilation.

But the build script is calling cmake under the hood.

So what I want to do is to tell cmake to use "cofigure.py" instead of "cmake .." and then use it like any other cmake library.

Is this possible?

Upvotes: 0

Views: 490

Answers (1)

truschival
truschival

Reputation: 343

I used to call Linux Kernel KBuild from CMake using ADD_CUSTOM_COMMAND() and ADD_CUSTOM_TARGET() This way you can run arbitrary commands (like your config.py) and use the output.

First setup the command with all command-line options as CMake-variables, in tou case this would be calling the config.py script ${CMAKE_CURRENT_SOURCE_DIR}/ext/oglplus/. Instead of encoding the Path to your script in the command (adding ext/oglplus) I think it may be better adding WORKING_DIRECTORY to the custom command: add_custom_command

SET(KBUILD_CMD ${CMAKE_MAKE_PROGRAM}
-C ${KERNEL_BUILD_DIR}
CROSS_COMPILE=${CROSS_COMPILE} ARCH=${ARCH}
EXTRA_CFLAGS=${KBUILD_EXTRA_CFLAGS}
INSTALL_MOD_PATH=${INSTALL_MOD_PATH}
M=${CMAKE_CURRENT_SOURCE_DIR}
KBUILD_EXTRA_SYMBOLS=${depends_module_ksyms}
)

Add a custom command that calls your build-script and creates a file in the CMAKE_CURRENT_BINARY_DIRECTORY (note the second COMMAND to touch a file)

ADD_CUSTOM_COMMAND(
OUTPUT  ${module_name}.built
COMMAND ${KBUILD_CMD} modules
COMMAND cmake -E touch ${module_name}.built
COMMENT "Kernel make modules ${module_name}"
VERBATIM
)

Add a custom target, its always out of date, but if you want it to be called automatically add ALL otherwise you have to explicityly call make module_build, I guess this is what you want.

ADD_CUSTOM_TARGET("${module_name}_build" ALL
DEPENDS  ${depends_module_ksyms}
${CMAKE_CURRENT_BINARY_DIR}/${module_name}.built
COMMENT "Building Kernel Module ${module_name}"
)

Upvotes: 1

Related Questions