[CMake] set directory for object file generation

Michael Wild themiwi at gmail.com
Fri Dec 4 03:28:50 EST 2009


On 3. Dec, 2009, at 22:08 , Voisard, Shane S CIV NSWCDD, K54 wrote:

> Is there a way to set the directory that cmake (2.8) uses for object  
> file generation?  I haven't found an online resource for this.
>
> There is a difference when we build the same project on a Windows  
> vs. Linux platform.
>
> On WIN32, cmake creates '<PROJ_NAME>/<PROJ_NAME>.dir/ directory for  
> object files.
>
> On UNIX, cmake creates '<PROJ_NAME>/<PROJ_NAME>.dir/ENTIRE/ABS/PATH/ 
> TO/<PROJ_NAME> directory for object files.

I only can reproduce this behavior if I have sources which are NOT  
under the top-level source directory, i.e. something like this:

$HOME/projects/super-duper/CMakeLists.txt:
#--------------------------------------------
cmake_minimum_required(VERSION 2.8)
project(test)
add_executable(test $env(HOME)/external/test.cxx)
#--------------------------------------------

Am I assuming correctly that that's what you're doing? In that case  
you could do something like this:
#-------------------------------------------
# - Create include-wrappers for external source files
#
#  CREATE_INCLUDE_WRAPPERS(SRCVAR file1 ...)
#
# The variable SRCVAR will contain a list of source files suitable to
# pass to add_library or add_executable.
#
function(create_include_wrappers srcvar)
   if(UNIX)
     set(srcs)
     # loop over all additional arguments
     foreach(s ${ARGN})
       # get absolute path
       get_filename_component(as "${s}" ABSOLUTE)
       # relative path w.r.t toplevel-source directory
       file(RELATIVE_PATH rs "${CMAKE_SOURCE_DIR}" "${as}")
       # if it begins with .. it is external
       if(rs MATCHES "^\.\.")
         # this is an external source
         # get the basename
         get_filename_component(rw "${s}" NAME)
         # append it to the current binary directory (might need  
something better here)
         set(w "${CMAKE_CURRENT_BINARY_DIR}/wrappers/wrapped_${rw}")
         # prepare variable for the configured file
         set(EXTERNAL_SOURCE "${as}")
         # configure the wrapper
         configure_file("${CMAKE_SOURCE_DIR}/CMake/ 
wrapper_template.in" "${w}" @ONLY)
         # append the wrapper to the list of source files
         list(srcs APPEND "${w}")
       else()
         # if it's not external, simply append
         list(srcs APPEND "${s}")
       endif()
     endforeach()
   else()
     # Presumably we don't need the wrappers on non-UNIX
     set(srcs "${ARGN}")
   endif()
   # propagate the list of source files to the caller scope
   set(${srcvar} "${srcs}" PARENT_SCOPE)
endfunction()
#-------------------------------------------

The file ${CMAKE_SOURCE_DIR}/CMake/wrapper_template.in could look  
somthing like this:
/*-------------------------------------------*/
/* AUTOMATICALLY GENERATED! DO NOT EDIT      */
#include "@EXTERNAL_SOURCE@"
/*-------------------------------------------*/


However, such a scheme will slow down configuration and compilation.

Michael


More information about the CMake mailing list