Alex Alex - 1 year ago 143
C++ Question

CMake output directory by platform

I would like to output my library into different folder based on the platform/compiler/architecture/configuration, so it should look like this :

platform(arch)-compiler(version)-configuration

A few examples would be:

win32-vc12.0-debug
win32-vc12.0-release
win64-vc14.0-debug
win64-vc14.0-release

unix-gcc6.1-debug
unix-gcc6.1-release
unix-clang3.8-debug
unix-clang3.8-release


I actually have no idea how to generate those, could anyone guide me ?
Thank you

Answer Source

One way would be to set the bin directories for the project:

math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir ${CMAKE_SYSTEM_NAME}${platform_bits}-${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION})

foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)

    foreach(var CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config} CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config} CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config})
        set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}-${config}")
        string(TOLOWER "${${var}}" ${var})
    endforeach()

endforeach()

This won't give you the exact directory names you are looking for, but is a good start. You can look at the CMake variables list for a list of platform specific configuration information. You would then need to apply some logic that will transform the CMake defined values into the specific ones you are looking for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download