[CMake] linux kernel module, output directory and clean issues

Tim Schooley tim at sbdev.net
Fri Aug 17 07:52:08 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Alan,

Many thanks for all your help. I've finally understood what's going on.
A rather important misunderstanding on my part was that cmake has no
control over how the Kbuild makefile places files! *slaps forehead*.

There is just one remaining "issue", being that cmake won't remove the
directory ".tmp_versions/", even though it is listed as an output. :(
Anyways, for future reference, this is how I'm now building kernel
modules (there are never enough examples out there!):

- ----

Set( MODULE_OUTPUT_FILES    w00t.o
                            w00t.mod.c
                            w00t.mod.o
                            w00t.ko
                            .w00t.o.cmd
                            .w00t.ko.cmd
                            .w00t.mod.o.cmd
                            .tmp_versions/w00t.mod
                            .tmp_versions/ )

Set( MODULE_SOURCE_FILES    Kbuild
                            w00t.c )

Set( DRIVER_FILE        w00t.ko )
Set( DRIVER_TARGET_NAME w00t-module )
Set( DRIVER_BIN_FILE    ${CMAKE_BINARY_DIR}/bin/${DRIVER_FILE} )
Set( MODULE_SOURCE_DIR  ${PROJECT_SOURCE_DIR}/src/w00t )

Set( KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build" )
Set( KBUILD_CMD ${CMAKE_MAKE_PROGRAM}
                -C ${KERNEL_DIR}
                M=${MODULE_SOURCE_DIR}
                modules )

Add_Custom_Command( OUTPUT  ${DRIVER_BIN_FILE}
                            ${MODULE_OUTPUT_FILES}
                    COMMAND ${KBUILD_CMD}
                    COMMAND cp -f ${DRIVER_FILE} ${DRIVER_BIN_FILE}
                    WORKING_DIRECTORY ${MODULE_SOURCE_DIR}
                    DEPENDS ${MODULE_SOURCE_FILES} VERBATIM )

Add_Custom_Target ( ${DRIVER_TARGET_NAME}
                    ALL
                    DEPENDS ${DRIVER_BIN_FILE} )

- ----

Thanks again for all your help!

Kind regards,

Tim

Alan W. Irwin wrote:
> On 2007-08-16 15:18+0100 Tim Schooley wrote:
> 
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Hi all!
>>
>> [My first post, please bear with me...]
>>
>> I've been porting a Makefile project to use CMake. Thankfully, it's gone
>> very smoothly, and now I'm just trying to iron out some ripples. CMake
>> truly rocks.
>>
>> I found some advice on this mailing list for building linux kernel
>> modules, and I'm using the following for it:
>>
>> - ---
>>
>> Set( DRIVER_FILE mydriver.ko )
>> Set( KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build" )
>> Set( KBUILD_CMD ${CMAKE_MAKE_PROGRAM}
>>                -C ${KERNEL_DIR}
>>                M=${CMAKE_CURRENT_SOURCE_DIR} modules)
>>
>> Add_Custom_Command( OUTPUT ${DRIVER_FILE}
>>                    COMMAND ${KBUILD_CMD}
>>                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
>>                    DEPENDS ${src} Kbuild VERBATIM )
>>
>> Add_Custom_Target ( driver ALL DEPENDS ${DRIVER_FILE} )
>>
>> - ---
>>
>> My issue is this: no matter how I specify the output, I cannot for the
>> life of me get the mydriver.ko file to be outputted to the
>> ${PROJECT_BINARY_DIR}/bin directory. I've searched around for similar
>> issues/code, but come to a dead end.
> 
> Hi Tim:
> 
> To address the CMake issue of where it expects to find files,
> use full pathnames for all files, and drop the
> WORKING_DIRECTORY.  That is, do something like the following:
> 
> Add_Custom_Command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${DRIVER_FILE}
>                    COMMAND ${KBUILD_CMD}
>                    DEPENDS ${src} Kbuild VERBATIM )
> 
> Add_Custom_Target ( driver ALL DEPENDS
> ${CMAKE_CURRENT_BINARY_DIR}/${DRIVER_FILE} )
> 
> Without the WORKING_DIRECTORY the external make command you access with
> ${CMAKE_MAKE_PROGRAM} should be issued from the ${CMAKE_CURRENT_BINARY_DIR}
> directory (which I assume is identical to ${PROJECT_BINARY_DIR}/bin in your
> above example.  However, if the above does not work, you can be absolutely
> specific by doing, e.g.,
> 
> Add_Custom_Command( OUTPUT ${CMAKE_BINARY_DIR}/bin/${DRIVER_FILE}
>                    COMMAND ${KBUILD_CMD}
>                    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
>                    DEPENDS ${src} Kbuild VERBATIM )
> 
> Add_Custom_Target ( driver ALL DEPENDS
> ${CMAKE_BINARY_DIR}/bin/${DRIVER_FILE} )
> 
> Anyhow, one of the two variations above should take care of the CMake
> issues.  That leaves the issue of where the external make command outputs
> its files. I am guessing here that it will output the ${DRIVER_FILE} result
> to ${CMAKE_BINARY_DIR}/bin/ or what I think is the equivalent
> ${CMAKE_CURRENT_BINARY_DIR}.  If the external make programme does not
> cooperate that way by default then you may have to try additional make
> options to get it to do that or add a specific stanza to copy the output
> file from make to the directory that you want.
> 
> Alan
> __________________________
> Alan W. Irwin
> 
> Astronomical research affiliation with Department of Physics and Astronomy,
> University of Victoria (astrowww.phys.uvic.ca).
> 
> Programming affiliations with the FreeEOS equation-of-state implementation
> for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
> package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
> Linux Links project (loll.sf.net); and the Linux Brochure Project
> (lbproject.sf.net).
> __________________________
> 
> Linux-powered Science
> __________________________
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGxYvoChPkLPgrpHQRAh0FAJ97I41L/QekvKTkOWUaPX6mVciMUQCbBWTj
/7/E58Z3z9o/a/zkx33hTAM=
=jQq6
-----END PGP SIGNATURE-----



More information about the CMake mailing list